Skip to content

Commit 575a7eb

Browse files
checkpoint: partial dev
1 parent e73482d commit 575a7eb

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/mplfinance/_arg_validators.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from mplfinance._helpers import _list_of_dict
66
import matplotlib as mpl
77
import warnings
8+
from mplfinance._utils import _date_to_mdate
9+
from mplfinance._utils import _date_to_iloc_extrapolate
810

911
def _check_and_prepare_data(data, config):
1012
'''
@@ -76,6 +78,28 @@ def _check_and_prepare_data(data, config):
7678

7779
return dates, opens, highs, lows, closes, volumes
7880

81+
def _check_and_convert_xlim_configuration(data, config):
82+
'''
83+
Check, if user entered `xlim` kwarg, if user entered dates
84+
then we may need to convert them to iloc or matplotlib dates.
85+
'''
86+
if config['xlim'] is None:
87+
return None
88+
89+
xlim = config['xlim']
90+
91+
if not _xlim_validator(xlim):
92+
raise ValueError('Bad xlim configuration #1')
93+
94+
if all([_is_date_like(dt) for dt in xlim]):
95+
if config['show_nontrading']:
96+
xlim = [ _date_to_mdate(dt) for dt in xlim]
97+
else
98+
xlim = [ _date_to_iloc_extrapolate(data.index.to_series(),dt) for dt in xlim]
99+
100+
return xlim
101+
102+
79103
def _get_valid_plot_types(plottype=None):
80104

81105
_alias_types = {
@@ -140,6 +164,11 @@ def _is_datelike(value):
140164
return False
141165
return False
142166

167+
def _xlim_validator(value):
168+
return (isinstance(value, (list,tuple)) and len(value) == 2
169+
and (all([isinstance(v,(int,float)) for v in value])
170+
or all([_is_date_like(v) for v in value])))
171+
143172
def _vlines_validator(value):
144173
'''Validate `vlines` kwarg value: must be "datelike" or sequence of "datelike"
145174
'''

src/mplfinance/_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ def _date_to_iloc(dtseries,date):
194194
if isinstance(loc2,slice): loc2 = loc2.stop - 1
195195
return (loc1+loc2)/2.0
196196

197+
def _date_to_iloc_extrapolate(dtseries,date):
198+
d1s = dtseries.loc[date:]
199+
if len(d1s) < 1:
200+
# xtrapolate forward:
201+
pass
202+
d1 = d1s.index[0]
203+
d2s = dtseries.loc[:date]
204+
if len(d2s) < 1:
205+
# extrapolate backward:
206+
pass
207+
d2 = dtseries.loc[:date].index[-1]
208+
# If there are duplicate dates in the series, for example in a renko plot
209+
# then .get_loc(date) will return a slice containing all the dups, so:
210+
loc1 = dtseries.index.get_loc(d1)
211+
if isinstance(loc1,slice): loc1 = loc1.start
212+
loc2 = dtseries.index.get_loc(d2)
213+
if isinstance(loc2,slice): loc2 = loc2.stop - 1
214+
return (loc1+loc2)/2.0
215+
216+
197217
def _date_to_mdate(date):
198218
if isinstance(date,str):
199219
pydt = pd.to_datetime(date).to_pydatetime()

src/mplfinance/plotting.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
from mplfinance._arg_validators import _alines_validator, _tlines_validator
3838
from mplfinance._arg_validators import _scale_padding_validator, _yscale_validator
3939
from mplfinance._arg_validators import _valid_panel_id, _check_for_external_axes
40+
from mplfinance._arg_validators import _xlim_validator
4041

4142
from mplfinance._panels import _build_panels
4243
from mplfinance._panels import _set_ticks_on_bottom_panel_only
@@ -185,8 +186,7 @@ def _valid_plot_kwargs():
185186
and all([isinstance(v,(int,float)) for v in value])},
186187

187188
'xlim' : {'Default' : None,
188-
'Validator' : lambda value: isinstance(value, (list,tuple)) and len(value) == 2
189-
and all([isinstance(v,(int,float)) for v in value])},
189+
'Validator' : lambda value: _xlim_validator(value) },
190190

191191
'set_ylim_panelB' : {'Default' : None,
192192
'Validator' : lambda value: _warn_set_ylim_deprecated(value) },
@@ -295,6 +295,8 @@ def plot( data, **kwargs ):
295295

296296
dates,opens,highs,lows,closes,volumes = _check_and_prepare_data(data, config)
297297

298+
config['xlim'] = _check_and_convert_xlim_configuration(data, config)
299+
298300
if config['type'] in VALID_PMOVE_TYPES and config['addplot'] is not None:
299301
err = "`addplot` is not supported for `type='" + config['type'] +"'`"
300302
raise ValueError(err)
@@ -363,7 +365,7 @@ def plot( data, **kwargs ):
363365
panels = _build_panels(fig, config)
364366
volumeAxes = panels.at[config['volume_panel'],'axes'][0] if config['volume'] is True else None
365367

366-
fmtstring = _determine_format_string( dates, config['datetime_format'] )
368+
fmtstring = _determine_format_string(dates, config['datetime_format'])
367369

368370
ptype = config['type']
369371

0 commit comments

Comments
 (0)