If i set cash    100000$  set_holdings does not work, if i set 1000000$ everything works well

 

  1. class DydxTestAlgorithm(QCAlgorithm):
  2. def initialize(self):
  3. self.set_start_date(2025, 2, 12)
  4. self.set_end_date(2025, 3, 31)
  5. self.set_cash(100000)
  6. self.set_brokerage_model(DydxPerpetualBrokerageModel())
  7. symbolBTC = "BTC-USD"
  8. symbolETH = "ETH-USD"
  9. self._btc_symbol = self.add_data(DydxPerpetualData, symbolBTC, Resolution.DAILY).symbol
  10. self._eth_symbol = self.add_data(DydxPerpetualData, symbolETH, Resolution.DAILY).symbol
  11. self.set_benchmark(symbolBTC)
  12. #self.set_benchmark(symbolETH)
  13. self._btc_sma = self.sma(self._btc_symbol, 31)
  14. self._btc_ema = self.ema(self._btc_symbol, 31)
  15. self._eth_sma = self.sma(self._eth_symbol, 31)
  16. self._eth_ema = self.sma(self._eth_symbol, 31)
  17. self.debug(f"BTC Tradable: {self.securities[self._btc_symbol].is_tradable}, Leverage: {self.securities[self._btc_symbol].leverage}, Properties: {self.securities[self._btc_symbol].symbol_properties}")
  18. self.debug(f"ETH Tradable: {self.securities[self._eth_symbol].is_tradable}, Leverage: {self.securities[self._eth_symbol].leverage}, Properties: {self.securities[self._eth_symbol].symbol_properties}")
  19. def on_order_event(self, order_event):
  20. self.debug(f"Order: {order_event.symbol}, Status: {order_event.status}, Qty: {order_event.quantity}, Message: {order_event.message}, Fer :{order_event.order_fee}, Fill price: {order_event.fill_price}, Time: {order_event.utc_time}")
  21. def on_data(self, data: Slice):
  22. if not (self._btc_symbol in data and self._eth_symbol in data):
  23. self.debug("Data not available for both symbols")
  24. return
  25. btc_current_price = data[self._btc_symbol].close
  26. if not self.portfolio[self._btc_symbol].invested and self._btc_ema.current.value > self._btc_sma.current.value and data[self._btc_symbol].close < self._btc_ema.current.value:
  27. self.set_holdings(self._btc_symbol, 0.5)
  28. self.debug('LONG ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
  29. elif not self.portfolio[self._btc_symbol].invested and self._eth_ema.current.value > self._eth_sma.current.value and data[self._eth_symbol].close < self._eth_ema.current.value:
  30. self.set_holdings(self._eth_symbol, 0.5)
  31. self.debug('LONG ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
  32. elif not self.portfolio[self._eth_symbol].invested and self._btc_ema.current.value < self._btc_sma.current.value and data[self._btc_symbol].close > self._btc_ema.current.value:
  33. self.set_holdings(self._btc_symbol, -0.5)
  34. self.debug('SHORT ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
  35. elif not self.portfolio[self._eth_symbol].invested and self._eth_ema.current.value < self._eth_sma.current.value and data[self._eth_symbol].close > self._eth_ema.current.value:
  36. self.set_holdings(self._eth_symbol, -0.5)
  37. self.debug('SHORT ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
  38. #liquidate if opposite signal
  39. elif self.portfolio[self._btc_symbol].quantity < 0 and self._btc_ema.current.value > self._btc_sma.current.value and data[self._btc_symbol].close < self._btc_ema.current.value:
  40. self.liquidate(self._btc_symbol)
  41. self.debug('LIQUID SHORT ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
  42. elif self.portfolio[self._eth_symbol].quantity < 0 and self._eth_ema.current.value > self._eth_sma.current.value and data[self._eth_symbol].close < self._eth_ema.current.value:
  43. self.liquidate(self._eth_symbol)
  44. self.debug('LIQUID SHORT ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
  45. elif self.portfolio[self._btc_symbol].quantity > 0 and self._btc_ema.current.value < self._btc_sma.current.value and data[self._btc_symbol].close > self._btc_ema.current.value:
  46. self.liquidate(self._btc_symbol)
  47. self.debug('LIQUID LONG ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
  48. elif self.portfolio[self._eth_symbol].quantity > 0 and self._eth_ema.current.value < self._eth_sma.current.value and data[self._eth_symbol].close > self._eth_ema.current.value:
  49. self.liquidate(self._eth_symbol)
  50. self.debug('LIQUID LONG ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
+ Expand

Author

Oilan Online

16 days ago