I am using the QuantConnect platform to implement some trading strategies and I am currently trying to run some tests on custom time frames (4h, 8h, 12h etc.) using consolidators, but the indicator values differ for the same time resolution using consolidators vs. no consolidators. I have isolated the problem to this simple example, where I just create 2 EMA indicators on a 1 h time frame, one using a consolidator and the other one with no consolidator and log their values, which are different.
Thanks for any feedback.
namespace QuantConnect
{
public class ConsolidatorAlgorithm : QCAlgorithm
{
private readonly Resolution _resolution = Resolution.Hour;
private readonly string _ticker = "ETHUSD";
private readonly int _startingCash = 2000;
private readonly int _fastPeriod = 12;
private ExponentialMovingAverage _fastEmaCustomTimeFrame;
private ExponentialMovingAverage _fastEmaStandardResolution;
private string _baseSymbol;
public override void Initialize()
{
SetStartDate(2017, 1,1); //Set Start Date
SetEndDate(2017, 1, 2); //Set End Date
SetCash(_startingCash); //Set Strategy Cash
QuantConnect.Securities.Crypto.Crypto crypto = AddCrypto(_ticker, _resolution);
_baseSymbol = crypto.BaseCurrencySymbol;
SetBrokerageModel(BrokerageName.Bitfinex, AccountType.Cash);
TradeBarConsolidator consolidator = new TradeBarConsolidator(TimeSpan.FromHours(1));
SubscriptionManager.AddConsolidator(_ticker, consolidator);
consolidator.DataConsolidated += OnCustomHandler;
_fastEmaCustomTimeFrame = EMA(_ticker, _fastPeriod);
_fastEmaStandardResolution = EMA(_ticker, _fastPeriod, _resolution);
RegisterIndicator(_ticker, _fastEmaCustomTimeFrame, consolidator);
var history = History<TradeBar>(_ticker, 12);
foreach (var bar in history) {
_fastEmaCustomTimeFrame.Update(bar.EndTime, bar.Close);
_fastEmaStandardResolution.Update(bar.EndTime, bar.Close);
}
}
public void OnCustomHandler(object sender, TradeBar data)
{
if (!_fastEmaCustomTimeFrame.IsReady && !_fastEmaStandardResolution.IsReady) {
return;
}
Log($"ema custom time frame: {_fastEmaCustomTimeFrame}");
}
public void OnData(TradeBars data)
{
if (!_fastEmaCustomTimeFrame.IsReady && !_fastEmaStandardResolution.IsReady) {
return;
}
Log($"ema standard time resolution: {_fastEmaStandardResolution}");
}
}
}
Log output:
2017-01-01 00:00:00 : Launching analysis for a7d9bc8bc4829b1ba77ee9753d0f3cdc with LEAN Engine v2.4.0.0.6246
2017-01-01 00:00:00 : ema custom time frame: 8.32998
2017-01-01 00:00:00 : ema standard time resolution: 8.32998
2017-01-01 01:00:00 : ema custom time frame: 8.36372
2017-01-01 01:00:00 : ema standard time resolution: 8.33768
2017-01-01 02:00:00 : ema custom time frame: 8.38526
2017-01-01 02:00:00 : ema standard time resolution: 8.36111
2017-01-01 03:00:00 : ema custom time frame: 8.4027
2017-01-01 03:00:00 : ema standard time resolution: 8.36863
2017-01-01 04:00:00 : ema custom time frame: 8.40478
2017-01-01 04:00:00 : ema standard time resolution: 8.375
2017-01-01 05:00:00 : ema custom time frame: 8.40164
2017-01-01 05:00:00 : ema standard time resolution: 8.37577
2017-01-01 06:00:00 : ema custom time frame: 8.3678
2017-01-01 06:00:00 : ema standard time resolution: 8.34873
2017-01-01 07:00:00 : ema custom time frame: 8.32476
2017-01-01 07:00:00 : ema standard time resolution: 8.33046
2017-01-01 08:00:00 : ema custom time frame: 8.29785
2017-01-01 08:00:00 : ema standard time resolution: 8.31501
2017-01-01 09:00:00 : ema custom time frame: 8.30319
2017-01-01 09:00:00 : ema standard time resolution: 8.32654
2017-01-01 10:00:00 : ema custom time frame: 8.32169
2017-01-01 10:00:00 : ema standard time resolution: 8.33015
2017-01-01 11:00:00 : ema custom time frame: 8.3405
2017-01-01 11:00:00 : ema standard time resolution: 8.34397...