44"""
55
66import numpy as np
7- import pandas as pd
87import matplotlib .dates as mdates
98import 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
442447from matplotlib .ticker import Formatter
443448class IntegerIndexDateTimeFormatter (Formatter ):
0 commit comments