SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Tue Dec 12, 2017 7:40 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Jun 29, 2015 4:12 pm 
Offline

Joined: Wed Mar 17, 2010 4:55 pm
Posts: 19
Dear Support

The following script creates new instruments (removing them if they already exist) and loads historical data.

In the line

Code:
DataManager.Add(fxInstr, quote);


I get a null reference exception http://screencast.com/t/T14KPImNx.
I checked the instrument and it is not null and seems to be properly set.

What do we wrong? Many thanks for your help.

Quote:
using System;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Xml;

using OpenQuant.API;
using OpenQuant.API.Indicators;

public class InstrDescr
{
public string Symbol;
public string Currency;
public string Exchange;
};

public class MyScript : Script
{
public override void Run()
{
var dataPath = System.Environment.GetEnvironmentVariable("STRATHOME") + "/data/hist";

Console.WriteLine("Data import path set to {0}.", dataPath);

var instruments = new[] {
new { Symbol = "AUDNZD", Currency = "NZD", FileExt = ".FXCM.dat"},
new { Symbol = "AUDSGD", Currency = "SGD", FileExt = ".COMP.dat"},
new { Symbol = "CHFJPY", Currency = "JPY", FileExt = ".FXCM.dat"},
new { Symbol = "EURCAD", Currency = "CAD", FileExt = ".FXCM.dat"},
new { Symbol = "EURNZD", Currency = "NZD", FileExt = ".FXCM.dat"},
new { Symbol = "EURSGD", Currency = "SGD", FileExt = ".COMP.dat"},
new { Symbol = "GBPAUD", Currency = "AUD", FileExt = ".FXCM.dat"},
new { Symbol = "GBPCAD", Currency = "CAD", FileExt = ".FXCM.dat"},
new { Symbol = "GBPCHF", Currency = "CHF", FileExt = ".FXCM.dat"},
new { Symbol = "GBPUSD", Currency = "USD", FileExt = ".FXCM.dat"},
new { Symbol = "NZDCHF", Currency = "CHF", FileExt = ".FXCM.dat"},
new { Symbol = "USDJPY", Currency = "JPY", FileExt = ".FXCM.dat"}
};

bool ask = true;
bool removeExisting = false;

foreach (var instr in instruments) {
var existing = InstrumentManager.Instruments[instr.Symbol];

if (existing != null)
{
if (ask == true) {

string message = string.Format("Instrument {0} already exists. Delete all existing?", instr.Symbol);
string caption = "Delete existing instruments";
var result = MessageBox.Show(message, caption, MessageBoxButtons.YesNo);

ask = false;
if (result == System.Windows.Forms.DialogResult.Yes)
removeExisting = true;
else
{
Console.WriteLine("Instrument creation aborted.");
break;
}
}

if (removeExisting)
{
InstrumentManager.Remove(existing);
Console.WriteLine("Removed exiting instrument with symbol {0}.", instr.Symbol);
}
}

var fxInstr = new Instrument(InstrumentType.FX, instr.Symbol);
fxInstr.Currency = instr.Currency;
fxInstr.Exchange = "IDEALPRO";
Console.WriteLine("Created new instrument with symbol {0}.", instr.Symbol);

var fileName = dataPath + "/" + instr.Symbol + instr.FileExt;
Console.WriteLine("Reading historical data from file {0}.", fileName);

FMCXRow[] dataRows = loadFMCX(fileName);
Console.WriteLine("Loaded {0} data records.", dataRows.Length);

foreach (var row in dataRows) {
var quote = new Quote(row.Time, row.Bid, 1, row.Ask, 1);
DataManager.Add(fxInstr, quote);
}

var firstObsDate = dataRows[0].Time;
var lastObsDate = dataRows[dataRows.Length-1].Time;
Console.WriteLine("Added {0} data records from {1} to {2}.", dataRows.Length, firstObsDate.ToString(), lastObsDate.ToString());
}
}
}


Top
 Profile  
 
PostPosted: Mon Jun 29, 2015 7:09 pm 
Offline

Joined: Wed Mar 17, 2010 4:55 pm
Posts: 19
Dear Support

I think the confusion comes from an API design which is not that common for a .NET programmer because it does some steps implicit.

The following script works perfectly. The difference is that I grab the instrument again from the

Code:
Instrument ii = InstrumentManager.Instruments["TestStock"];
,

even though the instrument is added implicitly when it is created. This is confusing. Either require an explicit "Add" so that it is clear or allow implicit changing of properties and data. It is strange because not even code like this

Code:
i.Currency = "AUD";


works, once the instrument is created and the script is executed a second time.

Thanks for a quick reply on how others see this point.

Regards, Daniel

Code:
using System;
using System.IO;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

public class MyScript : Script
{
   public override void Run()
   {
      Random rng = new System.Random();
      Instrument i = new Instrument(InstrumentType.Stock, "TestStock", "xxxx", "GBP");
   
      Console.WriteLine("new instrument created");

      Instrument ii = InstrumentManager.Instruments["TestStock"];
      if (ii == null)
         Console.WriteLine("instrument with symbol {0} not found", "TestStock");

      Console.WriteLine("currency {0}", ii.Currency);
      
      ii.Description = "TestStock";
      ii.Exchange = "Exchange";
      ii.Currency = "USD";
      ii.Factor = 1.0;
      ii.TickSize = 1.0;
      ii.PriceFormat = "F4";
      ii.Group = "Portfolio";
      ii.Sector = "Sector";      

      var now = DateTime.Now;
      DateTime beginTime = now.AddDays(-100);
      
      DataManager.DeleteQuoteSeries(ii);
      
      while (beginTime <= now) {
         var quote = new Quote(beginTime, rng.NextDouble(), 1, rng.NextDouble(), 1);
         Console.WriteLine("adding quote for {0}", beginTime.ToString());
         DataManager.Add(ii, quote);   
         beginTime = beginTime.AddDays(1);
      }
   }
}


Top
 Profile  
 
PostPosted: Tue Jun 30, 2015 11:12 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6783
Hi,

the reason is that OpenQuant API is a wrapper around the core SmartQuant framework, which is hidden from the end user (due to political reasons coming from our old story with QuantHouse). That's why you have this kind of mapping between framework instruments and their wrappers available in the OQ instrument list.

As a general workaround I would sugget you to start looking at OQ2014, which doesn't have this wrapper layer and provides end users with direct access to the core framework.

Regards,
Anton


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group