-
Notifications
You must be signed in to change notification settings - Fork 0
/
Open Price mean reversion strategy
37 lines (31 loc) · 1.56 KB
/
Open Price mean reversion strategy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# region imports
from AlgorithmImports import *
# endregion
class FatFluorescentOrangeOwlet(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1, 1) # Set Start Date
self.SetEndDate(2023, 1, 1) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.symbol = self.AddEquity("SPY", Resolution.Minute).Symbol
# make a rolling window of size 2 to compare last close with present
self.rollingWindow = RollingWindow[TradeBar](2)
# consolidate minute data to daily data
self.Consolidate(self.symbol, Resolution.Daily, self.CustomBarHandler)
# trade everyday, stop before 15 minutes from marketclose and exit all open positions at the end of the day
self.Schedule.On(self.DateRules.EveryDay(self.symbol),
self.TimeRules.BeforeMarketClose(self.symbol,15),
self.ExitPositions)
def OnData(self, data: Slice):
if not self.rollingWindow.IsReady:
return
# check the bar at opening of trading time (9:31AM) as bar data is available after a minute of trading open
if not(self.Time.hour==9 and self.Time.minute == 31):
return
if data[self.symbol].Open >= 1.01*self.rollingWindow[0].Close:
self.SetHoldings(self.symbol,-1)
if data[self.symbol].Open < 0.99*self.rollingWindow[0].Close:
self.SetHoldings(self.symbol,1)
def ExitPositions(self):
self.Liquidate(self.symbol)
def CustomBarHandler(self,bar):
self.rollingWindow.Add(bar)