SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Sat Oct 24, 2020 11:17 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Fri Dec 14, 2012 1:49 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
In my strategy I only use trade and bar daily (length 86400), so that I can know Bar.Open when market open.

The problem is, in live trading, if the strategy stopped and I restart it again, although I have loaded recorded today's trades data(past trades), the barfactory will ignore these historical trades and return Bar.Open using the first incoming trades' price, which is not the actual Open price of the day. (note that I only need today's open, so it is not in historical bars)

Is there a way to ask barfactory process historical trades data, so that I can get today's open price precisely, whenever I restart my strategy? Or is there other way round this problem?


Another question is, is the bar daily with length 86400 working in the same way with daily bar? I mean, when generating this bar in live trading, the incoming bar's open is first trade after 09:30:00, or the first trade after 00:00:00?


Thanks a lot in advance.

Regards,

JC


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 2:03 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

There is no way to pass historical trades into the BarFactory. However, you can obtain historical trades with DataManager.GetHistoricalTrades method and compress them in bars using DataManager.CompressBars method. Then you just need to take the last bar and use its Open price.

Both Daily and 86400 bars start at 00:00:00. If you need them to start at 9:30 please take a look at MarketDataFilter:
viewtopic.php?f=64&t=9740
you can filter trades/quotes that are out of the trading session, so the bars will be built only from the trades inside the trading session.

Regards,
Sergey.


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 2:16 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
Baraz Sergey wrote:
Hi,

There is no way to pass historical trades into the BarFactory. However, you can obtain historical trades with DataManager.GetHistoricalTrades method and compress them in bars using DataManager.CompressBars method. Then you just need to take the last bar and use its Open price.

Both Daily and 86400 bars start at 00:00:00. If you need them to start at 9:30 please take a look at MarketDataFilter:
viewtopic.php?f=64&t=9740
you can filter trades/quotes that are out of the trading session, so the bars will be built only from the trades inside the trading session.

Regards,
Sergey.


Thanks a lot for your quick reply and detailed solution. I will have a try and post my codes here. Thanks a lot!

Regards,

JC


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 3:22 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
There is another issue coming up. I use the following to get today's daily bar from historical recorded trades(today's)

Code:
   DateTime datetime3;
   DateTime datetime4;
   BarSeries todaybar;

   public override void OnStrategyStart()
   {       datetime3 = DateTime.Today;
      datetime4 = DateTime.Now;
      foreach (Trade trade in GetHistoricalTrades(datetime3, datetime4))
         Trades.Add(trade);
      if (Trades.Count!=0)
      {
         todaybar = DataManager.CompressBars(Trades,0,86400);
         //Trades.CompressBars(0,86400);
         Console.Out.WriteLine((reportCount++)+" - OnStart, open price "+todaybar.Last.Open+"--->"+Instrument.Symbol);
      }
         }



And I am using a filter in Scenario to filter all the trades that is not from the trading hour.

However, the on bar open and on bar will not be fired correctly. Because the bar factory still think bar daily with 86400 length start from 00:00:00 and end at 00:00:00. Which means the OnBarOpen will fire on 00:00:00 but not 09:30:00. Is there a way to solve this problem?

Thanks a lot.


Regards,

JC


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 4:05 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

OnBarOpen is fired when the first trade of this bar is emitted. If you filter out all trades from 00:00:00 till 9:30:00 (includng the bounds) the BarOpen event should get fired at 9:30 (check Clock.Now property).

OnBar happens at 00:00:00 in case of Daily or 86400 bars, there is no way to change it. But you can add a timer on 16:00 for example (if session ends at 16:00) and use the Instrument.Bar property to get the latest bar. This bar will not be updated till the midnight because of your MarketDataFilter.

Regards,
Sergey.


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 4:11 pm 
Offline

Joined: Wed Oct 08, 2003 1:06 pm
Posts: 833
Hi,

OnBarOpen is fired when the first trade of this bar is emitted. If you filter out all trades from 00:00:00 till 9:30:00 (includng the bounds) the BarOpen event should get fired at 9:30 (check Clock.Now property).

OnBar happens at 00:00:00 in case of Daily or 86400 bars, there is no way to change it. But you can add a timer (AddTimer method) on 16:00 for example (if session ends at 16:00) and use the Instrument.Bar property to get the latest bar. This bar will not be updated till the midnight because of your MarketDataFilter.

Regards,
Sergey.


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 4:18 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
Baraz Sergey wrote:
Hi,

OnBarOpen is fired when the first trade of this bar is emitted. If you filter out all trades from 00:00:00 till 9:30:00 (includng the bounds) the BarOpen event should get fired at 9:30 (check Clock.Now property).

OnBar happens at 00:00:00 in case of Daily or 86400 bars, there is no way to change it. But you can add a timer on 16:00 for example (if session ends at 16:00) and use the Instrument.Bar property to get the latest bar. This bar will not be updated till the midnight because of your MarketDataFilter.

Regards,
Sergey.


Thanks a lot for your help Sergey, I got it.

Could you please help me check my codes for the market data filter? It is not working, I defined it in both Scenario.cs and code.cs.

In scenario.cs:

Code:
using System;
using System.Collections.Generic;

using OpenQuant.API;
using OpenQuant.API.Engine;

public class MyScenario : Scenario
{
   class MyFilter:MarketDataFilter
   {   
      public override Trade FilterTrade(Trade trade, string symbol)
      {
         if ((trade.DateTime.Hour>=9)&&(trade.DateTime.Minute>=30))
            return trade;
         else
            return null;
      }
      public override Quote FilterQuote(Quote quote, string symbol)
      {
         return quote;
      }
   }
   public override void Run()
   {
      MarketDataProvider.Filter = new MyFilter();
      
      Start();
   }
}


In code.cs:

Code:
   class MyFilter:MarketDataFilter
   {   
      public override Trade FilterTrade(Trade trade, string symbol)
      {
         if ((trade.DateTime.Hour>=9)&&(trade.DateTime.Minute>=30))
            return trade;
         else
            return null;
      }
      public override Quote FilterQuote(Quote quote, string symbol)
      {
         return quote;
      }
   }

   public override void OnStrategyStart()
   {
      MarketDataProvider.Filter = new MyFilter();
        }



Thank you so much for your help.


Best regards,

JC


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 5:10 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
I think these codes working ok. Let me check after market open.

I found this filter can't filter those trades I loaded through GetHistoricalTrades. That's why I thought it was not working.

Thanks a lot for your help!

Regards,

JC


Top
 Profile  
 
PostPosted: Fri Dec 14, 2012 7:50 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
The code is ok. Now I am getting trade data only after 9:30


Top
 Profile  
 
PostPosted: Sat Dec 15, 2012 5:12 am 
Offline

Joined: Mon Nov 12, 2012 2:38 am
Posts: 59
Hi,
Why you add filter at both scenario.cs and code.cs? I guess only scenario.cs is OK


Top
 Profile  
 
PostPosted: Mon Dec 17, 2012 1:22 pm 
Offline

Joined: Tue Sep 11, 2012 12:41 pm
Posts: 41
gzadigo wrote:
Hi,
Why you add filter at both scenario.cs and code.cs? I guess only scenario.cs is OK


OK thanks a lot. I will only keep the filter in scenario.cs

It is just I am not quite clear about the functionality of scenario.cs. I thought it is only for back test, to select different groups of instruments, etc..


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 3 hours


Who is online

Users browsing this forum: No registered users and 2 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