SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Thu Feb 20, 2020 6:41 pm

All times are UTC + 3 hours




Post new topic Reply to topic  [ 32 posts ]  Go to page 1, 2, 3  Next
Author Message
PostPosted: Fri Nov 30, 2007 3:05 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
The code below pre-loads five last days of 1min bars from historical data base to strategy bar series on startup. Then it creates and draws SMA indicator on this series.

In this scenario you don't need to wait (128 minutes in this example) to run a strategy in live or paper trading mode. Indeed you should have historical data in the data base to use this technique. Note that you can capture real time data into historical data base when you run a strategy live, see http://www.smartquant.com/forums/viewto ... 7031#17031

Code:
using System;
using System.Drawing;

using OpenQuant.API;
using OpenQuant.API.Indicators;

public class MyStrategy : Strategy
{
   SMA sma;
   
   DateTime datetime1;
   DateTime datetime2;
       
   public override void OnStrategyStart()
   {
      datetime2 = DateTime.Now;
      datetime1 = datetime2.AddDays(-5); 

      foreach (Bar bar in GetHistoricalBars(datetime1, datetime2, BarType.Time, 60))
         Bars.Add(bar);             
       
      sma = new SMA(Bars, 128);

      Draw(sma);
   }
}


This code gets historical bars from the local OpenQuant data base. Note that you can modify the code so that it will download historical data from a historical data provider instead (for example IB).

Code:
GetHistoricalBars("IB", datetime1, datetime2, BarType.Time, 60);


Last edited by Dr. Anton Fokin on Tue Aug 19, 2008 12:20 pm, edited 4 times in total.

Top
 Profile  
 
 Post subject: Preload quote data
PostPosted: Fri Mar 07, 2008 7:35 am 
Offline

Joined: Sun Dec 09, 2007 7:01 am
Posts: 15
How do I preload quote data? There is no Quotes.Add(quote) like Bars.Add(bar)
In the same light how do I preload bars that are built from quote data?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 15, 2008 1:16 am 
Offline

Joined: Thu Mar 16, 2006 12:15 pm
Posts: 184
How to preload data with the timeframe as indicated in the project?
Or are e.g. the 5min bars then build from preloaded 1minbars?

_________________
Expect the unexpected. May your MM/RM be with you.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 11:48 am 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
fliesch wrote:
How to preload data with the timeframe as indicated in the project?
Or are e.g. the 5min bars then build from preloaded 1minbars?


Load 1 min bars and then use BarSeries.Compress to make 5 min bars. Then add 5 min bars to Strategy.Bars.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 16, 2008 3:32 pm 
Offline

Joined: Thu Mar 16, 2006 12:15 pm
Posts: 184
exact code?

How to access the timeframe chosen for a project? (in order not to have to change the timeframe in the code while changing timeframes in the menu?)

_________________
Expect the unexpected. May your MM/RM be with you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 17, 2008 6:35 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
You can access the list of data requests in a Strartegy :

Code:
   BarRequest barRequest = null;
      
      foreach (BarRequest request in DataRequests.BarRequests)
         barRequest = request;
      
      Console.WriteLine(barRequest.BarSize);


Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 7:17 pm 
Offline

Joined: Mon Feb 04, 2008 6:58 pm
Posts: 24
Anton,

A minor glitch. You define the sma variable:
Code:
SMA sma;


Shouldn't that be:
Code:
SMA sma1;
?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 02, 2008 4:35 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Indeed. Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 4:46 pm 
Offline

Joined: Thu Jan 31, 2008 11:39 am
Posts: 166
I am using 2 second bars in our solution, and I want to pre-load data at the start of paper/live trading (to have the respective indicators ready from start).

But the data is not loaded to the strategy. I use this code:

Code:
   DateTime datetime2 = DateTime.Now;          
         DateTime datetime1 = datetime2.AddDays(-3); 

         foreach (Bar bar in GetHistoricalBars(datetime1, datetime2, BarType.Time, 2))
            Bars.Add(bar);   
         Console.WriteLine(Bars.Count);


