4
4
"""
5
5
6
6
import numpy as np
7
- import pandas as pd
8
7
import matplotlib .dates as mdates
9
8
import datetime
10
9
@@ -90,9 +89,9 @@ def _calculate_atr(atr_length, highs, lows, closes):
90
89
all_lows : list of lows
91
90
all_closes : list of closes
92
91
"""
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 ):
96
95
raise ValueError ("Specified atr_length is larger than the length of the dataset: " + str (len (closes )))
97
96
atr = 0
98
97
for i in range (len (highs )- atr_length , len (highs )):
@@ -150,8 +149,8 @@ def _valid_renko_kwargs():
150
149
kwarg value is one of the allowed values.
151
150
'''
152
151
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' },
155
154
'atr_length' : { 'Default' : 14 ,
156
155
'Validator' : lambda value : isinstance (value ,int ) },
157
156
}
@@ -365,11 +364,19 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
365
364
366
365
if brick_size == 'atr' :
367
366
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 ))
368
375
369
376
alpha = marketcolors ['alpha' ]
370
377
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 )
373
380
euc = mcolors .to_rgba (marketcolors ['edge' ][ 'up' ], 1.0 )
374
381
edc = mcolors .to_rgba (marketcolors ['edge' ]['down' ], 1.0 )
375
382
@@ -390,12 +397,13 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
390
397
391
398
if num_bricks != 0 :
392
399
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 ]
399
407
400
408
if cdiff [i ] > 0 :
401
409
bricks .extend ([1 ]* num_bricks )
@@ -424,9 +432,6 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
424
432
(x , y + brick_size ),
425
433
(x + 1 , y + brick_size ),
426
434
(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
430
435
431
436
useAA = 0 , # use tuple here
432
437
lw = None
@@ -437,7 +442,7 @@ def _construct_renko_collections(dates, highs, lows, volumes, config_renko_param
437
442
linewidths = lw
438
443
)
439
444
440
- return (rectCollection , ), new_dates , new_volumes , bricks_df
445
+ return (rectCollection , ), new_dates , new_volumes , brick_values
441
446
442
447
from matplotlib .ticker import Formatter
443
448
class IntegerIndexDateTimeFormatter (Formatter ):
0 commit comments