Share a new discussioEMA crossover data points not matching with Tradingview, OHLC not matching
# region imports
from AlgorithmImports import *
# endregion
import numpy as np
import pandas as pd
class EmaCrossoverAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 1, 1)
self.SetEndDate(2023, 7, 29)
self.SetCash(1000)
self.AddEquity("SPY")
self.resolution = Resolution.Minute
# Create custom indicators for 20-period and 30-period EMAs
self.ema_20 = self.EMA("SPY", 20, Resolution.Minute)
self.ema_30 = self.EMA("SPY", 30, Resolution.Minute)
# Variables to track previous crossover state
self.prev_ema_crossover = 0
# Schedule the event to check for crossovers
self.Schedule.On(self.DateRules.EveryDay(), self.TimeRules.Every(TimeSpan.FromMinutes(3)), self.CheckForCrossovers)
def OnData(self, slice):
# Do whatever you need with the incoming data
pass
def CheckForCrossovers(self):
if not self.ema_20.IsReady or not self.ema_30.IsReady:
return
ema_20_value = self.ema_20.Current.Value
ema_30_value = self.ema_30.Current.Value
# Check for the EMA crossover
ema_crossover = 0
if ema_20_value > ema_30_value:
ema_crossover = 1
elif ema_20_value < ema_30_value:
ema_crossover = -1
# Check for changes in the crossover state
if ema_crossover != self.prev_ema_crossover:
self.prev_ema_crossover = ema_crossover
# Issue new signals only when the crossover direction changes
if ema_20_value > ema_30_value:
self.SetHoldings("SPY", 1)
self.Debug(f"Buy: EMA(20) = {ema_20_value:.2f}, EMA(30) = {ema_30_value:.2f}")
elif ema_20_value < ema_30_value:
self.Liquidate()
self.Debug(f"Sell: EMA(20) = {ema_20_value:.2f}, EMA(30) = {ema_30_value:.2f}")
Mia Alissi
Based on the provided extracts, the Python code for the EMA crossover algorithm seems to be correctly implemented. The algorithm is designed to create a buy signal when the 20-period EMA crosses above the 30-period EMA and a sell signal when the 20-period EMA crosses below the 30-period EMA.
However, if the EMA crossover data points are not matching with TradingView, it could be due to differences in the data source or the calculation method used for the EMA.
In QuantConnect, the EMA is calculated using the formula:
Where:
The first EMA value is calculated using the Simple Moving Average (SMA) of the first N samples, and then the EMA formula is used for the following samples. This was mentioned in the extract from the QuantConnect forum discussion.
If the OHLC values are not matching, it could be due to differences in the data source, data adjustments (for dividends, splits, etc.), or the timezone used for the data.
It's also worth noting that QuantConnect has recently updated the EMA indicator implementation to fix a small bug on the first value calculation, matching the implementations of other platforms like TradingView and TA-Lib. This was also mentioned in the extract from the QuantConnect forum discussion.
If the issue persists, it would be helpful to compare the raw data used in both platforms and the exact calculation method used for the EMA and OHLC values.
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.
Pradeep reddy
hi all platforms are identical except quantconnect check the image below. the crossover should happen at 11:36 est which was matched on all platforms. only one crossover in Aday but QuantConnect shows 6 crossovers which scared me to use the platform.
QUantconnect shows like below, attached backtest logs also. based on this will decide whether to be on this platform or not.
2023-07-31 11:24:00SPYSell MarketÂ
Fill: $457.30 USD
-1FilledLiquidated+2023-07-31 12:27:00SPYBuy MarketFill: $457.14 USD
1Filled +2023-07-31 12:39:00SPYSell MarketFill: $457.01 USD
-1FilledLiquidatedÂ
Pradeep reddy
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!