版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_44580977/article/details/102477238
枚举20~60作为移动平均天数参数,选出金额最高的做为参数
import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas_datareader as web
class QuantAverBreak:
def __init__(self):
self.skip_days = 0 # 持股/持币状态
self.cash_hold = 100000 # 初始资金
self.posit_num = 0 # 持股数目
self.market_total = 0 # 持股市值
def run_factor_plot(self, stock_df, N):
stock_df['Ma_n'] = stock_df.Close.rolling(window=N).mean() # 增加N移动平均线
list_diff = np.sign(stock_df.Close - stock_df.Ma_n)
# 当符号为负时为收盘价向下跌破Ma_n,当符号为正时收盘价向上突破Ma_n
stock_df['signal'] = np.sign(list_diff - list_diff.shift(1))
for kl_index, today in stock_df.iterrows():
# 买入/卖出执行代码
if today.signal > 0 and self.skip_days == 0: # 买入
start = stock_df.index.get_loc(kl_index)
self.skip_days = -1
self.posit_num = int(self.cash_hold / today.Close)
self.cash_hold = 0
elif today.signal < 0 and self.skip_days == -1: # 卖出 避免未买先卖
end = stock_df.index.get_loc(kl_index)
self.skip_days = 0
self.cash_hold = int(self.posit_num * today.Close)
self.market_total = 0
if self.skip_days == -1: # 持股
self.market_total = int(self.posit_num * today.Close)
stock_df.loc[kl_index, 'total'] = self.market_total
else: # 空仓
stock_df.loc[kl_index, 'total'] = self.cash_hold
return stock_df['total'][-1]
stock = web.DataReader("600410.SS", "yahoo", datetime.datetime(2018, 1, 1), datetime.datetime(2019, 1, 1))
ma_list = []
profit_list = []
for ma in range(20, 60):
examp_trade = QuantAverBreak()
ma_list.append(ma)
profit_list.append(examp_trade.run_factor_plot(stock, ma))
profit_max=max(profit_list)
print(profit_list.index(max(profit_list)))
ma_max=ma_list[profit_list.index(max(profit_list))]
plt.bar(ma_list, profit_list)
plt.annotate('ma='+str(ma_max)+'\n'+str(profit_max),\
xy=(ma_max,profit_max),xytext=(ma_max-5, profit_max-10),arrowprops=dict(facecolor='yellow',shrink=0.1),\
horizontalalignment='left',verticalalignment='top')
# 设置坐标标签字体大小
plt.xlabel('均线参数')
plt.ylabel('资金收益')
# 设置坐标轴的取值范围
plt.xlim(min(ma_list)-1, max(ma_list)+1)
plt.ylim(min(profit_list)*0.99, max(profit_list)*1.01)
# 设置x坐标轴刻度
plt.xticks(np.arange(min(ma_list), max(ma_list)+1, 1))
# 设置图例字体大小
plt.legend(['profit_list'], loc='best')
plt.title("均线最优参数")
plt.show()
输出结果