The previous post explained how to implement Martin's strategy, but didn't explore its ability to generalize, so this time around let's try backtesting 3,000 stocks to see the profitability ratio.
Batch Crawl Stock Data
Here to crawl the data continue to use tushare, according to the stock code to traverse, because crawl data need some time, may wish to use multi-threaded to crawl, here to pay attention to tushare regulations can not be crawled more than 500 times per minute, unless you have a lot of points, so the number of threads should be appropriately adjusted to lower.
First we generate the codes of all the stocks in SSE and SZSE:
# SSE code shanghaicode = [] for i in range(600000, 604000, 1): (str(i)) #SFC code shenzhencode = [] for i in range(1000000, 1005000, 1): i = str(i)[1:] (i)
Then a crawl function is defined and broker is the instance created in the previous post:
def getalldata(code): if (datapath + code + '.csv'): print(code + 'already existed!') return metadata = broker.get_stock_pro(code) if len(metadata) == 0: return metadata.to_csv('C:/Users/abc/Desktop/' + code + '.csv',index = False) print(code + 'finished!')
Importing modules needed for multithreading
from import ThreadPoolExecutor #multi-threaded
Iterate through all the code to start crawling, max_workers can be adjusted appropriately
executor = ThreadPoolExecutor(max_workers=3) for datatemp in (getalldata, shenzhencode): pass executor = ThreadPoolExecutor(max_workers=3) for datatemp in (getalldata, shanghaicode): pass
Batch Backtesting Stocks
After the data crawl can start back to test, because back to test is the CPU bottleneck computing, so here we do not use multi-threaded, the speed is about the same.
First, the backtesting program for a stock is encapsulated into a function, the backtesting period is set to the whole year of 2020, and the starting capital is set to $200,000 dollars:
def martinmulti(code): broker = backtesting(200000,'20200101', '20201231') # Get stock data metadata = pd.read_csv(datapath + code) data = (metadata['close']) exdata = (metadata['pre_close']) everyChange = (metadata['change']) date = metadata['trade_date'].values everyChange = everyChange/data # Start backtesting (data, exdata, everyChange, date) dicttemp = {'Stock Code': code,'Termination of cash': } return dicttemp
Iterate through the stock code backtest and record the termination cash
cashlist = (columns= ['Stock Code','Termination of cash']) for code in datalist: datatemp = martinmulti(code) cashlist = (datatemp,ignore_index=True)
The backtesting process is as follows
Next look at which stocks made the biggest profits:
Look at the average.
() Out[12]: Termination cash 208279.115166
You can see from the mean that Martin Strategy earns as a relatively safe method to earn not much, of course it is impossible to find a once-and-for-all method, and with the average can not represent everything, so look at how the percentage of profit:
['-serif'] = ['SimHei'] ('ggplot') ("Profit distribution(ten thousand dollars)") bins = [] for i in range(10000, 600000, 10000): (i) (cashlist['Termination of cash'],bins = bins) (x = ().values,ls="-",c="green")#Adding vertical lines
You can see that there are folds and doubles, and the vast majority of them are concentrated next to the $20w, the distribution graph is shifted towards the right side of the $200,000 as a whole, and the strategy still needs to be improved.
To this point this article about python Martin strategy backtesting 3000 stocks on this article, more related python stock strategy backtesting content please search my previous articles or continue to browse the following related articles I hope that you will support me more in the future!