-
Notifications
You must be signed in to change notification settings - Fork 40
/
calc.py
146 lines (125 loc) · 4.58 KB
/
calc.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
# -*- coding: UTF-8 -*-
import baostock as bs
import numpy as np
import pandas as pd
import datetime as dt
import requests
import os
SCKEY=os.environ.get('SCKEY')
def send_server(title, text):
print(title)
print()
print(text)
if SCKEY == '' or SCKEY is None:
print("\nWarning: 微信消息无法发送,请设置sendkey!")
return
api = "https://sctapi.ftqq.com/{}.send".format(SCKEY)
content = text.replace('\n','\n\n')
data = {
'text':title, #标题
'desp':content} #内容
res = requests.post(api, data = data)
return(res)
def get_stock_code_name(stock_code):
# 获取证券基本资料
rs = bs.query_stock_basic(code=stock_code)
# rs = bs.query_stock_basic(code_name="浦发银行") # 支持模糊查询
# 打印结果集
data_list = []
while (rs.error_code == '0') & rs.next():
# 获取一条记录,将记录合并在一起
data_list.append(rs.get_row_data())
result = pd.DataFrame(data_list, columns=rs.fields)
return result.loc[0,'code_name']
def calc_MA2500(stock_code):
t = dt.datetime.utcnow()
t = t + dt.timedelta(hours=8) # 当前的北京时间
t11 = t + dt.timedelta(days=-365*11-3) # 得到11年前的时间,减去3个闰年
d = t.strftime('%Y-%m-%d')
d11 = t11.strftime('%Y-%m-%d')
#TIME
result = pd.DataFrame()
stock_name = get_stock_code_name(stock_code)
# 得到三栏的表格: date日期, code证卷代码, close收盘的点数。
k = bs.query_history_k_data_plus(stock_code,"date,code,close",start_date=d11, end_date=d,frequency="d", adjustflag="3")
# 得到k线数据
result = pd.concat([result,k.get_data()],axis=0,ignore_index=True)
result.date = pd.to_datetime(result.date)
result = result.sort_values(by='date',ascending=False) # 按日期排序,今天日期放在最上面
result = result.reset_index(drop=True)
today = pd.DataFrame()
# 得到最近2500天的收盘点数列表
today['close'] = result.nlargest(2500,'date').close
# 计算MA2500均值
MA2500 = today.close.astype(float).mean()
# 计算MA2500的上下约20%的浮动区间, 并且只取两位小数
MAdiv = int(MA2500/1.2*100)/100
MAmul = int((MA2500*1.2)*100)/100
MA2500 = int(MA2500*100)/100
# 今天的收盘
close_today = int(float(result.loc[0,'close'])*100)/100
date_today = result.loc[0,'date']
date_today_str = str(date_today.date()) + ": "
'''
# for debug purpose
print()
print("{}{}收盘点数: {}".format(date_today_str, stock_name, close_today))
print("MA2500÷1.2点数: {}".format(MAdiv))
print("MA2500 点数: {}".format(MA2500))
print("MA2500x1.2点数: {}".format(MAmul))
print()
'''
# recommend:
# 低于÷1.2线: ✔
# 高于÷1.2线: ✓
# 高于MA2500线: =
# 高于x1.2线: ✗
#
if (close_today <= MAdiv):
recommend = "✔"
title = date_today_str + stock_name + "低于÷1.2线" + ": " + recommend
else:
if (close_today <= MA2500):
judge = "÷1.2"
recommend = "✓"
elif (close_today <= MAmul):
judge = "MA2500"
recommend = "="
else:
judge = "*1.2"
recommend = "✗"
title = date_today_str + stock_name + "高于"+judge+"线" + ": " + recommend
#GENERATE TITLE
# 斜杠用来代码换行
text = date_today_str + stock_name + "收盘: " + str(close_today) + \
"\n" + date_today_str + "MA2500数据" + \
"\n\t *1.2点数: " + str(MAmul) + \
"\n\t 均值点数 " + str(MA2500) + \
"\n\t /1.2点数: " + str(MAdiv)
return title, text
def main():
title = "每周MA2500计算服务异常"
text = "登录获取股市数据失败"
try:
# login boastock server first
lg = bs.login()
if (lg.error_code == '0'):
title = ''
text = ''
STOCK_CODES = os.environ.get('STOCK_CODES')
if STOCK_CODES == '' or STOCK_CODES is None:
STOCK_CODES = 'sz.399001'
for code in STOCK_CODES.split():
brief, content = calc_MA2500(code)
if title == "":
title = brief
text += brief + "\n"
text += content + "\n"
send_server(title, text)
# logout at last
bs.logout
except Exception:
print(Exception)
send_server(title, text)
if __name__ == '__main__':
main()