Can anyone explain why I'm not able to retrieve crypto quotebar data in backtesting? The self.quote_bar_window never receives the data (all NONE). If I remove the “if then” statement, the program hit a runtime error. Any help would be appreciated.
for quote_bar in self.crypto_history:
self.quote_bar_window.add(quote_bar)
if not self.quote_bar_window.is_ready:
return
class CryptoAIScapping(QCAlgorithm):
def initialize(self):
self.set_warm_up(100, Resolution.DAILY)
self.set_start_date(2024, 10, 28)
self.set_end_date(2024, 10, 30)
self.set_cash(10000)
self.crypto_symbol = self.add_crypto("BTCUSD", Resolution.MINUTE).symbol
self.crypto_history = self.history("BTCUSD", 3, Resolution.MINUTE)
self.quote_bar_window = RollingWindow[QuoteBar](3)
self.set_risk_management(MaximumDrawdownPercentPerSecurity(0.20))
# Correct method calls for indicators
self.std = self.std(self.crypto_symbol, 22)
self.rsi = self.rsi(self.crypto_symbol, 14)
self.atr = self.aroon(self.crypto_symbol, 10, 20)
self.aroon = self.aroon(self.crypto_symbol, 10, 20) # Corrected to include both up and down periods
self.macd = self.macd(self.crypto_symbol, 12, 26, 9, MovingAverageType.Exponential)
self.set_benchmark("BTCUSD")
def on_data(self, data):
if self.is_warming_up:
return
for quote_bar in self.crypto_history:
self.quote_bar_window.add(quote_bar)
if not self.quote_bar_window.is_ready:
return
if not all([self.rsi.IsReady, self.atr.IsReady, self.aroon.IsReady, self.macd.IsReady]):
return
Bar0 = self.quote_bar_window[0] # Current bar has index zero.
Bar1 = self.quote_bar_window[1] # Previous bar has index one.
Bar2 = self.quote_bar_window[2] # Bar from two periods ago
# Compute the difference between the current close and the previous close
close_diff0 = Bar0.close - Bar1.close
close_diff1 = Bar1.close - Bar2.close
# Compute the difference between the current volume and the previous volume
volume_diff0 = Bar0.volume - Bar1.volume
volume_diff1 = Bar1.volume - Bar2.volume
close_spread_AB0 = Bar0.ask.close - Bar1.bid.close
close_spread_AB1 = Bar1.ask.close - Bar2.bid.close
size_spread_AB0 = Bar0.last_ask_size - Bar1.last_bid_size
size_spread_AB1 = Bar1.last_ask_size - Bar2.last_bid_size
ask_rangeOC_0 = Bar0.ask.open - Bar0.ask.close
ask_rangeHL_0 = Bar0.ask.high - Bar0.ask.low
ask_rangeHC_0 = Bar0.ask.high - Bar0.ask.close
ask_rangeHO_0 = Bar0.ask.high - Bar0.ask.open
ask_rangeOL_0 = Bar0.ask.open - Bar0. ask.low
ask_rangeCL_0 = Bar0.ask.close - Bar0.ask.low
Mia Alissi
To address the issues in the code and ensure correct functionality, consider the following guidelines based on the documentation:
RollingWindow Initialization: Ensure that
RollingWindow
is initialized with the correct data type, such asRollingWindow[QuoteBar]
. Use theadd
method to insert data into theRollingWindow
.Indicator Initialization: Initialize indicators using the correct method calls. For example, use
self.SMA("symbol", period)
for a simple moving average. Check the readiness of indicators usingis_ready
.Attribute Errors in QuoteBar: Use the correct attributes of the
QuoteBar
class. For example, access bid and ask prices usingquote_bar.bid.close
andquote_bar.ask.close
.Undefined Variables: Ensure that all variables, such as
MinMaxScaler
,l1_model
, andPrice
, are defined or imported before use.Logic Errors in Indicator Readiness: Verify that the logic for checking indicator readiness is correctly implemented. Use
is_ready
to ensure indicators are ready before processing data.Correct Method Calls: Use the correct method calls in the algorithm, such as
self.set_start_date
instead ofself.SetStartDate
.Data Access from RollingWindow: Ensure that data accessed from
RollingWindow
is compatible and correctly indexed. Use index 0 for the most recent item and larger indices for older items.By following these guidelines, you can effectively retrieve and process crypto quote bar data in your backtesting algorithm.
Sources: Register Indicator Regression Algorithm, Gradient Boosting Model, Intraday Dynamic Pairs Trading Using Correlation And Cointegration Approach, Momentum Effect In Commodities Futures, Rolling Window 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.
Douglas McWilliams
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!