Skip to main content

模拟买卖信号


def caculate_max_drawdown(data, window=252):
"""
计算最大回撤比
:param data:
:param window: int, 时间窗口设置,默认为252(日k)
:return:
"""
# 模拟持仓金额:投入的总金额 *(1+收益率)
data['close'] = 10000 * (1 + data['cum_profit'])
# 选取时间周期中的最大净值
data['roll_max'] = data['close'].rolling(window=window, min_periods=1).max()
# 计算当天的回撤比 = (谷值 — 峰值)/峰值 = 谷值/峰值 - 1
data['daily_dd'] = data['close'] / data['roll_max'] - 1
# 选取时间周期内最大的回撤比,即最大回撤
data['max_dd'] = data['daily_dd'].rolling(window, min_periods=1).min()

return data

def compose_signal(data):
"""
整合信号
:param data:
:return:
"""
data['buy_signal'] = np.where((data['buy_signal'] == 1)
& (data['buy_signal'].shift(1) == 1), 0, data['buy_signal'])
data['sell_signal'] = np.where((data['sell_signal'] == -1)
& (data['sell_signal'].shift(1) == -1), 0, data['sell_signal'])
data['signal'] = data['buy_signal'] + data['sell_signal']
return data



def calculate_prof_pct(data):
"""
计算单次收益率:开仓、平仓(开仓的全部股数)
:param data:
:return:
"""
# 筛选信号不为0的,并且计算涨跌幅
data.loc[data['signal'] != 0, 'profit_pct'] = data['close'].pct_change()
data = data[data['signal'] == -1] # 筛选平仓后的数据:单次收益
return data


def week_period_strategy(code, time_freq, start_date, end_date):
"""
周期选股(周四买,周一卖)
:param code:
:param time_freq:
:param start_date:
:param end_date:
:return:
"""
data = st.get_single_price(code, time_freq, start_date, end_date)
# 新建周期字段
data['weekday'] = data.index.weekday
# 周四买入
data['buy_signal'] = np.where((data['weekday'] == 3), 1, 0)
# 周一卖出
data['sell_signal'] = np.where((data['weekday'] == 0), -1, 0)

data = base.compose_signal(data) # 整合信号
data = base.calculate_prof_pct(data) # 计算收益
# data = base.calculate_cum_prof(data) # 计算累计收益率
# data = base.caculate_max_drawdown(data) # 最大回撤
return data


if __name__ == '__main__':
df = week_period_strategy('000001.XSHE', 'daily', None, datetime.date.today())
print(df[['close', 'signal', 'profit_pct']])
print(df[['close', 'signal', 'profit_pct']].describe())
# df['profit_pct'].plot()
# plt.hist(df['profit_pct'], bins=30)
# plt.show()

# 查看平安银行最大回撤
# df = st.get_single_price('000001.XSHE', 'daily', '2006-01-01', '2021-01-01')
# df = caculate_max_drawdown(df)
# print(df[['close', 'roll_max', 'daily_dd', 'max_dd']])
# df[['daily_dd', 'max_dd']].plot()
# plt.show()

# 计算夏普比率
# df = st.get_single_price('000001.XSHE', 'daily', '2006-01-01', '2021-01-01')
# sharpe = calculate_sharpe(df)
# print(sharpe)