Skip to content

Commit 50d5d7e

Browse files
committed
add valid_renko_kwargs and call process_kwargs
1 parent 73a1c05 commit 50d5d7e

File tree

3 files changed

+65
-59
lines changed

3 files changed

+65
-59
lines changed

src/mplfinance/_arg_validators.py

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -55,48 +55,7 @@ def _mav_validator(mav_value):
5555
if not isinstance(num,int) and num > 1:
5656
return False
5757
return True
58-
59-
def _renko_params_validator(params_dict):
60-
'''
61-
Ranko params may not have > 3 parameters, their values may be:
62-
type: must be either 'pmove' or 'pclose'
63-
brick_size: must be either an int/float greater than 0 or 'atr'
64-
atr_length: must only exist if brick_size is 'atr', must be an int greater than 0
65-
'''
66-
if len(params_dict) > 3:
67-
return False
68-
69-
# Validate type
70-
if 'type' in params_dict:
71-
if params_dict['type'] not in ['pmove', 'pclose']:
72-
return False
73-
else:
74-
params_dict['type'] = 'pmove' # Set default value of type to price movement
75-
76-
# Validate brick_size and atr_length
77-
if 'brick_size' in params_dict:
78-
brick_size = params_dict['brick_size']
79-
if not isinstance(brick_size, int) and not isinstance(brick_size, float):
80-
if brick_size != 'atr':
81-
return False
82-
else:
83-
if 'atr_length' not in params_dict:
84-
params_dict['atr_length'] = 14 # Set default value of atr_length to 14
85-
else:
86-
if not isinstance(params_dict['atr_length'], int) or params_dict['atr_length'] < 1:
87-
return False
88-
else:
89-
if brick_size < 0 or 'atr_length' in params_dict:
90-
return False
91-
else:
92-
params_dict['brick_size'] = 'atr'
93-
params_dict['atr_length'] = 14
94-
95-
return True
96-
97-
98-
99-
58+
10059

