forked from Rockyzsu/stock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpush_msn.py
248 lines (218 loc) · 7.34 KB
/
push_msn.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
# -*-coding=utf-8-*-
__author__ = 'Rocky'
'''
http://30daydo.com
Contact: [email protected]
'''
import smtplib, time,os,datetime
from email.mime.text import MIMEText
from email.header import Header
from toolkit import Toolkit
from email.mime.multipart import MIMEMultipart
from email import Encoders, Utils
from toolkit import Toolkit
import tushare as ts
from pandas import Series
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import itchat
# 推送股价信息到手机
class MailSend():
def __init__(self, smtp_server, from_mail, password, to_mail):
self.server = smtp_server
self.username = from_mail.split("@")[0]
self.from_mail = from_mail
self.password = password
self.to_mail = to_mail
# 初始化邮箱设置
def send_txt(self, name, price, percent, status):
if 'up' == status:
content = '%s > %.2f , %.2f' % (name, price, percent)
if 'down' == status:
content = '%s < %.2f , %.2f' % (name, price, percent)
content = content + '%'
print(content)
subject = '%s' % name
self.msg = MIMEText(content, 'plain', 'utf-8')
self.msg['to'] = self.to_mail
self.msg['from'] = self.from_mail
self.msg['Subject'] = subject
self.msg['Date'] = Utils.formatdate(localtime=1)
try:
self.smtp = smtplib.SMTP_SSL(port=465)
self.smtp.connect(self.server)
self.smtp.login(self.username, self.password)
self.smtp.sendmail(self.msg['from'], self.msg['to'], self.msg.as_string())
self.smtp.quit()
print("sent")
except smtplib.SMTPException as e:
print(e)
return 0
def push_wechat(name, real_price, real_percent, type):
name='wwwei'
itchat.auto_login(hotReload=True)
account=itchat.get_friends(name)
for i in account:
if i['PYQuanPin']==name:
toName=i['UserName']
content=name+' ' + str(real_price)+' '+ str(real_percent)+' percent '+ type
itchat.send(content,toUserName=toName)
def push_msg(name, price, percent, status):
cfg = Toolkit.getUserData('data.cfg')
from_mail = cfg['from_mail']
password = cfg['password']
to_mail = cfg['to_mail']
obj = MailSend('smtp.qq.com', from_mail, password, to_mail)
obj.send_txt(name, price, percent, status)
def read_stock(name):
f = open(name)
stock_list = []
for s in f.readlines():
s = s.strip()
row = s.split(';')
# print(row)
# print("code :",row[0])
# rint "price :",row[1]
stock_list.append(row)
return stock_list
def meet_price(code, price_up, price_down,type):
try:
df = ts.get_realtime_quotes(code)
except Exception as e:
print(e)
time.sleep(5)
return 0
real_price = df['price'].values[0]
name = df['name'].values[0]
real_price = float(real_price)
pre_close = float(df['pre_close'].values[0])
percent = (real_price - pre_close) / pre_close * 100
# print(percent)
# percent=df['']
# print(type(real_price))
if real_price >= price_up:
print('%s price higher than %.2f , %.2f' % (name, real_price, percent),)
print('%')
if type=='msn':
push_msg(name, real_price, percent, 'up')
return 1
elif type=='wechat':
push_wechat(name, real_price, percent, 'up')
if real_price <= price_down:
print('%s price lower than %.2f , %.2f' % (name, real_price, percent),)
print('%')
if type=='msn':
push_msg(name, real_price, percent, 'down')
return 1
elif type=='wechat':
push_wechat(name, real_price, percent, 'down')
def meet_percent(code, percent_up, percent_down,type):
try:
df = ts.get_realtime_quotes(code)
except Exception as e:
print(e)
time.sleep(5)
return 0
real_price = df['price'].values[0]
name = df['name'].values[0]
real_price = float(real_price)
pre_close = float(df['pre_close'].values[0])
real_percent = (real_price - pre_close) / pre_close * 100
# print(percent)
# percent=df['']
# print(type(real_price))
if real_percent >= percent_up:
print('%s percent higher than %.2f , %.2f' % (name, real_percent, real_price),)
if type=='msn':
push_msg(name, real_price, real_price, 'up')
return 1
elif type=='wechat':
push_wechat(name, real_price, real_percent, 'down')
return 1
if real_percent <= percent_down:
print('%s percent lower than %.2f , %.2f' % (name, real_percent, real_price),)
print('%')
if type=='mns':
push_msg(name, real_price, real_percent, 'down')
return 1
elif type=='wechat':
push_wechat(name, real_price, real_percent, 'down')
return 1
# 推送一般的实盘消息
def general_info():
t_all = ts.get_today_all()
result = []
t1 = t_all[t_all['changepercent'] <= -9.0].count()['changepercent']
result.append(t1)
for i in range(-9, 9, 1):
temp = t_all[(i * 1.00 < t_all['changepercent']) & (t_all['changepercent'] <= (i + 1) * 1.00)].count()[
'changepercent']
result.append(temp)
t2 = t_all[t_all['changepercent'] > 9.0].count()['changepercent']
result.append(t2)
return result
#test in sourcetree
#test in house
#开板提示
def break_ceil(code):
while 1:
time.sleep(2)
try:
df=ts.get_realtime_quotes(code)
except:
time.sleep(5)
continue
v=long(df['b1_v'].values[0])
print(datetime.datetime.now().strftime("%H:%M:%S"))
print(v)
#print(type(v))
if v<=10000 :
print(u"小于万手,小心!跑")
push_msg('break',10,10,'down')
#这里可以优化,不必每次都登陆。
def monitor_break():
#all_base=pd.read_csv('bases.csv',dtype={'code':np.str})
break_ceil('002868')
def visual():
data = general_info()
s = Series(data=data, index=[range(-10, 10)])
print(s)
fg = s.plot(kind='bar', table=True)
plt.show(fg)
def main():
# read_stock()
choice = input("Input your choice:\n")
if str(choice) == '1':
# using price:
stock_lists_price = read_stock('price.txt')
while 1:
t = 0
for each_stock in stock_lists_price:
code = each_stock[0]
price_down = float(each_stock[1])
price_up = float(each_stock[2])
t = meet_price(code, price_up, price_down)
if t:
stock_lists_price.remove(each_stock)
if str(choice) == '2':
# using percent
stock_lists_percent = read_stock('percent.txt')
while 1:
t = 0
for each_stock in stock_lists_percent:
code = each_stock[0]
percent_down = float(each_stock[1])
percent_up = float(each_stock[2])
t = meet_percent(code, percent_up, percent_down,type)
if t:
stock_lists_percent.remove(each_stock)
if __name__ == '__main__':
path=os.path.join(os.getcwd(),'data')
if os.path.exists(path)==False:
os.mkdir(path)
os.chdir(path)
main()
# general_info()
#visual()
#monitor_break()