def find_max_min(prices):
prices_ = prices.copy()
prices_.index = linspace(1., len(prices_), len(prices_))
#kr = KernelReg([prices_.values], [prices_.index.values], var_type='c', bw=[1.8, 1])
kr = KernelReg([prices_.values], [prices_.index.values], var_type='c', bw=[2]) # ????????????? ?
# Either a user-specified bandwidth or the method for bandwidth selection.
# If a string, valid values are ‘cv_ls’ (least-squares cross-validation) and ‘aic’ (AIC Hurvich bandwidth estimation).
# Default is ‘cv_ls’.
f = kr.fit([prices_.index.values])
smooth_prices = pd.Series(data=f[0], index=prices.index)
local_max = argrelextrema(smooth_prices.values, np.greater)[0]
local_min = argrelextrema(smooth_prices.values, np.less)[0]
price_local_max_dt = []
for i in local_max:
if (i > 1) and (i < len(prices) - 1):
price_local_max_dt.append(prices.iloc[i - 2:i + 2].argmax())
price_local_min_dt = []
for i in local_min:
if (i > 1) and (i < len(prices) - 1):
price_local_min_dt.append(prices.iloc[i - 2:i + 2].argmin())
prices.name = 'price'
maxima = pd.DataFrame(prices.loc[price_local_max_dt])
minima = pd.DataFrame(prices.loc[price_local_min_dt])
max_min = pd.concat([maxima, minima]).sort_index()
max_min.index.name = 'date'
max_min = max_min.reset_index()
max_min = max_min[~max_min.date.duplicated()]
p = prices.reset_index()
max_min['day_num'] = p[p['index'].isin(max_min.date)].index.values
max_min = max_min.set_index('day_num').price
return max_min
评论列表
文章目录