10160
def _bypass_kwarg_validation(value):
10261
''' For some kwargs, we either don't know enough, or
@@ -142,24 +101,24 @@ def _process_kwargs(kwargs, vkwargs):
142101
# now validate kwargs, and for any valid kwargs
143102
# replace the appropriate value in config:
144103
for key in kwargs.keys():
145-
if key not in vkwargs:
146-
raise KeyError('Unrecognized kwarg="'+str(key)+'"')
147-
else:
148-
value = kwargs[key]
149-
try:
150-
valid = vkwargs[key]['Validator'](value)
151-
except Exception as ex:
152-
raise ValueError('kwarg "'+key+'" validator raised exception to value: "'+str(value)+'"') from ex
153-
if not valid:
154-
import inspect
155-
v = inspect.getsource(vkwargs[key]['Validator']).strip()
156-
raise ValueError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n '+v)
104+
if key not in vkwargs:
105+
raise KeyError('Unrecognized kwarg="'+str(key)+'"')
106+
else:
107+
value = kwargs[key]
108+
try:
109+
valid = vkwargs[key]['Validator'](value)
110+
except Exception as ex:
111+
raise ValueError('kwarg "'+key+'" validator raised exception to value: "'+str(value)+'"') from ex
112+
if not valid:
113+
import inspect
114+
v = inspect.getsource(vkwargs[key]['Validator']).strip()
115+
raise ValueError('kwarg "'+key+'" validator returned False for value: "'+str(value)+'"\n '+v)
157116

158117
# ---------------------------------------------------------------
159118
# At this point in the loop, if we have not raised an exception,
160119
# then kwarg is valid as far as we can tell, therefore,
161120
# go ahead and replace the appropriate value in config:
162121

163-
config[key] = value
122+
config[key] = value
164123

165124
return config

src/mplfinance/_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,26 @@ def _construct_candlestick_collections(dates, opens, highs, lows, closes, market
262262

263263
return rangeCollection, barCollection
264264

265+
def _construct_renko_collections(dates, renko_params, closes, marketcolors=None):
266+
"""Represent the price change with bricks
267+
268+
Parameters
269+
----------
270+
renko_params : dictionary
271+
type : type of renko chart
272+
brick_size : size of each brick
273+
atr_legnth : length of time used for calculating atr
274+
closes : sequence
275+
sequence of closing values
276+
marketcolors : dict of colors: up, down, edge, wick, alpha
277+
278+
Returns
279+
-------
280+
ret : tuple
281+
(lineCollection, barCollection)
282+
"""
283+
return True
284+
265285
from matplotlib.ticker import Formatter
266286
class IntegerIndexDateTimeFormatter(Formatter):
267287
"""

src/mplfinance/plotting.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
from mplfinance import _styles
2323

24-
from mplfinance._arg_validators import _check_and_prepare_data, _mav_validator, _renko_params_validator
24+
from mplfinance._arg_validators import _check_and_prepare_data, _mav_validator
2525
from mplfinance._arg_validators import _process_kwargs, _validate_vkwargs_dict
2626
from mplfinance._arg_validators import _kwarg_not_implemented, _bypass_kwarg_validation
2727

@@ -75,8 +75,8 @@ def _valid_plot_kwargs():
7575
'mav' : { 'Default' : None,
7676
'Validator' : _mav_validator },
7777

78-
'renko_params': { 'Default' : dict(type='pmove', brick_size='atr', atr_length=14),
79-
'Validator' : _renko_params_validator },
78+
'renko_params': { 'Default' : dict(),
79+
'Validator' : lambda value: isinstance(value,dict) },
8080

8181
'study' : { 'Default' : None,
8282
#'Validator' : lambda value: isinstance(value,dict) }, #{'studyname': {study parms}} example: {'TE':{'mav':20,'upper':2,'lower':2}}
@@ -129,7 +129,33 @@ def _valid_plot_kwargs():
129129
_validate_vkwargs_dict(vkwargs)
130130

131131
return vkwargs
132-
132+
133+
134+
def _valid_renko_kwargs():
135+
'''
136+
Construct and return the "valid renko kwargs table" for the mplfinance.plot(type='renko') function.
137+
A valid kwargs table is a `dict` of `dict`s. The keys of the outer dict are the
138+
valid key-words for the function. The value for each key is a dict containing
139+
2 specific keys: "Default", and "Validator" with the following values:
140+
"Default" - The default value for the kwarg if none is specified.
141+
"Validator" - A function that takes the caller specified value for the kwarg,
142+
and validates that it is the correct type, and (for kwargs with
143+
a limited set of allowed values) may also validate that the
144+
kwarg value is one of the allowed values.
145+
'''
146+
vkwargs = {
147+
'type' : { 'Default' : 'pmove',
148+
'Validator' : lambda value: value in ['pmove','pclose'] },
149+
'brick_size' : { 'Default' : 2.0,
150+
'Validator' : lambda value: isinstance(value,float) or isinstance(value,int) or value == 'atr' },
151+
'atr_length' : { 'Default' : 14,
152+
'Validator' : lambda value: isinstance(value,int) },
153+
}
154+
155+
_validate_vkwargs_dict(vkwargs)
156+
157+
return vkwargs
158+
133159

134160
def rcParams_to_df(rcp,name=None):
135161
keys = []
@@ -253,6 +279,7 @@ def plot( data, **kwargs ):
253279
collections = _construct_ohlc_collections(xdates, opens, highs, lows, closes,
254280
marketcolors=style['marketcolors'] )
255281
elif ptype == 'renko':
282+
renko_params = _process_kwargs(kwargs['renko_params'], _valid_renko_kwargs())
256283
collections = _construct_renko_collections(xdates, renko_params, closes,
257284
marketcolors=style['marketcolors'] )
258285
elif ptype == 'line':

0 commit comments

Comments
 (0)