-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_module.py
More file actions
44 lines (37 loc) · 1.74 KB
/
Copy pathstock_module.py
File metadata and controls
44 lines (37 loc) · 1.74 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
def get_setting(): # 取得 stock.txt 中的股票設定資訊
try:
with open('stock.txt') as f: #←以讀取模式開啟檔案
slist = f.readlines() #←以行為單位讀取所有資料
# print('讀入:', slist)
res = []
for lst in slist:
s = lst.split(',')
res.append([s[0], float(s[1]), float(s[2])])
except:
print('stock.txt 讀取錯誤')
return None
return res
import twstock
def get_price(stockid): # 取得股票名稱和及時股價
rt = twstock.realtime.get(stockid) # 取得台積電的及時交易資訊
if rt['success']: # 如果讀取成功
return (rt['info']['name'], #←傳回 (股票名稱, 及時價格)
float(rt['realtime']['latest_trade_price']))
else:
return (False, False)
def get_best(stockid): # 檢查是否符合四大買賣點
stock = twstock.Stock(stockid)
bp = twstock.BestFourPoint(stock).best_four_point()
if(bp):
return ('買進' if bp[0] else '賣出', bp[1]) #←傳回買進或賣出的建議
else:
return (False, False) #←都不符合
import requests # 匯入 requests 套件
def send_ifttt(v1, v2, v3): # 送出包含 3 個網址參數的 HTTP GET 要求
url = ('https://maker.ifttt.com/trigger/toline/with/' +
'key/bwxOrTArs7mvk4sPL04467')
data = {"value1" : v1, "value2" : v2, "value3" : v3}
r = requests.post(url, data = data) # 送出 HTTP GET
if r.text[:5] == 'Congr': # 傳回文字若以 Congr 開頭就表示成功了
print('已傳送 (' +str(data['value1'])+', '+str(data['value2'])+', '+str(data['value3'])+ ') 到 Line')
return r.text