[mw_shl_code
=
python,true]
import
numpy as npimport matplotlib.pyplot as plt
from
PythonApi
import
*
def
init(context):
context.s1
=
context.run_info.base_book_id
context.gz
=
{
'40'
:[],
'60'
:[],
'80'
:[]}
context.close
=
[]
context.date
=
[]
def
before_trading(context):
pass
def
handle_bar(context):
close
=
history_bars(context.s1,
1
,
'1d'
,
'CLOSE'
)
context.close.append(close[
-
1
])
context.date.append(context.now)
get_fin
=
get_finance(context.s1,
33
,
5
,
0
,
0
)
month
=
get_fin[
-
1
].date_val.month
if
month
=
=
3
:
for
i
in
context.gz.keys():
context.gz[i].append(get_fin[
-
1
].values
*
float
(i)
+
get_fin[
-
2
].values
*
float
(i)
-
get_fin[
0
].values
*
float
(i))
if
month
=
=
6
:
for
i
in
context.gz.keys():
context.gz[i].append(get_fin[
-
1
].values
*
float
(i)
+
get_fin[
-
3
].values
*
float
(i)
-
get_fin[
0
].values
*
float
(i))
if
month
=
=
9
:
for
i
in
context.gz.keys():
context.gz[i].append(get_fin[
-
1
].values
*
float
(i)
+
get_fin[
-
4
].values
*
float
(i)
-
get_fin[
0
].values
*
float
(i))
if
month
=
=
12
:
for
i
in
context.gz.keys():
context.gz[i].append(get_fin[
-
1
].values
*
float
(i))
def
exit(context):
test_report_none()
plt_show(context)
def
plt_show(context):
plt.rcParams[
'font.sans-serif'
]
=
[
'SimHei'
]
plt.rcParams[
'axes.unicode_minus'
]
=
False
fig
=
plt.figure(figsize
=
(
20
,
8
))
X
=
context.date
Y
=
[context.close]
labels
=
[
'close'
]
for
i
in
context.gz.keys():
Y.append(context.gz[i])
labels.append(i
+
'倍动态PE估值'
)
for
i
in
range
(
len
(Y)):
plt.plot(X,Y[i],label
=
labels[i])
plt.title(context.s1)
plt.xlabel(
'日期'
)
plt.ylabel(
'价格'
,rotation
=
90
)
plt.legend(loc
=
'best'
)
plt.show()