/
getDaily.py
263 lines (227 loc) · 7.08 KB
/
getDaily.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# Kavin Autar
# 2013/11/17
# Last modified 2013/11/17
# Version 1.3
# Checks stocks
# Imports
import logger as log
import sys
import db
import pyql
import datetime
def numericInputFixup(input):
if "K" in str(input):
output = int(float(input.strip("K")) * 1000)
log.debug("trying to fixup numeric data %s into %s" % (input, output))
return output
elif "M" in str(input):
output = int(float(input.strip("M")) * 1000000)
log.debug("trying to fixup numeric data %s into %s" % (input, output))
return output
elif "B" in str(input):
output = int(float(input.strip("B")) * 1000000000)
log.debug("trying to fixup numeric data %s into %s" % (input, output))
return output
else:
log.debug("%s does not require numeric fixup" % input)
return input
def validate(input):
if input is None:
return input
if input == "N/A":
return None
if "%" in input: # remove % symbols
input = input.strip("%")
return numericInputFixup(input)
def getIndustryIDs():
sqlQuery = "SELECT ID FROM INDUSTRY;"
ids = db.selectQueryNoWhere(sqlQuery)
return ids
def getCompanyCodes():
industryIDs = getIndustryIDs()
companiesSQLQuery = """SELECT c.code || co.suffix as ycode
from company c
join country co on c.country_id = co.id
where c.industry_id = %s;
"""
# list of lists, each interior list comtains company codes for that id
companyByID = []
for row in industryIDs:
industryID = row['id']
idList = [industryID]
companies = db.selectQuery(companiesSQLQuery, idList)
companyList = []
for company in companies:
companyList.append(company['ycode'])
companyByID.append(companyList)
log.info("industry %s has %s companies" % (industryID, len(companyList)))
return companyByID
def insertData(data):
ycode = data['Symbol']
code = ycode.split(".")[0]
today = str(datetime.date.today())
keys = ["AverageDailyVolume",
"BookValue",
"Change",
"ChangeFromFiftydayMovingAverage",
"ChangeFromTwoHundreddayMovingAverage",
"ChangeFromYearHigh",
"ChangeFromYearLow",
"DaysHigh",
"DaysLow",
"DividendShare",
"DividendYield",
"EarningsShare",
"EBITDA",
"EPSEstimateCurrentYear",
"EPSEstimateCurrentYear",
"EPSEstimateNextQuarter",
"FiftydayMovingAverage",
"LastTradePriceOnly",
"MarketCapitalization",
"Open",
"PERatio",
"PercebtChangeFromYearHigh",
"PercentChange",
"PercentChangeFromFiftydayMovingAverage",
"PercentChangeFromTwoHundreddayMovingAverage",
"PercentChangeFromYearLow",
"PreviousClose",
"PriceBook",
"PriceEPSEstimateCurrentYear",
"PriceEPSEstimateCurrentYear",
"PriceEPSEstimateNextYear",
"PriceEPSEstimateNextYear",
"PriceSales",
"TwoHundreddayMovingAverage",
"Volume",
"YearHigh",
"YearLow"]
# validate data, 0 all null values
for key in keys:
data[key] = validate(data[key])
averagesQuery = """INSERT INTO AVERAGES
(DATE,
CODE,
AVERAGEDAILYVOLUME,
TWOHUNDREDDAYMOVINGAVERAGE,
FIFTYDAYMOVINGAVERAGE)
VALUES (%s, %s, %s, %s, %s);"""
changeQuery = """INSERT INTO CHANGE
(DATE,
CODE,
change,
changefromfiftydaymovingaverage,
changefromtwohundreddaymovingaverage,
changefromyearhigh,
changefromyearlow,
percentchangefromyearhigh,
percentchange,
percentchangefromfiftydaymovingaverage,
percentchangefromtwohundreddaymovingaverage,
percentchangefromyearlow)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"""
dailyQuery = """INSERT INTO DAILY
(DATE,
CODE,
volume,
dayshigh,
dayslow,
bookvalue,
open,
previousclose,
pricebook,
dividendshare,
dividendyield,
earningsshare,
marketcapitalization,
yearhigh,
yearlow,
peratio,
pricesales,
ebitda)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"""
epsQuery = """INSERT INTO EPS
(DATE,
CODE,
EPSESTIMATECURRENTYEAR,
EPSESTIMATENEXTQUARTER,
PRICEEPSESTIMATECURRENTYEAR,
PRICEEPSESTIMATENEXTYEAR)
VALUES (%s, %s, %s, %s, %s, %s);"""
historicQuery = """INSERT INTO HISTORIC_DATA
(DATE,
CODE,
HIGH,
LOW,
OPEN,
CLOSE,
VOLUME)
VALUES (%s, %s, %s, %s, %s, %s, %s);"""
avgValues = [today,
code,
data['AverageDailyVolume'],
data['TwoHundreddayMovingAverage'],
data['FiftydayMovingAverage']]
changeValues = [today,
code,
data['Change'],
data['ChangeFromFiftydayMovingAverage'],
data['ChangeFromTwoHundreddayMovingAverage'],
data['ChangeFromYearHigh'],
data['ChangeFromYearLow'],
data['PercebtChangeFromYearHigh'],
data['PercentChange'],
data['PercentChangeFromFiftydayMovingAverage'],
data['PercentChangeFromTwoHundreddayMovingAverage'],
data['PercentChangeFromYearLow']]
dailyValues = [today,
code,
data['Volume'],
data['DaysHigh'],
data['DaysLow'],
data['BookValue'],
data['Open'],
data['PreviousClose'],
data['PriceBook'],
data['DividendShare'],
data['DividendYield'],
data['EarningsShare'],
data['MarketCapitalization'],
data['YearHigh'],
data['YearLow'],
data['PERatio'],
data['PriceSales'],
data['EBITDA']]
epsValues = [today,
code,
data['EPSEstimateCurrentYear'],
data['EPSEstimateNextQuarter'],
data['PriceEPSEstimateCurrentYear'],
data['PriceEPSEstimateNextYear']]
historicValues = [today,
code,
data['DaysHigh'],
data['DaysLow'],
data['Open'],
data['PreviousClose'],
data['Volume']]
db.insertSingle(averagesQuery, avgValues)
db.insertSingle(changeQuery, changeValues)
db.insertSingle(dailyQuery, dailyValues)
db.insertSingle(epsQuery, epsValues)
db.insertSingle(historicQuery, historicValues)
def getDailyData():
# list of lists each list contains codes
companyCodesPerIndustry = getCompanyCodes()
for companies in companyCodesPerIndustry:
companyDataList = pyql.lookup(companies)
try:
for companyData in companyDataList:
insertData(companyData)
except:
log.error("no compainies in %s" % str(companies))
def main():
getDailyData()
if __name__ == "__main__":
main()