This repository provides all Python codes and Jupyter Notebooks of the book Python for Finance

Related tags

Finance py4fi
Overview

Python for Finance (O'Reilly)

This repository provides all Python codes and Jupyter Notebooks of the book Python for Finance -- Analyze Big Financial Data by Yves Hilpisch.

Oder the book here http://shop.oreilly.com/product/0636920032441.do or under http://www.amazon.com/Python-Finance-Analyze-Financial-Data/dp/1491945281/.

There are two code versions available: for Python 3.6 and Python 2.7 (in the legacy folder; not maintained anymore).

Python Packages

There is now a yaml file for the installation of required Python packages in the repository. This is to be used with the conda package manager (see https://conda.io/docs/user-guide/tasks/manage-environments.html). If you do not have Miniconda or Anaconda installed, we recommend to install Miniconda 3.6 first (see https://conda.io/miniconda.html).

After you have cloned the repository, do on the Linux/Mac shell:

cd py4fi
conda env create -f py4fi_conda.yml
source activate py4fi
cd jupyter36
jupyter notebook

On Windows, do:

cd py4fi
conda env create -f py4fi_conda.yml
activate py4fi
cd jupyter36
jupyter notebook

Then you can navigate to the Jupyter Notebook files and get started.

Yahoo! Finance & Google Finance Issues

Recently Yahoo! Finance stopped their original financial data API service that is used in the book in many different places (and been so by many others in the field for years) via the pandas-datareader package.

One way of fixing it in some places is to simply replace data_source='yahoo' by data_source='google' (and maybe working with an alternative symbol). However, Google Finance has also changed data availability via the their API such that this does only help partially.

Another way is to use this fix: https://github.com/ranaroussi/fix-yahoo-finance -- this is done for some code in chapter 11.

In the majority of cases where financial data is used, the pandas-datareader based code has been replaced by simple pandas code that accesses data files that are now part of the repository (CSV data files provided with data as provided either by the Thomson Reuters Eikon API or by data sources from FXCM Forex Capital Markets Ltd.). This makes sure that the code base is not subject to unforeseen API changes by third parties.

Quant Platform

You can immediately use all codes and Jupyter Notebooks by registering on the Quant Platform under http://oreilly.quant-platform.com.

Python for Algorithmic Trading Course & Certificate

Check out our Python for Algorithmic Trading Course under http://pyalgo.tpq.io.

Check out also our Python for Algorithmic Trading Certificate Program under http://certificate.tpq.io.

Company Information

© Dr. Yves J. Hilpisch | The Python Quants GmbH

The Quant Platform (http://pqp.io) and all codes/Jupyter notebooks come with no representations or warranties, to the extent permitted by applicable law.

http://tpq.io | [email protected] | http://twitter.com/dyjh

Quant Platform | http://oreilly.quant-platform.com

Derivatives Analytics with Python (Wiley Finance) | http://derivatives-analytics-with-python.com

Python for Finance (O'Reilly) | http://python-for-finance.com

Python for Algorithmic Trading Course | http://pyalgo.tpq.io

Python for Finance Online Training | http://training.tpq.io

Comments
  • Conda environment creation fails

    Conda environment creation fails

    The creation of the conda enviornment as shown in the readme fails on windows with the last version of conda (conda 4.5.11).

    conda env create -f py4fi_conda.yml
    
    Solving environment: failed
    
    ResolvePackageNotFound:
      - ncurses==6.0=hd04f020_2
      - requests==2.18.4=py36h4516966_1
      - xlsxwriter==1.0.2=py36h3736301_0
      - tornado==4.5.2=py36h468dda9_0
      - itsdangerous==0.24=py36h49fbb8d_1
      - cycler==0.10.0=py36hfc81398_0
      - jinja2==2.9.6=py36hde4beb4_1
      - gmp==6.1.2=hb37e062_1
      - tk==8.6.7=h35a86e2_3
      - lzo==2.10=hb6b8854_1
      - testpath==0.3.1=py36h625a49b_0
      - appnope==0.1.0=py36hf537a9a_0
      - setuptools==36.5.0=py36h2134326_0
      - cffi==1.10.0=py36h880867e_1
      - jupyter==1.0.0=py36h598a6cc_0
      - numba==0.35.0=np113py36_6
      - pandocfilters==1.4.2=py36h3b0b094_1
      - pyqt==5.6.0=py36he5c6137_6
      - jupyter_core==4.3.0=py36h93810fe_0
      - cryptography==2.0.3=py36h22d4226_1
      - expat==2.2.5=hb8e80ba_0
      - chardet==3.0.4=py36h96c241c_1
      - patsy==0.4.1=py36ha1b3fa5_0
      - zeromq==4.2.2=ha360ad0_2
      - jedi==0.10.2=py36h6325097_0
      - zlib==1.2.11=hf3cbc9b_2
      - sqlite==3.20.1=h7e4c145_2
      - traitlets==4.3.2=py36h65bd3ce_0
      - python==3.6.3=h5ce8c04_4
      - mpc==1.0.3=hc455b36_4
      - werkzeug==0.12.2=py36h168efa1_0
      - matplotlib==2.1.0=py36h5068139_0
      - pexpect==4.2.1=py36h3eac828_0
      - wheel==0.29.0=py36h3597b6d_1
      - html5lib==0.999999999=py36h79312fd_0
      - pysocks==1.6.7=py36hfa33cec_1
      - libedit==3.1=hb4e282d_0
      - jpeg==9b=he5867d9_2
      - mpmath==0.19=py36h9185fea_2
      - nbconvert==5.3.1=py36h810822e_0
      - xlwt==1.2.0=py36h5ad1178_0
      - pyzmq==16.0.2=py36h087ffad_2
      - fastcache==1.0.2=py36h8606a76_0
      - pytz==2017.2=py36h2e7dfbc_1
      - pyopenssl==17.2.0=py36h5d7bf08_0
      - nbformat==4.4.0=py36h827af21_0
      - pcre==8.41=hfb6ab37_1
      - gettext==0.19.8.1=h15daf44_3
      - libiconv==1.15=hdd342a3_7
      - urllib3==1.22=py36h68b9469_0
      - certifi==2017.7.27.1=py36hd973bb6_0
      - bleach==2.0.0=py36h8fcea71_0
      - ipython_genutils==0.2.0=py36h241746c_0
      - numexpr==2.6.2=py36h8fc668d_2
      - prompt_toolkit==1.0.15=py36haeda067_0
      - widgetsnbextension==3.0.2=py36h91f43ea_1
      - hdf5==1.10.1=ha036c08_1
      - sympy==1.1.1=py36h7f3cf04_0
      - qt==5.6.2=h9975529_14
      - flask==0.12.2=py36h5658096_0
      - ipykernel==4.6.1=py36h3208c25_0
      - libffi==3.2.1=h475c297_4
      - gmpy2==2.0.8=py36h7ef02cb_1
      - freetype==2.8=h12048fb_1
      - asn1crypto==0.22.0=py36hb705621_1
      - xlrd==1.1.0=py36h336f4a2_1
      - intel-openmp==2018.0.0=h68bdfb3_7
      - pymc3==3.2=py36h1e7238b_0
      - openpyxl==2.4.8=py36he899640_1
      - readline==7.0=hc1231fa_4
      - pip==9.0.1=py36h1555ced_4
      - libgfortran==3.0.1=h93005f0_2
      - pycparser==2.18=py36h724b2fc_1
      - libgpuarray==0.6.9=0
      - python-dateutil==2.6.1=py36h86d2abb_1
      - six==1.11.0=py36h0e22d5e_1
      - libsodium==1.0.13=hba5e272_2
      - numpy==1.13.3=py36h2cdce51_0
      - simplegeneric==0.8.1=py36he5b5b09_0
      - ptyprocess==0.5.2=py36he6521c3_0
      - wcwidth==0.1.7=py36h8c6ec74_0
      - ipython==6.1.0=py36hf612aae_1
      - bzip2==1.0.6=h649919c_2
      - icu==58.2=h4b95b61_1
      - qtconsole==4.3.1=py36hd96c0ff_0
      - click==6.7=py36hec950be_0
      - jupyter_client==5.1.0=py36hf6c435f_0
      - libcxx==4.0.1=h579ed51_0
      - libpng==1.6.32=h6184301_3
      - dbus==1.10.22=h50d9ad6_0
      - terminado==0.6=py36h656782e_0
      - mako==1.0.7=py36h55379d4_0
      - pyparsing==2.2.0=py36hb281f35_0
      - pytables==3.4.2=py36hfbd7ab0_2
      - et_xmlfile==1.0.1=py36h1315bdc_0
      - nose==1.3.7=py36h73fae2b_2
      - markupsafe==1.0=py36h3a1e703_1
      - tqdm==4.19.4=py36he502594_0
      - entrypoints==0.2.3=py36hd81d71f_2
      - h5py==2.7.0=py36h6400cee_1
      - pandoc==1.19.2.1=ha5e8f32_1
      - scikit-learn==0.19.1=py36hffbff8c_0
      - decorator==4.1.2=py36h69a1b52_0
      - notebook==5.2.1=py36h640abe8_0
      - mkl==2018.0.0=h5ef208c_6
      - cython==0.26.1=py36hd51f8eb_0
      - jupyter_console==5.2.0=py36hccf5b1c_1
      - pandas==0.21.0=py36hfed917e_1
      - jsonschema==2.6.0=py36hb385e00_0
      - mkl-service==1.1.2=py36h7ea6df4_4
      - jdcal==1.3=py36h1986823_0
      - ipywidgets==7.0.0=py36h24d3910_0
      - glib==2.53.6=h33f6a65_2
      - ca-certificates==2017.08.26=ha1e5d58_0
      - pygments==2.2.0=py36h240cd3f_0
      - sip==4.18.1=py36h2824476_2
      - xz==5.2.3=h0278029_2
      - mpfr==3.1.5=h7fa3772_1
      - openssl==1.0.2m=h86d3e6a_1
      - webencodings==0.5.1=py36h3b9701d_1
      - mistune==0.8.1=py36h638d0ca_0
      - pickleshare==0.7.4=py36hf512f8e_0
      - scipy==1.0.0=py36h1de22e9_0
      - libcxxabi==4.0.1=hebd6815_0
      - idna==2.6=py36h8628d0a_1
    

    i'm pretty sure it's a version mismatch - but couldn't figure out a way to fix it

    according to an issuei found (https://github.com/ContinuumIO/anaconda-issues/issues/9480#issuecomment-394873967) exporting via conda env export --no-builds > environment.yml should help fix the issue.

    opened by xmatthias 4
  • 【chapter6】data['Mov_Vol'] = data['Return'].rolling(window=252).std() * math.sqrt(252)

    【chapter6】data['Mov_Vol'] = data['Return'].rolling(window=252).std() * math.sqrt(252)

    Rolling.std(ddof=1, *args, **kwargs) calculates rolling standard deviation, normalized by N-1 by default. It means, by default, it uses the sample standard deviation function, in which the denominator is a N-1. The denominator can be set to 1 in this case, if std(ddof=251), then N-251=1. I guess the multiplying of math.sqrt(252) here may be a mistake of confusing rolling.std with numpy.std. The default ddof in numpy.std is 0, which means its denominator is a N. In that case, multiplying of math.sqrt(252) can offset the denominator of 252 exactly.

    opened by zhuyuanping 2
  • mcs_full_vector_numpy.py has a drift a S[0]

    mcs_full_vector_numpy.py has a drift a S[0]

    Hi @yhilpisch, the fixed for #4 by setting the first time slice (t=0) to 0 does not correctly fixed the issue. On first look, it appears that it has solved the issue. However, on closer inspection of the equation, I notice that even though setting the first time slice (t=0) to 0, a drift will still be included at time slice resulting in an over-estimation.

    opened by joelowj 2
  • mcs_full_vector_numpy.py does not converge to the true price.

    mcs_full_vector_numpy.py does not converge to the true price.

    Hi @yhilpisch,

    Thank you for the great work as an undergraduate student I have learn a lot from your book!

    Example 3-4 code which estimates the theoretical value of a European call option via Monte Carlo simulation appears to yield different results from that of bsm_call_value() function in Chapter 3.

    Using the analytical formula for the valuation of European call option in BSM model for S0 = 100, K = 105, T = 1.0, r = 0.05 and sigma = 0.2, we arrive at a European call price of 8.02135.

    Using the code from example 3-4 which estimates the theoretical value of a European call option via Monte Carlo simulation using the same parameter should yield the same result as that of the analytical formula. However, the results yield by mcs_full_vector_numpy.py appears to be significantly different from that of bsm_call_value().

    My suspect is that random.standard_normal((M + 1, I)) should be changed to random.standard_normal((M, I)) such that the calculation converge to the true price.

    screen shot 2018-07-08 at 2 17 27 pm screen shot 2018-07-08 at 2 18 09 pm
    opened by joelowj 2
  • Q about bsm_vega?

    Q about bsm_vega?

    If I understand it right, the Vega of an option, the derivative of the norm cdf is needed. The notation you used in your book is N'(d_1), with a prime. The calculation in bsm_vega has used:

     stats.norm.cdf( ) 
    

    which doesn't appear to be correct. Shouldn't this be just the norm pdf? ie. exp(-x^2) normalized?

    opened by kechan 2
  • Example in 6.4 not working ( Netfonds's API )

    Example in 6.4 not working ( Netfonds's API )

    The API seems not working. I wonder whether I've got it wrong? This is URL I get from frombook, but the page is empty http://hopey.netfonds.no/posdump.php?date=20140922&paper=AAPL.O&csv_format=csv

    This is URL I get from IPython. No data, only column name http://www.netfonds.no/quotes/posdump.php?date=20140922&paper=NKE.N&csv_format=csv

    Do anyone figure out the solution ?

    opened by tnlin 1
Owner
Yves Hilpisch
CEO The Python Quants & The AI Machine | Adjunct Professor of Computational Finance | Python, AI, Finance & Algorithmic Trading
Yves Hilpisch
Q-Fin: A Python library for mathematical finance.

Q-Fin A Python library for mathematical finance. Installation https://pypi.org/project/QFin/ pip install qfin Bond Pricing Option Pricing Black-Schol

Roman Paolucci 247 Jan 1, 2023
Yahoo! Finance market data downloader (+faster Pandas Datareader)

Yahoo! Finance market data downloader Ever since Yahoo! finance decommissioned their historical data API, many programs that relied on it to stop work

Ran Aroussi 8.4k Jan 1, 2023
High-performance TensorFlow library for quantitative finance.

TF Quant Finance: TensorFlow based Quant Finance Library Table of contents Introduction Installation TensorFlow training Development roadmap Examples

Google 3.5k Jan 1, 2023
This repository contains a set of plugins for Volatility 3

volatility_plugins This repository contains a set of plugins for Volatility 3 These plugins are not compatible with Volatility 2 To use these plugins

Immersive-Labs-Sec 10 Nov 30, 2022
Common financial risk and performance metrics. Used by zipline and pyfolio.

empyrical Common financial risk metrics. Table of Contents Installation Usage Support Contributing Testing Installation pip install empyrical Usage S

Quantopian, Inc. 1k Dec 26, 2022
Portfolio and risk analytics in Python

pyfolio pyfolio is a Python library for performance and risk analysis of financial portfolios developed by Quantopian Inc. It works well with the Zipl

Quantopian, Inc. 4.8k Jan 8, 2023
Technical Analysis Library using Pandas and Numpy

Technical Analysis Library in Python It is a Technical Analysis library useful to do feature engineering from financial time series datasets (Open, Cl

Darío López Padial 3.4k Jan 2, 2023
An open source reinforcement learning framework for training, evaluating, and deploying robust trading agents.

TensorTrade: Trade Efficiently with Reinforcement Learning TensorTrade is still in Beta, meaning it should be used very cautiously if used in producti

null 4k Dec 30, 2022
Find big moving stocks before they move using machine learning and anomaly detection

Surpriver - Find High Moving Stocks before they Move Find high moving stocks before they move using anomaly detection and machine learning. Surpriver

Tradytics 1.5k Dec 31, 2022
rotki is an open source portfolio tracking, analytics, accounting and tax reporting tool that respects your privacy.

rotki is an open source portfolio tracking, analytics, accounting and tax reporting tool that respects your privacy. The mission of rotki is to bring transparency into the crypto and financial sectors through the use of open source.

Rotki 2k Dec 30, 2022
Fourth and final milestone project

Milestone Project 4: Pound Dog Click link to visit "Pound Dog" Aim of the project The aim of this project is to provide access to a website informing

Jamie Wilson 1 Oct 31, 2021
A python wrapper for Alpha Vantage API for financial data.

alpha_vantage Python module to get stock data/cryptocurrencies from the Alpha Vantage API Alpha Vantage delivers a free API for real time financial da

Romel Torres 3.8k Jan 7, 2023
Python sync/async framework for Interactive Brokers API

Introduction The goal of the IB-insync library is to make working with the Trader Workstation API from Interactive Brokers as easy as possible. The ma

Ewald de Wit 2k Dec 30, 2022
bt - flexible backtesting for Python

bt - Flexible Backtesting for Python bt is currently in alpha stage - if you find a bug, please submit an issue. Read the docs here: http://pmorissett

Philippe Morissette 1.6k Jan 5, 2023
ffn - a financial function library for Python

ffn - Financial Functions for Python Alpha release - please let me know if you find any bugs! If you are looking for a full backtesting framework, ple

Philippe Morissette 1.4k Jan 1, 2023
An Algorithmic Trading Library for Crypto-Assets in Python

Service Master Develop CI Badge Catalyst is an algorithmic trading library for crypto-assets written in Python. It allows trading strategies to be eas

Enigma 2.4k Jan 5, 2023
Python library for backtesting trading strategies & analyzing financial markets (formerly pythalesians)

finmarketpy (formerly pythalesians) finmarketpy is a Python based library that enables you to analyze market data and also to backtest trading strateg

Cuemacro 3k Dec 30, 2022
Python Backtesting library for trading strategies

backtrader Yahoo API Note: [2018-11-16] After some testing it would seem that data downloads can be again relied upon over the web interface (or API v

DRo 9.8k Dec 30, 2022