SmartQuant Discussion
http://www.smartquant.com/forums/

Get bar.Open after market opened with recorded trades data
http://www.smartquant.com/forums/viewtopic.php?f=60&t=10419
Page 1 of 1

Author:  jchen [ Fri Dec 14, 2012 1:49 pm ]
Post subject:  Get bar.Open after market opened with recorded trades data

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

Author:  Baraz Sergey [ Fri Dec 14, 2012 2:03 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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.

Author:  jchen [ Fri Dec 14, 2012 2:16 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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

Author:  jchen [ Fri Dec 14, 2012 3:22 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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

Author:  Baraz Sergey [ Fri Dec 14, 2012 4:05 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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.

Author:  Baraz Sergey [ Fri Dec 14, 2012 4:11 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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.

Author:  jchen [ Fri Dec 14, 2012 4:18 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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

Author:  jchen [ Fri Dec 14, 2012 5:10 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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

Author:  jchen [ Fri Dec 14, 2012 7:50 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

The code is ok. Now I am getting trade data only after 9:30

Author:  gzadigo [ Sat Dec 15, 2012 5:12 am ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

Hi,
Why you add filter at both scenario.cs and code.cs? I guess only scenario.cs is OK

Author:  jchen [ Mon Dec 17, 2012 1:22 pm ]
Post subject:  Re: Get bar.Open after market opened with recorded trades da

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..

Page 1 of 1 All times are UTC + 3 hours
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/