forked from paperswithbacktest/awesome-systematic-trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
turn-of-the-month-in-equity-indexes.py
33 lines (24 loc) · 1.12 KB
/
turn-of-the-month-in-equity-indexes.py
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
# https://quantpedia.com/strategies/turn-of-the-month-in-equity-indexes/
#
# Buy SPY ETF 1 day (some papers say 4 days) before the end of the month and sell the 3rd trading day of the new month at the close.
from AlgorithmImports import *
class TurnoftheMonthinEquityIndexes(QCAlgorithm):
def Initialize(self):
self.SetStartDate(1998, 1, 1)
self.SetCash(100000)
self.symbol = self.AddEquity("SPY", Resolution.Daily).Symbol
self.sell_flag = False
self.days = 0
self.Schedule.On(self.DateRules.MonthStart(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol), self.Rebalance)
self.Schedule.On(self.DateRules.MonthEnd(self.symbol), self.TimeRules.AfterMarketOpen(self.symbol), self.Purchase)
def Purchase(self):
self.SetHoldings(self.symbol, 1)
def Rebalance(self):
self.sell_flag = True
def OnData(self, data):
if self.sell_flag:
self.days += 1
if self.days == 3:
self.Liquidate(self.symbol)
self.sell_flag = False
self.days = 0