forked from paperswithbacktest/awesome-systematic-trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
january-barometer.py
59 lines (49 loc) · 2.03 KB
/
january-barometer.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# https://quantpedia.com/strategies/january-barometer/
#
# Invest in the equity market in each January. Stay invested in equity markets (via ETF, fund, or futures) only if January return is positive; otherwise, switch investments to T-Bills.
from AlgorithmImports import *
class JanuaryBarometer(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2000, 1, 1)
self.SetCash(100000)
data = self.AddEquity("SPY", Resolution.Daily)
data.SetLeverage(10)
self.market = data.Symbol
data = self.AddEquity("BIL", Resolution.Daily)
data.SetLeverage(10)
self.t_bills = data.Symbol
self.start_price = None
self.recent_month = -1
def OnData(self, data):
if self.recent_month == self.Time.month:
return
self.recent_month = self.Time.month
if (
self.Securities[self.market].GetLastData()
and self.Securities[self.t_bills].GetLastData()
):
if (
self.Time.date()
- self.Securities[self.market].GetLastData().Time.date()
).days < 5 and (
self.Time.date()
- self.Securities[self.t_bills].GetLastData().Time.date()
).days < 5:
if self.Time.month == 1:
self.Liquidate(self.t_bills)
self.SetHoldings(self.market, 1)
self.start_price = self.Securities[self.market].Price
if self.Time.month == 2 and self.start_price:
returns = (
self.Securities[self.market].Price - self.start_price
) / self.start_price
if returns > 0:
self.SetHoldings(self.market, 1)
else:
self.start_price = None
self.Liquidate(self.market)
self.SetHoldings(self.t_bills, 1)
else:
self.Liquidate()
else:
self.Liquidate()