According to the statistical rules written above, I designed a particularly simple and rude trading strategy, which is essentially chasing the price:
Near the close, randomly select some stocks to buy from the stocks that rose by more than 5% that day, and sell them as long as they make x% profit the next day. Sell at the close if it doesn't rise to x% the next day. And plot the yield curve.
1 # Write a backtest myself to test my rich password 2 # The latest out-of-sample data is used 3 import pandas as pd 4 import random 5 import matplotlib.pyplot as plt 6 import numpy as np 7 #Data needed to read in: open high and close low and gain on the day 8 datapath = r'D:\MyIntern\example\newdata\\' 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, :] # data of the day 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 # # According to statistics, in the past year and a half, the number of stocks that meet the requirements is at least 15 per day. Consider randomly selecting ten stocks to buy at the closing price every day 42 43 # stock picking 44 select_stock_list =  #Summary of stock symbols selected for each day 45 for i in date: 46 curdata = cur_increase.loc[i, ] # data of the day 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 random selections from eligible stocks 51 select_stock_list.append(select_stock) 52 53 # Calculate the rate of return, Assuming equal weights to buy these stocks 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 #up 3%sold when 62 payoff[payoff<0.03] = close_payoff # If it doesn't rise to 3 all day%，sell at close 63 ori = ori * (1+payoff.mean()) #The daily rate of return is the average of each stock’s rate of return 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('time') 71 plt.ylabel('net worth(Initially 100)') 72 plt.show()
I was particularly pleasantly surprised because the yield curve drawn is incredibly beautiful:
I am very excited, I think how easy it is to make a fortune. Later I found out that my code was wrong. According to the code I wrote, a stock that goes up 1% a day can be sold for 5%. . . No wonder the earnings are so good.
When I fixed this strategy my strategy became very unreliable.
And the trend of the broader market during this time period is as follows:
I can't beat the market with this strategy, so frustrating.
After tossing in such a circle, the biggest gain is to learn to use plt to draw a line chart and set the coordinate axis spacing. .
The first data[1::20] is to draw the scale (draw a scale every 20 dates), the next data[1::20] is to set the label, and rotation is the rotation angle of the label, which is obviously rotated by 90 degrees. More tags can be dropped.