Hi,

I have some problems with my set_cash function in my backtest because each time my cash is 10 000, my backtest doesn't work but when my cash is 100 000 it finally works : 

Moreover, my portofolio[ticker].quantity function doesn't work because in my book order I have some sell and buy but my function returns always 0.0.

Help me please ????

from AlgorithmImports import *


class FocusedOrangeCoyote(QCAlgorithm):


def Initialize(self):
self.SetStartDate(2022, 8, 1)
self.SetEndDate(2022, 12, 1)
self.SetCash(10000)
self.capEngage = 0.01 * self.Portfolio.Cash
self.symbol = self.AddEquity("GOOG", Resolution.Hour).Symbol
self.set_security_initializer(lambda security: security.set_fee_model(ConstantFeeModel(0, "USD")))


self.maxDrawdown = 0
self.maxDrawup = 0
self.bar = 250
self.purchase_price = None
self.set_warm_up(self.bar, Resolution.Hour)


def CalculateMaxDrawup(self):
history = self.History(self.symbol, self.bar, Resolution.Hour)
if history.empty:
return 0


prices = history["close"].values
lowWaterMark = prices[0]
maxDrawup = 0


for price in prices:
if price < lowWaterMark:
lowWaterMark = price


drawup = (price - lowWaterMark)
if drawup > maxDrawup:
maxDrawup = drawup

self.Debug(f"Max Drawup: {maxDrawup} at {self.time}")
return maxDrawup


def CalculateMaxDrawdown(self):
history = self.History(self.symbol, self.bar, Resolution.Hour)
if history.empty:
return 0


prices = history["close"].values
highWaterMark = prices[0]
maxDrawdown = 0


for price in prices:
if price > highWaterMark:
highWaterMark = price


drawdown = (highWaterMark - price)
if drawdown > maxDrawdown:
maxDrawdown = drawdown

self.Debug(f"Max Drawdown: {maxDrawdown} at {self.time}")
return maxDrawdown


def OnData(self, data: Slice):


price = self.Securities[self.symbol].Price
self.maxDrawdown = self.CalculateMaxDrawdown()
self.maxDrawup = self.CalculateMaxDrawup()


if self.Portfolio.Cash < 10000 * 0.5:
self.Debug(f"Current Cash: {self.Portfolio.Cash}")
return

self.debug(self.capEngage)


if not self.Portfolio.Invested and self.maxDrawdown > 0 and (self.capEngage / self.maxDrawdown) > 2:
self.debug("achat")
quantity = round(self.capEngage / self.maxDrawdown)
self.Debug(quantity)
self.set_holdings(self.symbol, quantity)
self.purchase_price = price
self.Debug(f"Buy {self.portfolio[self.symbol].quantity} @ {price} at {self.time}")
self.debug(self.portfolio.invested)


if not self.Portfolio.Invested and self.maxDrawup > 0 and self.capEngage / self.maxDrawup > 2:
quantity = -self.capEngage / self.maxDrawup
self.set_holdings(self.symbol, quantity)
self.purchase_price = price
self.Debug(f"Sell short {self.Portfolio[self.symbol].Quantity} @ {price} at {self.time}")


if self.Portfolio.Invested:
if price < self.purchase_price:
self.Debug(f"Liquidate {self.Portfolio[self.symbol].Quantity} @ {price} at {self.time}")
self.Liquidate(self.symbol)


if self.Portfolio[self.symbol].Quantity > 0 and price > self.purchase_price + self.maxDrawdown:
self.Debug(f"Sell half of {self.Portfolio[self.symbol].Quantity} @ {price} at {self.time}")
self.Liquidate(self.symbol, (self.Portfolio[self.symbol].Quantity / 2))


if self.Portfolio[self.symbol].Quantity < 0 and price < self.purchase_price - self.maxDrawup:
self.Debug(f"Buy short half {self.Portfolio[self.symbol].Quantity} @ {price} at {self.time}")
self.Liquidate(self.symbol, (-self.Portfolio[self.symbol].Quantity / 2))