If i set cash 100000$ set_holdings does not work, if i set 1000000$ everything works well
+ Expand
class DydxTestAlgorithm(QCAlgorithm):
def initialize(self):
self.set_start_date(2025, 2, 12)
self.set_end_date(2025, 3, 31)
self.set_cash(100000)
self.set_brokerage_model(DydxPerpetualBrokerageModel())
symbolBTC = "BTC-USD"
symbolETH = "ETH-USD"
self._btc_symbol = self.add_data(DydxPerpetualData, symbolBTC, Resolution.DAILY).symbol
self._eth_symbol = self.add_data(DydxPerpetualData, symbolETH, Resolution.DAILY).symbol
self.set_benchmark(symbolBTC)
#self.set_benchmark(symbolETH)
self._btc_sma = self.sma(self._btc_symbol, 31)
self._btc_ema = self.ema(self._btc_symbol, 31)
self._eth_sma = self.sma(self._eth_symbol, 31)
self._eth_ema = self.sma(self._eth_symbol, 31)
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}")
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}")
def on_order_event(self, order_event):
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}")
def on_data(self, data: Slice):
if not (self._btc_symbol in data and self._eth_symbol in data):
self.debug("Data not available for both symbols")
return
btc_current_price = data[self._btc_symbol].close
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:
self.set_holdings(self._btc_symbol, 0.5)
self.debug('LONG ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
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:
self.set_holdings(self._eth_symbol, 0.5)
self.debug('LONG ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
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:
self.set_holdings(self._btc_symbol, -0.5)
self.debug('SHORT ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
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:
self.set_holdings(self._eth_symbol, -0.5)
self.debug('SHORT ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
#liquidate if opposite signal
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:
self.liquidate(self._btc_symbol)
self.debug('LIQUID SHORT ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
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:
self.liquidate(self._eth_symbol)
self.debug('LIQUID SHORT ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
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:
self.liquidate(self._btc_symbol)
self.debug('LIQUID LONG ' + str(self._btc_symbol) + ' price: ' + str(self.securities[self._btc_symbol].price))
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:
self.liquidate(self._eth_symbol)
self.debug('LIQUID LONG ' + str(self._eth_symbol) + ' price: ' + str(self.securities[self._eth_symbol].price))
Mia Alissi
Here are some potential issues and solutions based on the context provided and the usage of
set_holdings
when starting with a cash amount of $100,000 versus $1,000,000:Potential Issues
Leverage and Buying Power:
Order Size and Costs:
Fees and Margins:
Solutions
Check Leverage and Margin Requirements:
DydxPerpetualBrokerageModel
. Ensure it is appropriately set so that you can place desired trades with your starting cash.DydxPerpetualBrokerageModel
documentation or settings in your algorithm.Adjust Trade Sizes or Strategy Logic:
calculate_order_quantity
method can help determine feasible order sizes given your current portfolio and cash constraints.Investigate Brokerage Model Details:
DydxPerpetualBrokerageModel
, is accurate and doesn't enforce additional margin or trading requirements that apply to the lower cash balance compared to larger amounts.By addressing these areas, you can ensure that your
set_holdings
logic functions correctly and efficiently across different starting cash values.Sources: Holdings - QuantConnect.com, QuantConnect Paper Trading - QuantConnect.com, Brokerage Model Algorithm, Key Concepts - QuantConnect.com, Holdings - QuantConnect.com
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!