-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpricechart.py
341 lines (292 loc) · 12.4 KB
/
pricechart.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
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
# -*- coding: utf-8 -*-
"""
Drawing realtime plot for trading
"""
# Major library imports
import time
import os
import sys
import wx
import random
#from numpy import arange, vstack
import numpy as np
# locale
#basepath = os.path.abspath(os.path.dirname(sys.argv[0]))
#localedir = os.path.join(basepath, "locale")
#langid = wx.LANGUAGE_DEFAULT
#domain = "messages"
#mylocale = wx.Locale(langid)
#mylocale.AddCatalogLookupPathPrefix(localedir)
#mylocale.AddCatalog(domain)
#_ = wx.GetTranslation
import gettext
_ = gettext.gettext
# Enthought library imports
from enable.api import Window, BaseTool
from enable.component_editor import ComponentEditor
from traitsui.api import Item, View, Group, HGroup, VGroup, Handler
from traits.api import HasTraits, Instance, Int, Float, Str, Any
from enable.example_support import DemoFrame, demo_main
from traits.has_traits import on_trait_change
from pyface.timer.api import Timer
# Chaco imports
from chaco.api import (ArrayPlotData,
ArrayDataSource,
HPlotContainer,
VPlotContainer,
OverlayPlotContainer,
Plot,
PlotAxis,
BarPlot,
FilledLinePlot,
DataRange1D,
LinearMapper,
create_line_plot,
add_default_axes,
add_default_grids
)
from chaco.tools.api import PanTool, ZoomTool, LineInspector, BroadcasterTool
from chaco.data_range_2d import DataRange2D
################################################################################
## Attributes to use for the plot view.
size = (900, 600)
title = "Candlestick plot"
bg_color = "lightgray"
color_green = (139 / 255.0, 186 / 255.0, 71 / 255.0, 1)
color_red = (255 / 255.0, 33 / 255.0, 33 / 255.0, 1)
################################################################################
class XYTool(BaseTool):
"""
CustomTool to return [X,Y] axis data
"""
callback = Any()
def normal_mouse_move(self, event):
#print dir(event)
if self.callback is not None:
#print "Screen: ", event.x, event.y
data = self.component.map_data((event.x, event.y))
return self.callback(self, data)
return []
class AnimationHandler(Handler):
def init(self, info):
super(AnimationHandler, self).init(info)
#info.object.timer = Timer(10, info.object.on_timer)
info.object.timer = Timer(300, info.object.on_timer)
def closed(self, info, is_ok):
super(AnimationHandler, self).closed(info, is_ok)
info.object.timer.Stop()
class Trait(HasTraits):
"""
Main Drawing view for plot
"""
container = VPlotContainer(padding=0, fill_padding=True,
bgcolor="lightgray", use_backbuffer=True)
#container2 = OverlayPlotContainer(padding = 5, fill_padding = True,
# bgcolor = "lightgray", use_backbuffer=True)
str_time = Str
float_open = Float
float_high = Float
float_low = Float
float_close = Float
float_volumn = Float
plot = Instance(Plot)
# TraitsUI view
traits_view = View(Group(
VGroup(
#HGroup(Item("str_time", label="時間"), show_border = True, label = u"詳細資料"),
HGroup(Item("str_time", label=_("time")),
Item("float_open", label=_("open")),
Item("float_high", label=_("high")),
Item("float_low", label=_("low")),
Item("float_close", label=_("close")),
Item("float_volumn", label=_("volumn")),
show_border=True, label=u"詳細資料"),
VGroup(Item(
"container", editor=ComponentEditor(), show_label=False))
)),
resizable=True,
width=size[0],
height=size[1],
handler=AnimationHandler(),
title=u"Autotrader")
#title=u"Plot 中文")
# Constructor
def __init__(self, dataframe=None):
super(Trait, self).__init__()
# main data frame for whole plot
self.df = dataframe
self.plot = self._create_plot()
############################################################################
### adjust boundary
def _compute_range2d(self):
#if len(self.df)> 100:
lowy = min(self.df.low[-100:])
lowy = lowy * 0.998
lowx = len(self.df) - 100
highy = max(self.df.high[-300:])
highy = highy * 1.002
highx = len(self.df)
range2d = DataRange2D(low=(lowx, lowy),
high=(highx, highy))
return range2d
@on_trait_change('redraw', post_init=True)
def _update_range2d(self):
self.plot.range2d = self._compute_range2d()
def _create_plot(self):
# count data
if len(self.df) > 0:
self.indexes = np.arange(len(self.df.date_time))
time_ds = ArrayDataSource(self.indexes)
vol_ds = ArrayDataSource(self.df.volumn.values, sort_order="none")
xmapper = LinearMapper(range=DataRange1D(time_ds))
vol_mapper = LinearMapper(range=DataRange1D(vol_ds))
####################################################################
# create volumn plot
vol_plot = BarPlot(index=time_ds, value=vol_ds,
index_mapper=xmapper,
value_mapper=vol_mapper,
line_color="transparent",
fill_color="blue",
bar_width=0.6,
antialias=False,
height=100,
resizable="h",
origin="bottom left",
bgcolor="white",
border_visible=True
)
vol_plot.padding = 30
vol_plot.padding_left = 40
vol_plot.tools.append(
PanTool(vol_plot, constrain=True, constrain_direction="x"))
add_default_grids(vol_plot)
add_default_axes(vol_plot)
#print vol_plot.index_mapper.range.high
#print vol_plot.value_mapper.range.low, vol_plot.value_mapper.range.high
self.vol_plot = vol_plot
self.container.add(vol_plot)
####################################################################
## Create price plot
sorted_vals = np.vstack(
(self.df.open, self.df.high, self.df.low, self.df.close))
sorted_vals.sort(0)
__bool = self.df.close.values - self.df.open.values
self.up_boolean = __bool >= 0
self.down_boolean = np.invert(self.up_boolean)
pd = ArrayPlotData(
up_index=self.indexes[self.up_boolean],
up_min=sorted_vals[0][self.up_boolean],
up_bar_min=sorted_vals[1][self.up_boolean],
up_bar_max=sorted_vals[2][self.up_boolean],
up_max=sorted_vals[3][self.up_boolean],
down_index=self.indexes[self.down_boolean],
down_min=sorted_vals[0][self.down_boolean],
down_bar_min=sorted_vals[1][self.down_boolean],
down_bar_max=sorted_vals[2][self.down_boolean],
down_max=sorted_vals[3][self.down_boolean],
volumn=self.df.volumn.values,
index=self.indexes
)
price_plot = Plot(pd)
up_plot = price_plot.candle_plot(
("up_index", "up_min", "up_bar_min", "up_bar_max", "up_max"),
color=color_red,
bgcolor="azure",
bar_line_color="black",
stem_color="black",
end_cap=False)[0]
down_plot = price_plot.candle_plot(
("down_index", "down_min", "down_bar_min",
"down_bar_max", "down_max"),
color=color_green,
bar_line_color="black",
stem_color="black",
end_cap=False)[0]
price_plot.fill_padding = True
price_plot.padding = 30
price_plot.padding_left = 40
price_plot.tools.append(ZoomTool(component=price_plot, tool_mode="box", zoom_factor=1.2, always_on=False))
price_plot.tools.append(PanTool(price_plot, drag_button="left"))
price_plot.tools.append(
XYTool(price_plot, callback=self._update_ohlc)) # get data
self._add_line_tool(up_plot)
self._add_line_tool(down_plot)
price_plot.range2d = self._compute_range2d()
price_plot.index_mapper = vol_plot.index_mapper # maper vol_plot and price_plot
self.price_plot = price_plot
self.container.add(price_plot)
def _update_ohlc(self, tool, xyarray):
"""Update Open, High, Low, Close, DateTime, items"""
if xyarray.size == 2:
# [x,y] ndarray
xindex = int(round(xyarray[0]))
if xindex >= 0 and xindex <= self.df.last_valid_index():
self.str_time = self.df.date_time[xindex].strftime("%H:%M")
self.float_open = self.df.open[xindex]
self.float_high = self.df.high[xindex]
self.float_low = self.df.low[xindex]
self.float_close = self.df.close[xindex]
self.float_volumn = self.df.volumn[xindex]
#print self.df.open[xindex]
def _add_line_tool(self, input_plot):
input_plot.overlays.append(LineInspector(input_plot, axis='index',
#inspect_mode="indexed", # will show two line
color="gray",
write_metadata=True,
is_listener=True))
############################################################################
### check plot data
def update_plot_data(self):
"""Update drawing"""
#t1 = time.time()
# vol_plot value update
self.vol_plot.value.set_data(self.df.volumn.values)
# price_plot value update
sorted_vals = np.vstack(
(self.df.open, self.df.high, self.df.low, self.df.close))
sorted_vals.sort(0)
__bool = self.df.close.values - self.df.open.values
self.up_boolean = __bool >= 0
self.down_boolean = np.invert(self.up_boolean)
self.price_plot.data.set_data(
'up_index', self.indexes[self.up_boolean])
self.price_plot.data.set_data(
'up_min', sorted_vals[0][self.up_boolean])
self.price_plot.data.set_data(
'up_bar_min', sorted_vals[1][self.up_boolean])
self.price_plot.data.set_data(
'up_bar_max', sorted_vals[2][self.up_boolean])
self.price_plot.data.set_data(
'up_max', sorted_vals[3][self.up_boolean])
self.price_plot.data.set_data(
'down_index', self.indexes[self.down_boolean])
self.price_plot.data.set_data(
'down_min', sorted_vals[0][self.down_boolean])
self.price_plot.data.set_data(
'down_bar_min', sorted_vals[1][self.down_boolean])
self.price_plot.data.set_data(
'down_bar_max', sorted_vals[2][self.down_boolean])
self.price_plot.data.set_data(
'down_max', sorted_vals[3][self.down_boolean])
#print "T1", time.time() - t1
def on_timer(self):
# debug
#self.df.volumn[self.df.last_valid_index()] = random.randrange(300,3000)
#self.vol_plot.value.set_data(self.df.volumn.values)
self.update_plot_data()
self.container.request_redraw()
return
if __name__ == "__main__":
# TODO test
import pandas
res = pandas.read_csv('T.CSV', names=['date', 'time', 'open', 'high', 'low', 'close', 'volumn', 'nan'], parse_dates=[[0, 1]])
gui = Trait(res)
gui.configure_traits()
#app = wx.GetApp()
#if app is None:
#app = wx.PySimpleApp()
##frame = PlotFrame(t.dataframe, size=(600,500), title='Plot')
#frame = PlotFrame(res, None, size=(700,400), title='Plot')
#app.SetTopWindow(frame)
#app.MainLoop()
#demo_main(PlotFrame, size=(600,500), title="plot")