-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRun.py
More file actions
138 lines (125 loc) · 5.09 KB
/
Copy pathRun.py
File metadata and controls
138 lines (125 loc) · 5.09 KB
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
'''
@author: Hanjie
'''
from utils.pathtools import *
from dataSpider import *
import traceback
import os.path
import requests;
import struct
import pandas
'''
Usage: call this function to download history data csv file from yahoo for specified stock code
Input parameter: e.g. sz000001 or ss600000
'''
def downloadHistoryQuoteFile(code, debug=False, http_proxy=""):
historyQuoteUrl = "http://table.finance.yahoo.com/table.csv?s=" + code[2:8] + "." + code[0:2]
if debug:
print(historyQuoteUrl)
localFilename = code[2:8] + ".csv"
if debug:
print("creating " + localFilename)
if http_proxy<>"":
proxy = {}
proxy['http'] = http_proxy
r = requests.get(historyQuoteUrl, stream=True, proxies=proxy)
else:
r = requests.get(historyQuoteUrl, stream=True)
with open("./dataRepository/"+localFilename, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
f.flush()
f.close()
return localFilename
'''
Usage: call this function to calculate the Moving Average: MA and append the value to the data csv file from yahoo for specified stock code
Input parameter: e.g. sz000001 or ss600000
'''
def calculateMA(code):
fileName = convertOsPath(os.path.join(dataSpiderConfig._configDic['basePath'], 'dataRepository', (str(code) + '.csv')))
fileName_MA = convertOsPath(os.path.join(dataSpiderConfig._configDic['basePath'], 'dataRepository', (str(code)+ '_ma' + '.csv')))
if os.path.exists(fileName):
if os.path.exists(fileName_MA):
print ("600000_ma.csv already exist")
return "MA file exists"
stock_data = pandas.read_csv(fileName, parse_dates=[1])
stock_data.sort('Date', inplace=True)
ListMA = [5, 10, 20, 60]
for ma in ListMA:
stock_data['MA_' + str(ma)] = pandas.rolling_mean(stock_data['Close'], ma)
stock_data.sort('Date', ascending=False, inplace=True)
stock_data.to_csv('./dataRepository/600000_ma.csv', index=False)
else:
print (fileName + " does not exist, exiting")
return "Error: no fileName exists"
'''
Usage: call this function to read all stock code from TDX software, and write to stockCode.csv
Input parameter: the directory where the TDX is installed
'''
def readStockCodeFromTDX(tdxDir):
fo = open('.\stockCode.csv', 'wb');
fi = open(tdxDir+'\T0002\hq_cache\shex.tnf','rb')
fi.seek(50)
ss = fi.read(250)
while ss<>'':
if ss[0] == '6':
fo.write(ss[0:6])
fo.write(',')
if (ss[30]).encode('hex') == "00":
fo.write(ss[24:30])
elif (ss[31]).encode('hex') == "00":
fo.write(ss[24:31])
else:
fo.write(ss[24:32])
fo.write('\n')
ss = fi.read(250)
fi.close()
fi = open(tdxDir+'\T0002\hq_cache\szex.tnf','rb')
fi.seek(50)
ss = fi.read(250)
while ss<>'':
if ss[0:2] == '00' or ss[0:3] == '300':
fo.write(ss[0:6])
fo.write(',')
if (ss[30]).encode('hex') == "00":
fo.write(ss[24:30])
elif (ss[31]).encode('hex') == "00":
fo.write(ss[24:31])
else:
fo.write(ss[24:32])
fo.write('\n')
ss = fi.read(250)
fi.close()
fo.close()
if __name__ == '__main__':
if(not os.path.exists("./conf.ini")):
print("Conf.int file is missing, exiting...")
exit
else:
print ("Start to read the configuration file from ./conf.ini ...")
dataSpiderConfig = dataSpider("conf.ini")
if dataSpiderConfig._configDic['debugMode']:
print(dataSpiderConfig._configDic)
stockCode = os.path.join(dataSpiderConfig._configDic['basePath'], 'dataRepository', 'stockCode.csv')
if os.path.exists(stockCode):
print("The default stockCod file exists as ", stockCode)
else:
tdxRoot = os.path.join(dataSpiderConfig._configDic['basePath'], 'tdxRoot')
if dataSpiderConfig._configDic['debugMode']:
print(tdxRoot)
if os.path.exists(tdxRoot):
print("creating the stockCode file as ", stockCode)
readStockCodeFromTDX(tdxRoot)
else:
print('tdxRoot does not exist, which is mandatory here. exiting...')
sys.exit()
headers = ['code', 'name']
stockInfo = pandas.read_csv(stockCode, header=None, names=headers)
for code in stockInfo.code:
fileName = convertOsPath(os.path.join(dataSpiderConfig._configDic['basePath'],'dataRepository', (str(code) + '.csv')))
if not os.path.exists(fileName):
#downloadHistoryQuoteFile("ss"+str(code), False, dataSpiderConfig._configDic['http_proxy'])
downloadHistoryQuoteFile("ss"+str(code), True)
#returnValue = calculateMA(code)
#print (returnValue)