Historical Data is stored in the Instrument, but Console.WriteLine(Bars.Count) returns 0...why?

The trade data is stored in the OnTrade event using DataManager.Add(this.Instrument,trade). Is it because I am storing trades and not bars?
Can you suggest something?

Edit: Will I see the pre loaded bars in the chart?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 7:36 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
If you load bars from historical data base, you should have bars in the database.

Yes, you should see it on the chart.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 22, 2008 10:44 pm 
Offline

Joined: Tue May 03, 2005 9:36 pm
Posts: 136
Location: Sydney
I don't know if this will work but try using GetHistoricalTrades instead.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 8:23 am 
Offline

Joined: Thu Jan 31, 2008 11:39 am
Posts: 166
Thx. I can make it work by compressing the Trades in the DataManager to 2 Second Bars.

I thought storing the Trades would be enough, and the program would build the 2 Second Bars out of the trades.

I assume if I want to pre-load 2 Second Bars, then I have to store the data as 2 Second Bars, and not Trades, if I don't want to manually compress the Trades to 2 Second Bars every day before startup - is that correct?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 23, 2008 12:57 pm 
Offline

Joined: Tue Aug 05, 2003 3:43 pm
Posts: 6816
Yes, you just set up your live strategy to generate 2 sec bars from trades and call DataManager.Add(bar) together with DataManager.Add(trade). Easy.

Regards,
Anton


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 6:17 am 
Offline

Joined: Fri Apr 04, 2008 6:11 am
Posts: 138
Is there a quick way to calculate the necessary time offset by the required number of bars?

For example, in my strategy I need 200 bars of daily data before any trades are generated. At first I thought this solution would work fine:
Code:
DateTime now = DateTime.Now;
DateTime then = now.AddDays(-200);

foreach (Bar bar in GetHistoricalBars(then, now, BarType.Time, 86400))
   Bars.Add(bar);


But then I realized that 200 daily bars and 200 days ago are not the same length. Weekends and market holidays need to be taken into account to correctly calculate the desired DateTime offset. 200 daily bars is more along the lines of 290 days ago. Anyone have a quick solution for that (besides just preloading a larger amount of bars than needed)?


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 09, 2008 6:38 am 
Offline

Joined: Fri Apr 04, 2008 6:11 am
Posts: 138
Also, out of curiosity, why aren't the Daily requests listed in the DataRequests.BarRequests collection?

I couldn't figure out why the code below wasn't printing anything (I only had Trade and Daily listed under Market Data), but then I threw in a 1-min bar request and got the output I was expecting (but the daily bar request still wasn't present).

Code:
foreach (BarRequest request in DataRequests.BarRequests)
   Console.WriteLine("Need to preload bars of size " + request.BarSize);   

// only the 1-min bar request is outputted



When I run the strategy for real, I always use daily bars but sometimes I'll use 1, 5, or 10-sec bars to test it on my paper trading account. So I don't have to modify the code each time I switch the bar size, I'm using the current code to handle the preloading.

Code:
   private void PreloadHistoricalBars()
   {
      DateTime now = DateTime.Now;
      DateTime then = now.AddDays(-290);
      
      foreach (BarRequest request in DataRequests.BarRequests)
         PreloadHistoricalBars(then, now, request.BarType, request.BarSize);
      
      if (DataRequests.HasDailyRequest)
         PreloadHistoricalBars(then, now, BarType.Time, 86400);
   }
   
   private void PreloadHistoricalBars(DateTime then, DateTime now, BarType barType, long barSize)
   {
      foreach (Bar bar in GetHistoricalBars(then, now, barType, barSize))
         Bars.Add(bar);
   }


Is there a way I can write that so I don't have to handle the "HasDailyRequest" separately from the DataRequests.BarRequests in the top method?


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

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 1 guest


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