SmartQuant Discussion

Automated Quantitative Strategy Development, SmartQuant Product Discussion and Technical Support Forums
It is currently Wed Dec 13, 2017 2:47 am

All times are UTC + 3 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GetHistoricalBars + ATR
PostPosted: Wed Jul 27, 2016 9:45 pm 
Offline

Joined: Thu Jun 09, 2016 8:25 pm
Posts: 15
Hello, I've been trying to backtest on historical data downloaded onto OQ 2014. My data has 1-minute bars, whose number per day is not consistent due to differing volumes of trading. Hence, I've managed to use the GetHistoricalBars to count the number of bars between datetimes, and then using the count to construct ATR indicators.

However, the ATR fails to draw on the chart. Putting GetHistoricalBars on OnBar takes ages to load. Is there another method I should be using?

Code:
      protected override void OnStrategyStart()
      {
         datetime1 = new DateTime(2016, 07, 26);
         datetime2 = datetime1.AddDays(-30);
         datetime3 = datetime1.AddDays(-60)

         bars1 = new BarSeries();
         bars2 = new BarSeries();

         // 30 day ATR
         foreach (Bar bar1 in DataManager.GetHistoricalBars(Instrument, datetime2, datetime1, BarType.Time, 60))
            bars1.Add(bar1);
         
         // 60 day ATR
         foreach (Bar bar2 in DataManager.GetHistoricalBars(Instrument, datetime3, datetime1, BarType.Time, 60))
            bars2.Add(bar2);

         atr1 = new ATR(bars1, bars1.Count);
         atr2 = new ATR(bars2, bars2.Count);

         AddGroups();
      }

      protected override void OnBar(Instrument instrument, Bar bar)
      {
         // Add bars to group.
         Bars.Add(bar);
         bars1.Add(bar);
         bars2.Add(bar);

         // Log atr1 and atr2.
         Log(atr1.Last, atr1Group);
         Log(atr2.Last, atr2Group);
      }

      protected override void OnFill(Fill fill)
      {
         // Add fill to group.
         Log(fill, fillGroup);
      }

      private void AddGroups()
      {
         // Create bars group.
         barsGroup = new Group("Bars");
         barsGroup.Add("Pad", DataObjectType.String, 0);
         barsGroup.Add("SelectorKey", Instrument.Symbol);
         
         // Create fills group.
         fillGroup = new Group("Fills");
         fillGroup.Add("Pad", 0);
         fillGroup.Add("SelectorKey", Instrument.Symbol);
         
         // Create equity group.
         equityGroup = new Group("Equity");
         equityGroup.Add("Pad", 2);
         equityGroup.Add("SelectorKey", Instrument.Symbol);
   
         // Create atr1 values group.
         atr1Group = new Group("atr1");
         atr1Group.Add("Pad", 1);
         atr1Group.Add("SelectorKey", Instrument.Symbol);
         atr1Group.Add("Color", Color.Blue);

         // Create atr2 values group.
         atr2Group = new Group("atr2");
         atr2Group .Add("Pad", 1);
         atr2Group .Add("SelectorKey", Instrument.Symbol);
         atr2Group .Add("Color", Color.Green);
      
         // Create log monitor groups.
         closeMonitorGroup = new Group("Close");
         closeMonitorGroup.Add("LogName", "Close");
         closeMonitorGroup.Add("StrategyName", "MyStrategy");
         closeMonitorGroup.Add("Symbol", Instrument.Symbol);
         
         openMonitorGroup = new Group("Open");
         openMonitorGroup.Add("LogName", "Open");
         openMonitorGroup.Add("StrategyName", "MyStrategy");
         openMonitorGroup.Add("Symbol", Instrument.Symbol);

         positionMonitorGroup = new Group("Position");
         positionMonitorGroup.Add("LogName", "Position");
         positionMonitorGroup.Add("StrategyName", "MyStrategy");
         positionMonitorGroup.Add("Symbol", Instrument.Symbol);

         // Add groups to manager.
         GroupManager.Add(barsGroup);
         GroupManager.Add(atr1Group);
         GroupManager.Add(atr2Group);
         GroupManager.Add(fillGroup);
         GroupManager.Add(equityGroup);
         GroupManager.Add(closeMonitorGroup);
         GroupManager.Add(openMonitorGroup);
         GroupManager.Add(positionMonitorGroup);
      }
   }





Top
 Profile  
 
PostPosted: Fri Jul 29, 2016 9:59 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 392
You are using not correct approach.
Code:
         atr1 = new ATR(bars1, bars1.Count);
         atr2 = new ATR(bars2, bars2.Count);

This means that period of ATR will be equal to Count of elements in array instead amount of days.

You should normalize your bars, probably better will compress daily array of 1min bars to daily bars and next use compressed bars in indicators.

By this reason you should not add new 1min bars to bars1/bars2 arrays.


Top
 Profile  
 
PostPosted: Fri Jul 29, 2016 5:11 pm 
Offline

Joined: Thu Jun 09, 2016 8:25 pm
Posts: 15
The bars.Count will retrieve the count of elements with a specified number of days (30, 60). Is there a way of going about it such that I won't have to compress into daily?

I thought about adding data into gaps of data to get an constant number of bars within a day, and somehow using a bar filter to prevent trades from occurring on those data. But this nevertheless will result in problems in ATR calculations.

Is the solution changing the market data provider?


Top
 Profile  
 
PostPosted: Mon Aug 01, 2016 11:42 am 
Offline

Joined: Wed May 05, 2010 9:49 pm
Posts: 392
Adding/removing 1min bars for normalizing amount will affect on calculation of indicators.
So I choose compression of bars.


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

All times are UTC + 3 hours


Who is online

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