@@ -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+
6587def _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+
197240def _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:
0 commit comments