Hi everyone, I am trying to gain some experience on QuantConnect and I am currently trying to develop a Range Trading algorithm. I am quite sure it's not working and I think the main problem is the short selling. I am not even sure if the StopLoss and TakeProfit are set up correctly. Here's the code:
class Strategy(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022, 1, 1)
self.SetEndDate(2022, 1, 31)
self.SetCash(100000)
self.stock = self.AddEquity("TSLA", Resolution.Minute)
self.Max = self.MAX("TSLA", 60, Resolution.Minute)
self.Min = self.MIN("TSLA", 60, Resolution.Minute)
self.SetWarmUp(60, Resolution.Minute)
consolidator = TradeBarConsolidator(TimeSpan.FromMinutes(15)) ## Selected resolution is hourly, so this will create a 6-hour consolidator
consolidator.DataConsolidated += self.OnDataConsolidated ## Tell consolidator which function to run at consolidation intervals
self.SubscriptionManager.AddConsolidator("TSLA", consolidator) ## Add consolidator to algorithm
def OnData(self, data):
pass
def OnDataConsolidated(self, sender, bar):
# if self.IsWarmingUp or not (self.Max.IsReady and self.Min.IsReady):
# return
Min = 0
Max = 0
Close = self.Securities["TSLA"].Close
stopPrice = Close * 0.99
limitPrice = Close * 1.01
if self.Max.IsReady and self.Min.IsReady:
Min = self.Min.Current.Value
Max = self.Max.Current.Value
if Close >= Max:
self.MarketOrder("TSLA", 10)
# self.StopLimitOrder("TSLA", 10, stopPrice, limitPrice)
elif Close < Min:
self.MarketOrder("TSLA", -10)
# self.StopLimitOrder("TSLA", 10, stopPrice, limitPrice)
self.StopLimitOrder("TSLA", 10, stopPrice, limitPrice)
I am quite sure it's not short selling. Does anyone have any idea?
Thanks
Vladimir
Federico Maria Sobrero
Try “Buy HC Sell LC with Stop Loss and Take Profit” and your setup.
If you are satisfied with my answer, please accept it.
Federico Maria Sobrero
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!