计算最高权益曲线
#最高权益曲线
df['max_equity'] = df['equity'].expanding().max()
1元波动图
#1元波动图
df['equity'] = (df['rate'] +1).cumprod()
#max_equity 为有史以来的最大值序列
df['max_equity'] = df['equity'].expanding().max()
mdd = round((df['equity'] / df['equity'].expanding(min_periods=1).max()).min() - 1, 3)
print(mdd)
df[['equity','max_equity']].plot()
最大回撤率
#最大回撤率
mdd = round((df['equity'] / df['equity'].expanding(min_periods=1).max()).min() - 1, 3)
print(mdd)
简单pandas策略
#pandas回测双均线策略
df['fast_sma'] = df['close'].rolling(50).mean()
df['slow_sma'] = df['close'].rolling(200).mean()
df['diff_sma'] = df['fast_sma'] - df['slow_sma']
df['signal'] = np.where(df['diff_sma']>0,1,0)
df['signal'] = np.where(df['diff_sma']<0,-1,df['signal'])
df["returns"] = np.log(df["close"] / df["close"].shift(1))
df["strategy"] = df["signal"].shift(1) * df["returns"]
df[[“returns”, “strategy”]].cumsum().plot()
pandas海龟策略
df['n1_high'] = df['close'].rolling(42).max().shift(1)
df['n2_low'] = df['close'].rolling(21).min().shift(1)
df[['close','n1_high','n2_low']].plot()
计算交易信号
df['signal'] = np.where(df['n1_high']>df['close'],1,0)
df['signal'] = np.where(df['close']<df['n2_low'],-1,df['signal'])