FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
UserPyScript/monitor/monitor.py at master · youerning/UserPyScript · GitHub
youerning
/
UserPyScript
Public
Notifications
You must be signed in to change notification settings
Fork
19
Star
34
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
UserPyScript
/
monitor
/
monitor.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
437 lines (350 loc) · 12.7 KB
Breadcrumbs
UserPyScript
/
monitor
/
monitor.py
Copy path
File metadata and controls
437 lines (350 loc) · 12.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#coding: utf-8
"""
monitor change of stock price every period that user set
requirement 1: add specify stock in a way, confile file or web page
requirement 2: add rule of monitor, price change today, price change recent day
requirement 3: inform specify user immediately with specify way of notice, like email,etc.
"""
import
pandas
as
pd
import
tushare
as
ts
import
datetime
import
os
import
json
from
configparser
import
ConfigParser
from
utils
import
cache
confile
=
"monitor.conf"
name_cache
=
"stock.json"
sent_cache
=
"sent.json"
conf
=
ConfigParser
()
conf
.
read
(
confile
)
class
bar
(
object
):
"""a custom data type
"""
def
__init__
(
self
,
code
,
code_data
):
self
.
code
=
code
self
.
_df
=
code_data
self
.
data
=
dict
(
code_data
.
iloc
[
-
1
])
self
.
get_stockname
()
def
get_stockname
(
self
):
if
not
os
.
path
.
exists
(
name_cache
):
down_code
()
stocks
=
json
.
load
(
open
(
name_cache
))
stock_name
=
stocks
.
get
(
self
.
code
,
"None"
)
self
.
name
=
stock_name
def
down_code
():
"""download code and name of all of A stock and save as stock.json in working directory
Returns:
None
"""
import
requests
import
re
from
bs4
import
BeautifulSoup
import
json
url
=
"http://quote.eastmoney.com/stocklist.html"
headers
=
{
"user-agent"
:
"Mozilla/5.0 (Windows NT 10.0; Win64; x64)
\
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
}
page
=
requests
.
get
(
url
,
headers
=
headers
)
soup
=
BeautifulSoup
(
page
.
content
.
decode
(
"gbk"
),
"lxml"
)
pattern
=
"(.+?)\((\d+)\)"
css_select
=
"div#quotesearch ul li a"
stock_lis
=
soup
.
select
(
css_select
)
stocks
=
{}
for
stock
in
stock_lis
:
ret
=
re
.
findall
(
pattern
,
stock
.
text
)
stocks
[
ret
[
0
][
1
]]
=
ret
[
0
][
0
]
with
open
(
name_cache
,
"w"
)
as
wf
:
json
.
dump
(
stocks
,
wf
)
def
trade_time
():
"""is ts trade time
Returns:
True or False
"""
now
=
datetime
.
datetime
.
now
()
# the trade period of morning
mopen
=
"9:30"
mend
=
"11:30"
# the trade period of afternoon
aopen
=
"13:00"
aend
=
"15:00"
mopen
=
gen_time
(
mopen
)
mend
=
gen_time
(
mend
)
aopen
=
gen_time
(
aopen
)
aend
=
gen_time
(
aend
)
# whether now in morning period
morning
=
mopen
<
now
<
mend
# whether now in afternoon period
afternoon
=
aopen
<
now
<
aend
if
morning
or
afternoon
:
return
True
else
:
return
False
def
gen_time
(
dt
):
"""generate the time for specify hour and minute,like t=9:30
Args:
dt: the specify time in format, 9:30
Returns:
return the datetime with specify hour and minute today
example:
datetime.datetime(2018, 3, 1, 9, 30)
"""
moment
=
datetime
.
datetime
.
strptime
(
dt
,
"%H:%M"
)
ret
=
datetime
.
datetime
.
combine
(
datetime
.
datetime
.
today
(),
moment
.
time
())
return
ret
@
cache
def
fetch
(
code
):
"""fetch history data of the code of stock
fetch the data of stock in recent year,may be 3 year, use the method get_k_data of tushare
Args:
code: the code of specify stock
Returns:
a dataframe object of pandas
example:
open close high low volume code
date
2015-07-14 4.333 4.333 4.333 4.333 2443864.0 000725
2015-07-15 4.677 3.958 4.677 3.939 20725290.0 000725
2015-07-16 3.939 4.027 4.136 3.762 12367056.0 000725
2015-07-17 4.047 4.254 4.333 4.037 14418724.0 000725
2015-07-20 4.264 4.234 4.342 4.165 13036186.0 000725
"""
df
=
ts
.
get_k_data
(
code
)
df
.
index
=
pd
.
to_datetime
(
df
.
date
)
df
.
drop
(
"date"
,
axis
=
1
,
inplace
=
True
)
return
df
def
pchange
(
df
):
"""add two column of price-change to the df the args pass
add the column name pchange mean price change ration.
add the column name change mean how many tick changed.
Args:
df: a dataframe of specify stock.
Returns:
a dataframe object of pandas
example:
open close high low volume code pchange change
date
2015-07-14 4.333 4.333 4.333 4.333 2443864.0 000725 NaN NaN
2015-07-15 4.677 3.958 4.677 3.939 20725290.0 000725 -0.086545 -0.375
2015-07-16 3.939 4.027 4.136 3.762 12367056.0 000725 0.017433 0.069
2015-07-17 4.047 4.254 4.333 4.037 14418724.0 000725 0.056370 0.227
2015-07-20 4.264 4.234 4.342 4.165 13036186.0 000725 -0.004701 -0.020
"""
df
[
"pchange"
]
=
df
.
close
.
pct_change
()
df
[
"change"
]
=
df
.
close
.
diff
()
return
df
def
ma
(
df
,
days
=
[
10
,
20
,
50
]):
"""add ma to the df
add ma10,20,50 to the dataframe the args pass
Args:
df: a dataframe of specify stock.
Returns:
a dataframe object of pandas
example:
open close high low volume code pchange change
\
date
2018-02-22 5.59 5.66 5.68 5.54 5418361.0 000725 0.023508 0.13
2018-02-23 5.64 5.58 5.66 5.51 5013632.0 000725 -0.014134 -0.08
2018-02-26 5.65 5.79 5.86 5.47 11591063.0 000725 0.037634 0.21
2018-02-27 5.80 5.71 5.85 5.68 6707226.0 000725 -0.013817 -0.08
2018-02-28 5.61 5.65 5.73 5.56 4516456.0 000725 -0.010508 -0.06
MA10 MA20 MA50
date
2018-02-22 5.502 5.8915 5.8682
2018-02-23 5.492 5.8380 5.8682
2018-02-26 5.499 5.7950 5.8706
2018-02-27 5.534 5.7560 5.8688
2018-02-28 5.559 5.7110 5.8672
"""
for
ma
in
days
:
column_name
=
"MA{}"
.
format
(
ma
)
df
[
column_name
]
=
pd
.
rolling_mean
(
df
.
close
,
ma
)
return
df
def
report
(
s_lis
,
h_lis
,
subject
=
"Breakthrough"
):
"""create report for given code
Args:
s_lis: soft list
h_lis: hard list
subject: subject of notification
Returns:
a string of report
example:
breakthrough: soft-breakthrough
Code: 000725 京东方A
Price:5.82 3.2%(Percent change) 0.23(Price change)
MA10/20/50:5.72 5.68 5.56
"""
ret
=
[]
if
subject
==
"Breakthrough"
:
tpl
=
"""breakthrough: {brk}
Code: {code} {name}
Price: {close} {pchange:.3} {change:.3}
MA10/20/50: {MA10:.5} {MA20:.5} {MA50:.5}
"""
for
stock
in
s_lis
:
# print(code.name)
if
should_sent
(
stock
.
code
,
"soft-breakthrough"
):
msg
=
tpl
.
format
(
name
=
stock
.
name
,
brk
=
"soft-breakthrough(3%)"
,
**
stock
.
data
)
ret
.
append
(
msg
)
for
stock
in
h_lis
:
# print(code.name)
if
should_sent
(
stock
.
code
,
"hard-breakthrough"
):
msg
=
tpl
.
format
(
name
=
stock
.
name
,
brk
=
"hard-breakthrough(5%)"
,
**
stock
.
data
)
ret
.
append
(
msg
)
elif
subject
==
"Withdraw"
:
tpl
=
"""withdraw: {wd}
Code: {code} {name}
Price: {close} {pchange:.3} {change:.3}
MA10/20/50: {MA10:.5} {MA20:.5} {MA50:.5}
"""
for
stock
in
s_lis
:
# print(code.name)
if
should_sent
(
stock
.
code
,
"soft-withdown"
):
msg
=
tpl
.
format
(
name
=
stock
.
name
,
wd
=
"soft-withdown(3%)"
,
**
stock
.
data
)
ret
.
append
(
msg
)
for
stock
in
h_lis
:
# print(code.name)
if
should_sent
(
stock
.
code
,
"hard-withdown"
):
msg
=
tpl
.
format
(
name
=
stock
.
name
,
wd
=
"hard-withdown(5%)"
,
**
stock
.
data
)
ret
.
append
(
msg
)
elif
subject
==
"Stop loss"
:
tpl
=
"""stop loss: {sl}
Code: {code} {name}
Price: {close} {pchange:.3} {change:.3}
MA10/20/50: {MA10:.5} {MA20:.5} {MA50:.5}
"""
for
stock
in
s_lis
:
# print(code.name)
if
should_sent
(
stock
.
code
,
"soft-stoploss"
):
msg
=
tpl
.
format
(
name
=
stock
.
name
,
sl
=
"soft-stoploss(3%)"
,
**
stock
.
data
)
ret
.
append
(
msg
)
for
stock
in
h_lis
:
# print(code.name)
if
should_sent
(
stock
.
code
,
"hard-stoploss"
):
msg
=
tpl
.
format
(
name
=
stock
.
name
,
sl
=
"hard-stoploss(5%)"
,
**
stock
.
data
)
ret
.
append
(
msg
)
if
ret
:
split_line
=
""
.
join
([
"
\n
"
,
"-"
*
30
,
"
\n
"
])
message
=
split_line
.
join
(
ret
)
inform
(
subject
,
message
)
def
inform
(
subject
,
msg
):
"""inform user
"""
from
utils
import
sendMail
print
(
"send eamil..."
)
sendMail
(
subject
,
msg
)
print
(
subject
,
"
\n
"
,
msg
)
def
should_sent
(
code
,
status
):
"""have it sent?
if the stock have sent at same price change point before,just ingore.
Args:
code: code of the stock
status: status of the stock, contain soft-withdraw,soft-breakthrough,etc.
Returns:
True or False
example:
if it never sent before, send.
"""
tformat
=
"%Y-%m-%d %H:%M"
now
=
datetime
.
datetime
.
now
()
if
not
os
.
path
.
exists
(
sent_cache
):
# print("not exists")
with
open
(
sent_cache
,
"w"
)
as
wf
:
content
=
{}
content
[
"last_time"
]
=
now
.
strftime
(
tformat
)
content
[
"status"
]
=
{}
json
.
dump
(
content
,
wf
)
content
=
json
.
load
(
open
(
sent_cache
,
"r"
))
last_time
=
datetime
.
datetime
.
strptime
(
content
[
"last_time"
],
tformat
)
if
now
.
date
()
>
last_time
.
date
():
# print("before")
content
[
"last_time"
]
=
now
.
strftime
(
tformat
)
content
[
"status"
]
=
{}
content
[
"status"
][
code
]
=
status
with
open
(
sent_cache
,
"w"
)
as
wf
:
json
.
dump
(
content
,
wf
)
return
True
elif
now
.
date
()
==
last_time
.
date
():
# print("now")
last_status
=
content
[
"status"
].
get
(
code
)
if
last_status
==
status
:
return
False
else
:
content
[
"status"
][
code
]
=
status
with
open
(
sent_cache
,
"w"
)
as
wf
:
json
.
dump
(
content
,
wf
)
return
True
else
:
print
(
"strange date..."
)
return
False
def
main
():
# define hard-breakthrough,soft-breakthrough list
hbrk_lis
=
[]
sbrk_lis
=
[]
# define hard-withdraw, sort-withdraw list
hwd_lis
=
[]
swd_lis
=
[]
# define soft stop loss,hard stop loss list
ssl_lis
=
[]
hsl_lis
=
[]
attention_lis
=
conf
[
"default"
][
"attention"
]
attention_lis
=
attention_lis
.
split
()
position_lis
=
conf
.
options
(
"position"
)
# position_lis = position_lis.split()
# breakthrough, index 0 is soft-breakthrough, index 1 is hard-breakthrough
brk
=
conf
[
"default"
][
"breakthrough"
]
brk
=
[
float
(
p
)
for
p
in
brk
.
split
()]
# withdraw, index 0 is soft,index 1 is hard
wd
=
conf
[
"default"
][
"withdraw"
]
wd
=
[
float
(
p
)
for
p
in
wd
.
split
()]
for
stock
in
attention_lis
:
df
=
fetch
(
stock
)
df
=
pchange
(
df
)
df
=
ma
(
df
)
if
df
.
pchange
[
-
1
]
>
brk
[
1
]:
# print(stock)
data
=
bar
(
stock
,
df
)
hbrk_lis
.
append
(
data
)
continue
if
df
.
pchange
[
-
1
]
>
brk
[
0
]:
# print(stock)
data
=
bar
(
stock
,
df
)
sbrk_lis
.
append
(
data
)
if
hbrk_lis
or
sbrk_lis
:
report
(
sbrk_lis
,
hbrk_lis
)
for
stock
in
position_lis
:
# the cost of the stock
cost
=
float
(
conf
[
"position"
][
stock
])
df
=
fetch
(
stock
)
df
=
pchange
(
df
)
df
=
ma
(
df
)
# check if the percent of price change of stock decrease 5%
if
df
.
pchange
[
-
1
]
<
-
wd
[
1
]:
data
=
bar
(
stock
,
df
)
hwd_lis
.
append
(
data
)
continue
# check if the percent of price change of stock decrease 3%
if
df
.
pchange
[
-
1
]
<
-
wd
[
0
]:
data
=
bar
(
stock
,
df
)
swd_lis
.
append
(
data
)
if
swd_lis
or
hwd_lis
:
report
(
swd_lis
,
hwd_lis
,
subject
=
"Withdraw"
)
for
stock
in
position_lis
:
# the cost of the stock
cost
=
float
(
conf
[
"position"
][
stock
])
df
=
fetch
(
stock
)
df
=
pchange
(
df
)
df
=
ma
(
df
)
# position of stop loss
sslp
=
cost
*
(
1
-
wd
[
0
])
hslp
=
cost
*
(
1
-
wd
[
1
])
if
df
.
close
[
-
1
]
<
hslp
:
data
=
bar
(
stock
,
df
)
hsl_lis
.
append
(
data
)
continue
if
df
.
close
[
-
1
]
<
sslp
:
data
=
bar
(
stock
,
df
)
ssl_lis
.
append
(
data
)
if
ssl_lis
or
hsl_lis
:
report
(
ssl_lis
,
hsl_lis
,
subject
=
"Stop loss"
)
if
__name__
==
'__main__'
:
if
trade_time
:
main
()
Back
|
FazBrowse Home
|
New Git URL