SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Tue Dec 10, 2019 6:40 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Accessing Live Bar data
PostPosted: Mon Oct 07, 2019 9:17 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
Hi Team,

I am trying to access Live market data (Bar) using IQFeed. Below is my sample program:

Code:
public partial class IQFeedLiveDataDemo : Form
  {
    Framework framework;
    IDataProvider dataProvider;
    long totalBarCount = 0;

    List<Instrument> subscribedInstruments = new List<Instrument>();

    public IQFeedLiveDataDemo()
    {
      InitializeComponent();
     
      framework = Framework.Current;
      dataProvider = framework.ProviderManager.GetDataProvider("IQFeed");

      dataProvider.Connect();

      framework.EventManager.Dispatcher.Bar += Dispatcher_Bar;
      framework.EventManager.Dispatcher.Trade += Dispatcher_Trade;
      framework.EventManager.Dispatcher.Ask += Dispatcher_Ask;
      framework.EventManager.Dispatcher.BarOpen += Dispatcher_BarOpen;
      framework.EventManager.Dispatcher.Bid += Dispatcher_Bid;
     
    }

    private void Dispatcher_Bid(object sender, Bid bid)
    {
     
    }

    private void Dispatcher_BarOpen(object sender, Bar bar)
    {
     
    }

    private void Dispatcher_Ask(object sender, Ask ask)
    {
     
    }

    private void Dispatcher_Trade(object sender, Trade trade)
    {
     
    }

    private void Dispatcher_Bar(object sender, Bar bar)
    {
      dataGridViewResult.BeginInvoke(new Action(() => dataGridViewResult.Rows.Add(framework.InstrumentManager.GetById(bar.InstrumentId).Symbol, bar.OpenDateTime, bar.CloseDateTime, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume)));
      totalBarCount++;
      labelTotalCount.BeginInvoke(new Action(() => labelTotalCount.Text = string.Format($"Total: {totalBarCount,8}")));
    }

    private void buttonOk_Click(object sender, EventArgs e)
    {
      UnsubscribeAll();

      dataGridViewResult.Rows.Clear();
      totalBarCount = 0;

      var symbols = textBoxSymbol.Text.Split(',').Select(s => s.Trim());

      foreach (var symbol in symbols)
      {
        Instrument instrument = framework.InstrumentManager[symbol];       

        if (instrument != null)
        {
          dataProvider.Subscribe(instrument);
          subscribedInstruments.Add(instrument);
        }
        else
        {
          MessageBox.Show($"Instrument with symbol {symbol} doesn't exist. Skipping!");
        }
      }
    }

    private void UnsubscribeAll()
    {
      foreach (var instrument in subscribedInstruments)
      {
        dataProvider.Unsubscribe(instrument);
      }

      subscribedInstruments.Clear();
    }
  }


But when I subscribe say for AAPL, only Ask/Bid events are triggered, never Bar. Where can I specify Bar size for live market data?

I was not able to set DataProvider property to my IQFeed provider instance since only get is allowed for this property. I also tried multiple ways like setting Provider property of instrument to my provider instance and subscribing using framework.DataProvider.Subscribe(instrument), but nothing worked for me.

Can you please suggest what I might be missing? Please let me know if you need more information from my side.


Top
 Profile  
 
PostPosted: Tue Oct 08, 2019 11:03 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
Hi,
usually we creating bars using our BarFactory. So provider does not provide bars for you.
As in sample strategies, just add new request to BarFactory, eg:
Code:
BarFactory.Add(instrument1, BarType.Time, barSize);


IQFeed has ability to send bars in realtime.
For using realtime bars, you should set IQFeed.UseIntervalBars to true.
In addition, here is a Requests property where you can specify your requests.

Fields:
BeginFilterTime/EndFilterTime = HHmmSS (optional)
IntervalType = 's' (default or empty) for time intervals in seconds, 'v' for volume intervals, 't' for tick intervals
Interval = x, value of interval according IntervalType

Finally, after subscribing to instrument, IQFeed provider will send request for necessary interval bars.


Top
 Profile  
 
