python 枚举法选择最优策略参数

枚举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()

输出结果
在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值