• 后续:尝试交易策略


    根据上文写的统计规律,我设计了一个特别简单粗暴的交易策略,本质上是追涨:

    临近收盘时,从当天涨幅5%以上的股票中随机选取一些股票买入,第二天只要盈利x%就卖掉。如果第二天没有涨到x%就在收盘时卖掉。并把收益曲线作图画出来。

    mybacktest.py

     1 # 自己写一个回测检验我的致富密码
     2 # 用的是样本外最新数据
     3 import pandas as pd
     4 import random
     5 import matplotlib.pyplot as plt
     6 import numpy as np
     7 #读入需要数据:高开低收和当天涨幅
     8 datapath = r'D:MyInternexample
    ewdata\'
     9 plt.rcParams['font.sans-serif'] = ['SimHei']
    10 plt.rcParams['axes.unicode_minus'] = False
    11 startdate = 20190102
    12 enddate = 20200630
    13 low = pd.read_pickle(datapath+'low.pkl').loc[:, startdate: enddate].T
    14 high = pd.read_pickle(datapath+'high.pkl').loc[:, startdate: enddate].T
    15 open = pd.read_pickle(datapath+'open.pkl').loc[:, startdate: enddate].T
    16 close = pd.read_pickle(datapath+'close.pkl').loc[:, startdate: enddate].T
    17 
    18 # cur_increase = (close-open)/open
    19 # high_increase = (high-close.shift())/close.shift()
    20 # pd.to_pickle(cur_increase, datapath+'increase.pkl')
    21 # pd.to_pickle(high_increase, datapath+'high_increase.pkl')
    22 # close_increase = (close-close.shift())/close.shift()
    23 # pd.to_pickle(close_increase, datapath+'close_increase.pkl')
    24 # low_decrease = (low-close.shift())/close.shift()
    25 # pd.to_pickle(low_decrease, datapath+'low_decrease.pkl')
    26 
    27 cur_increase = pd.read_pickle(datapath+'increase.pkl')
    28 high_increase = pd.read_pickle(datapath+'high_increase.pkl')
    29 close_increase = pd.read_pickle(datapath+'close_increase.pkl')
    30 low_decrease = pd.read_pickle(datapath+'low_decrease.pkl')
    31 date = open.index
    32 
    33 # limit_up_stocks = []
    34 # for i in date:
    35 #     curdata = cur_increase.loc[i, :] # 当天数据
    36 #     condition1 = curdata >= 0.05
    37 #     condition2 = curdata <=0.092
    38 #     curdata = curdata[condition1 & condition2]
    39 #     limit_up_stocks.append(len(curdata))
    40 # print(pd.Series(limit_up_stocks).min())
    41 # # 经过统计,过去的一年半时间里符合要求的股票每天最少为15支 考虑每天随机选取十只股票以收盘价买入
    42 
    43 # 选股
    44 select_stock_list = [] #每一日选出的股票代码汇总
    45 for i in date:
    46     curdata = cur_increase.loc[i, ] # 当天数据
    47     condition1 = curdata >= 0.05
    48     condition2 = curdata <=0.092
    49     curdata = curdata[condition1 & condition2]
    50     select_stock = random.sample(list(curdata.index),len(list(curdata.index))) #从符合要求的股票中随机挑选10支
    51     select_stock_list.append(select_stock)
    52 
    53 # 计算收益率, 假设等权重买入这些股票
    54 count = 0
    55 ori = 100
    56 earnings = []
    57 for i in date[1:]:
    58     select_stock = select_stock_list[count]
    59     payoff = high_increase[select_stock].loc[i, :]
    60     close_payoff = close_increase[select_stock].loc[i, :]
    61     payoff[payoff>0.03] = 0.03 #在涨了3%的时候卖掉
    62     payoff[payoff<0.03] = close_payoff # 如果全天都没涨到3%,就收盘时卖掉
    63     ori = ori * (1+payoff.mean()) #当天的收益率就是每只股票收益率的均值
    64     earnings.append(ori)
    65     count = count + 1
    66 
    67 date = [str(i) for i in date]
    68 plt.plot(date[1:], earnings)
    69 plt.xticks(date[1::20],(date[1::20]),rotation=60)
    70 plt.xlabel('时间')
    71 plt.ylabel('净值(初始为100)')
    72 plt.show()

    我本来特别惊喜,因为画出来的收益曲线难以置信的美好:

     我特别激动,觉得发财怎么这么简单。后来发现我代码写错了。按照我写的代码,每天上涨了1%的股票可以用5%卖出去。。。怪不得收益那么好。

    当我修正了这个策略以后我的策略就变得十分不靠谱了。

     而这个时间段里大盘的走势是这样的:

     我这个策略都跑不赢大盘,太沮丧了。

    折腾这么一圈,最大的收获是学会了用plt画折线统计图并且设置坐标轴间距。。

    1  plt.xticks(date[1::20],(date[1::20]),rotation=60)

    第一个data[1::20]是画刻度(每隔20个日期画一个刻度),后面一个data[1::20]是设置标签,rotation则是标签的的旋转角度,旋转90度显然可以放下更多的标签。

  • 相关阅读:
    flutter 屏幕宽高 状态栏高度
    flutter 图片圆角
    flutter ListView嵌套高度问题
    Dubbo原码解析(version:2.5.3)
    ms
    InnoDB锁问题 & DB事务隔离级别
    Spring父容器与子容器
    Spring bean 的加载过程和生命周期
    logback
    Disconf (version : 2.6.21)
  • 原文地址:https://www.cnblogs.com/fangziyuan/p/13496031.html
Copyright © 2020-2023  润新知