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

how to use IInstrumentProvider
http://www.smartquant.com/forums/viewtopic.php?f=60&t=14973
Page 1 of 1

Author:  ambl [ Fri Oct 18, 2019 5:45 am ]
Post subject:  how to use IInstrumentProvider

Hi could someone please provide an example on how to use the api to search for an instrument with an IInstrumentProvider. I don't think an example is given in the documentation.

thanks.

Author:  ambl [ Fri Oct 18, 2019 11:19 am ]
Post subject:  Re: how to use IInstrumentProvider

This is a synchronous solution I came up with that seems to work. Im wondering if I'm doing it sensibly and using it as it was intended to be used? Any comments from experienced users?

public List<Instrument> Search(IInstrumentProvider provider, InstrumentType type, string exchange, string symbol, byte currencyId)
{
Console.WriteLine($"Search({provider}, {type}, {exchange} ,{symbol} ,{currencyId})");
List<Instrument> found = new List<Instrument>();
string requestId = "${provider}.{type}.{exchange}.{symbol}.{currencyId}";
InstrumentDefinitionRequest definitionRequest = new InstrumentDefinitionRequest()
{
FilterExchange = exchange,
FilterSymbol = symbol,
FilterType = type,
Id = requestId
};
bool spin = true;

void OnInstrumentDefinition(object sender, InstrumentDefinitionEventArgs args)
{
Console.WriteLine($"OnInstrumentDefinition({args.Definition})");
if (args.Definition.RequestId == requestId)
{
foreach (Instrument instrument in args.Definition.Instruments)
{
if (instrument.CurrencyId == currencyId)
{
found.Add(instrument);
}
}
Framework.EventManager.Dispatcher.InstrumentDefinition -= OnInstrumentDefinition;
spin = false;
}
}

Framework.EventManager.Dispatcher.InstrumentDefinition += OnInstrumentDefinition;

if (!provider.IsConnected)
{
provider.Connect();
}
provider.Send(definitionRequest);


while (spin)
{ }
return found;
}

Author:  skuvv [ Fri Oct 18, 2019 3:53 pm ]
Post subject:  Re: how to use IInstrumentProvider

Hi,
InstrumentProvider will send two events InstrumentDefinition and InstrumentDefinitionEnd.
InstrumentDefinitionEnd means that your request completed.
InstrumentDefinition contains information about instrument.
In general your code is right, you send request and wait/process events.

this line "Framework.EventManager.Dispatcher.InstrumentDefinition -= OnInstrumentDefinition;" should be moved to InstrumentDefinitionEnd, otherwise you can skip the futher InstrumentDefinitions.

instead of 'spin' it will better to wait InstrumentDefinitionEnd.

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