Skip to content

Commit 89a6729

Browse files
Merge pull request #76 from coffincw/renko-fix
Fix number of Renko bricks being drawn when trend switches directions
2 parents 3e1cc72 + 849ffc9 commit 89a6729

File tree

3 files changed

+60
-52
lines changed

3 files changed

+60
-52
lines changed

examples/renko_charts.ipynb

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

src/mplfinance/_utils.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,13 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
391391

392392
volume_cache = 0 # holds the volumes for the dates that were skipped
393393

394-
394+
last_diff_sign = 0 # direction the bricks were last going in -1 -> down, 1 -> up
395395
for i in range(len(cdiff)):
396-
num_bricks = abs(int(round(cdiff[i], 0)))
396+
num_bricks = abs(int(cdiff[i]))
397+
curr_diff_sign = cdiff[i]/abs(cdiff[i])
398+
if last_diff_sign != 0 and num_bricks > 0 and curr_diff_sign != last_diff_sign:
399+
num_bricks -= 1
400+
last_diff_sign = curr_diff_sign
397401

398402
if num_bricks != 0:
399403
new_dates.extend([dates[i]]*num_bricks)
@@ -442,7 +446,7 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
442446
linewidths=lw
443447
)
444448

445-
return (rectCollection, ), new_dates, new_volumes, brick_values
449+
return (rectCollection, ), new_dates, new_volumes, brick_values, brick_size
446450

447451
from matplotlib.ticker import Formatter
448452
class IntegerIndexDateTimeFormatter(Formatter):

src/mplfinance/plotting.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def plot( data, **kwargs ):
264264
collections = _construct_ohlc_collections(xdates, opens, highs, lows, closes,
265265
marketcolors=style['marketcolors'] )
266266
elif ptype == 'renko':
267-
collections, new_dates, volumes, brick_values = _construct_renko_collections(dates, highs, lows, volumes, config['renko_params'], closes,
267+
collections, new_dates, volumes, brick_values, brick_size = _construct_renko_collections(dates, highs, lows, volumes, config['renko_params'], closes,
268268
marketcolors=style['marketcolors'] )
269269

270270
formatter = IntegerIndexDateTimeFormatter(new_dates, fmtstring)
@@ -316,8 +316,12 @@ def plot( data, **kwargs ):
316316
avg_dist_between_points = (xdates[-1] - xdates[0]) / float(len(xdates))
317317
minx = xdates[0] - avg_dist_between_points
318318
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])
319+
if ptype is not 'renko':
320+
miny = min([low for low in lows if low != -1])
321+
maxy = max([high for high in highs if high != -1])
322+
else:
323+
miny = min([brick for brick in brick_values])
324+
maxy = max([brick+brick_size for brick in brick_values])
321325
corners = (minx, miny), (maxx, maxy)
322326
ax1.update_datalim(corners)
323327

0 commit comments

Comments
 (0)