Hello,
I'm trying to figure out why the RSI is vastly different from trading view on the 5 minute timeframe. I understand there may be differences in data but this wildly different, ranging from 5, 10, even 20 points different. From what I can see I'm registering the indicator correctly with automatic updates. But when I log the values below, they are completely off.
Eg: July 18 14:00:00 (EST)
TradingView: 70.98
Log: 62.18
For reference, the RSI only became wildly different when I started to consolidate bars to 5 minutes. The 1 minute timeframe matched nearly perfectly with TradingView.
from AlgorithmImports import *
from datetime import datetime, date
class AlertLightBrownHyena(QCAlgorithm):
def Initialize(self):
self.ticker = 'SPY'
self.startingCash = 100000
self.startDate = '2023-06-01'
self.endDate = '-'
# Equities
self.resolution = Resolution.Minute
self.equity = self.AddEquity(self.ticker, self.resolution)
self.equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
# Backtesting
self.SetStartDate(datetime.fromisoformat(self.startDate))
self.SetEndDate(datetime.fromisoformat(self.endDate)) if self.endDate != '-' else self.SetEndDate(datetime.now())
self.SetCash(self.startingCash)
self.SetTimeZone("America/New_York")
self.SetBenchmark(self.equity.Symbol)
self.SetWarmUp(100)
# Consolidators
self.consolidator = self.Consolidate(self.equity.Symbol, timedelta(minutes=5), self.OnFiveMinData)
# Indicators
self.rsi = RelativeStrengthIndex(14, MovingAverageType.Simple)
self.RegisterIndicator(self.equity.Symbol, self.rsi, self.consolidator)
self.rsiSMA = IndicatorExtensions.SMA(self.rsi, 14)
def OnFiveMinData(self, data):
self.Log(f"Close: {round(data.Close, 2)}, RSI: {round(self.rsi.Current.Value, 2)}, RSI SMA: {round(self.rsiSMA.Current.Value, 2)}")
Mia Alissi
The discrepancy you're seeing between the RSI values in your algorithm and those on TradingView could be due to a few factors.
Data Source: Different platforms may use different data sources, and even small discrepancies in the data can lead to significant differences in calculated indicators.
Calculation Method: There might be slight differences in the way the RSI is calculated on different platforms.
Resolution: You mentioned that the discrepancy became significant when you started consolidating bars to 5 minutes. This suggests that the issue might be related to the way you're handling data consolidation.
In your code, you're creating a 5-minute consolidator and registering the RSI indicator to this consolidator. This means the RSI is calculated based on 5-minute bars. If TradingView is calculating RSI based on 1-minute bars, this could explain the discrepancy.
Here's the relevant part of your code:
If you want your RSI calculation to match that of TradingView more closely, you might need to adjust your data consolidation or calculation method to match theirs.
Please note that it's generally difficult to perfectly match indicator values across different platforms due to the reasons mentioned above. The important thing is that your algorithm behaves as expected based on the data and indicators it uses.
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.
Joshua Woelfel
Everything is exactly the same in TradingView. 5 minute timeframe on the chart. RSI is using the chart timeframe so also 5 minutes. Default RSI settings (14 length, using bar close, etc).
Â
Mia Alissi
If you're certain that the settings are identical on both platforms, then the discrepancy might be due to the following factors:
Warm-up Period Differences: Some platforms use all of the historical data to warm up their indicators while LEAN indicators have a fixed number of bars they need to warm up. As a result, indicators with long memory like the Exponential Moving Average can have slightly different values across platforms. source
Timestamp Differences: We timestamp our data to the time when the period ends. Many other platforms timestamp to the beginning of the candle. source
Price Differences: If the input data is slightly different, the indicator values will be different. To test if it's a difference in price data, feed in the data from the third-party platform into our indicators. We validate the indicators against third-party sources and when the values are the same, the indicator values are similar too. source
Brokerage Limitations: If the brokerage model that you use in backtesting is not the same brokerage that you use in live trading, deviations may occur between backtesting and live trading. source
Tick Slice Sizes: In backtesting, we collect ticks into slices that span 1 millisecond before injecting them into your algorithm. In live trading, we collect ticks into slices that span up to 70 milliseconds before injecting them into your algorithm. This difference in slice sizes can cause deviations between your algorithm's live and OOS backtest equity curves. source
If you've checked all these factors and the discrepancy still exists, it might be worth reaching out to the QuantConnect community or support for further assistance.
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.
Joshua Woelfel
Another example:
July 14, 13:15:00
TradingView: 40
Log: 18
A 22 point difference! Something is definitely wrong with my code sample but I can't figure it out.
Mia Alissi
The discrepancy you're seeing is indeed quite large. As mentioned earlier, there could be several reasons for this, including differences in data sources, calculation methods, and data consolidation.
However, if you're confident that all these factors are identical on both platforms, then it might be worth checking the following:
Indicator Warm-up: In your code, you're setting a warm-up period of 100 bars. This means that the first 100 bars of data are used to warm up the indicator, and the indicator values might not be accurate during this period. If you're comparing the RSI values during the warm-up period, this could explain the discrepancy.
Data Normalization: You're setting the data normalization mode to
Raw
. This means that the data is not adjusted for splits or dividends. If TradingView is using adjusted data, this could explain the discrepancy.Here's the relevant part of your code:
If you've checked all these factors and the discrepancy still exists, it might be worth reaching out to the QuantConnect community or support for further assistance. They might be able to help you debug your code and figure out why you're seeing this discrepancy.
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.
Joshua Woelfel
I've commented out the data normalization line and increased warmup to 500 and the numbers are the same unfortunately.
Mia Alissi
If you've already checked the data normalization, warm-up period, and other factors mentioned earlier, and the discrepancy still exists, it might be worth considering the following:
Data Source: Even if the settings are identical, different platforms may use different data sources. Small discrepancies in the data can lead to significant differences in calculated indicators.
Indicator Calculation: There might be slight differences in the way the RSI is calculated on different platforms.
Resolution: You mentioned that the discrepancy became significant when you started consolidating bars to 5 minutes. This suggests that the issue might be related to the way you're handling data consolidation.
If you've checked all these factors and the discrepancy still exists, it might be worth reaching out to the QuantConnect community or support for further assistance. They might be able to help you debug your code and figure out why you're seeing this discrepancy.
Unfortunately, without more information, it's difficult to provide a more specific solution. The issue could be related to something specific in your code or setup that isn't apparent from the information provided.
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.
Joshua Woelfel
Resolved
Specifying the moving average as Simple skewed the results. It is more similar to TradingView when using the default Moving Average Wilders. Sort of misleading since the TradingView settings has SMA active (screenshot above) but that seems separate from the actual calculation of the RSI.
Mia Alissi
I'm glad to hear that you've resolved the issue. The discrepancy was indeed due to the different types of moving averages used in the RSI calculation.
In QuantConnect, the
RelativeStrengthIndex
function takes two arguments: the period and the type of moving average. By default, it uses Wilder's moving average, but in your original code, you specified it to use a simple moving average. This led to the discrepancy with TradingView, which uses Wilder's moving average for RSI calculation despite the SMA setting being active.Here's the corrected part of your code:
This highlights the importance of understanding the default settings and calculations used by different platforms when comparing indicator values. Thank you for sharing your solution, it will be helpful for others facing a similar issue.
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.
Joshua Woelfel
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!