-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.cs
157 lines (142 loc) · 5.84 KB
/
Main.cs
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections;
using System.Collections.Generic;
using QuantConnect.Securities;
using QuantConnect.Models;
using QuantConnect.Indicators;
namespace QuantConnect
{
/*
Summary:
Based on a macroeconomic indicator(CAPE Ratio), we are looking for entry/exit points for momentum stocks
CAPE data: January 1990 - December 2014
Goals:
Capitalize in overvalued markets by generating returns with momentum and selling before the crash
Capitalize in undervalued markets by purchasing stocks at bottom of trough
*/
public partial class Bubble : QCAlgorithm
{
decimal currCape;
decimal[] c = new decimal[4];
decimal[] cCopy = new decimal[4];
bool newLow = false;
int counter = 0;
int counter2 = 0;
MovingAverageConvergenceDivergence macd;
RelativeStrengthIndex rsi = new RelativeStrengthIndex(14);
ArrayList symbols = new ArrayList();
Dictionary <string, RelativeStrengthIndex> rsiDic = new Dictionary<string, RelativeStrengthIndex>();
Dictionary <string, MovingAverageConvergenceDivergence> macdDic = new Dictionary<string, MovingAverageConvergenceDivergence>();
public override void Initialize()
{
SetCash(100000);
symbols.Add("SPY");
SetStartDate(1998,1,1);
SetEndDate(2014,12,1);
//Present Social Media Stocks:
// symbols.Add("FB");symbols.Add("LNKD");symbols.Add("GRPN");symbols.Add("TWTR");
// SetStartDate(2011, 1, 1);
// SetEndDate(2014, 12, 1);
//2008 Financials:
// symbols.Add("C");symbols.Add("AIG");symbols.Add("BAC");symbols.Add("HBOS");
// SetStartDate(2003, 1, 1);
// SetEndDate(2011, 1, 1);
//2000 Dot.com:
// symbols.Add("IPET");symbols.Add("WBVN");symbols.Add("GCTY");
// SetStartDate(1998, 1, 1);
// SetEndDate(2000, 1, 1);
//CAPE data
AddData<CAPE>("CAPE");
foreach (string stock in symbols)
{
AddSecurity(SecurityType.Equity, stock, Resolution.Minute);
macd = MACD(stock, 12, 26, 9, MovingAverageType.Exponential, Resolution.Daily);
macdDic.Add(stock, macd);
rsi = RSI(stock, 14, MovingAverageType.Exponential, Resolution.Daily);
rsiDic.Add(stock, rsi);
}
}
//Trying to find if current Cape is the lowest Cape in three months to indicate selling period
public void OnData(CAPE data)
{
newLow = false;
//Adds first four Cape Ratios to array c
currCape = data.Cape;
if( counter < 4)
{
c[counter++] = currCape;
}
//Replaces oldest Cape with current Cape
//Checks to see if current Cape is lowest in the previous quarter
//Indicating a sell off
else
{
Array.Copy(c, cCopy, 4);
Array.Sort(cCopy);
if(cCopy[0] > currCape) newLow = true;
c[counter2++] = currCape;
if(counter2 == 4) counter2 = 0;
}
Debug("Current Cape: "+ currCape + " on "+ data.Time);
if(newLow) Debug("New Low has been hit on " + data.Time);
}
public void OnData(TradeBars data)
{
try
{
//Bubble territory
if(currCape > 20 && newLow == false)
{
foreach(string stock in symbols)
{
//Order stock based on MACD
//During market hours, stock is trading, and sufficient cash
if (Securities[stock].Holdings.Quantity == 0 && rsiDic[stock] < 70
&&Securities[stock].Price != 0 && Portfolio.Cash >Securities[stock].Price*100
&& Time.Hour== 9 && Time.Minute==30)
{
buy(stock);
}
//Utilize RSI for overbought territories and liquidate that stock
if(rsiDic[stock] > 70 && Securities[stock].Holdings.Quantity > 0
&& Time.Hour== 9 && Time.Minute==30)
{
sell(stock);
}
}
}
// Undervalued territory
else if(newLow == true)
{
foreach(string stock in symbols)
{
//Sell stock based on MACD
if(Securities[stock].Holdings.Quantity >0 && rsiDic[stock] > 30
&& Time.Hour== 9 && Time.Minute==30)
{
sell(stock);
}
//Utilize RSI and MACD to understand oversold territories
else if(Securities[stock].Holdings.Quantity == 0 && rsiDic[stock] < 30
&&Securities[stock].Price != 0 && Portfolio.Cash >Securities[stock].Price*100
&& Time.Hour== 9 && Time.Minute==30)
{
buy(stock);
}
}
}
// Cape Ratio is missing from orignial data
// Most recent cape data is most likely to be missing
else if(currCape == 0)
{
Debug("Exiting due to no CAPE!");
Quit("CAPE ratio not supplied in data, exiting.");
}
}
catch(Exception err)
{
Error(err.Message);
}
}
}
}