Back

Hello community of people that actually know how to program! Please help me with something that I don't believe should be too hard.

I am self taught in Python but am still in the process of mastering it. I was told that the best way to learn is by doing so I have already been applying it anyway to this excellent website. All I need to know is how to prevent trades from triggering multiple times during Moving Average crossover strategies - or in that case any type of crossover strategy. Is there a way to require a certain percentage gain/loss from the previously opened position that would cancel the next crossover trade. Also would love to simply just limit activity to one buy and sell per security, per day.

#All Hail NASDAQ

class SamAlgorythm:

def Initialize(self):
self.SetStartDate(2013, 12, 1) # 2013/12/01 Earliest start date for selected ETFs
self.SetEndDate(2020, 6, 4)
self.SetCash(250000000) # Idk how to adjust for the 1000 splits TVIX has done
self.SetWarmup(100)
self.SetBenchmark("QQQ")
self.AddEquity("QQQ", Resolution.Hour)
self.AddEquity("TQQQ", Resolution.Hour) # 3x QQQ
self.AddEquity("TVIX", Resolution.Hour) # 2x VIX (er sumthin)
self.mothersma = self.SMA("QQQ", 25, Resolution.Hour)
self.othersma = self.SMA("QQQ", 75, Resolution.Hour)

def OnData(self, data):
if self.mothersma.Current.Value > self.othersma.Current.Value:
self.SetHoldings("TQQQ", 1) and self.SetHoldings("QQQ", 0) and self.SetHoldings("TVIX", 0)
elif self.mothersma.Current.Value <= self.othersma.Current.Value:
self.Liquidate("TQQQ")
self.SetHoldings("QQQ", 0.5) and self.SetHoldings("TVIX", 0.075)
else:
self.Liquidate("TQQQ") and self.Liquidate("QQQ") and self.Liquidate("TVIX")

Thanks in advance for any help and I look forward to eventually becoming a contributing member of this community!

Update Backtest







0

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.


  • Is there a way to require a certain percentage gain/loss from the previously opened position that would cancel the next crossover trade
Take a look here. You can for instance, access unrealized PnL with self.Portfolio['TQQQ'].UnrealizedProfit

and add that into the trading logic (i.e. your if statements).

  • Also would love to simply just limit activity to one buy and sell per security, per day.

OnData() is called every time data is streamed into the engine - in your case, every hour. A simple way to implement this would be to keep a dictionary like self.completed_trades = {'TQQQ": False, }, add if self.completed_trades['TQQQ'] == False, then perform the trade. After the trade, set it to True. Then reset the dictionary every day by:

def Initialize(self):
self.completed_trades = {'TQQQ': False}
self.Schedule.On(self.EveryDay('TQQQ'),
self.TimeRules.AfterMarketOpen('TQQQ', 0),
self.ResetVariables)

def OnData(self, data):
# if trade executes
self.completed_trades['TQQQ'] = True

# Define a scheduled function to be called
def ResetVariables(self):
self.completed_trades = {'TQQQ': False}

 

1

Update Backtest





0

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.


Loading...

This discussion is closed