-
Notifications
You must be signed in to change notification settings - Fork 1
/
HP_view.py
299 lines (281 loc) · 12.9 KB
/
HP_view.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
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
# -*- coding: utf-8 -*-
"""
#功能:通通股票分析软件框架窗口模块
#版本:Ver1.00
#设计人:独狼荷蒲
#电话:18578755056
#QQ:2775205
#百度:荷蒲指标
#开始设计日期: 2018-07-08
#公众号:独狼股票分析
#使用者请同意最后<版权声明>
#最后修改日期:2018年9月14日
#主程序:HP_main.py
"""
from tkinter import *
from tkinter.messagebox import *
from pandas import DataFrame, Series
import pandas as pd; import numpy as np
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
from matplotlib import ticker as mticker
from matplotlib.finance import candlestick_ohlc
from matplotlib.dates import DateFormatter, WeekdayLocator, DayLocator, MONDAY,YEARLY
from matplotlib.dates import MonthLocator,MONTHLY
import matplotlib
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import datetime as dt
import pylab
import matplotlib
import tkinter as tk
from tkinter import messagebox, filedialog, simpledialog, colorchooser
from tkinter import ttk
from PIL import Image, ImageTk
################################
#import HP_zwdata as sd
#import tushare as ts
import jqdatasdk as jq
############################
from HP_global import *
from HP_set import *
import HP_lib as mylib
import HP_draw as mydraw
class plotFrame3(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master #定义内部变量root
self.canvas=None
self.root.config(bg='black')
self.createPage()
def createPage(self):
ds=G_sday
de=G_eday
stockn=G_stock
matplotlib.use('TkAgg')
df1 = jq.get_price(stockn,start_date=ds,end_date=de, frequency='daily') # 聚宽获取股票数据
#df1 = ts.get_hist_data(stockn,start=ds,end=de) # tushare获取股票 数据
df2=df1.copy()
df2.dropna(inplace=True)
df2.insert(0,'date',df2.index)
df2=df2.reset_index(level=None, drop=True ,col_level=0, col_fill='')
print(df1)
print(df2)
days=df2
G_df=df2
MA1 = G_MA1
MA2 = G_MA2
Av1=mylib.G_MA(days['close'],MA1)
Av2=mylib.G_MA(days['close'],MA2)
SP = len(days.date.values[MA2-1:])
SP1 = len(days.date.values[MA1-1:])
fig = plt.figure(facecolor='#07000d',figsize=(7,4))
ax1 = plt.subplot2grid((7,4), (0,0), rowspan=4, colspan=4, axisbg='#07000d')
daysreshape = days.reset_index()
daysreshape['date']=mdates.date2num(daysreshape['date'].astype(dt.date))
daysreshape = daysreshape.reindex(columns=['date','open','high','low','close'])
candlestick_ohlc(ax1, daysreshape.values, width=.6, colorup='#ff1717', colordown='#53c156')
Label1 = str(MA1)+' MA'
Label2 = str(MA2)+' MA'
ax1.plot(days.date.values,Av1,'#e1edf9',label=Label1, linewidth=1.5)
ax1.plot(days.date.values[-SP:],Av2[-SP:],'#4ee6fd',label=Label2, linewidth=1.5)
ax1.grid(True, color='r')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax1.yaxis.label.set_color("w")
ax1.spines['bottom'].set_color("#5998ff")
ax1.spines['top'].set_color("#5998ff")
ax1.spines['left'].set_color("#5998ff")
ax1.spines['right'].set_color("#5998ff")
ax1.tick_params(axis='y', colors='w')
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))
ax1.tick_params(axis='x', colors='w')
plt.ylabel('Stock price')
ax1v = ax1.twinx()
ax1v.spines['bottom'].set_color("#5998ff")
ax1v.spines['top'].set_color("#5998ff")
ax1v.spines['left'].set_color("#5998ff")
ax1v.spines['right'].set_color("#5998ff")
ax1v.tick_params(axis='x', colors='w')
ax1v.tick_params(axis='y', colors='w')
ax0 = plt.subplot2grid((7,4), (4,0),sharex=ax1,rowspan=1, colspan=4, axisbg='#07000d')
v1=mylib.G_MA(days['volume'],G_MA1)
v2=mylib.G_MA(days['volume'],G_MA2)
v3=mylib.G_MA(days['volume'],G_MA3)
rsiCol = '#c1f9f7'
posCol = '#386d13'
negCol = '#8f2020'
ax0.plot(days.date.values, v1, rsiCol, linewidth=1)
ax0.plot(days.date.values, v2, posCol, linewidth=1)
ax0.bar(days.date.values,days.volume.values, facecolor='yellow', alpha=.4)
ax0.yaxis.label.set_color("w")
ax0.spines['bottom'].set_color("#5998ff")
ax0.spines['top'].set_color("#5998ff")
ax0.spines['left'].set_color("#5998ff")
ax0.spines['right'].set_color("#5998ff")
ax0.tick_params(axis='y', colors='w')
ax0.tick_params(axis='x', colors='w')
ax0.yaxis.set_major_locator(mticker.MaxNLocator(nbins=4, prune='upper'))#plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(nbins=4,prune='upper'))
ax0.tick_params(axis='x', colors='w')
plt.ylabel('volume')
if G_index=='KDJ' :
mydraw.draw_KDJ(ax1,days,9,3,3)
if G_index=='MACD' :
mydraw.draw_MACD(ax1,days,12,26,9)
if G_index=='RSI' :
mydraw.draw_RSI(ax1,days,6,12,24)
if G_index=='OBV' :
mydraw.draw_OBV(ax1,days,6,12)
plt.suptitle(stockn,color='w')
plt.setp(ax0.get_xticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.subplots_adjust(left=.04, bottom=.04, right=.96, top=.96, wspace=.15, hspace=0)
self.canvas =FigureCanvasTkAgg(fig, master=self.root)
toolbar =NavigationToolbar2TkAgg(self.canvas, self.root)
toolbar.update()
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
class MainFrame(Frame): # 继承Frame类
def __init__(self, master=None):
Frame.__init__(self, master)
self.root = master #定义内部变量root
self.createPage()
def createPage(self):
# 日期
Label(self , text=' ').grid(row=0, column=0)
label1 = Label(self , text='开始日期: ',ancho=S)
label1.grid(row=0, column=1)
# 输入框 (Entry)
self.date_s = StringVar()
entrydates = Entry(self, textvariable=self.date_s)
entrydates.grid(row=0, column=2)
self.date_s.set(G_sday)
Label(self , text=' ').grid(row=0, column=3)
label2 = Label(self , text='结束日期: ')
label2.grid(row=0, column=4)
# 输入框 (Entry)
self.date_e = StringVar()
entrydatee = Entry(self, textvariable=self.date_e)
self.date_e.set(G_eday)
entrydatee.grid(row=0, column=5)
Label(self , text=' ').grid(row=0, column=6)
label3 = Label(self , text='股票代码: ')
label3.grid(row=0, column=7)
# 输入框 (Entry)
self.stock = StringVar()
entrystock = Entry(self, textvariable=self.stock)
entrystock.grid(row=0, column=8)
Label(self , text=' ').grid(row=0, column=9)
# 按钮 (Button)
getname = Button(self , text='确认' ,command=self.st3)
getname.grid(row=0, column=10)
Label(self , text=' ').grid(row=0, column=11)
label4 = Label(self , text='指标: ')
label4.grid(row=0, column=12)
# Adding a Combobox
self.book = tk.StringVar()
bookChosen = ttk.Combobox(self , width=10, textvariable=self.book)
bookChosen['values'] = ('MACD', 'KDJ','RSI','OBV')
bookChosen.grid(row=0, column=13)
bookChosen.current(0) #设置初始显示值,值为元组['values']的下标
bookChosen.config(state='readonly') #设为只读模式
def st3(self):
ds=self.date_s.get()
de=self.date_e.get()
stockn=self.stock.get()
G_index=self.book.get()
stockn=mylib.jqsn(stockn) # 聚宽股票代码转换,不是聚宽数据,要注释掉
self.canvas._tkcanvas.pack_forget()
G_stock=stockn
matplotlib.use('TkAgg')
df1 = jq.get_price(stockn,start_date=ds,end_date=de, frequency='daily') # 聚宽获取股票数据
print(df1)
df2=df1.copy()
df2.dropna(inplace=True)
df2.insert(0,'date',df2.index)
df2=df2.sort_index(axis = 0,ascending = True,by = 'date')
df2=df2.reset_index(level=None, drop=True ,col_level=0, col_fill='')
print(df2)
days=df2
G_df=df2
MA1 = G_MA1
MA2 = G_MA2
Av1=mylib.G_MA(days['close'],MA1)
Av2=mylib.G_MA(days['close'],MA2)
SP = len(days.date.values[MA2-1:])
SP1 = len(days.date.values[MA1-1:])
fig = plt.figure(facecolor='#07000d',figsize=(7,4))
ax1 = plt.subplot2grid((7,4), (0,0), rowspan=4, colspan=4, axisbg='#07000d')
daysreshape = days.reset_index()
daysreshape['date']=mdates.date2num(daysreshape['date'].astype(dt.date))
daysreshape = daysreshape.reindex(columns=['date','open','high','low','close'])
candlestick_ohlc(ax1, daysreshape.values, width=.6, colorup='#ff1717', colordown='#53c156')
Label1 = str(MA1)+' MA'
Label2 = str(MA2)+' MA'
ax1.plot(days.date.values,Av1,'#e1edf9',label=Label1, linewidth=1.5)
ax1.plot(days.date.values[-SP:],Av2[-SP:],'#4ee6fd',label=Label2, linewidth=1.5)
ax1.grid(True, color='r')
ax1.xaxis.set_major_locator(mticker.MaxNLocator(10))
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax1.yaxis.label.set_color("w")
ax1.spines['bottom'].set_color("#5998ff")
ax1.spines['top'].set_color("#5998ff")
ax1.spines['left'].set_color("#5998ff")
ax1.spines['right'].set_color("#5998ff")
ax1.tick_params(axis='y', colors='w')
plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(prune='upper'))
ax1.tick_params(axis='x', colors='w')
plt.ylabel('Stock price')
ax1v = ax1.twinx()
ax1v.spines['bottom'].set_color("#5998ff")
ax1v.spines['top'].set_color("#5998ff")
ax1v.spines['left'].set_color("#5998ff")
ax1v.spines['right'].set_color("#5998ff")
ax1v.tick_params(axis='x', colors='w')
ax1v.tick_params(axis='y', colors='w')
ax0 = plt.subplot2grid((7,4), (4,0),sharex=ax1,rowspan=1, colspan=4, axisbg='#07000d')
v1=mylib.G_MA(days['volume'],G_MA1)
v2=mylib.G_MA(days['volume'],G_MA2)
rsiCol = '#c1f9f7'
posCol = '#386d13'
negCol = '#8f2020'
ax0.plot(days.date.values, v1, rsiCol, linewidth=1)
ax0.plot(days.date.values, v2, posCol, linewidth=1)
ax0.bar(days.date.values,days.volume.values, facecolor='yellow', alpha=.4)
ax0.yaxis.label.set_color("w")
ax0.spines['bottom'].set_color("#5998ff")
ax0.spines['top'].set_color("#5998ff")
ax0.spines['left'].set_color("#5998ff")
ax0.spines['right'].set_color("#5998ff")
ax0.tick_params(axis='y', colors='w')
ax0.tick_params(axis='x', colors='w')
ax0.yaxis.set_major_locator(mticker.MaxNLocator(nbins=4, prune='upper'))#plt.gca().yaxis.set_major_locator(mticker.MaxNLocator(nbins=4,prune='upper'))
ax0.tick_params(axis='x', colors='w')
plt.ylabel('volume')
if G_index=='KDJ' :
mydraw.draw_KDJ(ax1,days,9,3,3)
if G_index=='MACD' :
mydraw.draw_MACD(ax1,days,12,26,9)
if G_index=='RSI' :
mydraw.draw_RSI(ax1,days,6,12,24)
if G_index=='OBV' :
mydraw.draw_OBV(ax1,days,6,12)
plt.suptitle(stockn,color='w')
plt.setp(ax0.get_xticklabels(), visible=False)
plt.setp(ax1.get_xticklabels(), visible=False)
plt.subplots_adjust(left=.04, bottom=.04, right=.96, top=.96, wspace=.15, hspace=0)
self.canvas =FigureCanvasTkAgg(fig, master=self.root)
self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
'''
独狼荷蒲软件(或通通软件)版权声明
1、独狼荷蒲软件(或通通软件)均为软件作者设计,或开源软件改进而来,仅供学习和研究使用,不得用于任何商业用途。
2、用户必须明白,请用户在使用前必须详细阅读并遵守软件作者的“使用许可协议”。
3、作者不承担用户因使用这些软件对自己和他人造成任何形式的损失或伤害。
4、作者拥有核心算法的版权,未经明确许可,任何人不得非法复制;不得盗版。作者对其自行开发的或和他人共同开发的所有内容,
包括设计、布局结构、服务等拥有全部知识产权。没有作者的明确许可,任何人不得作全部或部分复制或仿造。
独狼荷蒲软件
QQ: 2775205
Tel: 18578755056
公众号:独狼股票分析
'''