SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Nov 21, 2018 3:21 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Sun Apr 19, 2015 11:35 am 
Offline

Joined: Thu Jan 22, 2015 6:23 pm
Posts: 11
Hi,
I'm running this following code in scenario.
The code fails only when the two 2 instruments are not exists in DB (InstrumentManager) and the failure is just after calling to Start().

class TesterScenario : Scenario
{
string ProjectName { get { return Solution.Projects[0].Name; } }
OpenQuant.API.Engine.InstrumentList ProjectInstruments { get { return Solution.Projects[0].Instruments; } }

public override void Run()
{
try
{
LoadInstruments(InstrumentType.Stock);
Start();
}
catch (Exception ex)
{
Console.WriteLine("Error in Scenario(..) -> {0}", ex.Message);
}
finally
{
Console.WriteLine("Strategy Ended");
}
}

private void LoadInstruments(InstrumentType instrumentType)
{
try
{
//clear all current instruments exists in project
ProjectInstruments.Clear();

//create and add two 2 instruments to strategy
var instruments = new List<string>() { "AA,,USD,SMART,1.00,0.01", "AAL,,USD,SMART,1.00,0.01" };

foreach (string ins in instruments)
{
var cols = ins.Split(',');
if (cols.Count() == 6)
{
//check if instrument exists in OQ database
var exists = InstrumentManager.Instruments.Cast<Instrument>().Where(i => i.Type == instrumentType && i.Symbol == cols[0]).FirstOrDefault();
if (exists == null)
{
//create the new instrument in OQ database
exists = new Instrument(instrumentType, cols[0], cols[3], cols[2])
{
Description = cols[1],
Factor = double.Parse(cols[4]),
TickSize = double.Parse(cols[5]),
};
}
else
{
//update existing instrument
exists.Description = cols[1];
exists.Currency = cols[2];
exists.Exchange = cols[3];
exists.Factor = double.Parse(cols[4]);
exists.TickSize = double.Parse(cols[5]);
}

//check if not already inserted
if (!ProjectInstruments.Cast<Instrument>().Any(i => i.Symbol == exists.Symbol))
ProjectInstruments.Add(exists);
}
}

var count = ProjectInstruments.Count;
if (count <= instruments.Count())
Console.WriteLine("{0} instruments loaded to strategy", count);
}
catch(Exception ex)
{
Console.WriteLine("Error::LoadInstruments() - {0}", ex.Message);
throw;
}
}
}

I can't figure out why i'm getting error = "Value cannot be null.Parameter name: key".
Can you please advise?


Top
 Profile  
 
PostPosted: Sun Apr 19, 2015 12:57 pm 
Offline
Site Admin

Joined: Thu Jul 17, 2003 10:39 am
Posts: 1478
Hi,

This is a wellknown issue with instrument's creation from the code.
After calling a constructor, variable does not point to the correct object instance.
Change your code like this:
Code:
//create the new instrument in OQ database
   new Instrument(instrumentType, cols[0], cols[3], cols[2])
   {
      Description = cols[1],
      Factor = double.Parse(cols[4], CultureInfo.InvariantCulture),
      TickSize = double.Parse(cols[5], CultureInfo.InvariantCulture),
   };
                  
   exists = InstrumentManager.Instruments[cols[0]];

_________________
SmartQuant Development Team


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

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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