-
Notifications
You must be signed in to change notification settings - Fork 5
/
future_fetch-dev-mail.py
executable file
·450 lines (299 loc) · 13.9 KB
/
future_fetch-dev-mail.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
"""
執行結果為於執行目錄下,將產生一個**期交所每日期貨收盤資料.txt**文字檔
期交所盤後約14:30分,網站才更新今日大盤資料
故14:30前需要去抓昨天以前的資料
期交所網站http://www.taifex.com.tw/chinese/3/3_1_1.asp
假設查詢不到該日期,將會將網頁跳至今天的日期
"""
import urllib, re
import time
import os
from datetime import datetime
from time import strftime
start = time.time() #記錄程式執行後的開始時間
today = strftime('%Y/%m/%d')
hour = strftime('%H%M')
year = strftime('%Y')
month = strftime('%m')
day = strftime('%d')
# 1430下午兩點半以前抓昨天以前的資料,1430以後抓今天以前的資料
if int(hour) < 1430:
day=int(day) #為何要抓昨天以前即前天資料? 不需要因為後段程式會抓今天的資料,尾部要抓今天資料是因為前半部遇到
#抓不到日期會跳到今天資料, 所以有設定條件日今天日期則Break跳出迴圈
if day < 9: #幫日期加一個零 => 01 02 ~ 09
download_date = year+'/'+month+'/'+'0'+str(day)
else:
download_date = year+'/'+month+'/'+str(day)
else:
day=int(day)
if day < 9: #幫日期加一個零 => 01 02 ~ 09
download_date = year+'/'+month+'/'+'0'+str(day)
else:
download_date = year+'/'+month+'/'+str(day)
download_date = today
f = open(u"today_future.txt",'w') #欲儲存資料的文字檔檔名
f.write("開盤價 最高價 最低價 最後成交價 成交量 未沖銷契約量 日期\n") #文字檔標頭
#fetch_year = [int(year)] # 想要哪何年資料,用逗號分隔
#fetch_month = [int(month)-4,int(month)] # 想要哪何月資料,用逗號分隔
fetch_year = [2015,2016]
fetch_month = [12,1,2]
print u"正在連結期交所網站抓取資料,請稍等。抓取一個月的資料約10秒,需等待多久取決於抓取多少月份的資料"
tmp = []
future_list = [] #儲存歷史資料
# 年月日迴圈
for iyear in fetch_year:
iyear = str(iyear)
for imonth in fetch_month:
imonth=str(imonth)
j=1
while j <=31:
print iyear,imonth,j # 每連結一天,於螢幕列印該天日期.
date = str(j)
optionUrl = "http://www.taifex.com.tw/chinese/3/3_1_1.asp?goday=&syear="+iyear+"&smonth="+imonth+"&sday="+date+"&COMMODITY_ID=TX" #連結期交所該天網頁
html = urllib.urlopen(optionUrl) #open file-like object
regexp = re.compile(r"<TD align=right class=\"12bk\">(?P<file>.*)</TD>") #Compile a regular expression pattern, returning a pattern object., 成交價格
check_date = re.compile(r"<h3 align=\'left\'>日期:(?P<file>.*)</h3>") # 該網頁的日期
# 正規表示式 (?P<file>.*) 一個括號裡面的東西為一個group
i = 0 #只抓前六筆符合的資料(開盤價 最高價 最低價 最後成交價 成交量 未沖銷契約量)
for line in html.readlines():
result = regexp.search(line)
date_ck = check_date.search(line)
#search(string[, pos[, endpos]]) --> match object or None.
#Scan through string looking for a match, and return a corresponding
#MatchObject instance. Return None if no position in the string matches.
if date_ck != None: #輸入不正確日期,會連結到今天的網頁
tmp1 = date_ck.group('file') #參數可以打 'file' 或只打數字 1 變數regexp一個括號裡面的東西為一個group
if tmp1 == download_date: # 假設網頁是今天跳出for迴圈,不儲存資料.期交所網站輸入不正確的日期會跳到今天日期的網頁
# http://www.taifex.com.tw/chinese/3/3_1_1.asp?goday=&syear=2015&smonth=5&sday=7&COMMODITY_ID=TX
break
if result != None: #列假日與國定假日都抓不到資料為None
fileName = result.group('file') #參數可以打 'file' 或只打數字 1 變數regexp一個括號裡面的東西為一個group
# MatchObject instances support the following methods and attributes:
# http://docs.python.org/release/2.5.2/lib/match-objects.html
i+=1
f.write('%-15s'%(fileName)) #儲存價格資料
tmp.append(fileName)
#future_list.append([fileName])
if i >= 6 : #只抓前六筆符合的資料(開盤價 最高價 最低價 最後成交價 成交量 未沖銷契約量),超過前六筆資料後列印日期與跳出for迴圈
tmp.append('%s-%s-%s'%(iyear,imonth,j))
future_list.append(tmp)
tmp = []
f.write('%s-%s-%s'%(iyear,imonth,j))
f.write('\n')
break
j+=1
html.close()
#f.close()
# 今天的日期
iyear = strftime('%Y')
imonth = strftime('%m')
date = strftime('%d')
tmp = []
optionUrl = "http://www.taifex.com.tw/chinese/3/3_1_1.asp?goday=&syear="+iyear+"&smonth="+imonth+"&sday="+date+"&COMMODITY_ID=TX" #連結期交所該天網頁
html = urllib.urlopen(optionUrl) #open file-like object
regexp = re.compile(r"<TD align=right class=\"12bk\">(?P<file>.*)</TD>") #Compile a regular expression pattern, returning a pattern object., 成交價格
check_date = re.compile(r"<h3 align=\'left\'>日期:(?P<file>.*)</h3>") # 該網頁的日期
# 正規表示式 (?P<file>.*) 一個括號裡面的東西為一個group
i = 0 #只抓前六筆符合的資料(開盤價 最高價 最低價 最後成交價 成交量 未沖銷契約量)
for line in html.readlines():
result = regexp.search(line)
date_ck = check_date.search(line)
#search(string[, pos[, endpos]]) --> match object or None.
#Scan through string looking for a match, and return a corresponding
#MatchObject instance. Return None if no position in the string matches.
if result != None: #列假日與國定假日都抓不到資料為None
fileName = result.group('file') #參數可以打 'file' 或只打數字 1 變數regexp一個括號裡面的東西為一個group
i+=1
f.write('%-15s'%(fileName)) #儲存價格資料
tmp.append(fileName)
#future_list.append([fileName])
if i >= 6 : #只抓前六筆符合的資料(開盤價 最高價 最低價 最後成交價 成交量 未沖銷契約量),超過前六筆資料後列印日期與跳出for迴圈
tmp.append('%s-%s-%s'%(iyear,imonth,date))
future_list.append(tmp)
f.write('%s-%s-%s'%(iyear,imonth,date))
tmp = []
break
#i+=1
html.close()
f.close()
future_file = open(u"calculate_future_average.txt",'w')
future_file.write("今天期貨收盤價==> %d"%(int(future_list[-1][3]))+"\n")
########
total = 0 #三日均價
index = -1
average_count = 1
while average_count <= 3:
total = total + float(future_list[index][3])
index = index - 1
average_count+=1
print "3日平均價==> %f"%(total/3)
future_file.write("期貨3日均價==> %d"%(total/3)+"\n")
########
high_price = [] #三日內最高價
index = -1
average_count = 1
while average_count <= 3:
high_price.append(float(future_list[index][1]))
index = index - 1
average_count+=1
print "期貨3日內最高價==> %d"%(max(high_price))
future_file.write("期貨3日內最高價==> %d"%(max(high_price))+"\n")
########
total = 0 #三日內最高價均價
index = -1
average_count = 1
while average_count <= 3:
total = total + float(future_list[index][1])
index = index - 1
average_count+=1
print "期貨3日內最高價均價==> %d"%(total/3)
future_file.write("期貨3日內最高價均價==> %d"%(total/3)+"\n")
########
low_price = [] #三日內最低價
index = -1
average_count = 1
while average_count <= 3:
low_price.append(float(future_list[index][2]))
index = index - 1
average_count+=1
print "期貨3日內最低價==> %d"%(min(low_price))
future_file.write("期貨3日內最低價==> %d"%(min(low_price))+"\n")
########
total = 0 #三日內最低價均價
index = -1
average_count = 1
while average_count <= 3:
total = total + float(future_list[index][2])
index = index - 1
average_count+=1
print "期貨3日內最低價均價==> %d"%(total/3)
future_file.write("期貨3日內最低價均價==> %d"%(total/3)+"\n")
########################################################################################
future_file.write("\n\n==============================================\n\n")
low_price = [] #5日內最低價
index = -1
average_count = 1
while average_count <= 5:
low_price.append(float(future_list[index][2]))
index = index - 1
average_count+=1
print "期貨5日內最低價==> %d"%(min(low_price))
future_file.write("期貨5日內最低價==> %d"%(min(low_price))+"\n")
high_price = [] #5日內最高價
index = -1
average_count = 1
while average_count <= 5:
high_price.append(float(future_list[index][1]))
index = index - 1
average_count+=1
print "期貨5日內高最價==> %d"%(max(high_price))
future_file.write("期貨5日內最高價==> %d"%(max(high_price))+"\n")
low_price = [] #10日內最低價
index = -1
average_count = 1
while average_count <= 10:
low_price.append(float(future_list[index][2]))
index = index - 1
average_count+=1
print "期貨10日內最低價==> %d"%(min(low_price))
future_file.write("期貨10日內最低價==> %d"%(min(low_price))+"\n")
high_price = [] #10日內最高價
index = -1
average_count = 1
while average_count <= 10:
high_price.append(float(future_list[index][1]))
index = index - 1
average_count+=1
print "期貨10日內高最價==> %d"%(max(high_price))
future_file.write("期貨10日內最高價==> %d"%(max(high_price))+"\n")
low_price = [] #20日內最低價
index = -1
average_count = 1
while average_count <= 20:
low_price.append(float(future_list[index][2]))
index = index - 1
average_count+=1
print "期貨20日內最低價==> %d"%(min(low_price))
future_file.write("期貨20日內最低價==> %d"%(min(low_price))+"\n")
high_price = [] #20日內最高價
index = -1
average_count = 1
while average_count <= 20:
high_price.append(float(future_list[index][1]))
index = index - 1
average_count+=1
print "期貨20日內高最價==> %d"%(max(high_price))
future_file.write("期貨20日內最高價==> %d"%(max(high_price))+"\n")
"""
low_price = [] #40日內最低價
index = -1
average_count = 1
while average_count <= 40:
low_price.append(float(future_list[index][2]))
index = index - 1
average_count+=1
print "期貨40日內最低價==> %d"%(min(low_price))
future_file.write("期貨40日內最低價==> %d"%(min(low_price))+"\n")
high_price = [] #40日內最高價
index = -1
average_count = 1
while average_count <= 40:
high_price.append(float(future_list[index][1]))
index = index - 1
average_count+=1
print "期貨40日內高最價==> %d"%(max(high_price))
future_file.write("期貨40日內最高價==> %d"%(max(high_price))+"\n")
low_price = [] #60日內最低價
index = -1
average_count = 1
while average_count <= 60:
low_price.append(float(future_list[index][2]))
index = index - 1
average_count+=1
print "期貨60日內最低價==> %d"%(min(low_price))
future_file.write("期貨60日內最低價==> %d"%(min(low_price))+"\n")
high_price = [] #60日內最高價
index = -1
average_count = 1
while average_count <= 60:
high_price.append(float(future_list[index][1]))
index = index - 1
average_count+=1
print "期貨60日內高最價==> %d"%(max(high_price))
future_file.write("期貨60日內最高價==> %d"%(max(high_price))+"\n")
"""
print u"抓取資料完成"
end = time.time() # 紀錄程式執行完的結束時間
elapsed = end - start # 總程式執行時間
print u"總共花費", elapsed, u"秒."
# 輸入 future_list 這個變數可查到抓回來的資料
# if tmp1 == download_date: 假設網頁是今天跳出for迴圈,不儲存資料.期交所網站輸入不正確的日期會跳到今天日期的網頁
future_file.close()
content = "風險管控,操作紀律." #沒有辦法換行
time_now = datetime.now().strftime("%Y%m%d-%H%M") #今天的日期 ex:2015-0411
title = str(time_now+"-盤後期貨週月季最高最低價") #Email郵件的標題 ex:2015-0411-選股機器人
attachment = 'calculate_future_average.txt' #附件名稱使用當日時間 ex:2015-0411.txt
f = open('/home/tim/GMAIL.txt','r') #於前一個相對目錄中放置登入GMAIL帳號密碼,目的為了不再GitHub顯示出來.
ID = f.readline().strip('\n') #不包含換行符號\n:q
PW = f.readline().strip('\n')
"""
os.system('sendEmail -o \
-f u160895@taipower.com.tw \
-t "WEI <weihautin@gmail.com>" u160895@taipower.com.tw \
-s smtp.gmail.com:587 \
-xu %s \
-xp %s \
-u %s \
-m %s \
-a %s \
'%(ID, PW, title, content, attachment))
"""
os.system('sendEmail -o \
-f u160895@taipower.com.tw \
-t "WEI <weihautin@gmail.com>" u160895@taipower.com.tw \
-s smtp.gmail.com:587 \
-xu %s \
-xp %s \
-u %s \
-o message-file=/home/tim/Stock_Bot/calculate_future_average.txt \
'%(ID, PW, title))