Skip to content

Commit

Permalink
Refactoring code for full backtest.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Kaplan committed Feb 8, 2023
1 parent dec4964 commit e544f1f
Show file tree
Hide file tree
Showing 496 changed files with 1,688 additions and 25 deletions.
225 changes: 202 additions & 23 deletions pairs_trading_backtest.py

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions read_market_data/MarketData.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from datetime import datetime, timedelta
from utils.convert_date import convert_date
from multiprocessing import Pool
from multiprocessing import Pool, cpu_count


class MarketData:
Expand Down Expand Up @@ -100,9 +100,10 @@ def read_data(self, symbol: str) -> pd.DataFrame:

def get_close_data(self, stock_list: list) -> pd.DataFrame:
# fetch the close data in parallel
n_cores = cpu_count()
close_df = pd.DataFrame()
assert len(stock_list) > 0
with Pool() as mp_pool:
with Pool(processes=(n_cores*2)) as mp_pool:
close_list = mp_pool.map(self.read_data, stock_list)
# close_list = list()
# for sym in stock_list:
Expand Down
3 changes: 3 additions & 0 deletions s_and_p_data/A.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,152.08
2023-02-01,155.45
2023-02-02,155.5
2023-02-03,154.55
2023-02-06,152.24
2023-02-07,154.51
3 changes: 3 additions & 0 deletions s_and_p_data/AAL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,16.14
2023-02-01,16.69
2023-02-02,17.06
2023-02-03,17.02
2023-02-06,16.92
2023-02-07,16.96
3 changes: 3 additions & 0 deletions s_and_p_data/AAP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,152.28
2023-02-01,156.84
2023-02-02,155.24
2023-02-03,152.17
2023-02-06,151.67
2023-02-07,151.47
3 changes: 3 additions & 0 deletions s_and_p_data/AAPL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,144.29
2023-02-01,145.43
2023-02-02,150.82
2023-02-03,154.5
2023-02-06,151.73
2023-02-07,154.65
3 changes: 3 additions & 0 deletions s_and_p_data/ABBV.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2539,3 +2539,6 @@ Date,Close
2023-01-31,147.75
2023-02-01,146.6
2023-02-02,144.84
2023-02-03,145.2
2023-02-06,145.02
2023-02-07,145.14
3 changes: 3 additions & 0 deletions s_and_p_data/ABC.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,168.96
2023-02-01,161.05
2023-02-02,158.47
2023-02-03,156.73
2023-02-06,154.49
2023-02-07,156.79
3 changes: 3 additions & 0 deletions s_and_p_data/ABT.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,110.55
2023-02-01,111.82
2023-02-02,112.25
2023-02-03,111.79
2023-02-06,110.75
2023-02-07,111.99
3 changes: 3 additions & 0 deletions s_and_p_data/ACN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,279.05
2023-02-01,283.6
2023-02-02,294.1
2023-02-03,289.22
2023-02-06,284.48
2023-02-07,287.82
3 changes: 3 additions & 0 deletions s_and_p_data/ADBE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,370.34
2023-02-01,383.92
2023-02-02,392.23
2023-02-03,379.33
2023-02-06,375.23
2023-02-07,383.82
3 changes: 3 additions & 0 deletions s_and_p_data/ADI.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,171.47
2023-02-01,176.59
2023-02-02,179.3
2023-02-03,178.82
2023-02-06,177.55
2023-02-07,181.02
3 changes: 3 additions & 0 deletions s_and_p_data/ADM.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,82.85
2023-02-01,83.37
2023-02-02,82.31
2023-02-03,82.28
2023-02-06,82.56
2023-02-07,82.66
3 changes: 3 additions & 0 deletions s_and_p_data/ADP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,225.81
2023-02-01,226.73
2023-02-02,228.99
2023-02-03,225.31
2023-02-06,223.47
2023-02-07,226.77
3 changes: 3 additions & 0 deletions s_and_p_data/ADSK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,215.16
2023-02-01,222.19
2023-02-02,228.22
2023-02-03,223.66
2023-02-06,218.34
2023-02-07,223.65
3 changes: 3 additions & 0 deletions s_and_p_data/AEE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,86.87
2023-02-01,87.29
2023-02-02,86.73
2023-02-03,85.3
2023-02-06,85.9
2023-02-07,85.99
3 changes: 3 additions & 0 deletions s_and_p_data/AEP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,93.96
2023-02-01,94.65
2023-02-02,94.55
2023-02-03,92.11
2023-02-06,92.5
2023-02-07,92.28
3 changes: 3 additions & 0 deletions s_and_p_data/AES.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,27.41
2023-02-01,26.8
2023-02-02,26.52
2023-02-03,26.24
2023-02-06,26.21
2023-02-07,26.42
3 changes: 3 additions & 0 deletions s_and_p_data/AFL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,73.5
2023-02-01,73.28
2023-02-02,68.9
2023-02-03,69.39
2023-02-06,69.58
2023-02-07,69.95
3 changes: 3 additions & 0 deletions s_and_p_data/AIG.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,63.22
2023-02-01,61.23
2023-02-02,59.03
2023-02-03,58.75
2023-02-06,59.96
2023-02-07,60.61
3 changes: 3 additions & 0 deletions s_and_p_data/AIZ.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,132.59
2023-02-01,132.25
2023-02-02,133.58
2023-02-03,133.63
2023-02-06,133.13
2023-02-07,135.65
3 changes: 3 additions & 0 deletions s_and_p_data/AJG.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,195.72
2023-02-01,196.28
2023-02-02,194.68
2023-02-03,193.49
2023-02-06,194.55
2023-02-07,197.47
3 changes: 3 additions & 0 deletions s_and_p_data/AKAM.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,88.95
2023-02-01,89.58
2023-02-02,91.28
2023-02-03,89.33
2023-02-06,87.49
2023-02-07,88.16
3 changes: 3 additions & 0 deletions s_and_p_data/ALB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,281.45
2023-02-01,285.9
2023-02-02,290.15
2023-02-03,287.23
2023-02-06,276.81
2023-02-07,279.34
3 changes: 3 additions & 0 deletions s_and_p_data/ALGN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,269.73
2023-02-01,282.53
2023-02-02,359.88
2023-02-03,343.1
2023-02-06,342.46
2023-02-07,345.07
3 changes: 3 additions & 0 deletions s_and_p_data/ALK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,51.34
2023-02-01,51.84
2023-02-02,53.58
2023-02-03,53.47
2023-02-06,52.43
2023-02-07,51.8
3 changes: 3 additions & 0 deletions s_and_p_data/ALL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,128.47
2023-02-01,128.16
2023-02-02,131.89
2023-02-03,131.33
2023-02-06,134.98
2023-02-07,136.72
3 changes: 3 additions & 0 deletions s_and_p_data/ALLE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2317,3 +2317,6 @@ Date,Close
2023-01-31,117.55
2023-02-01,120.05
2023-02-02,121.74
2023-02-03,122.93
2023-02-06,120.87
2023-02-07,120.26
3 changes: 3 additions & 0 deletions s_and_p_data/AMAT.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,111.49
2023-02-01,119.01
2023-02-02,124.26
2023-02-03,119.98
2023-02-06,117.69
2023-02-07,120.52
3 changes: 3 additions & 0 deletions s_and_p_data/AMCR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2697,3 +2697,6 @@ Date,Close
2023-01-31,12.06
2023-02-01,12.07
2023-02-02,12.2
2023-02-03,12.03
2023-02-06,11.93
2023-02-07,11.89
3 changes: 3 additions & 0 deletions s_and_p_data/AMD.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,75.15
2023-02-01,84.64
2023-02-02,88.31
2023-02-03,86.09
2023-02-06,83.68
2023-02-07,85.91
3 changes: 3 additions & 0 deletions s_and_p_data/AME.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,144.92
2023-02-01,145.15
2023-02-02,144.52
2023-02-03,146.38
2023-02-06,147.09
2023-02-07,146.74
3 changes: 3 additions & 0 deletions s_and_p_data/AMGN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,252.4
2023-02-01,246.41
2023-02-02,246.53
2023-02-03,245.17
2023-02-06,243.85
2023-02-07,244.0
3 changes: 3 additions & 0 deletions s_and_p_data/AMP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,350.12
2023-02-01,354.53
2023-02-02,347.71
2023-02-03,354.1
2023-02-06,351.03
2023-02-07,353.33
3 changes: 3 additions & 0 deletions s_and_p_data/AMT.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,223.39
2023-02-01,227.14
2023-02-02,227.39
2023-02-03,222.06
2023-02-06,220.57
2023-02-07,219.47
3 changes: 3 additions & 0 deletions s_and_p_data/AMZN.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,103.13
2023-02-01,105.15
2023-02-02,112.91
2023-02-03,103.39
2023-02-06,102.18
2023-02-07,102.11
3 changes: 3 additions & 0 deletions s_and_p_data/ANET.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2180,3 +2180,6 @@ Date,Close
2023-01-31,126.02
2023-02-01,130.52
2023-02-02,133.44
2023-02-03,131.96
2023-02-06,129.69
2023-02-07,134.91
3 changes: 3 additions & 0 deletions s_and_p_data/ANSS.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,266.36
2023-02-01,274.39
2023-02-02,282.5
2023-02-03,275.23
2023-02-06,272.43
2023-02-07,277.09
3 changes: 3 additions & 0 deletions s_and_p_data/AON.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,318.68
2023-02-01,319.38
2023-02-02,323.0
2023-02-03,313.64
2023-02-06,317.73
2023-02-07,319.8
3 changes: 3 additions & 0 deletions s_and_p_data/AOS.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,67.7
2023-02-01,69.96
2023-02-02,70.4
2023-02-03,69.05
2023-02-06,67.49
2023-02-07,66.63
3 changes: 3 additions & 0 deletions s_and_p_data/APA.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,44.33
2023-02-01,43.4
2023-02-02,42.36
2023-02-03,42.04
2023-02-06,42.02
2023-02-07,43.69
3 changes: 3 additions & 0 deletions s_and_p_data/APD.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,320.51
2023-02-01,318.13
2023-02-02,295.5
2023-02-03,285.02
2023-02-06,283.03
2023-02-07,285.07
3 changes: 3 additions & 0 deletions s_and_p_data/APH.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,79.77
2023-02-01,81.14
2023-02-02,82.09
2023-02-03,81.62
2023-02-06,81.8
2023-02-07,81.35
3 changes: 3 additions & 0 deletions s_and_p_data/APTV.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2819,3 +2819,6 @@ Date,Close
2023-01-31,113.09
2023-02-01,114.75
2023-02-02,118.68
2023-02-03,115.73
2023-02-06,114.95
2023-02-07,115.91
3 changes: 3 additions & 0 deletions s_and_p_data/ARE.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,160.74
2023-02-01,163.27
2023-02-02,170.82
2023-02-03,167.79
2023-02-06,167.26
2023-02-07,166.83
3 changes: 3 additions & 0 deletions s_and_p_data/ATO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,117.54
2023-02-01,118.97
2023-02-02,119.55
2023-02-03,117.19
2023-02-06,117.64
2023-02-07,116.9
3 changes: 3 additions & 0 deletions s_and_p_data/ATVI.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,76.57
2023-02-01,76.7
2023-02-02,77.11
2023-02-03,75.24
2023-02-06,71.58
2023-02-07,75.6
3 changes: 3 additions & 0 deletions s_and_p_data/AVB.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,177.44
2023-02-01,178.02
2023-02-02,184.5
2023-02-03,180.25
2023-02-06,178.79
2023-02-07,179.71
3 changes: 3 additions & 0 deletions s_and_p_data/AVGO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3396,3 +3396,6 @@ Date,Close
2023-01-31,585.01
2023-02-01,602.75
2023-02-02,605.77
2023-02-03,597.62
2023-02-06,601.3
2023-02-07,614.45
3 changes: 3 additions & 0 deletions s_and_p_data/AVY.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,189.44
2023-02-01,191.66
2023-02-02,188.56
2023-02-03,181.2
2023-02-06,178.54
2023-02-07,183.6
3 changes: 3 additions & 0 deletions s_and_p_data/AWK.csv
Original file line number Diff line number Diff line change
Expand Up @@ -3721,3 +3721,6 @@ Date,Close
2023-01-31,156.49
2023-02-01,158.62
2023-02-02,160.6
2023-02-03,156.61
2023-02-06,156.41
2023-02-07,155.47
3 changes: 3 additions & 0 deletions s_and_p_data/AXP.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,174.93
2023-02-01,173.61
2023-02-02,173.13
2023-02-03,178.86
2023-02-06,176.48
2023-02-07,178.7
3 changes: 3 additions & 0 deletions s_and_p_data/AZO.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,2438.85
2023-02-01,2495.68
2023-02-02,2443.55
2023-02-03,2398.51
2023-02-06,2425.88
2023-02-07,2419.81
3 changes: 3 additions & 0 deletions s_and_p_data/BA.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,213.0
2023-02-01,214.75
2023-02-02,209.34
2023-02-03,206.01
2023-02-06,206.81
2023-02-07,214.76
3 changes: 3 additions & 0 deletions s_and_p_data/BAC.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,35.48
2023-02-01,35.92
2023-02-02,36.13
2023-02-03,36.43
2023-02-06,36.37
2023-02-07,36.77
3 changes: 3 additions & 0 deletions s_and_p_data/BALL.csv
Original file line number Diff line number Diff line change
Expand Up @@ -4049,3 +4049,6 @@ Date,Close
2023-01-31,58.24
2023-02-01,58.44
2023-02-02,60.89
2023-02-03,58.97
2023-02-06,58.69
2023-02-07,57.6
Loading

0 comments on commit e544f1f

Please sign in to comment.