I am working on a simple crypto algorithm that works, but the backtest results do not incorporate fees from IBKR. I want to include IBKR's fee structure and trade execution for this algo, but when I run the algo, I get the error that Crypto is supported. I wondered if I did not understand something or if there wasn't support for the IBKR broker model and crypto. Here is my algorithm with the ibkr lines commented out:

  1. from AlgorithmImports import *
  2. class CryptoMomentumStrategy(QCAlgorithm):
  3. def Initialize(self):
  4. self.SetStartDate(2019, 1, 1)
  5. self.SetEndDate(2024, 1, 1)
  6. self.SetCash(1_000_000)
  7. # self.set_brokerage_model(BrokerageName.INTERACTIVE_BROKERS_BROKERAGE, AccountType.CASH)
  8. # Add BTCUSD crypto asset
  9. self.btc = self.AddCrypto("BTCUSD", Resolution.Hour, Market.GDAX).Symbol
  10. # Set the fee model for Interactive Brokers
  11. # self.Securities[self.btc].SetFeeModel(InteractiveBrokersFeeModel())
  12. self.lookback = 20 # Lookback period for momentum calculation
  13. self.momentum = None
  14. def OnData(self, data):
  15. if not data.Bars.ContainsKey(self.btc):
  16. return
  17. # Fetch historical data for momentum calculation
  18. history = self.History(self.btc, self.lookback, Resolution.Daily)
  19. if len(history) < self.lookback:
  20. return
  21. # Calculate momentum as the percentage price change over the lookback period
  22. start_price = history["close"].iloc[0]
  23. end_price = history["close"].iloc[-1]
  24. self.momentum = (end_price - start_price) / start_price
  25. # Trading logic based on momentum
  26. if self.momentum > 0 and not self.Portfolio[self.btc].Invested:
  27. self.SetHoldings(self.btc, 1.0) # Go long on positive momentum
  28. elif self.momentum < 0 and self.Portfolio[self.btc].Invested:
  29. self.Liquidate(self.btc) # Exit position on negative momentum
+ Expand

Author

Thomas Zaborenko

December 2024