-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_ticker_stats-2.py
182 lines (121 loc) · 3.52 KB
/
create_ticker_stats-2.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
# This script runs every minute 24x7
import requests
import os
import pandas as pd
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
import time
import datetime
from datetime import date
from datetime import datetime
from datetime import timedelta
import mysql.connector
import sys
import inspect
import alpaca_trade_api as tradeapi
timestamp = int(time.time())
localtime = time.ctime(timestamp)
day = localtime[0:3]
hour = localtime[11:13]
min = localtime[14:16]
hour = int(hour)
min = int(min)
print("Local time:", localtime, day, hour, min)
timestamp = int(time.time())
localtime = time.ctime(timestamp)
print("Local time:", localtime)
starttime = time.time()
#connect to database
import mysql.connector as connection
import pandas as pd
#print(df)
#quit()
today = date.today()
print(today)
if (day == 'Mon'):
yesterday = str(today - timedelta(days = 3))
myesterday = str(yesterday)
ydate = myesterday.replace("-","")
else:
yesterday = str(today - timedelta(days = 1))
myesterday = str(yesterday)
ydate = myesterday.replace("-","")
mtoday = str(today)
mdate = mtoday.replace("-","")
print(mdate, ydate)
# ----------------------------------------------------------------
x = 1
for x in range(1,7):
if (x == 1):
ticker = 'aapl'
elif (x == 2):
ticker = 'qqq'
elif (x == 3):
ticker = 'spxl'
elif (x == 4):
ticker = 'spy'
elif (x == 5):
ticker = 'tqqq'
elif (x == 6):
ticker = 'vxx'
filename = mdate+ "_polygonaggr.csv"
print(filename)
dfx = pd.read_csv (filename)
# column names
dfx.columns = ['symbol','volume','avg_vol','open_price','open','high','low','localtime','close','average','volume','starttime','endtime']
#print(dfx)
# only use rows where close > 0
options = [ticker]
df = dfx[dfx['symbol'].isin(options)]
# df = subset(dfx, symbol=='LTC-USD')
# print(df)
df['tp'] = (df['high'] + df['low'] + df['close']) /3
df = df.sort_values("starttime")
#print(df)
avg = df['close'].rolling(window=60,center=False).mean()
xavg = df.close.ewm(com=60).mean()
avgtp = df['tp'].rolling(window=60,center=False).mean()
std = df.open.std(axis = 0, ddof=1, skipna = True)
# add a column for the price change
df['change1'] = df.close.diff(periods=1)
df['pct1'] = (df['change1'] / df['close']) * 100
df1 = df
df1['avg'] = avg
df1['std'] = std
df1['xavg'] = xavg
df1['avgtp'] = avgtp
bbul = (df1['avgtp'] + df1['std'])
bbll = (df1['avgtp'] - df1['std'])
df1['bbul'] = bbul
df1['bbll'] = bbll
ul = (df1['bbul'] - df1['avgtp'])
ll = (df1['avgtp'] - df1['bbll'])
df1['ul'] = ul
df1['ll'] = ll
pctul = (df1['ul'] / df1['avgtp'] )
pctll = (df1['ll'] / df1['avgtp'] )
diff1 = df1['xavg'] - df1['avgtp']
df1['diff1'] = diff1
pct1 = df1['diff1'] / df1['avgtp']
df1['pct1'] = pct1
df1['pctul'] = pctul
df1['pctll'] = pctll
df2 = df1[['starttime','close','change1','volume','pct1', 'diff1','xavg','avg','open','high','low']]
#print(df2)
df3 = df2.tail(1)
print(df3)
df4 = df2.tail(3600)
#print(df4)
#print("if diff1 > 0, sell signal. If diff1 < 0, buy signal")
outfile2 = ticker + "_last4hrs.csv"
#print(outfile)
df4.to_csv(outfile2, encoding='utf-8', index=False, header=False)
outfile = ticker + "_lastrow.csv"
#print(outfile)
df3.to_csv(outfile, encoding='utf-8', index=False, header=False)
outfile1 = ticker+ "_allstats.csv"
df1.to_csv(outfile1, encoding='utf-8', index=False, header=True)
x =+ 1
endtime = time.time()
lapsedtime = (endtime - starttime)
print("Time to load data: ",lapsedtime)
quit()