forked from mementum/backtrader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinance.py
More file actions
594 lines (475 loc) · 18.9 KB
/
finance.py
File metadata and controls
594 lines (475 loc) · 18.9 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#!/usr/bin/env python
# -*- coding: utf-8; py-indent-offset:4 -*-
###############################################################################
#
# Copyright (C) 2015-2020 Daniel Rodriguez
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from ..utils.py3 import range, zip
import matplotlib.collections as mcol
import matplotlib.colors as mcolors
import matplotlib.legend as mlegend
import matplotlib.lines as mlines
from .utils import shade_color
class CandlestickPlotHandler(object):
legend_opens = [0.50, 0.50, 0.50]
legend_highs = [1.00, 1.00, 1.00]
legend_lows = [0.00, 0.00, 0.00]
legend_closes = [0.80, 0.00, 1.00]
def __init__(self,
ax, x, opens, highs, lows, closes,
colorup='k', colordown='r',
edgeup=None, edgedown=None,
tickup=None, tickdown=None,
width=1, tickwidth=1,
edgeadjust=0.05, edgeshading=-10,
alpha=1.0,
label='_nolegend',
fillup=True,
filldown=True,
**kwargs):
# Manager up/down bar colors
r, g, b = mcolors.colorConverter.to_rgb(colorup)
self.colorup = r, g, b, alpha
r, g, b = mcolors.colorConverter.to_rgb(colordown)
self.colordown = r, g, b, alpha
# Manage the edge up/down colors for the bars
if edgeup:
r, g, b = mcolors.colorConverter.to_rgb(edgeup)
self.edgeup = ((r, g, b, alpha),)
else:
self.edgeup = shade_color(self.colorup, edgeshading)
if edgedown:
r, g, b = mcolors.colorConverter.to_rgb(edgedown)
self.edgedown = ((r, g, b, alpha),)
else:
self.edgedown = shade_color(self.colordown, edgeshading)
# Manage the up/down tick colors
if tickup:
r, g, b = mcolors.colorConverter.to_rgb(tickup)
self.tickup = ((r, g, b, alpha),)
else:
self.tickup = self.edgeup
if tickdown:
r, g, b = mcolors.colorConverter.to_rgb(tickdown)
self.tickdown = ((r, g, b, alpha),)
else:
self.tickdown = self.edgedown
self.barcol, self.tickcol = self.barcollection(
x, opens, highs, lows, closes,
width, tickwidth, edgeadjust,
label=label,
fillup=fillup, filldown=filldown,
**kwargs)
# add collections to the axis and return them
ax.add_collection(self.tickcol)
ax.add_collection(self.barcol)
# Update the axis
ax.update_datalim(((0, min(lows)), (len(opens), max(highs))))
ax.autoscale_view()
# Add self as legend handler for this object
mlegend.Legend.update_default_handler_map({self.barcol: self})
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0 = handlebox.xdescent
y0 = handlebox.ydescent
width = handlebox.width / len(self.legend_opens)
height = handlebox.height
# Generate the x axis coordinates (handlebox based)
xs = [x0 + width * (i + 0.5) for i in range(len(self.legend_opens))]
barcol, tickcol = self.barcollection(
xs,
self.legend_opens, self.legend_highs,
self.legend_lows, self.legend_closes,
width=width, tickwidth=2,
scaling=height, bot=y0)
barcol.set_transform(handlebox.get_transform())
handlebox.add_artist(barcol)
tickcol.set_transform(handlebox.get_transform())
handlebox.add_artist(tickcol)
return barcol, tickcol
def barcollection(self,
xs,
opens, highs, lows, closes,
width, tickwidth=1, edgeadjust=0,
label='_nolegend',
scaling=1.0, bot=0,
fillup=True, filldown=True,
**kwargs):
# Prepack different zips of the series values
oc = lambda: zip(opens, closes) # NOQA: E731
xoc = lambda: zip(xs, opens, closes) # NOQA: E731
iohlc = lambda: zip(xs, opens, highs, lows, closes) # NOQA: E731
colorup = self.colorup if fillup else 'None'
colordown = self.colordown if filldown else 'None'
colord = {True: colorup, False: colordown}
colors = [colord[o < c] for o, c in oc()]
edgecolord = {True: self.edgeup, False: self.edgedown}
edgecolors = [edgecolord[o < c] for o, c in oc()]
tickcolord = {True: self.tickup, False: self.tickdown}
tickcolors = [tickcolord[o < c] for o, c in oc()]
delta = width / 2 - edgeadjust
def barbox(i, open, close):
# delta seen as closure
left, right = i - delta, i + delta
open = open * scaling + bot
close = close * scaling + bot
return (left, open), (left, close), (right, close), (right, open)
barareas = [barbox(i, o, c) for i, o, c in xoc()]
def tup(i, open, high, close):
high = high * scaling + bot
open = open * scaling + bot
close = close * scaling + bot
return (i, high), (i, max(open, close))
tickrangesup = [tup(i, o, h, c) for i, o, h, l, c in iohlc()]
def tdown(i, open, low, close):
low = low * scaling + bot
open = open * scaling + bot
close = close * scaling + bot
return (i, low), (i, min(open, close))
tickrangesdown = [tdown(i, o, l, c) for i, o, h, l, c in iohlc()]
# Extra variables for the collections
useaa = 0, # use tuple here
lw = 0.5, # and here
tlw = tickwidth,
# Bar collection for the candles
barcol = mcol.PolyCollection(
barareas,
facecolors=colors,
edgecolors=edgecolors,
antialiaseds=useaa,
linewidths=lw,
label=label,
**kwargs)
# LineCollections have a higher zorder than PolyCollections
# to ensure the edges of the bars are not overwriten by the Lines
# we need to put the bars slightly over the LineCollections
kwargs['zorder'] = barcol.get_zorder() * 0.9999
# Up/down ticks from the body
tickcol = mcol.LineCollection(
tickrangesup + tickrangesdown,
colors=tickcolors,
linewidths=tlw,
antialiaseds=useaa,
**kwargs)
# return barcol, tickcol
return barcol, tickcol
def plot_candlestick(ax,
x, opens, highs, lows, closes,
colorup='k', colordown='r',
edgeup=None, edgedown=None,
tickup=None, tickdown=None,
width=1, tickwidth=1.25,
edgeadjust=0.05, edgeshading=-10,
alpha=1.0,
label='_nolegend',
fillup=True,
filldown=True,
**kwargs):
chandler = CandlestickPlotHandler(
ax, x, opens, highs, lows, closes,
colorup, colordown,
edgeup, edgedown,
tickup, tickdown,
width, tickwidth,
edgeadjust, edgeshading,
alpha,
label,
fillup,
filldown,
**kwargs)
# Return the collections. the barcol goes first because
# is the larger, has the dominant zorder and defines the legend
return chandler.barcol, chandler.tickcol
class VolumePlotHandler(object):
legend_vols = [0.5, 1.0, 0.75]
legend_opens = [0, 1, 0]
legend_closes = [1, 0, 1]
def __init__(self,
ax, x, opens, closes, volumes,
colorup='k', colordown='r',
edgeup=None, edgedown=None,
edgeshading=-5, edgeadjust=0.05,
width=1, alpha=1.0,
**kwargs):
# Manage the up/down colors
r, g, b = mcolors.colorConverter.to_rgb(colorup)
self.colorup = r, g, b, alpha
r, g, b = mcolors.colorConverter.to_rgb(colordown)
self.colordown = r, g, b, alpha
# Prepare the edge colors
if not edgeup:
self.edgeup = shade_color(self.colorup, edgeshading)
else:
r, g, b = mcolors.colorConverter.to_rgb(edgeup)
self.edgeup = r, g, b, alpha
if not edgedown:
self.edgedown = shade_color(self.colordown, edgeshading)
else:
r, g, b = mcolors.colorConverter.to_rgb(edgedown)
self.edgedown = r, g, b, alpha
corners = (0, 0), (len(closes), max(volumes))
ax.update_datalim(corners)
ax.autoscale_view()
self.barcol = self.barcollection(
x, opens, closes, volumes,
width=width, edgeadjust=edgeadjust,
**kwargs)
# add to axes
ax.add_collection(self.barcol)
# Add a legend handler for this object
mlegend.Legend.update_default_handler_map({self.barcol: self})
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0 = handlebox.xdescent
y0 = handlebox.ydescent
width = handlebox.width / len(self.legend_vols)
height = handlebox.height
# Generate the x axis coordinates (handlebox based)
xs = [x0 + width * (i + 0.5) for i in range(len(self.legend_vols))]
barcol = self.barcollection(
xs, self.legend_opens, self.legend_closes, self.legend_vols,
width=width, vscaling=height, vbot=y0)
barcol.set_transform(handlebox.get_transform())
handlebox.add_artist(barcol)
return barcol
def barcollection(self,
x, opens, closes, vols,
width, edgeadjust=0,
vscaling=1.0, vbot=0,
**kwargs):
# Prepare the data
openclose = lambda: zip(opens, closes) # NOQA: E731
# Calculate bars colors
colord = {True: self.colorup, False: self.colordown}
colors = [colord[open < close] for open, close in openclose()]
edgecolord = {True: self.edgeup, False: self.edgedown}
edgecolors = [edgecolord[open < close] for open, close in openclose()]
# bar width to the sides
delta = width / 2 - edgeadjust
# small auxiliary func to return the bar coordinates
def volbar(i, v):
left, right = i - delta, i + delta
v = vbot + v * vscaling
return (left, vbot), (left, v), (right, v), (right, vbot)
barareas = [volbar(i, v) for i, v in zip(x, vols)]
barcol = mcol.PolyCollection(
barareas,
facecolors=colors,
edgecolors=edgecolors,
antialiaseds=(0,),
linewidths=(0.5,),
**kwargs)
return barcol
def plot_volume(
ax, x, opens, closes, volumes,
colorup='k', colordown='r',
edgeup=None, edgedown=None,
edgeshading=-5, edgeadjust=0.05,
width=1, alpha=1.0,
**kwargs):
vhandler = VolumePlotHandler(
ax, x, opens, closes, volumes,
colorup, colordown,
edgeup, edgedown,
edgeshading, edgeadjust,
width, alpha,
**kwargs)
return vhandler.barcol,
class OHLCPlotHandler(object):
legend_opens = [0.50, 0.50, 0.50]
legend_highs = [1.00, 1.00, 1.00]
legend_lows = [0.00, 0.00, 0.00]
legend_closes = [0.80, 0.20, 0.90]
def __init__(self,
ax, x, opens, highs, lows, closes,
colorup='k', colordown='r',
width=1, tickwidth=0.5,
alpha=1.0,
label='_nolegend',
**kwargs):
# Manager up/down bar colors
r, g, b = mcolors.colorConverter.to_rgb(colorup)
self.colorup = r, g, b, alpha
r, g, b = mcolors.colorConverter.to_rgb(colordown)
self.colordown = r, g, b, alpha
bcol, ocol, ccol = self.barcollection(
x, opens, highs, lows, closes,
width=width, tickwidth=tickwidth,
label=label,
**kwargs)
self.barcol = bcol
self.opencol = ocol
self.closecol = ccol
# add collections to the axis and return them
ax.add_collection(self.barcol)
ax.add_collection(self.opencol)
ax.add_collection(self.closecol)
# Update the axis
ax.update_datalim(((0, min(lows)), (len(opens), max(highs))))
ax.autoscale_view()
# Add self as legend handler for this object
mlegend.Legend.update_default_handler_map({self.barcol: self})
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0 = handlebox.xdescent
y0 = handlebox.ydescent
width = handlebox.width / len(self.legend_opens)
height = handlebox.height
# Generate the x axis coordinates (handlebox based)
xs = [x0 + width * (i + 0.5) for i in range(len(self.legend_opens))]
barcol, opencol, closecol = self.barcollection(
xs,
self.legend_opens, self.legend_highs,
self.legend_lows, self.legend_closes,
width=1.5, tickwidth=2,
scaling=height, bot=y0)
barcol.set_transform(handlebox.get_transform())
handlebox.add_artist(barcol)
# opencol.set_transform(handlebox.get_transform())
handlebox.add_artist(opencol)
# closecol.set_transform(handlebox.get_transform())
handlebox.add_artist(closecol)
return barcol, opencol, closecol
def barcollection(self,
xs,
opens, highs, lows, closes,
width, tickwidth,
label='_nolegend',
scaling=1.0, bot=0,
**kwargs):
# Prepack different zips of the series values
ihighlow = lambda: zip(xs, highs, lows) # NOQA: E731
iopen = lambda: zip(xs, opens) # NOQA: E731
iclose = lambda: zip(xs, closes) # NOQA: E731
openclose = lambda: zip(opens, closes) # NOQA: E731
colord = {True: self.colorup, False: self.colordown}
colors = [colord[open < close] for open, close in openclose()]
# Extra variables for the collections
useaa = 0,
lw = width,
tlw = tickwidth,
# Calculate the barranges
def barrange(i, high, low):
return (i, low * scaling + bot), (i, high * scaling + bot)
barranges = [barrange(i, high, low) for i, high, low in ihighlow()]
barcol = mcol.LineCollection(
barranges,
colors=colors,
linewidths=lw,
antialiaseds=useaa,
label=label,
**kwargs)
def tickopen(i, open):
open = open * scaling + bot
return (i - tickwidth, open), (i, open)
openticks = [tickopen(i, open) for i, open in iopen()]
opencol = mcol.LineCollection(
openticks,
colors=colors,
antialiaseds=useaa,
linewidths=tlw,
label='_nolegend',
**kwargs)
def tickclose(i, close):
close = close * scaling + bot
return (i, close), (i + tickwidth, close)
closeticks = [tickclose(i, close) for i, close in iclose()]
closecol = mcol.LineCollection(
closeticks,
colors=colors,
antialiaseds=useaa,
linewidths=tlw,
label='_nolegend',
**kwargs)
# return barcol, tickcol
return barcol, opencol, closecol
def plot_ohlc(ax, x, opens, highs, lows, closes,
colorup='k', colordown='r',
width=1.5, tickwidth=0.5,
alpha=1.0,
label='_nolegend',
**kwargs):
handler = OHLCPlotHandler(
ax, x, opens, highs, lows, closes,
colorup, colordown,
width, tickwidth,
alpha,
label,
**kwargs)
return handler.barcol, handler.opencol, handler.closecol
class LineOnClosePlotHandler(object):
legend_closes = [0.00, 0.66, 0.33, 1.00]
def __init__(self,
ax, x, closes, color='k',
width=1, alpha=1.0,
label='_nolegend',
**kwargs):
self.color = color
self.alpha = alpha
self.loc, = self.barcollection(
x, closes,
width=width,
label=label,
**kwargs)
# add collections to the axis and return them
ax.add_line(self.loc)
# Update the axis
ax.update_datalim(((x[0], min(closes)), (x[-1], max(closes))))
ax.autoscale_view()
# Add self as legend handler for this object
mlegend.Legend.update_default_handler_map({self.loc: self})
def legend_artist(self, legend, orig_handle, fontsize, handlebox):
x0 = handlebox.xdescent
y0 = handlebox.ydescent
width = handlebox.width / len(self.legend_closes)
height = handlebox.height
# Generate the x axis coordinates (handlebox based)
xs = [x0 + width * (i + 0.5) for i in range(len(self.legend_closes))]
linecol, = self.barcollection(
xs, self.legend_closes,
width=1.5,
scaling=height, bot=y0)
linecol.set_transform(handlebox.get_transform())
handlebox.add_artist(linecol)
return linecol,
def barcollection(self,
xs, closes,
width,
label='_nolegend',
scaling=1.0, bot=0,
**kwargs):
# Prepack different zips of the series values
scaled = [close * scaling + bot for close in closes]
loc = mlines.Line2D(
xs, scaled,
color=self.color,
lw=width,
label=label,
alpha=self.alpha,
**kwargs)
return loc,
def plot_lineonclose(ax, x, closes,
color='k',
width=1.5,
alpha=1.0,
label='_nolegend',
**kwargs):
handler = LineOnClosePlotHandler(
ax, x, closes,
color=color, width=width,
alpha=alpha, label=label,
**kwargs)
return handler.loc,