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

GetHistoricalBars + ATR
http://www.smartquant.com/forums/viewtopic.php?f=60&t=14597
Page 1 of 1

Author:  haroldmaj [ Wed Jul 27, 2016 9:45 pm ]
Post subject:  GetHistoricalBars + ATR

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);
      }
   }




Author:  skuvv [ Fri Jul 29, 2016 9:59 am ]
Post subject:  Re: GetHistoricalBars + ATR

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.

Author:  haroldmaj [ Fri Jul 29, 2016 5:11 pm ]
Post subject:  Re: GetHistoricalBars + ATR

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?

Author:  skuvv [ Mon Aug 01, 2016 11:42 am ]
Post subject:  Re: GetHistoricalBars + ATR

Adding/removing 1min bars for normalizing amount will affect on calculation of indicators.
So I choose compression of bars.

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