Hello, I am a fairly new programmer and would appreciate some guidance on debugging a project I'm working on. I am getting an error that I can't seem to fix. It looks like I have the logic correct as far as what I want my Algo to show me, can someone assist me in getting this back testable? Code is python, Thanks for your time !
class SmoothApricotCaribou(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2021, 8, 28) # Set Start Date
self.SetEndDate(2022,2,1)
self.SetCash(50000) # Set Strategy Cash
# self.AddEquity("SPY", Resolution.Minute)
crudeoiwti = self.AddFuture(Futures.Energies.CrudeOilWTI)
self.slowema = self.EMA("CrudeOilWTI", 21, Resolution.Daily)
self.fastema = self.EMA("CrudeOilWTI", 21, Resolution.Hourly)
self.rsi = self.RSI(14)
self.RegisterIndicator("CrudeOilWTI", self.sma, timedelta(minutes=10))
self.Schedule.On(self.DateRules.EveryDay("CrudeOilWTI"), self.TimeRules.At(1, 00), self.ClosePositions)
self.SetBenchmark("CrudeOilWTI")
self.SetWarmUp(timedelta(21))
def OnData(self, data):
if not self.Portfolio.Invested:
if self.Securities["CrudeOilWTI"].Close > self.slowema.Current.Value:
if self.Securities["CrudeOilWTI"].Close > self.fastema.Current.Value:
if self.rsi <= 30:
self.MarketOrder("CrudeOilWTI", 1)
if self.Securities["CrudeOilWTI"].Close < self.slowema.Current.Value:
if self.Securities["CrudeOilWTI"].Close < self.fastema.Current.Value:
if self.rsi >= 70:
self.MarketOrder("CrudeOilWTI", -1)
if self.Portfolio.Invested.IsLong:
if self.Securities["CrudeOilWTI"].Close < self.fastema.Current.Value:
self.Liquidate("CrudeOilWTI")
if self.Portfolio.Invested.IsShort:
if self.Securities["CrudeOilWTI"].Close > self.fastema.Current.Value:
self.Liquidate("CrudeOilWTI")
def ClosePositions(self):
self.Liquidate("CrudeOilWTI")
AK M
Hey Aegean,
Your code had a couple issues and I've resolved them for you… but it might help to get a deeper understanding of Python so the Documentation is easier to follow. You can find tutorials for python anywhere.
Below is the fixed code. The biggest issue was that you were using ‘CrudeOilWTI’ instead of the symbol for crude ‘self.crudeoiwti.Symbol’
AegeanFutures
Thank you for the time and help! I am working to learn python but I trade full time and need to backtest some of what I've been doing.
AegeanFutures
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!