As the title suggests, I've been working on a strategy using longer EMAs. Unfortunately, no matter what I've done, I haven't been able to get the data on both platforms to line up. In the case of the 30-minute (1500, Close), there is a ~0.4% difference in most cases. Is there anyone here who has experienced a similar issue? If so, how did you fix it?
Here is the code related to the EMAs.
Thank you in advance for the help!
from AlgorithmImports import *
class Example(QCAlgorithm):
def Initialize(self):
# Set parameters
self.SetStartDate(2024, 1, 1)
self.SetEndDate(2025, 1, 1)
self.set_time_zone(TimeZones.NEW_YORK)
self.set_account_currency('USD')
self.SetCash(100000)
# self.set_warm_up(timedelta(days=365*50))
# Add SPY data
spy = self.AddEquity("SPY", Resolution.Minute)
spy.set_fee_model(ConstantFeeModel(0))
self.symbol = spy.Symbol
self.set_benchmark(self.symbol)
# Define EMAs for various resolutions
#...
self.ema1500_30min = ExponentialMovingAverage(1500)
# Consolidators for higher resolutions
#...
self.Consolidate(self.symbol, timedelta(minutes=30), self.UpdateEMA_30min)
# State variables
#...
self.prev_ema1500_30min = RollingWindow[float](2)
self.backtest_start = True
#...
def UpdateEMA_30min(self, bar):
#...
self.ema1500_30min.Update(bar.EndTime, bar.Close)
def OnData(self, data):
if self.is_warming_up:
return
# Ensure all indicators are ready
if not self.AllEMAsReady():
return
#...
def AllEMAsReady(self):
"""Check if all EMAs are ready."""
return all([self.ema1500_30min.IsReady, self.prev_ema1500_30min.IsReady,])
Mia Alissi
To address the discrepancies in EMA values across different platforms, let's consider the following steps based on your code and the information gathered:
Warm-Up Period: Ensure that the EMA is properly warmed up with sufficient historical data. You mentioned a warm-up period in your code (
# self.set_warm_up(timedelta(days=365*50))
). Uncomment this line to ensure the EMA has enough data to stabilize its initial values.Timestamp Alignment: QuantConnect timestamps data at the end of the period, while other platforms might timestamp at the beginning. Ensure that your data handling accounts for this difference.
Resolution Consistency: Make sure the resolution and data frequency are consistent across platforms. You are using a 30-minute consolidator, which should match the resolution used on the other platform.
Smoothing Factor: Check if the smoothing factor used in the EMA calculation is consistent across platforms. QuantConnect's
ExponentialMovingAverage
uses a default smoothing factor, which you can adjust if needed.Historical Data Source: Ensure the historical data source and its quality are consistent across platforms. Differences in data can lead to different EMA values.
Code Review: Ensure that the EMA is being updated correctly with the consolidated data. In your
UpdateEMA_30min
method, you are updating the EMA with thebar.Close
. Make sure this aligns with how the other platform calculates the EMA.Here's a refined version of your code with the warm-up period enabled:
By ensuring these aspects are aligned, you should be able to reduce the discrepancies in EMA values across different platforms.
Sources: Key Concepts - QuantConnect.com, Warm Up Periods - QuantConnect.com, Ema Cross Futures Front Month Algorithm, Warmup Algorithm, Indicator Warmup Algorithm
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.
Stefan Dj
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.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!