PostPosted: Tue Oct 08, 2019 4:51 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
BarFactory is only available under Scenario object, not in framework, right?

I tried the other method you suggested using IQFeed class but still, it didn't hit Bar event.

Please find my latest code below:

Code:
  public partial class IQFeedLiveDataDemo : Form
  {
    Framework framework;
    IQFeed iQFeed;
    long totalBarCount = 0;

    List<Instrument> subscribedInstruments = new List<Instrument>();

    public IQFeedLiveDataDemo()
    {
      InitializeComponent();
     
      framework = Framework.Current;

      iQFeed = new IQFeed(framework);
      iQFeed.UseIntervalBars = true;
      IntervalBarRequestUser intervalBarRequestUser = new IntervalBarRequestUser();
      intervalBarRequestUser.IntervalType = 's';
      intervalBarRequestUser.Interval = 5;
      iQFeed.Requests = new[] { intervalBarRequestUser };
      iQFeed.Connect();

      framework.EventManager.Dispatcher.Bar += Dispatcher_Bar;
      framework.EventManager.Dispatcher.Trade += Dispatcher_Trade;
      framework.EventManager.Dispatcher.Ask += Dispatcher_Ask;
      framework.EventManager.Dispatcher.BarOpen += Dispatcher_BarOpen;
      framework.EventManager.Dispatcher.Bid += Dispatcher_Bid;
     
    }

    private void Dispatcher_Bid(object sender, Bid bid)
    {
     
    }

    private void Dispatcher_BarOpen(object sender, Bar bar)
    {
     
    }

    private void Dispatcher_Ask(object sender, Ask ask)
    {
     
    }

    private void Dispatcher_Trade(object sender, Trade trade)
    {
     
    }

    private void Dispatcher_Bar(object sender, Bar bar)
    {
      dataGridViewResult.BeginInvoke(new Action(() => dataGridViewResult.Rows.Add(framework.InstrumentManager.GetById(bar.InstrumentId).Symbol, bar.OpenDateTime, bar.CloseDateTime, bar.Open, bar.High, bar.Low, bar.Close, bar.Volume)));
      totalBarCount++;
      labelTotalCount.BeginInvoke(new Action(() => labelTotalCount.Text = string.Format($"Total: {totalBarCount,8}")));
    }

    private void buttonOk_Click(object sender, EventArgs e)
    {
      UnsubscribeAll();

      dataGridViewResult.Rows.Clear();
      totalBarCount = 0;

      var symbols = textBoxSymbol.Text.Split(',').Select(s => s.Trim());

      foreach (var symbol in symbols)
      {
        Instrument instrument = framework.InstrumentManager[symbol];

        if (instrument != null)
        {
          subscribedInstruments.Add(instrument);
        }
        else
        {
          MessageBox.Show($"Instrument with symbol {symbol} doesn't exist. Skipping!");
        }
      }
    }

    private void UnsubscribeAll()
    {
      foreach (var instrument in subscribedInstruments)
      {
        iQFeed.Unsubscribe(instrument);
      }

      subscribedInstruments.Clear();
    }
  }


Please let me know what I am missing here.


Top
 Profile  
 
PostPosted: Wed Oct 09, 2019 9:42 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
Bar factory available through framework.EventManager.BarFactory

Your code has no subscriptions, you fill subscribedInstruments but it stay unused.


Top
 Profile  
 
PostPosted: Thu Oct 10, 2019 6:01 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
Sorry, I might have removed subscribe statement mistakenly during some cleanup. Even after I added this code, only Ask/Bid events are hit, not Bar.

The code is below:

Code:
private void buttonOk_Click(object sender, EventArgs e)
    {
      UnsubscribeAll();

      dataGridViewResult.Rows.Clear();
      totalBarCount = 0;

      var symbols = textBoxSymbol.Text.Split(',').Select(s => s.Trim());

      foreach (var symbol in symbols)
      {
        Instrument instrument = framework.InstrumentManager[symbol];

        if (instrument != null)
        {
          iQFeed.Subscribe(instrument);
          subscribedInstruments.Add(instrument);
        }
        else
        {
          MessageBox.Show($"Instrument with symbol {symbol} doesn't exist. Skipping!");
        }
      }
    }


