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))
Ashutosh
Hi zqefs ebfv
Will you be able to attach the backtest here? The indentation of the code is messed up in the code you have provided.
Happy to help
Thanks,
Ashutosh
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.
Zqefs ebfv
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!