def mfe_mae(h, days=200, period=5):
"""
ln(1+mfe) + ln(1-mae)
:return:
"""
# ?? buy ???????? 1 ~ 200 ?? MAE ? MFE
days_list = list(range(0, 200, period))
h = h.copy()
h["ATR"] = tl.ATR(*np.asarray(h[["high", "low", "close"]].T))
e_ratios = []
# ????
size = h.shape[0]
counts = h.buy.value_counts()
buy_times = counts[True]
# ??
buy_index = np.where(h.buy == True)[0]
# ?? E1 ~ E200, E1 ??????
for e_days in days_list:
# ???? e_days=5 ?? MFE ? MAE
mfe = [0 for _ in range(size)]
mae = [0 for _ in range(size)]
for d in buy_index:
# MFE d : d + edays ?????????
if d == size-1:
# ???????
buy_times = buy_times - 1
break
cost = h.cost[d]
atr = h.ATR[d]
# ??????????? ?50%????200%???????150%??
high = h.high[d:d + e_days + 1].max()
mfe[d] = math.log(high/cost) # ???
# mfe[d] = high/cost/atr # ???
low = h.low[d:d + e_days + 1].min()
mae[d] = math.log(low/cost) # ???
# mae[d] = low/cost/atr # ???
# ? MFE ? MAE ??????
e = (sum(mfe) + sum(mae)) / buy_times # ???
# e = (sum(mfe) / buy_times) / (sum(mae) / buy_times) # ???
e_ratios.append(e)
# ?? DataFrame
return pd.DataFrame({"E-ratio":e_ratios}, index=days_list), h
评论列表
文章目录