Hello, I hope you all are having a great day. I am a beginner at QuantConnect and I am having difficulties trying to make this algorithm work. The first difficulty I had with this bot was with the StopMarketOrders. Even though I defined StopMarketOrders after every SetHoldings they didn't work in the backtest. After that, I decided to change the StopMarketOrders to Trailing Stop, hoping it would work, but it didn't succeed. I am still struggling with this problem.
Also, after adding the if isWarmingUp statement the backtest started to give errors saying “self.stopLongTicket is not defined”. Is there anyone who can help me? It is my first proper code at QuantConnect and I started to understand I have a lot missing. My code:
# region imports
from AlgorithmImports import *
# endregion
class FocusedBrownGoat(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 1, 1) # Set Start Date
self.SetEndDate(2021, 1, 1)
self.SetCash(100000) # Set Strategy Cash
self.ethusd = self.AddCrypto("ETHUSD", Resolution.Daily).Symbol
self.SetWarmUp(34)
self.ema34 = self.EMA(self.ethusd,34,Resolution.Daily)
self.ema8 = self.EMA(self.ethusd, 8, Resolution.Daily)
self.ema34window = RollingWindow[IndicatorDataPoint](2)
self.ema8window = RollingWindow[IndicatorDataPoint](2)
self.ema34.Updated += self.Ema34Updated
self.ema8.Updated += self.Ema8Updated
emachart = Chart("Trade Plot")
emachart.AddSeries(Series("Ema8", SeriesType.Line,0))
emachart.AddSeries(Series("Ema34", SeriesType.Line,0))
emachart.AddSeries(Series("Long", SeriesType.Scatter,0))
emachart.AddSeries(Series("SoldLong", SeriesType.Scatter,0))
emachart.AddSeries(Series("Short", SeriesType.Scatter,0))
emachart.AddSeries(Series("SoldShort", SeriesType.Scatter,0))
self.AddChart(emachart)
self.highestPrice = 0
self.lowestPrice = 0
self.isLong = False
self.isShort = False
self.SetBenchmark(self.ethusd)
def OnData(self, data):
if self.ethusd not in data:
return
if self.IsWarmingUp:
return
price = self.Securities[self.ethusd].Close
self.Plot("Trade Plot", "Ema8", self.ema8.Current.Value)
self.Plot("Trade Plot", "Ema34", self.ema34.Current.Value)
conditionlong = self.ema8window[0] > self.ema34window[0] and self.ema34window[1] > self.ema8window[1]
conditionshort = self.ema8window[0] < self.ema34window[0] and self.ema34window[1] < self.ema8window[1]
if not self.Portfolio.Invested:
if conditionlong == True:
self.SetHoldings(self.ethusd, 1)
self.stopLongTicket = self.StopMarketOrder(self.ethusd, -(self.Portfolio.TotalPortfolioValue/price), price * 0.98)
self.highestPrice = price
self.isLong == True
self.Log("Long")
self.Plot("Trade Plot", "Long", self.Securities[self.ethusd].Close)
if not self.Portfolio.Invested:
if conditionshort == True:
self.SetHoldings(self.ethusd, -1)
self.stopShortTicket = self.StopMarketOrder(self.ethusd, -(self.Portfolio.TotalPortfolioValue/price), price * 1.02)
self.lowestPrice = price
self.isShort == True
self.Log("Short")
self.Plot("Trade Plot", "Short", self.Securities[self.ethusd].Close)
else:
if self.Securities[self.ethusd].Close > self.highestPrice:
if self.isLong == True:
self.highestPrice = self.Securities[self.ethusd].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.Securities[self.ethusd].Close*0.98
self.stopLongTicket.Update(updateFields)
if self.Securities[self.ethusd].Close < self.lowestPrice:
if self.isShort == True:
self.lowestPrice = self.Securities[self.ethusd].Close
updateFields = UpdateOrderFields()
updateFields.StopPrice = self.Securities[self.ethusd].Close*1.02
self.stopShortTicket.Update(updateFields)
def OnOrderEvent(self, orderEvent):
if self.stopLongTicket is not None and self.stopLongTicket.OrderId == orderEvent.OrderId:
self.isLong = False
self.Plot("Trade Plot", "SoldLong", self.Securities[self.ethusd].Close)
if self.stopShortTicket is not None and self.stopShortTicket.OrderId == orderEvent.OrderId:
self.isShort = False
self.Plot("Trade Plot", "SoldShort", self.Securities[self.ethusd].Close)
def Ema34Updated(self, sender, updated):
self.ema34window.Add(updated)
def Ema8Updated(self, sender, updated):
self.ema8window.Add(updated)
Vladimir
Ertuğ ümsür
Try this drastically simplified but fairly robust version of your strategy.
If you are satisfied with my answer, please accept it and don't forget to like it.
Ertuğ ümsür
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!