Skip to content

Commit a9f689a

Browse files
committed
rm renko addplot + small changes + update example
1 parent 35f07c2 commit a9f689a

File tree

3 files changed

+45
-92
lines changed

3 files changed

+45
-92
lines changed

examples/renko_charts.ipynb

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

src/mplfinance/_utils.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"""
55

66
import numpy as np
7-
import pandas as pd
87
import matplotlib.dates as mdates
98
import datetime
109

@@ -90,9 +89,9 @@ def _calculate_atr(atr_length, highs, lows, closes):
9089
all_lows : list of lows
9190
all_closes : list of closes
9291
"""
93-
if atr_length < 0:
94-
raise ValueError("Specified atr_length may not be less than 0")
95-
elif atr_length > len(closes):
92+
if atr_length < 1:
93+
raise ValueError("Specified atr_length may not be less than 1")
94+
elif atr_length >= len(closes):
9695
raise ValueError("Specified atr_length is larger than the length of the dataset: " + str(len(closes)))
9796
atr = 0
9897
for i in range(len(highs)-atr_length, len(highs)):
@@ -150,8 +149,8 @@ def _valid_renko_kwargs():
150149
kwarg value is one of the allowed values.
151150
'''
152151
vkwargs = {
153-
'brick_size' : { 'Default' : 2.0,
154-
'Validator' : lambda value: isinstance(value,float) or isinstance(value,int) or value == 'atr' },
152+
'brick_size' : { 'Default' : 'atr',
153+
'Validator' : lambda value: isinstance(value,(float,int)) or value == 'atr' },
155154
'atr_length' : { 'Default' : 14,
156155
'Validator' : lambda value: isinstance(value,int) },
157156
}
@@ -365,11 +364,19 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
365364

366365
if brick_size == 'atr':
367366
brick_size = _calculate_atr(atr_length, highs, lows, closes)
367+
else: # is an integer or float
368+
total_atr = _calculate_atr(len(closes)-1, highs, lows, closes)
369+
upper_limit = 1.5*total_atr
370+
lower_limit = 0.01*total_atr
371+
if brick_size > upper_limit:
372+
raise ValueError("Specified brick_size may not be larger than (1.5* the Average True Value of the dataset) which has value: "+ str(upper_limit))
373+
elif brick_size < lower_limit:
374+
raise ValueError("Specified brick_size may not be smaller than (0.01* the Average True Value of the dataset) which has value: "+ str(lower_limit))
368375

369376
alpha = marketcolors['alpha']
370377

371-
uc = mcolors.to_rgba(marketcolors['candle'][ 'up' ], 1.0)
372-
dc = mcolors.to_rgba(marketcolors['candle']['down'], 1.0)
378+
uc = mcolors.to_rgba(marketcolors['candle'][ 'up' ], alpha)
379+
dc = mcolors.to_rgba(marketcolors['candle']['down'], alpha)
373380
euc = mcolors.to_rgba(marketcolors['edge'][ 'up' ], 1.0)
374381
edc = mcolors.to_rgba(marketcolors['edge']['down'], 1.0)
375382

@@ -390,12 +397,13 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
390397

391398
if num_bricks != 0:
392399
new_dates.extend([dates[i]]*num_bricks)
393-
if volumes[i] is None: # No values passed in for volume and volume=True
394-
volumes[i] = 0
395-
new_volumes.extend([volumes[i] + volume_cache]*num_bricks)
396-
volume_cache = 0
397-
else:
398-
volume_cache += volumes[i]
400+
401+
if volumes is not None: # only adds volumes if there are volume values when volume=True
402+
if num_bricks != 0:
403+
new_volumes.extend([volumes[i] + volume_cache]*num_bricks)
404+
volume_cache = 0
405+
else:
406+
volume_cache += volumes[i]
399407

400408
if cdiff[i] > 0:
401409
bricks.extend([1]*num_bricks)
@@ -424,9 +432,6 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
424432
(x, y+brick_size),
425433
(x+1, y+brick_size),
426434
(x+1, y)))
427-
428-
429-
bricks_df = pd.DataFrame(brick_values) # turn brick_values into a dataframe to be able to call .rolling to calculate mav
430435

431436
useAA = 0, # use tuple here
432437
lw = None
@@ -437,7 +442,7 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
437442
linewidths=lw
438443
)
439444

440-
return (rectCollection, ), new_dates, new_volumes, bricks_df
445+
return (rectCollection, ), new_dates, new_volumes, brick_values
441446

442447
from matplotlib.ticker import Formatter
443448
class IntegerIndexDateTimeFormatter(Formatter):

src/mplfinance/plotting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def plot( data, **kwargs ):
264264
elif ptype == 'line':
265265
ax1.plot(xdates, closes, color=config['linecolor'])
266266
else:
267-
raise ValueError('Unrecognized plot type = "'+ptype+'"')
267+
raise ValueError('Unrecognized plot type = "'+ ptype + '"')
268268

269269
if collections is not None:
270270
for collection in collections:
@@ -284,7 +284,7 @@ def plot( data, **kwargs ):
284284

285285
for mav in mavgs:
286286
if ptype == 'renko':
287-
mavprices = brick_values.rolling(mav).mean().values
287+
mavprices = pd.Series(brick_values).rolling(mav).mean().values
288288
else:
289289
mavprices = data['Close'].rolling(mav).mean().values
290290
if mavc:
@@ -316,7 +316,7 @@ def plot( data, **kwargs ):
316316
used_ax3 = False
317317
used_ax4 = False
318318
addplot = config['addplot']
319-
if addplot is not None:
319+
if addplot is not None and ptype is not 'renko':
320320
# Calculate the Order of Magnitude Range
321321
# If addplot['secondary_y'] == 'auto', then: If the addplot['data']
322322
# is out of the Order of Magnitude Range, then use secondary_y.

0 commit comments

Comments
 (0)