├── .gitignore
├── Clustering_Stocks_2.ipynb
├── FnO_list.xlsx
├── LICENSE
├── Pairs.csv
├── Preprocessing_1.ipynb
├── Pricing_data.xlsx
├── README.md
├── Returns_data.xlsx
└── number_line.png
/.gitignore:
--------------------------------------------------------------------------------
1 | # Byte-compiled / optimized / DLL files
2 | __pycache__/
3 | *.py[cod]
4 | *$py.class
5 |
6 | # C extensions
7 | *.so
8 |
9 | # Distribution / packaging
10 | .Python
11 | build/
12 | develop-eggs/
13 | dist/
14 | downloads/
15 | eggs/
16 | .eggs/
17 | lib/
18 | lib64/
19 | parts/
20 | sdist/
21 | var/
22 | wheels/
23 | *.egg-info/
24 | .installed.cfg
25 | *.egg
26 | MANIFEST
27 |
28 | # PyInstaller
29 | # Usually these files are written by a python script from a template
30 | # before PyInstaller builds the exe, so as to inject date/other infos into it.
31 | *.manifest
32 | *.spec
33 |
34 | # Installer logs
35 | pip-log.txt
36 | pip-delete-this-directory.txt
37 |
38 | # Unit test / coverage reports
39 | htmlcov/
40 | .tox/
41 | .coverage
42 | .coverage.*
43 | .cache
44 | nosetests.xml
45 | coverage.xml
46 | *.cover
47 | .hypothesis/
48 | .pytest_cache/
49 |
50 | # Translations
51 | *.mo
52 | *.pot
53 |
54 | # Django stuff:
55 | *.log
56 | local_settings.py
57 | db.sqlite3
58 |
59 | # Flask stuff:
60 | instance/
61 | .webassets-cache
62 |
63 | # Scrapy stuff:
64 | .scrapy
65 |
66 | # Sphinx documentation
67 | docs/_build/
68 |
69 | # PyBuilder
70 | target/
71 |
72 | # Jupyter Notebook
73 | .ipynb_checkpoints
74 |
75 | # pyenv
76 | .python-version
77 |
78 | # celery beat schedule file
79 | celerybeat-schedule
80 |
81 | # SageMath parsed files
82 | *.sage.py
83 |
84 | # Environments
85 | .env
86 | .venv
87 | env/
88 | venv/
89 | ENV/
90 | env.bak/
91 | venv.bak/
92 |
93 | # Spyder project settings
94 | .spyderproject
95 | .spyproject
96 |
97 | # Rope project settings
98 | .ropeproject
99 |
100 | # mkdocs documentation
101 | /site
102 |
103 | # mypy
104 | .mypy_cache/
105 |
--------------------------------------------------------------------------------
/FnO_list.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/techietrader/Pairs-Trading-Indian-Stocks-using-Machine-Learning/6949c6e76c4278bbe7fa4ff06b62e95e83a459f0/FnO_list.xlsx
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 techietrader
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/Pairs.csv:
--------------------------------------------------------------------------------
1 | Cluster_number,Ticker,Nearest_Neighbor
2 | 1,PAGEIND,TATAELXSI
3 | 2,GLENMARK,MANAPPURAM
4 | 2,MANAPPURAM,GLENMARK
5 | 2,MINDTREE,HEXAWARE
6 | 12,ADANIENT,SREINFRA
7 | 12,EXIDEIND,NHPC
8 | 13,JISLJALEQS,BEML
9 | 13,PTC,JISLJALEQS
10 | 13,SUZLON,JISLJALEQS
11 | 14,GODREJIND,UBL
12 | 14,HAVELLS,UBL
13 | 14,IGL,TVSMOTOR
14 | 14,UBL,GODREJIND
15 | 15,ASHOKLEY,MOTHERSUMI
16 | 15,ASIANPAINT,NESTLEIND
17 | 15,AUROPHARMA,CADILAHC
18 | 15,BAJAJ-AUTO,GODREJCP
19 | 15,BOSCHLTD,ZEEL
20 | 15,BRITANNIA,MARICO
21 | 15,CADILAHC,MUTHOOTFIN
22 | 15,DABUR,TITAN
23 | 15,EICHERMOT,HEROMOTOCO
24 | 15,GODREJCP,NESTLEIND
25 | 15,HCLTECH,TECHM
26 | 15,HEROMOTOCO,EICHERMOT
27 | 15,INDIGO,PETRONET
28 | 15,INFRATEL,NMDC
29 | 15,MOTHERSUMI,ASHOKLEY
30 | 15,MUTHOOTFIN,CADILAHC
31 | 15,NESTLEIND,GODREJCP
32 | 15,NMDC,INFRATEL
33 | 15,PETRONET,UPL
34 | 15,PIDILITIND,TITAN
35 | 15,SHREECEM,MUTHOOTFIN
36 | 15,SUNTV,PIDILITIND
37 | 15,TECHM,HCLTECH
38 | 15,TITAN,DABUR
39 | 15,ZEEL,BOSCHLTD
40 | 16,ADANIPORTS,GAIL
41 | 16,ADANIPOWER,JINDALSTEL
42 | 16,DHFL,LICHSGFIN
43 | 16,DLF,ADANIPORTS
44 | 16,GAIL,ADANIPORTS
45 | 16,HINDPETRO,SUNPHARMA
46 | 16,IDEA,DHFL
47 | 16,JINDALSTEL,RPOWER
48 | 16,PEL,ADANIPORTS
49 | 16,RELINFRA,ADANIENT
50 | 16,SAIL,JINDALSTEL
51 | 16,SRTRANSFIN,IDFCBANK
52 | 16,SUNPHARMA,ULTRACEMCO
53 | 16,TATAPOWER,GAIL
54 | 16,ULTRACEMCO,SUNPHARMA
55 | 17,BHEL,SREINFRA
56 | 17,MRF,CONCOR
57 | 17,NHPC,EXIDEIND
58 | 17,SREINFRA,ADANIENT
59 | 18,ACC,TORNTPHARM
60 | 18,AMBUJACEM,CIPLA
61 | 18,CESC,IRB
62 | 18,CIPLA,AMBUJACEM
63 | 18,DALMIABHA,DRREDDY
64 | 18,IDFCBANK,M&MFIN
65 | 18,INDIANB,M&MFIN
66 | 18,M&MFIN,CHOLAFIN
67 | 18,MFSL,RCOM
68 | 18,MRPL,OIL
69 | 18,OIL,CIPLA
70 | 18,RCOM,MFSL
71 | 18,RPOWER,M&MFIN
72 | 18,TORNTPHARM,ACC
73 | 18,TORNTPOWER,CESC
74 | 20,ALBK,ANDHRABANK
75 | 25,DCBBANK,KTKBANK
76 | 25,KTKBANK,SOUTHBANK
77 | 25,SOUTHBANK,KTKBANK
78 | 29,BANKBARODA,IDFC
79 | 29,CANBK,UNIONBANK
80 | 29,PNB,BANKINDIA
81 | 29,UNIONBANK,CANBK
82 | 30,AXISBANK,BANKBARODA
83 | 30,BHARTIARTL,POWERGRID
84 | 30,HINDUNILVR,INFY
85 | 30,ICICIBANK,TATAMOTORS
86 | 30,INFY,ITC
87 | 30,IOC,TATAMTRDVR
88 | 30,ITC,INFY
89 | 30,LT,NTPC
90 | 30,NTPC,POWERGRID
91 | 30,PFC,RECLTD
92 | 30,POWERGRID,TATASTEEL
93 | 30,TATAMOTORS,TATAMTRDVR
94 | 30,TATAMTRDVR,TATAMOTORS
95 | 30,TATASTEEL,POWERGRID
96 | 30,VEDL,TATASTEEL
97 | 32,BAJAJFINSV,GRASIM
98 | 32,BAJFINANCE,INDUSINDBK
99 | 32,BPCL,M&M
100 | 32,COALINDIA,BPCL
101 | 32,GRASIM,BAJAJFINSV
102 | 32,HINDALCO,GRASIM
103 | 32,HINDZINC,MARUTI
104 | 32,IBULHSGFIN,LICHSGFIN
105 | 32,IDFC,YESBANK
106 | 32,INDUSINDBK,BAJFINANCE
107 | 32,JSWSTEEL,HINDALCO
108 | 32,KOTAKBANK,M&M
109 | 32,LICHSGFIN,IBULHSGFIN
110 | 32,M&M,BPCL
111 | 32,MARUTI,M&M
112 | 32,RECLTD,HINDALCO
113 | 32,WIPRO,COALINDIA
114 | 32,YESBANK,COALINDIA
115 | 35,SBIN,HDFCBANK
116 | 35,SYNDIBANK,BANKBARODA
117 | 37,AMARAJABAT,VOLTAS
118 | 37,BALKRISIND,DIVISLAB
119 | 37,BEL,UPL
120 | 37,CONCOR,MRF
121 | 37,CUMMINSIND,VOLTAS
122 | 37,DIVISLAB,BALKRISIND
123 | 37,UPL,BEL
124 | 37,VOLTAS,AMARAJABAT
125 | 39,HDFC,ICICIBANK
126 | 39,HDFCBANK,HDFC
127 | 39,ONGC,TCS
128 | 39,RELIANCE,HDFCBANK
129 | 39,TCS,ONGC
130 |
--------------------------------------------------------------------------------
/Pricing_data.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/techietrader/Pairs-Trading-Indian-Stocks-using-Machine-Learning/6949c6e76c4278bbe7fa4ff06b62e95e83a459f0/Pricing_data.xlsx
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Pairs-Trading-Indian-Stocks-using-Machine-Learning
2 | keywords - Kmeans Clustering, Tsne, PCA, Indian Stocks, Johansen test
3 |
4 | ## FAQs
5 | ### What is Pairs Trading?
6 | Ans- It is a trading strategy where we go long on one stock and simultaneously short the other to keep portfolio as much neutral as possible.
7 |
8 | ### How do you choose stocks in the pair?
9 | Ans- Stocks chosen in the pair should replicate each other’s movement, meaning if one is going up the other should follow and vice-versa, meaning they should possess similarity. Normally, stocks are chosen on the basis of the similarity of their sector, business model, market capitalization, etc.
10 |
11 | ### How do you find whether stocks are similar? Stocks though in same sector can be dissimilar as well?
12 | Ans- Agreed!
13 |
14 | Co-integration test specifies co-movement of price of two stocks and it shows long term relationship between them. There are various ways to test stock’s co-integration. Augmented Dickey Fuller Test, Johansson Test.
15 | Co-integration test the stationarity of the spread between the stocks.
16 |
17 | ### What is Spread and what is Stationarity?
18 | Ans- Spread is the difference in the price of two stock and stationarity means normalizing the spread.
19 | The normalized spread than hovers around the mean and moves around with some standard deviation.
20 | The idea is to enter a trade by going long in one stock and shorting the other if the spread has deviated a bit from the mean. Once position is been taken we hope the spread to revert back to its mean.
21 |
22 | ### What if it doesn’t revert to mean and deviates farther apart?
23 | Ans- It may mean the co-integration has broken and that there is a mean shift.
24 |
25 | ### Hmmm... So it seems choosing appropriate pairs is crucial. How do we go about it?
26 | Ans- 1. One way is Brute Force. We simply try (n*(n-1))/2 number of combinations and test each combination for co-integration test. This is complex.
27 |
28 | 2. Using unsupervised learning.
29 |
30 | ### Tell me more about unsupervised learning?
31 | Ans- Clustering is one way we can reduce the brute force attempts which adds computing complexity.
32 | K-means clustering algorithm helps in finding similarity between stocks and groups them in clusters using a distance metric called ‘Euclidean Distance’.
33 | This way we can reduce number of co-integration attempts with improved accuracy.
34 |
35 | ### What is Euclidean Distance?
36 | Ans- Okay enough of talking. Lets dive in the project.
37 |
38 | By the way this is Euclidean distance-
39 | https://en.wikipedia.org/wiki/Euclidean_distance
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/Returns_data.xlsx:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/techietrader/Pairs-Trading-Indian-Stocks-using-Machine-Learning/6949c6e76c4278bbe7fa4ff06b62e95e83a459f0/Returns_data.xlsx
--------------------------------------------------------------------------------
/number_line.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/techietrader/Pairs-Trading-Indian-Stocks-using-Machine-Learning/6949c6e76c4278bbe7fa4ff06b62e95e83a459f0/number_line.png
--------------------------------------------------------------------------------