-
Notifications
You must be signed in to change notification settings - Fork 74
/
Algo Trading - Alternate Version.py
144 lines (110 loc) · 3.22 KB
/
Algo Trading - Alternate Version.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
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
#Algorithmic Trading with Machine Learning
# - Using Analysis of Highs Lows and Trading Volume
#imports
from time import *
from sklearn import tree
import datetime as dt
import matplotlib.pyplot as plt
from matplotlib import style
import pandas as pd
import pandas_datareader.data as web
import time
start_time = time.time()
import warnings
warnings.filterwarnings("ignore",category=DeprecationWarning)
#trading algorithm
def algo(t,h,l,v):
features = []
labels = []
for i in range(len(t) - acc):
temp_t = t[acc + i - 1]
temp_h = h[acc + i - 1]
temp_l = l[acc + i - 1]
temp_v = v[acc + i - 1]
features.append([temp_t, temp_h, temp_l, temp_v])
#1 means price went up
if t[acc + i] > t[acc + i - 1]:
labels.append([1])
else:
labels.append([0])
clf = tree.DecisionTreeClassifier()
clf.fit(features, labels)
temp_list = []
for i in range(acc):
temp_list.append([])
temp_list[i].append(t[-1*(acc - i)])
temp_list[i].append(h[-1*(acc - i)])
temp_list[i].append(l[-1*(acc - i)])
temp_list[i].append(v[-1*(acc - i)])
if clf.predict(temp_list)[0] == 1:
return 1
else:
return 0
#fields
acc = 10
Points = []
Highs = []
Lows = []
Volumes = []
dates = []
CashRecords = []
Cash = 100
Bought = False
days = 0
decision = 0
stockSymbol = 'AAPL'
style.use('ggplot')
start = dt.datetime(2015,1,1)
end = dt.datetime(2016,12,31)
#importing data
##df = web.DataReader(stockSymbol,'google',start,end)
##df.to_csv('data.csv')
df = pd.read_csv('data.csv', parse_dates = True)
for i in df[['Close']]:
for j in df[i]:
Points.append(round(j,2))
for i in df[['High']]:
for j in df[i]:
Highs.append(round(j,2))
for i in df[['Low']]:
for j in df[i]:
Lows.append(round(j,2))
for i in df[['Volume']]:
for j in df[i]:
Volumes.append(round(j,2))
for i in df[['Date']]:
for j in df[i]:
dates.append(dt.datetime.strptime(j, "%Y-%m-%d"))
#graph labels
plt.figure(num = stockSymbol)
plt.title(stockSymbol + " Stock Algorithmic Trading Analysis")
plt.xlabel('Date')
plt.ylabel('Stock Price / Cash')
while days <= len(df[['Close']]) - 1:
#stock info
days += 1
StockPrice = Points[days - 1]
if days == 1:
initP = StockPrice
initC = Cash
#your money
if Bought == True:
Cash = round(Cash*StockPrice/Points[days-2],2)
c = "green"
else:
c = "red"
CashRecords.append(Cash)
if days > acc:
decision = algo(Points[:days],Highs[:days],Lows[:days],Volumes[:days])
if Bought == True:
if decision == 0:
Bought = False
else:
if decision == 1:
Bought = True
plt.plot(dates[days - 2:days], Points[days - 2:days], color=c)
print("Ending Cash: " + str(CashRecords[-1]))
print("Expected Cash: " + str(round(CashRecords[0] * Points[-1] / Points[0],2)))
print("Performance: " + str(round(100 * CashRecords[-1] * Points[0] / (Points[-1] * CashRecords[0]),2)) + "%")
plt.plot(dates, CashRecords, color='blue')
plt.show()