├── .gitignore ├── LICENSE ├── README.markdown ├── data ├── inputDataDaily_CL_20120813.mat ├── inputDataOHLCDaily_20120504.mat ├── inputDataOHLCDaily_stocks_20120424.mat └── inputData_ETF.mat ├── notebook └── EP Chan Book.ipynb └── src └── hurst.py /.gitignore: -------------------------------------------------------------------------------- 1 | websites# Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | *.pyc 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | .hypothesis/ 49 | .pytest_cache/ 50 | 51 | # Translations 52 | *.mo 53 | *.pot 54 | 55 | # Django stuff: 56 | *.log 57 | local_settings.py 58 | db.sqlite3 59 | 60 | # Flask stuff: 61 | instance/ 62 | .webassets-cache 63 | 64 | # Scrapy stuff: 65 | .scrapy 66 | 67 | # Sphinx documentation 68 | docs/_build/ 69 | 70 | # PyBuilder 71 | target/ 72 | 73 | # Jupyter Notebook 74 | .ipynb_checkpoints 75 | 76 | # pyenv 77 | .python-version 78 | 79 | # celery beat schedule file 80 | celerybeat-schedule 81 | 82 | # SageMath parsed files 83 | *.sage.py 84 | 85 | # Environments 86 | .env 87 | .venv 88 | env/ 89 | venv/ 90 | ENV/ 91 | env.bak/ 92 | venv.bak/ 93 | 94 | # Spyder project settings 95 | .spyderproject 96 | .spyproject 97 | 98 | # Rope project settings 99 | .ropeproject 100 | 101 | # mkdocs documentation 102 | /site 103 | 104 | # mypy 105 | .mypy_cache/ 106 | 107 | # user created folders 108 | References/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2013-2018 Blackrock Digital LLC 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 13 | all 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 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # README 2 | 3 | ## Description 4 | 5 | This repo recreates the book *Algorithmic Trading Winning Strategies and their Rational* by Ernie Chan. 6 | 7 | The original code is in *MATLAB*. Many others have implemented this book in *Python* and helped me a lot during the study process. I try to implement every chapter using *Jupyter Notebook*. In the notebook, I can write down my thought process, therefore, make it easier for others who are interested in learning the book in *Python*. 8 | 9 | + This project is associated with virtural environment **qt-env** and ipykernel **qt**. 10 | 11 | ## qt-env requirements 12 | + ipykernel==4.8.2 13 | + bokeh==0.12.15 14 | + lxml==4.2.1 15 | + mysql-connector-python==8.0.11 16 | + pandas-datareader==0.6.0 17 | + pipdeptree==0.12.1 18 | + seaborn==0.8.1 19 | + SQLAlchemy==1.2.7 20 | + statsmodels==0.9.0 21 | + bs4==0.0.1 22 | + pykalman==0.9.5 23 | 24 | ## TODO 25 | - [ ] Complete **Trading calender spread**, 07/16/2018 -------------------------------------------------------------------------------- /data/inputDataDaily_CL_20120813.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zazhang/ep-chan-book-algo-trading/0f3f06508990ee41f3dd711e50293ff7b974336f/data/inputDataDaily_CL_20120813.mat -------------------------------------------------------------------------------- /data/inputDataOHLCDaily_20120504.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zazhang/ep-chan-book-algo-trading/0f3f06508990ee41f3dd711e50293ff7b974336f/data/inputDataOHLCDaily_20120504.mat -------------------------------------------------------------------------------- /data/inputDataOHLCDaily_stocks_20120424.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zazhang/ep-chan-book-algo-trading/0f3f06508990ee41f3dd711e50293ff7b974336f/data/inputDataOHLCDaily_stocks_20120424.mat -------------------------------------------------------------------------------- /data/inputData_ETF.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zazhang/ep-chan-book-algo-trading/0f3f06508990ee41f3dd711e50293ff7b974336f/data/inputData_ETF.mat -------------------------------------------------------------------------------- /src/hurst.py: -------------------------------------------------------------------------------- 1 | #!usr/bin/env ipython 2 | 3 | """Hurst Exponent 4 | https://www.quantstart.com/articles/Basics-of-Statistical-Mean-Reversion-Testing 5 | https://stackoverflow.com/questions/39488806/hurst-exponent-in-python 6 | """ 7 | 8 | from numpy import log, polyfit, var, subtract 9 | 10 | 11 | def hurst_ernie_chan(p, lag_range=None): 12 | 13 | p_log = log10(p) # use log price 14 | 15 | variancetau = [] 16 | tau = [] 17 | 18 | # Create the range of lag values 19 | if lag_range == None: 20 | lags = [2] 21 | else: 22 | lags = range(2, lag_range) # lag_range < len(ts) 23 | 24 | for lag in lags: 25 | 26 | # Write the different lags into a vector to compute a set of tau or lags 27 | tau.append(lag) 28 | 29 | # call this pp or the price difference 30 | pp = subtract(p_log[lag:], p_log[:-lag]) 31 | variancetau.append(var(pp)) 32 | 33 | # we now have a set of tau or lags and a corresponding set of variances 34 | #print tau 35 | #print variancetau 36 | 37 | # plot the log of those variance against the log of tau and get the slope 38 | m = polyfit(log10(tau),log10(variancetau),1) 39 | 40 | hurst = m[0] / 2 41 | 42 | return hurst --------------------------------------------------------------------------------