SmartQuant Discussion
http://www.smartquant.com/forums/

Error in loading symbols to project
http://www.smartquant.com/forums/viewtopic.php?f=44&t=10942
Page 1 of 1

Author:  shomen [ Sun Apr 19, 2015 11:35 am ]
Post subject:  Error in loading symbols to project

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?

Author:  Alexei Kurov [ Sun Apr 19, 2015 12:57 pm ]
Post subject:  Re: Error in loading symbols to project

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]];

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/