Lets say you had the example code below, one function responsible for a buy condition and one for a sell condition that were each ran at the scheduler at a timed cadence. Well lets say the Buy signal triggered, followed by the sell signal. Before allowing the Buy Signal to fire again, I want a 7 day pause. How would one best go about this?
from AlgorithmImports import *
from QuantConnect.Data.Market import TradeBar
class RollingWindowAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2022,1,1) #Set Start Date
#self.SetEndDate(2021,10,1)
self.Tickers = ('SPY','QQQ')
self.SetCash(100000)
for symbol in self.tickers:
self.AddEquity(symbol, Resolution.Hour).Symbol
sma20 = self.SMA(symbol, 20, Resolution.Daily, Field.Close)
sma50 = self.SMA(symbol, 50, Resolution.Daily, Field.Close)
symbolData = SymbolData(symbol, sma20, sma50)
self.symbolDataBySymbol[symbol] = symbolData
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 10), self.BuySignal)
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.AfterMarketOpen("SPY", 10), self.SellSignal)
def OnData(self, data):
pass
def BuySignal(self):
if self.trade == False:
return
for symbol, symbolData in self.symbolDataBySymbol.items():
if not self.Portfolio[symbol].Invested and (self.Securities[symbol].Close > symbolData.sma20.Current.Value):
self.SetHoldings(symbol, .1, False, "Buy Signal")
def SellSignal(self):
if self.trade == False:
return
for symbol, symbolData in self.symbolDataBySymbol.items():
if self.Portfolio[symbol].Invested and (symbolData.sma20.Current.Value < symbolData.sma50.Current.Value):
self.Liquidate()
#SOME FORM OF LOGIC THAT DELAYS self.BuySignal from firing for 7 days, regardless of the Scheduler
class SymbolData:
def __init__(self, symbol, sma20,sma50):
self.Symbol = symbol
self.sma20 = sma20
self.sma50 = sma50
Is there a way to count x amount of days before allowing the BuySignal function to be run again?
Nico Xenox
Hey Axist,
you can check if the current date is bigger than the date you bought + x amount of days.
This will not enter the buy order block if the current time is not bigger than 7 days
Hope it helps ;)
Axist
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!