Skip to content

Commit 5526fed

Browse files
checkpoint; preliminary extrapolation working
1 parent 575a7eb commit 5526fed

File tree

4 files changed

+475
-35
lines changed

4 files changed

+475
-35
lines changed

examples/scratch_pad/date_to_iloc_extrapolation.ipynb

Lines changed: 424 additions & 9 deletions
Large diffs are not rendered by default.

src/mplfinance/_arg_validators.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
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
108

119
def _check_and_prepare_data(data, config):
1210
'''
@@ -78,28 +76,6 @@ def _check_and_prepare_data(data, config):
7876

7977
return dates, opens, highs, lows, closes, volumes
8078

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-
10379
def _get_valid_plot_types(plottype=None):
10480

10581
_alias_types = {

src/mplfinance/_utils.py

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ def _check_input(opens, closes, highs, lows):
6262
if not same_missing:
6363
raise ValueError('O,H,L,C must have the same missing data!')
6464

65+
def _check_and_convert_xlim_configuration(data, config):
66+
'''
67+
Check, if user entered `xlim` kwarg, if user entered dates
68+
then we may need to convert them to iloc or matplotlib dates.
69+
'''
70+
if config['xlim'] is None:
71+
return None
72+
73+
xlim = config['xlim']
74+
75+
if not _xlim_validator(xlim):
76+
raise ValueError('Bad xlim configuration #1')
77+
78+
if all([_is_date_like(dt) for dt in xlim]):
79+
if config['show_nontrading']:
80+
xlim = [ _date_to_mdate(dt) for dt in xlim]
81+
else:
82+
xlim = [ _date_to_iloc_extrapolate(data.index.to_series(),dt) for dt in xlim]
83+
84+
return xlim
85+
86+
6587
def _construct_mpf_collections(ptype,dates,xdates,opens,highs,lows,closes,volumes,config,style):
6688
collections = None
6789
if ptype == 'candle' or ptype == 'candlestick':
@@ -194,16 +216,42 @@ def _date_to_iloc(dtseries,date):
194216
if isinstance(loc2,slice): loc2 = loc2.stop - 1
195217
return (loc1+loc2)/2.0
196218

219+
def _date_to_iloc_linear(dtseries,date,trace=False):
220+
'''Find the slope and yintercept for the line:
221+
iloc = (slope)*(date) + (yintercept)
222+
'''
223+
d1 = _date_to_mdate(dtseries.index[0])
224+
d2 = _date_to_mdate(dtseries.index[-1])
225+
226+
if trace: print('d1,d2=',d1,d2)
227+
i1 = 0.0
228+
i2 = len(dtseries) - 1.0
229+
if trace: print('i1,i2=',i1,i2)
230+
231+
slope = (i2 - i1) / (d2 - d1)
232+
yitrcpt1 = i1 - (slope*d1)
233+
if trace: print('slope,yitrcpt=',slope,yitrcpt1)
234+
yitrcpt2 = i2 - (slope*d2)
235+
if trace: print('slope,yitrcpt=',slope,yitrcpt2)
236+
if yitrcpt1 != yitrcpt2:
237+
print('WARNING: yintercepts NOT equal!!!')
238+
return (slope,(yitrcpt1+yitrcpt2)/2.0)
239+
197240
def _date_to_iloc_extrapolate(dtseries,date):
198241
d1s = dtseries.loc[date:]
199242
if len(d1s) < 1:
200243
# xtrapolate forward:
201-
pass
244+
m,b = _date_to_iloc_linear(dtseries,date)
245+
iloc = m*_date_to_mdate(date) + b
246+
return iloc
202247
d1 = d1s.index[0]
203248
d2s = dtseries.loc[:date]
204249
if len(d2s) < 1:
205250
# extrapolate backward:
206-
pass
251+
m,b = _date_to_iloc_linear(dtseries,date)
252+
iloc = m*_date_to_mdate(date) + b
253+
return iloc
254+
# Below here we *interpolate* (not extrapolate)
207255
d2 = dtseries.loc[:date].index[-1]
208256
# If there are duplicate dates in the series, for example in a renko plot
209257
# then .get_loc(date) will return a slice containing all the dups, so:

src/mplfinance/plotting.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from mplfinance._utils import _updown_colors
2727
from mplfinance._utils import IntegerIndexDateTimeFormatter
2828
from mplfinance._utils import _mscatter
29+
from mplfinance._utils import _check_and_convert_xlim_configuration
2930

3031
from mplfinance import _styles
3132

0 commit comments

Comments
 (0)