SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Fri Dec 13, 2019 4:24 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Fri Oct 18, 2019 5:45 am 
Offline

Joined: Wed Aug 07, 2019 7:49 am
Posts: 20
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.


Top
 Profile  
 
PostPosted: Fri Oct 18, 2019 11:19 am 
Offline

Joined: Wed Aug 07, 2019 7:49 am
Posts: 20
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;
}


Top
 Profile  
 
PostPosted: Fri Oct 18, 2019 3:53 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
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.


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: No registered users and 3 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