Skip to content

Commit 08019e4

Browse files
committed
Specified size pmove renko working w/out colors
1 parent 50d5d7e commit 08019e4

File tree

3 files changed

+139
-78
lines changed

3 files changed

+139
-78
lines changed

examples/customization_and_styles.ipynb

Lines changed: 84 additions & 65 deletions
Large diffs are not rendered by default.

src/mplfinance/_utils.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import datetime
99

1010
from matplotlib import colors as mcolors
11-
from matplotlib.collections import LineCollection, PolyCollection
11+
from matplotlib.patches import Rectangle
12+
from matplotlib.collections import LineCollection, PolyCollection, PatchCollection
1213

1314
from six.moves import zip
1415

@@ -262,7 +263,7 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
262263

263264
return rangeCollection, barCollection
264265

265-
def _construct_renko_collections(dates, renko_params, closes, marketcolors=None):
266+
def _construct_renko_collections(renko_params, closes, marketcolors=None):
266267
"""Represent the price change with bricks
267268
268269
Parameters
@@ -278,9 +279,47 @@ def _construct_renko_collections(dates, renko_params, closes, marketcolors=None)
278279
Returns
279280
-------
280281
ret : tuple
281-
(lineCollection, barCollection)
282+
PatchCollection
282283
"""
283-
return True
284+
if marketcolors is None:
285+
marketcolors = _get_mpfstyle('classic')['marketcolors']
286+
print('default market colors:',marketcolors)
287+
288+
renko_type = renko_params['type']
289+
brick_size = renko_params['brick_size']
290+
atr_length = renko_params['atr_length']
291+
292+
cdiff = [(closes[i+1] - closes[i])/brick_size for i in range(len(closes)-1)] # fill cdiff with close price change
293+
294+
bricks = []
295+
296+
prev_num = 0
297+
298+
for diff in cdiff:
299+
if diff > 0:
300+
bricks.extend([1]*int(round(diff, 0)))
301+
else:
302+
bricks.extend([-1]*abs(int(round(diff, 0))))
303+
304+
patches = []
305+
for index, number in enumerate(bricks):
306+
if number == 1: # positive
307+
facecolor='green'
308+
else: # negative
309+
facecolor='red'
310+
311+
prev_num += number
312+
313+
renko = Rectangle(
314+
xy=(index, prev_num * brick_size),
315+
width=1,
316+
height=brick_size,
317+
facecolor=facecolor,
318+
alpha=0.5
319+
)
320+
patches.append(renko) # add rectangle to the plot
321+
322+
return (PatchCollection(patches, match_original=False), )
284323

285324
from matplotlib.ticker import Formatter
286325
class IntegerIndexDateTimeFormatter(Formatter):

src/mplfinance/plotting.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def _valid_plot_kwargs():
6464

6565
vkwargs = {
6666
'type' : { 'Default' : 'ohlc',
67-
'Validator' : lambda value: value in ['candle','candlestick','ohlc','bars','ohlc_bars','line'] },
67+
'Validator' : lambda value: value in ['candle','candlestick','ohlc','bars','ohlc_bars','line', 'renko'] },
6868

6969
'style' : { 'Default' : 'default',
7070
'Validator' : lambda value: value in _styles.available_styles() or isinstance(value,dict) },
@@ -280,7 +280,7 @@ def plot( data, **kwargs ):
280280
marketcolors=style['marketcolors'] )
281281
elif ptype == 'renko':
282282
renko_params = _process_kwargs(kwargs['renko_params'], _valid_renko_kwargs())
283-
collections = _construct_renko_collections(xdates, renko_params, closes,
283+
collections = _construct_renko_collections(renko_params, closes,
284284
marketcolors=style['marketcolors'] )
285285
elif ptype == 'line':
286286
ax1.plot(xdates, closes, color=config['linecolor'])
@@ -310,13 +310,16 @@ def plot( data, **kwargs ):
310310
else:
311311
ax1.plot(xdates, mavprices)
312312

313-
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
314-
minx = xdates[0] - avg_dist_between_points
315-
maxx = xdates[-1] + avg_dist_between_points
316-
miny = min([low for low in lows if low != -1])
317-
maxy = max([high for high in highs if high != -1])
318-
corners = (minx, miny), (maxx, maxy)
319-
ax1.update_datalim(corners)
313+
if ptype == 'renko':
314+
ax1.autoscale()
315+
else:
316+
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
317+
minx = xdates[0] - avg_dist_between_points
318+
maxx = xdates[-1] + avg_dist_between_points
319+
miny = min([low for low in lows if low != -1])
320+
maxy = max([high for high in highs if high != -1])
321+
corners = (minx, miny), (maxx, maxy)
322+
ax1.update_datalim(corners)
320323

321324
if config['volume']:
322325
vup,vdown = style['marketcolors']['volume'].values()

0 commit comments

Comments
 (0)