-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
201 lines (141 loc) · 4.38 KB
/
simulation.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# invest on 4 or more consecutive negative days
import pandas as pd
from sqlalchemy.types import Text
from sqlalchemy import create_engine
import matplotlib
import matplotlib.pyplot as plt
from matplotlib import animation
from matplotlib.animation import FuncAnimation
import numpy as np
from scipy.stats import norm
import statistics
from datetime import datetime, timedelta
from pytz import timezone
import os
import requests
import glob
import analysis.open_close.filter_a as fila
engine = create_engine('sqlite:////Users/kevinkoh/Desktop/bluecheese/bluecheese.db')
today = datetime.now(timezone('US/Eastern'))
today_string = today.strftime('%Y-%m-%d')
cwd = os.getcwd()
ticker_list = os.path.join(cwd, 'tickers', 'tickers.txt')
sim_log = os.path.join(cwd, 'results', 'sim.txt')
sim_image = os.path.join(cwd, 'results', 'sim.png')
sim_results = os.path.join(cwd, 'results', 'results.txt')
sim_gif = os.path.join(cwd, 'results', 'sim.gif')
sql = """
SELECT * FROM prod
WHERE Ticker = 'AAPL'
ORDER BY Date DESC
LIMIT 1
"""
data = pd.read_sql(sql, engine)
with open(sim_log, 'r') as s:
line = s.readline()
start_date = line.split(',')[0]
start_dt = datetime.strptime(start_date, '%Y-%m-%d') + timedelta(days = 1)
money = float(line.split(',')[1].strip())
final_date = data['Date'][0].split()[0]
final_dt = datetime.strptime(final_date, '%Y-%m-%d')
print('start...', start_dt)
print('end...', final_dt)
print('money...', money)
fig = plt.figure(figsize=(7,5))
plt.style.use('seaborn-deep')
listpos = []
balance_sheet = []
# change these tickers
sim_tickers = ['AAPL', 'AMZN', 'TSLA', 'MSFT', 'GOOGL']
def init():
plt.clf()
plt.title('Investing on 4 or more negative consecutive days')
plt.xlabel('Days')
plt.ylabel('Balance')
def animate(i):
global listpos
listpos.append(balance_sheet[i])
plt.plot(range(1,len(listpos)+1), listpos, color = '#00FF00')
plt.xlim(0, len(listpos))
def add_to_log(date, money, companies):
date_string = date.strftime('%Y-%m-%d')
with open(sim_log, 'r+') as f:
content = f.read()
f.seek(0)
f.write('{}, {}, {}\n{}'.format(date_string, money, companies, content))
f.flush()
def get_data(ticker):
end = start_dt
start = end + timedelta(days = -30)
sql = """
SELECT * FROM prod
WHERE Ticker = '{}' and Date BETWEEN '{}' AND '{}'
ORDER BY Date DESC
""".format(ticker, start, end)
df = pd.read_sql(sql, engine)
return df
def thumbsup():
final = []
for temp in sim_tickers:
ticker = temp.strip()
try:
df = get_data(ticker)
except:
print('error')
continue
result = fila.gogo(df)
count = result[0]
mode = result[1]
if mode == 'minus' and count >= 4:
final.append([ticker, mode, count])
return final
def main():
global money, start_dt
with open(sim_results, 'a') as f:
while start_dt <= final_dt:
print('{}'.format(start_dt))
f.write('\n{}\n'.format(start_dt))
f.flush()
final = thumbsup()
totalgain = 0
company_string = ''
for value in final:
start = start_dt
end = start_dt + timedelta(days = 1)
ticker = value[0]
mode = value[1]
count = value[2]
sql = """
SELECT * FROM prod
WHERE Ticker = '{}' and Date BETWEEN '{}' AND '{}'
ORDER BY Date DESC
""".format(ticker, start, end)
df = pd.read_sql(sql, engine)
if df.shape[0] != 1:
continue
company_string += '{} '.format(ticker)
result = fila.invest(money * (1/len(final)), ticker, df['Open'][0], df['Close'][0], mode, count)
totalgain += result['gain/loss']
f.write('{}, Open: {}, Close: {}, Bought: {}, Sold: {}, Gain/Loss: {}, Mode: {}, Count: {}\n'.format(result['ticker'], result['open'], result['close'], result['bought'], result['sold'], result['gain/loss'], result['mode'], result['count']))
f.flush()
money += totalgain
money = ((money * 100)//1)/100
f.write('current balance: {}\n'.format(money))
f.flush()
add_to_log(start_dt, money, company_string)
start_dt = start_dt + timedelta(days = 1)
def display():
global balance_sheet
with open(sim_log, 'r') as f:
for x in f:
balance_sheet.append(float(x.split(',')[1]))
balance_sheet.reverse()
ani = FuncAnimation(fig, animate, frames=len(balance_sheet), interval = 100, repeat=True, init_func=init)
with open(sim_gif, 'wb') as gif:
writergif = animation.PillowWriter()
ani.save(gif, writer=writergif)
plt.savefig(sim_image)
plt.close()
if __name__ == '__main__':
main()
display()