forked from SravB/Algorithmic-Trading
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithmic Trading.py
114 lines (88 loc) · 2.54 KB
/
Algorithmic Trading.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
#Algorithmic Trading with Machine Learning
#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):
features = []
labels = []
for i in range(len(t) - acc + 1):
features.append(t[-1*acc:-1])
#1 means price went up
if t[-1] > t[-2]:
labels.append(1)
else:
labels.append(0)
clf = tree.DecisionTreeClassifier()
clf.fit(features, labels)
if clf.predict(t[-1*acc+1:])[0] == 1:
return 1
else:
return 0
#fields
acc = 10
Points = []
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']]:
count = 0
for j in df[i]:
Points.append(round(j,2))
for i in df[['Date']]:
count = 0
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])
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()