-
Couldn't load subscription status.
- Fork 237
Description
For Crypto backtesting
there's no any info about how to use CcxtBacktesting in the documentation
all the workarounds which I take fire errors and problem
example
import pandas_ta # pip install pandas_ta
from datetime import datetime
from lumibot.entities import Asset
from lumibot.strategies.strategy import Strategy
from lumibot.backtesting import CcxtBacktesting
class RsiStrategy(Strategy):
parameters = {
"symbol": "BTC/USDT",
"exchange": "binance"
}
def initialize(self):
# Run every hour (adjust as desired)
self.sleeptime = "1H"
self.set_market("24/7") # Crypto is always open
# Specify RSI thresholds
self.rsi_overbought = 70
self.rsi_oversold = 30
# Configure the trading asset (example: BTC/USDT)
self.base_asset = Asset(symbol="BTC", asset_type=Asset.AssetType.CRYPTO)
self.quote_asset = Asset(symbol="USDT", asset_type=Asset.AssetType.CRYPTO)
def on_trading_iteration(self):
# Get last 100 one-hour closes for RSI
bars = self.get_historical_prices(self.base_asset, 100, "hour", quote=self.quote_asset)
if bars is None or bars.df.shape[0] < 20:
return # Not enough data yet
df = bars.df
rsi = df.ta.rsi(length=14)
latest_rsi = rsi.iloc[-1]
self.log_message(f"Current RSI: {latest_rsi}")
# Check current position
current_position = self.get_position(self.base_asset)
position_qty = current_position.quantity if current_position else 0
# Buy if RSI crosses below oversold and not already in position
if latest_rsi < self.rsi_oversold and position_qty == 0:
order = self.create_order(self.base_asset, 0.1, "buy", quote=self.quote_asset)
self.submit_order(order)
self.log_message("Buy order submitted (RSI < 30)")
# Sell if RSI crosses above overbought and currently in position
elif latest_rsi > self.rsi_overbought and position_qty > 0:
order = self.create_order(self.base_asset, position_qty, "sell", quote=self.quote_asset)
self.submit_order(order)
self.log_message("Sell order submitted (RSI > 70)")
if __name__ == "__main__":
# Set the backtest parameters
backtesting_start = datetime(2024, 1, 1)
backtesting_end = datetime(2024, 12, 31)
# Run the backtest using CcxtBacktesting
results = RsiStrategy.run_backtest(
datasource_class=CcxtBacktesting,
backtesting_start=backtesting_start,
backtesting_end=backtesting_end,
benchmark_asset="SPY",
# CcxtBacktesting specific parameters
exchange="binance", # or "bybit", "okx", etc.
symbol="BTC/USDT",
budget=10000, # Starting capital
show_plot=True,
save_tearsheet=True
)fire this error
File "/opt/anaconda3/lib/python3.11/site-packages/lumibot/data_sources/ccxt_backtesting_data.py", line 158, in _pull_source_bars
data = self.cache_db.download_ohlcv(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/anaconda3/lib/python3.11/site-packages/lumibot/tools/ccxt_data_store.py", line 186, in download_ohlcv
df = self._fill_missing_data(df, timeframe)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/anaconda3/lib/python3.11/site-packages/lumibot/tools/ccxt_data_store.py", line 444, in _fill_missing_data
df.set_index("datetime", inplace=True)
^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'set_index'
if CCXT not supported, please mention to not waste all that time on non-exist info