I have coded a simple long only RSI momentum strategy with a trend filter. The issue that I am facing is that I am getting drastically different backtest results when running the algo on the default 1h resolution bars VS when running the algo on hour bars (generated by consolidating 1m bars). I am concerned that I am consolidating data incorrectly. Can anyone please offer some advice?
Algo 1 directly below uses the default 1h bars.
Algo 2 in the attached backtest uses the 1m consolidated into 1h bars.
Algo 1
from AlgorithmImports import *
class DeterminedSkyBlueDinosaur(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 3, 4)
self.SetCash(100000)
self.ticker=self.AddCrypto("ETHUSD", Resolution.Hour)
self.rsi=self.RSI(self.ticker.Symbol, 14, Resolution.Hour)
self.TREND=self.SMA(self.ticker.Symbol, 9, Resolution.Daily)
mins_in_day=24
warmup_days=9
warmup_time=mins_in_day*warmup_days
self.SetWarmup(warmup_time)
self.prev_rsi_value=None
def OnData(self, data):
if self.IsWarmingUp:
return
if not self.ticker.Price > self.TREND.Current.Value:
return
rsi_value=self.rsi.Current.Value
trade_quantity=int(self.Portfolio.Cash/self.ticker.Price)*0.9
if self.prev_rsi_value is None:
self.prev_rsi_value=rsi_value
if not self.Portfolio.Invested:
if 5<rsi_value<21 and rsi_value>self.prev_rsi_value:
self.MarketOrder(self.ticker.Symbol, trade_quantity)
self.StopMarketOrder(self.ticker.Symbol, -trade_quantity,self.ticker.Price*0.8)
if self.Portfolio.Invested:
if rsi_value>90:
self.Liquidate()
self.prev_rsi_value=rsi_value
THANK YOU :)
Mia Alissi
Based on the information provided in the extracts, it seems that the difference in results between using the default 1h bars and the consolidated 1m bars could be due to the way data is handled in each case.
In the forum discussion 10414, a user noted a difference in the indicators calculated with and without consolidation. Another user, Cole S, explained that the values are different because having an indicator run on minute resolution for a longer time period has many more data points and changes the indicator noticeably.
In the forum discussion 12644, a user tried to consolidate future data and only got one-period data feed from OnDataConsolidated.
In the documentation, it is mentioned that if the indicator uses more than just the close price to calculate its value, the open, high, and low values of the first consolidated bar may be incorrect, causing the initial indicator values to be incorrect.
Given these points, it's possible that the way you're consolidating your data in Algo 2 is affecting your indicators and thus your backtest results. You might want to review your consolidation process and ensure that it's correctly implemented.
Unfortunately, without the specific code for Algo 2, it's hard to provide a more precise answer. I would recommend checking the documentation on data consolidation and comparing it with your implementation.
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.
James hirst
“Unfortunately, without the specific code for Algo 2, it's hard to provide a more precise answer” Here is the code, any idea on what the issue is?:
from AlgorithmImports import *
class DeterminedSkyBlueDinosaur(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 3, 4)
self.SetCash(100000)
self.ticker=self.AddCrypto("ETHUSD", Resolution.Minute)
self.rsi=self.RSI(self.ticker.Symbol, 14, Resolution.Minute)
self.TREND=self.SMA(self.ticker.Symbol, 9, Resolution.Daily)
mins_in_day=24*60
warmup_days=9
warmup_time=mins_in_day*warmup_days
self.SetWarmup(warmup_time)
consolidator = TradeBarConsolidator((timedelta(minutes=60)))
consolidator.DataConsolidated += self.OnDataConsolidated
self.SubscriptionManager.AddConsolidator(self.ticker.Symbol, consolidator)
self.prev_rsi_value=None
def OnDataConsolidated(self, sender, bar):
self.rsi.Update(bar.EndTime, bar.Close)
if self.IsWarmingUp:
return
if not self.ticker.Price > self.TREND.Current.Value:
return
rsi_value=self.rsi.Current.Value
trade_quantity=int(self.Portfolio.Cash/self.ticker.Price)*0.9
if self.prev_rsi_value is None:
self.prev_rsi_value=rsi_value
if not self.Portfolio.Invested:
if 5<rsi_value<21 and rsi_value>self.prev_rsi_value:
self.MarketOrder(self.ticker.Symbol, trade_quantity)
self.StopMarketOrder(self.ticker.Symbol, -trade_quantity,self.ticker.Price*0.8)
if self.Portfolio.Invested:
if rsi_value>90:
self.Liquidate()
self.prev_rsi_value=rsi_value
Mia Alissi
Looking at your Algo 2 code, it seems like you're correctly consolidating the minute data into hourly bars. However, there are a few things you might want to consider:
Data Resolution: In the forum discussion 11613, a user noticed a significant difference in performance when running an algorithm on a lower resolution. When they changed the original algorithm to minute resolution, the returns almost halved. This could be one reason why you're seeing different results between Algo 1 and Algo 2.
Data Availability: According to the documentation, the amount of historical data you can access depends on the resolution of your data subscriptions. For minute resolution, you can access 1 year of historical data. This might affect your backtest results if your algorithm relies on a longer history.
Data Timing: The documentation also mentions that in backtests, your algorithm receives data at perfect timing. If you request minute resolution data, your algorithm receives the bars at the top of each minute. In live trading, bars have a slight delay, so you may receive them milliseconds after the top of each minute. This could potentially affect your backtest results.
Indicator Calculation: In the forum discussion 10414, a user noted a difference in the indicators calculated with and without consolidation. Having an indicator run on minute resolution for a longer time period has many more data points and changes the indicator noticeably.
Given these points, it's possible that the difference in results between Algo 1 and Algo 2 is due to the difference in data resolution and the way the indicators are calculated. You might want to experiment with different resolutions and see how it affects your backtest results.
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.
James hirst
The issue is because there is only 1 year of minute data available via the backtest. My code is correct, that is the reason why. Thanks Mia A.I.
Mia Alissi
I'm glad you found the reason for the discrepancy in your backtest results, James. You're correct, the amount of historical data you can access depends on the resolution of your data subscriptions. For minute resolution, you can access 1 year of historical data as mentioned in the documentation. This could indeed affect your backtest results if your algorithm relies on a longer history. If you have any more questions or need further assistance, feel free to ask.
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.
James hirst
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!