Py4fi2nd - Jupyter Notebooks and code for Python for Finance (2nd ed., O'Reilly) by Yves Hilpisch.

Overview

Python for Finance (2nd ed., O'Reilly)

This repository provides all Python codes and Jupyter Notebooks of the book Python for Finance -- Mastering Data-Driven Finance (2nd edition) by Yves Hilpisch.

Visit the book page of O'Reilly under http://bit.ly/python-finance-2e or order the book under https://www.amazon.com/Python-Finance-Mastering-Data-Driven/dp/1492024333/.

The codes of the book are based on Python 3.7. The codes presented in this Github repository are tested for Python 3.6 since at the time of creating it, TensorFlow was not yet compatible with Python 3.7. Once this has happened, appropriate changes (e.g. to the conda yaml file, see below) will be made.

Python Packages

There is 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.7 first (see https://conda.io/miniconda.html).

After you have cloned the repository, do on the shell (currently works on Mac OS):

cd py4fi2nd
conda env create -f py4fi2nd.yml
source activate py4fi2nd
jupyter notebook

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

Quant Platform

You can immediately use all codes and Jupyter Notebooks by registering on the Quant Platform under http://py4fi.pqp.io.

Python for Finance Training & University Certificate

Check out our Python for Finance & Algorithmic Trading online trainings under http://training.tpq.io.

Check out also our University Certificate Program in Python for Algorithmic Trading 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://pqp.io

Derivatives Analytics with Python (Wiley Finance) | http://dawp.tpq.io

Python for Finance (O'Reilly) | http://pff.tpq.io

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

University Certificate in Python for Algorithmic Trading | http://certificate.tpq.io

Comments
  • Docker install: /bin/sh: 1: /install.sh not found

    Docker install: /bin/sh: 1: /install.sh not found

    I've put the Dockerfile and install.sh in an droplet, directory /install/Setup/Docker$ Runs$ sudo docker build -t py4fi:basic .

    this goes well, down to Step 5/7, when I get the /bin/sh: 1: /install.sh not found error.

    So what's going on? why is Docker looking in bin/sh and not in current directory? It did find /install.sh the moment earlier?

    opened by havarsak 8
  • question: chapter 15, regression, the calculation of return

    question: chapter 15, regression, the calculation of return

    chapter 15, regression section, in the book, it is: In [28]: data['strat_ols_1'] = data['pos_ols_1'] * data['returns'] In [29]: data['strat_ols_2'] = data['pos_ols_2'] * data['returns']

    should we use shift(1) as same as in section "Vectorized Backtesting" (cell 15)? In [28]: data['strat_ols_1'] = data['pos_ols_1'].shift(1) * data['returns'] In [29]: data['strat_ols_2'] = data['pos_ols_2'].shift(1) * data['returns']

    (cell 15 is : In [15]: data['Strategy'] = data['Position'].shift(1) * data['Returns'] )

    opened by js20181010 4
  • Conda environment creation fails for 2nd Edition also?

    Conda environment creation fails for 2nd Edition also?

    I noticed there was a similar conda env create issue for the 1st edition's repo.

    conda 4.5.11

    conda env create -f py4fi2nd.yml
    Using Anaconda API: https://api.anaconda.org
    Solving environment: failed
    
    ResolvePackageNotFound:
      - readline=7.0
      - compiler-rt=4.0.1
      - libffi=3.2.1
      - llvm-lto-tapi=4.0.1
      - clang=4.0.1
      - gettext=0.19.8.1
      - dbus=1.13.2
      - mpc=1.1.0
      - ld64=274.2
      - gmp=6.1.2
      - clangxx=4.0.1
      - llvm=4.0.1
      - expat=2.2.6
      - cctools=895
      - libedit=3.1.20170329
      - ptyprocess=0.6.0
      - libcxx=4.0.1
      - libcxxabi=4.0.1
      - clangxx_osx-64=4.0.1
      - glib=2.56.2
      - ncurses=6.1
      - clang_osx-64=4.0.1
      - libgfortran=3.0.1
      - appnope=0.1.0
    
    opened by jp-dev 4
  • couldn't install yaml

    couldn't install yaml

    I am using Anaconda 4.7.10, and couldn't install the env from yaml

    Here is the message

    Collecting package metadata (repodata.json): done Solving environment: failed

    ResolvePackageNotFound:

    • libgfortran=3.0.1
    • cctools=895
    • libcxxabi=4.0.1
    • appnope=0.1.0
    • libffi=3.2.1
    • clangxx_osx-64=4.0.1
    • glib=2.56.2
    • llvm-lto-tapi=4.0.1
    • expat=2.2.6
    • gettext=0.19.8.1
    • ld64=274.2
    • clang=4.0.1
    • libedit=3.1.20170329
    • ncurses=6.1
    • dbus=1.13.2
    • clang_osx-64=4.0.1
    • llvm=4.0.1
    • gmp=6.1.2
    • libcxx=4.0.1
    • clangxx=4.0.1
    • mpc=1.1.0
    • compiler-rt=4.0.1
    • readline=7.0
    • ptyprocess=0.6.0
    opened by zhghao 3
  • Solving enviroment: failed py4fi2nd

    Solving enviroment: failed py4fi2nd

    Windows OS Anaconda installed Run through Anaconda command line as Administrator

    R:\py4fi2nd>conda env create -f py4fi2nd.yml Collecting package metadata (repodata.json): done Solving environment: failed

    ResolvePackageNotFound:

    • dbus=1.13.2
    • libcxxabi=4.0.1
    • libcxx=4.0.1
    • gmp=6.1.2
    • gettext=0.19.8.1
    • cctools=895
    • ncurses=6.1
    • clangxx_osx-64=4.0.1
    • appnope=0.1.0
    • clang_osx-64=4.0.1
    • libedit=3.1.20170329
    • ld64=274.2
    • compiler-rt=4.0.1
    • clang=4.0.1
    • glib=2.56.2
    • llvm=4.0.1
    • libgfortran=3.0.1
    • readline=7.0
    • clangxx=4.0.1
    • llvm-lto-tapi=4.0.1
    • expat=2.2.6
    opened by CinderZhang 2
  • Issue creating envirionment using p4fi2nd.yml

    Issue creating envirionment using p4fi2nd.yml

    I have a linux Mint system and am using latest version of miniconda.

    I have followed the installation instructions as per the github page for creating an environment for py4fi2nd.

    When I issue the terminal command: conda env create -f py4fi2nd.yml

    I get the following issue:

    ResolvePackageNotFound:

    • libcxxabi=4.0.1
    • clangxx_osx-64=4.0.1
    • clangxx=4.0.1
    • cctools=895
    • libgfortran=3.0.1
    • llvm=4.0.1
    • appnope=0.1.0
    • clang_osx-64=4.0.1
    • libcxx=4.0.1
    • llvm-lto-tapi=4.0.1
    • clang=4.0.1
    • ld64=274.2
    • compiler-rt=4.0.1 The environment is not created.

    Do I need to change the yaml file in some way?

    Kind Regards

    Mark

    opened by markgahagan1 2
  • Help on Chapter 17

    Help on Chapter 17

    Hello. I was hoping to get help on a error that I cannot figure out. The code is as follows:


    [1] import numpy as np import pandas as pd import datetime as dt

    [2] from pylab import mpl, plt plt.style.use('seaborn') mpl.rcParams['font.family'] = 'serif' %matplotlib inline

    [3] import sys sys.path.append(r'C:\Users****\dx-master\dx') #I was getting an error without 'r' in front of C

    [4] dates = [dt.datetime(2020, 1, 1), dt.datetime(2020, 7, 1), dt.datetime(2021, 1, 1)]

    [5] (dates[1] - dates[0]).days / 365. Out:0.4986301369863014

    [6] (dates[2] - dates[1]).days / 365. Out:0.5041095890410959

    [7] fractions = [0.0, 0.5, 1.0]

    #DX Package

    #Frame -- Helper Function

    #get_year_deltas.py

    #Python for Finance, 2nd ed. #(c) Dr. Yvess J. Hilpisch

    import numpy as np

    def get_year_deltas(date_list, day_count=365.): ''' Return vector of floats with day deltas in years. Initial value normalized to zero.

    Parameters ========== date_list : list or array collection of datetime objects day_count : float number of days for a year (to account for different conventions)

    Results
    =======
    delta_list : array
        year fractions
    '''
    
    start = date_list[0]
    delta_list = [(date - start).days / day_count
                  for date in date_list]
    return np.array(delta_list)
    

    [8] from get_year_deltas import get_year_deltas

    ModuleNotFoundError Traceback (most recent call last) in ----> 1 from get_year_deltas import get_year_deltas

    ModuleNotFoundError: No module named 'get_year_deltas'


    I cant seem to figure out why the module isnt being found. Any help would be appreciated.

    opened by crm2730 2
  • where can download the pylab model?

    where can download the pylab model?

    i see the issue on working code ModuleNotFoundError Traceback (most recent call last) in 1 import numpy as np 2 import pandas as pd ----> 3 from pylab import plt, mpl 4 plt.style.use('seaborn') 5 mpl.rcParams['font.family'] = 'serif' ModuleNotFoundError: No module named 'pylab' could you can tell me where can download the module? thank you.

    opened by GavinMoreYoung 2
  • Ch_02: there's no explicit solution in the book for the issue of Floating Point Arithmetic

    Ch_02: there's no explicit solution in the book for the issue of Floating Point Arithmetic

    this book is good and comprehensive for people who want to enter the word of Python for finance but when I read the ch2 for the issue Floating-Point Arithmetic, I don't find the explicit way to address it in the book (yes, the author mentions Decimal module but he does not provide the corresponding fix to the example he describes before) - I don't know if the author intends to want the readers to search for the answer on their own.

    so I posted in StackOverflow to look for the answer and those are Q/A

    Questions

    1. First Example

    Expecting:

    In[1]: a = 0.35 + 0.1
    In[2]: print(a)
    0.45
    

    Actual:

    In[1]: a = 0.35 + 0.1
    In[2]: print(a)
    0.44999999999999996
    

    My take to present:

    format(a, '.2f')
    '0.45'
    

    I thought Decimal could help but it looks like the same

    In[1]:import decimal
          from decimal import Decimal
    
    In[2]:Decimal(0.35) + Decimal(0.1)
    Decimal('0.4499999999999999833466546306')
    

    2. Second Example

    Expecting:

    In[1]:b = 0.35
    In[2]:b.as_integer_ratio()
    (7, 20)
    

    Actual:

    In[1]:b = 0.35
    In[2]:b.as_integer_ratio()
    (3152519739159347, 9007199254740992)
    

    Answers using Decimal and fractions

    You are starting with imprecise floats. Start with exact decimals, from string representation:

    Decimal("0.35") + Decimal("0.1")
    # => Decimal('0.45')
    
    Decimal("0.35").as_integer_ratio()
    # => (9, 20)
    

    Or start with exact numbers (e.g. integers):

     Decimal(35) / Decimal(100)
     # => Decimal('0.35')
    
     from fractions import Fraction
     Fraction(35, 100)
     # => Fraction(7, 20)
    
    
    
    
    
    
    
    
    opened by paulyang0125 1
  • .yml - Solving environment failed

    .yml - Solving environment failed

    i could use some help with the following error message. i'm on a Windows7 system running anaconda3.

    when i run the following command in a conda window to setup the py4fi2nd environment, i get the following:

    \py4fi2nd\>conda env create -f py4fi2nd.yml
    Collecting package metadata (repodata.json): done
    Solving environment: failed
    
    ResolvePackageNotFound:
    - ncurses=6.1
    - glib-2.56.2
    -compiler-rt=4.0.1
    ... (long list of packages)
    -clang_osx-64=4.0.1
    -gmp=6.1.2
    -readline=7.0
    

    thanks for any suggestions that might help. i'm open to modifying the .yml file if it needs to be customized for Windows, for example- clang_osx-64=4.0.1 is probably for MacOS?

    Or, I can import the libraries to the env as needed.

    -jw

    opened by joe-wojniak 1
  • Chapter 19 19.3 In[95]

    Chapter 19 19.3 In[95]

    I have a question related to American Put Option Analysis by using the valuation_mcs_american.py and the code given in Chapter 19 Section 19.3: For In[95]: for maturity in (dt.datetime(2020, 12, 31), dt.datetime(2021, 12, 31): When I type this and after finish the whole In[95]: It reported an error of saying: Only size-1 arrays can be converted to Python scalars However, if I only designate: maturity=dt.datetime(2020, 12, 31), there is no error happening again I would like to know what could have happened and if there are some further guidelines, please let me know, thanks

    opened by ghost 3
  • Ch 02 Cloud: install linux system tools, basic packages and jupyter notebook fails

    Ch 02 Cloud: install linux system tools, basic packages and jupyter notebook fails

    Now I'm installing from local to remote machine. The files are correctly copied over to remote machine, and run as ROOT.

    The installation works fine. Jupyter notebook is started.

    I have two problems. First, I can't restart the notebook. If I write : jupyter notebook then I only get the suggestion to install with apt install jupyter-core

    opened by havarsak 3
  • Input/Output chapter 9 has path commands that throw errors in windows

    Input/Output chapter 9 has path commands that throw errors in windows

    Referring the the Cell numbers as in the PFF second edition, chapter 9.

    Cell 8: ll Spath* ll is possibly intended to refer to a python module/library, but if so it has not been declared. $path* fails as a Syntax error at the $. There are multiple occurrences of this case in the notebook.

    Cell 25: Code in the Notebook differs from that in the Book. Book has: !rm -f $path* Notebook has: get_ipython().system('rm -f $path*') However, !rm -f appears to be a linux system command. [Windows: del /f {path/filename} The path variable intended by $path* has not been successfully declared, due the Cell 8 issue.

    Some guidance on how both of these are supposed to work in windows would be appreciated.

    opened by gahobbsau 1
Owner
Yves Hilpisch
CEO The Python Quants & The AI Machine | Adjunct Professor of Computational Finance | Python, AI, Finance & Algorithmic Trading
Yves Hilpisch
📚 A collection of Jupyter notebooks for learning and experimenting with OpenVINO 👓

A collection of ready-to-run Python* notebooks for learning and experimenting with OpenVINO developer tools. The notebooks are meant to provide an introduction to OpenVINO basics and teach developers how to leverage our APIs for optimized deep learning inference in their applications.

OpenVINO Toolkit 840 Jan 3, 2023
Library extending Jupyter notebooks to integrate with Apache TinkerPop and RDF SPARQL.

Graph Notebook: easily query and visualize graphs The graph notebook provides an easy way to interact with graph databases using Jupyter notebooks. Us

Amazon Web Services 501 Dec 28, 2022
Advanced Deep Learning with TensorFlow 2 and Keras (Updated for 2nd Edition)

Advanced Deep Learning with TensorFlow 2 and Keras (Updated for 2nd Edition)

Packt 1.5k Jan 3, 2023
This is 2nd term discrete maths project done by UCU students that uses backtracking to solve various problems.

Backtracking Project Sponsors This is a project made by UCU students: Olha Liuba - crossword solver implementation Hanna Yershova - sudoku solver impl

Dasha 4 Oct 17, 2021
2nd solution of ICDAR 2021 Competition on Scientific Literature Parsing, Task B.

TableMASTER-mmocr Contents About The Project Method Description Dependency Getting Started Prerequisites Installation Usage Data preprocess Train Infe

Jianquan Ye 298 Dec 21, 2022
Kaggle G2Net Gravitational Wave Detection : 2nd place solution

Kaggle G2Net Gravitational Wave Detection : 2nd place solution

Hiroshechka Y 33 Dec 26, 2022
Solutions of Reinforcement Learning 2nd Edition

Solutions of Reinforcement Learning, An Introduction

YIFAN WANG 1.4k Dec 30, 2022
The 2nd Version Of Slothybot

SlothyBot Go to this website: "https://bitly.com/SlothyBot" The 2nd Version Of Slothybot. The Bot Has Many Features, Such As: Moderation Commands; Kic

Slothy 0 Jun 1, 2022
Xview3 solution - XView3 challenge, 2nd place solution

Xview3, 2nd place solution https://iuu.xview.us/ test split aggregate score publ

Selim Seferbekov 24 Nov 23, 2022
This is the solution for 2nd rank in Kaggle competition: Feedback Prize - Evaluating Student Writing.

Feedback Prize - Evaluating Student Writing This is the solution for 2nd rank in Kaggle competition: Feedback Prize - Evaluating Student Writing. The

Udbhav Bamba 41 Dec 14, 2022
Reinforcement Learning for finance

Reinforcement Learning for Finance We apply reinforcement learning for stock trading. Fetch Data Example import utils # fetch symbols from yahoo fina

Tomoaki Fujii 159 Jan 3, 2023
Source code and notebooks to reproduce experiments and benchmarks on Bias Faces in the Wild (BFW).

Face Recognition: Too Bias, or Not Too Bias? Robinson, Joseph P., Gennady Livitz, Yann Henon, Can Qin, Yun Fu, and Samson Timoner. "Face recognition:

Joseph P. Robinson 41 Dec 12, 2022
Ready-to-use code and tutorial notebooks to boost your way into few-shot image classification.

Easy Few-Shot Learning Ready-to-use code and tutorial notebooks to boost your way into few-shot image classification. This repository is made for you

Sicara 399 Jan 8, 2023
Notebooks em Python para Métodos Eletromagnéticos

GeoSci Labs This is a repository of code used to power the notebooks and interactive examples for https://em.geosci.xyz and https://gpg.geosci.xyz. Th

Victor Cezar Tocantins 1 Nov 16, 2021
Repository for scripts and notebooks from the book: Programming PyTorch for Deep Learning

Repository for scripts and notebooks from the book: Programming PyTorch for Deep Learning

Ian Pointer 368 Dec 17, 2022
Nb workflows - A workflow platform which allows you to run parameterized notebooks programmatically

NB Workflows Description If SQL is a lingua franca for querying data, Jupyter sh

Xavier Petit 6 Aug 18, 2022
A Jupyter notebook to play with NVIDIA's StyleGAN3 and OpenAI's CLIP for a text-based guided image generation.

A Jupyter notebook to play with NVIDIA's StyleGAN3 and OpenAI's CLIP for a text-based guided image generation.

Eugenio Herrera 175 Dec 29, 2022
Repository of Jupyter notebook tutorials for teaching the Deep Learning Course at the University of Amsterdam (MSc AI), Fall 2020

Repository of Jupyter notebook tutorials for teaching the Deep Learning Course at the University of Amsterdam (MSc AI), Fall 2020

Phillip Lippe 1.1k Jan 7, 2023
This Jupyter notebook shows one way to implement a simple first-order low-pass filter on sampled data in discrete time.

How to Implement a First-Order Low-Pass Filter in Discrete Time We often teach or learn about filters in continuous time, but then need to implement t

Joshua Marshall 4 Aug 24, 2022