API Support Forum
OEC API > Market Data > How to Use Subscription
Author Topic: How to Use Subscription
(9 messages, Page 1 of 1)
Moderators: VPfau
NVerner2
Posts: 12
Joined: Apr 17, 2012


Posted: May 10, 2012 @ 09:20 AM             Msg. 1 of 9
Hi,

I would like to download a set number of minutes of past data from the OEC Server and use it in my program. I have looked through the forums and the documentation, but I was not able to figure out why I can't access the data. Here is my code:

Subscription sub = OECClient.Global.RequestBars(
mainfrm.m_cli.Contracts["NQM2"], //Contract
DateTime.UtcNow - TimeSpan.FromMinutes(30), //StartDate
DateTime.UtcNow, //EndDate
SubscriptionType.Bar, //SubscriptionType
1); //Interval

The code compiles and runs okay, but I don't see any data in the local variables. The Subscription class has no data fields. Where are the bars? Did I write the code incorrectly? I am writing my code by modifying the Advanced Example and using a simulated account.

Any help would be appreciated. Thanks.

Nathan verner & Fred isabelle
RuslanK
Posts: 69
Joined: Jun 02, 2010


Posted: May 10, 2012 @ 09:28 AM             Msg. 2 of 9
Hello,

Your code initiates subscription, but to receive bars you should handle OnBarsReceivedEvent (please see here -http://www.openecry.com/api/api/OEC.API.OnBarsReceivedEvent.html).

Ruslan Kartokhin
Software Developer
NVerner2
Posts: 12
Joined: Apr 17, 2012


Posted: May 10, 2012 @ 10:27 AM             Msg. 3 of 9
Is this working? I can't see my other post...

Nathan verner & Fred isabelle
NVerner2
Posts: 12
Joined: Apr 17, 2012


Posted: May 10, 2012 @ 10:30 AM             Msg. 4 of 9
Thanks for the quick reply. I've been messing around with the code, but I haven't gotten anything to work. Can you please look at my code and tell me what I'm doing wrong? Thanks.

public void Start()
{

...

Bar[] bars = null;

Subscription sub = OECClient.Global.RequestBars(
mainfrm.m_cli.Contracts["NQM2"], //Contract
DateTime.UtcNow - TimeSpan.FromMinutes(30), //StartDate
DateTime.UtcNow, //EndDate
SubscriptionType.Bar, //SubscriptionType
1); //Interval

mainfrm.m_cli.OnBarsReceived += barReceived(sub, bars);

}

OnBarsReceivedEvent barReceived(Subscription Sub, Bar[] bar)
{
return OnBarsReceivedEvent
}

Nathan verner & Fred isabelle
RuslanK
Posts: 69
Joined: Jun 02, 2010


Posted: May 10, 2012 @ 10:36 AM             Msg. 5 of 9
Change your code related to attaching event handler to this:

mainfrm.m_cli.OnBarsReceived += OnBarsReceived;

Your event handler should look like this:

public void OnBarsReceived(Subscription Subscription, Bar[] Bars)
{
foreach (var bar in Bars)
{
//process bar here
}
}

My good advice - you need to read a book about C# basics at least.

Ruslan Kartokhin
Software Developer
NVerner2
Posts: 12
Joined: Apr 17, 2012


Posted: May 14, 2012 @ 05:17 AM             Msg. 6 of 9
Thanks again for your advice. I was able to get the data. However, for some strange reason, after one request for bars, I get two OnBarsReceived Events. So if I were using your example code, the OnBarsReceived method would be called twice every time a new bar came in. On the first time, the bars are full, but on the second time, the bars are empty, which causes my program to throw an exception. What do you think the problem is, and how should I fix it?

Thanks.

Nathan verner & Fred isabelle
RuslanK
Posts: 69
Joined: Jun 02, 2010


Posted: May 14, 2012 @ 08:06 AM             Msg. 7 of 9
There is no any problem - this is by design:

When application requests historical bars/ticks they come in series of OnBarReceived events with empty dataset at the end of sequence to signal app that no more data should be expected.

When you use RequestBars method - that means you will not get any data after "empty" bars.

In case of using of SubscribeBars method your application will start to receive real-time bars after "empty" bars came.


Ruslan Kartokhin
Software Developer

Edited by RuslanK on May 14, 2012 at 09:23:52
NVerner2
Posts: 12
Joined: Apr 17, 2012


Posted: May 15, 2012 @ 04:09 AM             Msg. 8 of 9
OK, I have it figured out now. Thanks for your help.

Nathan verner & Fred isabelle