Problem with HeikinAshi candles

Hello everyone!

I have an algo that trades HeikinAshi candles. First I tested it on daily bars. Then I changed the equity resolution to hourly bars, keeping the HeikinAshi at Daily.


self.AddEquity(self.symbol, Resolution.Daily)
self.ha = self.HeikinAshi(self.symbol, Resolution.Daily)


self.AddEquity(self.symbol, Resolution.Hour)
self.ha = self.HeikinAshi(self.symbol, Resolution.Daily)

The backtest results vary significantly, so I suspect I'm doing something wrong. I want to keep the resolution of HeikenAshi bars at Daily reguardless of equity resolution, and expect the same backtest result.

Update Backtest

it seems that you have to use consolidators when using hour candles to consolidate to daily.

example of 3 day candle (spy has minute resolution)

Addequity uses minute when no resolution is provided

Problem solved.

I trided to:

1) Subscribe HeikinAshi to consolidated daily bars with RegisterIndicator:

consolidator = TradeBarConsolidator(timedelta(1))
self.SubscriptionManager.AddConsolidator(self.symbol, consolidator)
self.ha = HeikinAshi()
self.RegisterIndicator(self.symbol, self.ha, consolidator)

This throws a runtime error SystemError : <bound method 'RegisterIndicator'> returned a result with an error set. 

2) Update HeikinAshi on DataConsolidated  event:

consolidator.DataConsolidated += self.OnDailyBarHandler
def OnDailyBarHandler(self, sender, bar):

This actually worked, but OnDailyBarHandler fires right after market open. My rebalancing happens before market open, so at that time I always have a one day lagged HeikinAshi candle.

I needed to have a completed daily candle right after market close, so I did the following:

3) Consoidate daily candle manually

def Initialize(self):
self.ha = HeikinAshi()
self.daily_candle = TradeBar()
self.TimeRules.BeforeMarketClose(self.symbol, -1),

def AfterMarketClose(self):
if not self.ha.IsReady: return
# rebalance

def OnData(self, data):
bar = data[self.symbol]
if bar is None: return
if not == # new day
self.daily_candle = TradeBar()
self.daily_candle.Time = bar.Time
self.daily_candle.Period = timedelta(1)
self.daily_candle.Symbol = self.symbol
self.daily_candle.Open = bar.Open
self.daily_candle.High = bar.High
self.daily_candle.Low = bar.Low
self.daily_candle.Value = bar.Close
self.daily_candle.Volume = bar.Volume
self.daily_candle.Time = bar.Time
self.daily_candle.High = max(self.daily_candle.High, bar.High)
self.daily_candle.Low = min(self.daily_candle.Low, bar.Low)
self.daily_candle.Close = bar.Close
self.daily_candle.Value = bar.Close
self.daily_candle.Volume += bar.Volume

Now I get very similar backtest results on any equity resolution.


Update Backtest


The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


This discussion is closed