I am subscribing fo AAPL from my GUI text control.

Also, what provider BarFactory will use if I subscribe for Live data using BarFactory. I see we cannot set DataProvider property using Framework instance, it is ready-only property.

Thanks,


Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 9:55 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
I cannot debug your code now because I do not have an IQFeed account.
I can advise you to verify IQFeed bars using strategy with OQ2014. This will let you know, if it works or not at all.


Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 1:20 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
I remember that I was able to run Live strategy with IQFeed as Data Provider in OQ2014.

But our objective at the moment is to test and verify the capabilities of the framework in simplest way possible which includes being able to get live data from IQFeed without actually running a strategy.

Also, what provider BarFactory will use if I subscribe for Live data using BarFactory. I see we cannot set DataProvider property using Framework instance, it is ready-only property. How can we set Data Provider for framework.EventManager.BarFactory?


Last edited by harsh on Fri Oct 11, 2019 4:42 pm, edited 1 time in total.

Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 2:45 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
harsh wrote:
I remember that I was able to run Live strategy with IQFeed as Data Provider in OQ2014.

But out objective at the moment is to test and verify the capabilities of the framework in simplest way possible which includes being able to get live data from IQFeed without actually running a strategy.

Also, what provider BarFactory will use if I subscribe for Live data using BarFactory. I see we cannot set DataProvider property using Framework instance, it is ready-only property. How can we set Data Provider for framework.EventManager.BarFactory?

BarFactory.Add(...) contains providerId, just use it.


Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 6:04 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
I also tried using BarFactory but this way it didn't trigger any event at all.

Code:
framework.EventManager.BarFactory.Add(instrument, BarType.Tick, 1, providerId: 4);


I am using the Id field from IB properties I see in OQ2014.

Do I need to make any additional calls to make this work?


Thanks,
Shashwat


Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 6:46 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
New release 1.0.7222 available for downloading, fixes with IQFeed.
I tested interval bars using DemoApp with disabling BarFactory, all ok.
By default DemoApp subscribe bar using BarFactory and it works too.


Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 7:10 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
Thanks, I will download and test with latest release.

Where is that DemoApp? Can I have access to it?


Top
 Profile  
 
PostPosted: Fri Oct 11, 2019 9:07 pm 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
harsh wrote:
Thanks, I will download and test with latest release.

Where is that DemoApp? Can I have access to it?

It is placed in the following folder:
C:\Program Files\SmartQuant Ltd\OpenQuant 2014\Samples\DemoApp


Top
 Profile  
 
PostPosted: Tue Oct 15, 2019 6:10 pm 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
Hi skuvv,

Bar event is now getting triggered with IQFeed Subscribe call normally after I updated to the latest release.

But I was not able to get live data from usign BarFactory.Add(...). Bar event will not be triggered.

I am adding instrument correctly with provider id (taken from OQ for IQFeed). Can you please share all the necessary steps/calls need to subscribe for live data using BarFactory with IQFeed?


Top
 Profile  
 
PostPosted: Wed Oct 16, 2019 11:23 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 562
Common usage scenario(according DemoApp):
subscribe instrument:
Code:
framework.SubscriptionManager.Subscribe(DataProvider, instrument);

add barfactory item:
Code:
framework.EventManager.BarFactory.Add(instrument, BarType.Time, barSize);


Don't do it directly with newly created instance of barfactory/provider or they will not be linked with framework.


Top
 Profile  
 
PostPosted: Thu Oct 17, 2019 9:26 am 
Offline

Joined: Mon Sep 02, 2019 10:31 am
Posts: 27
Do we need to call both, Subscribe from SubscriptionManager and Add from EventManager.BarFactory? or these are two ways for subscribing the live data?

If both statements are needed then do we still need to pass providerId in BarFactory.Add(..) call as you suggested earlier?

Thanks,


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 19 posts ]  Go to page 1, 2  Next

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 4 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:  
Powered by phpBB® Forum Software © phpBB Group