-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdaily_close.py
executable file
·156 lines (109 loc) · 3.31 KB
/
daily_close.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
import requests
import os
from datetime import date, timedelta
from datetime import datetime
import pandas as pd
from pandas.tseries.holiday import get_calendar, HolidayCalendarFactory, GoodFriday
import time# as timecode
import mysql.connector
import sys
import telegram_send
import signal
import argparse
timestamp = int(time.time())
localtime = time.ctime(timestamp)
print("Local time:", localtime)
today = date.today()
yesterday = str(today - timedelta(days = 1))
today = str(today)
mdate = today.replace("-","")
# for testing set today to past date
#today = '2020-11-20'
print("Today: ",today, "Yesterday: ",yesterday)
global polygon_api_key
#connect to database
con = mydb = mysql.connector.connect(user='root', password='waWWii21156!', host='127.0.0.1', database='pts', allow_local_infile=True)
db_connection = mysql.connector.connect(user='root', password='waWWii21156!', host='127.0.0.1', database='pts')
table = "equities"
import datetime
f = open("closings.csv", "w")
header = "date,ticker,status,open,high,low,close,volume,afterHours,preMarket\n"
f.write(header)
sql = "select count(*) from " +table+ " where tradeable = 'True'"
cursor1 = con.cursor()
print ("sql:", (sql))
cursor1.execute(sql)
result = cursor1.fetchall()
for x in result:
cnt = (x[0])
print("Count: ",cnt)
sql = "select symbol,name from " +table+ " where tradeable = 'True'"
cursor1 = con.cursor()
print ("sql:", (sql))
cursor1.execute(sql)
result = cursor1.fetchall()
for x in result:
symbol = str(x[0])
name = str(x[1])
print("Symbol: ",symbol)
a = "https://api.polygon.io/v1/open-close/"
b = "/"
c = "?apiKey=jg0i3J_ZFD1_v4wBRQFEzp9NHDlYwHQff5_8_u"
url = a + symbol + b + yesterday + c
url = str(url)
print(url)
getstring = url
#print(getstring)
resp = requests.get(getstring)
print(resp)
if resp.status_code != 200:
# This means something went wrong.
raise ApiError('GET /historical/ {}'.format(resp.status_code))
r=resp.json()
print(r)
ticker = r['symbol']
date = r['from']
status = r['status']
open = r['open']
high = r['high']
low = r['low']
close = r['close']
volume = r['volume']
afterHours = r['afterHours']
preMarket = r['preMarket']
ticker = str(ticker)
date = str(date)
status = str(status)
open = str(open)
high = str(high)
low = str(low)
close = str(close)
volume = str(volume)
afterHours = str(afterHours)
preMarket = str(preMarket)
print("ticker:",ticker, date, status, close)
out = date + ", " +ticker+ "," +status+ "," +open+ "," +high+ "," +low+ "," +close+ "," +volume+ "," +afterHours+ "," +preMarket+ "\n"
print(out)
f.write(out)
f.close()
cursor1.close()
table = "closings"
sql1 = "create table IF NOT EXISTS " +table+ " (date char(10),ticker char(10) ,status int(1),open float,high float,low float,close float,volume int(12),afterHours float,preMarket float, pct_change float)"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
con.commit()
cursor.close()
sql1 = "load data local infile '/Users/danielsavage/flagler/algos/algo1/closings.csv' into table " +table+ " fields terminated by ','"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
con.commit()
cursor.close()
sql = "update closings as a, equities as b set a.shortable = b.shortable where a.symbol = b.symbol"
print(sql1)
cursor = con.cursor()
cursor.execute(sql1)
con.commit()
cursor.close()
quit()