-
Notifications
You must be signed in to change notification settings - Fork 22
/
zigzag_plt.py
122 lines (83 loc) · 3.21 KB
/
zigzag_plt.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
#!/usr/bin/python3
import matplotlib
# Avoid FutureWarning: Pandas will require you to explicitly register matplotlib converters.
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd
## Klang
from Klang import Kl,Klang
Klang.Klang_init()
##
#设置字体 ,显示股票中文名称
matplotlib.rcParams["font.sans-serif"] = ['AR PL UKai CN']
#
# load stock data by code
#
import sys
codename = "sh.600010"
if len(sys.argv)>1:
codename = sys.argv[1]
show = 1
if len(sys.argv)>2:
show = int(sys.argv[2])
Kl.code(codename)
print(codename,Kl.cur_name)
loaded_data = Kl.day_df
#print(loaded_data)
# Convert 'Timestamp' to 'float'.
# Need time be in float days format - see date2num.
loaded_data['datetime'] = [mdates.date2num(np.datetime64(d)) for d in loaded_data['datetime']]
# Create zigzag trendline.
########################################
# Find peaks(max).
data_x = loaded_data['datetime'].values
data_y = loaded_data['close'].values
peak_indexes = signal.argrelextrema(data_y, np.greater)
peak_indexes = peak_indexes[0]
# Find valleys(min).
valley_indexes = signal.argrelextrema(data_y, np.less)
valley_indexes = valley_indexes[0]
# Instantiate axes.
(fig, ax) = plt.subplots( figsize=(21, 7) )
# Merge peaks and valleys data points using pandas.
df_peaks = pd.DataFrame({'datetime': data_x[peak_indexes], 'zigzag_y': data_y[peak_indexes]})
df_valleys = pd.DataFrame({'datetime': data_x[valley_indexes], 'zigzag_y': data_y[valley_indexes]})
# Plot zigzag trendline.
ax.plot(df_peaks['datetime'].values, df_peaks['zigzag_y'].values,
color='red', label="zigzag_peak")
ax.plot(df_valleys['datetime'].values, df_valleys['zigzag_y'].values,
color='blue', label="zigzag_valley")
# Plot close price line.
ax.plot(data_x, data_y, linestyle='dashed', color='black', label="Close Price", linewidth=1)
# Customize graph.
##########################
plt.xlabel('Date')
plt.ylabel('Price')
plt.title( codename + "-" + Kl.cur_name + ' Prices - ZigZag trendline')
# Format time.
ax.xaxis_date()
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y-%m-%d"))
plt.gcf().autofmt_xdate() # Beautify the x-labels
plt.autoscale(tight=True)
plt.legend(loc='best')
plt.grid(True, linestyle='dashed')
plt.savefig("images/" + codename+"_zigzag.png",dpi=200,bbox_inches='tight')
if show == 1:
plt.show()
display = 0
if display == 1:
from bokeh.plotting import figure, output_file, show
output_file("images/" + codename+"_zigzag.html")
graph = figure(title = codename + "-" + Kl.cur_name + ' Prices - ZigZag trendline',width=1200,height=400)
# name of the x-axis
graph.xaxis.axis_label = "日期"
# name of the y-axis
graph.yaxis.axis_label = "价格"
graph.line(data_x, data_y,line_dash = "dotdash",line_color="black")
graph.line(df_valleys['datetime'].values, df_valleys['zigzag_y'].values,line_color="blue")
graph.line(df_peaks['datetime'].values, df_peaks['zigzag_y'].values,line_color="red")
show(graph)