SlideShare una empresa de Scribd logo
1 de 45
Start hacking finance data
with Python
driller@patraqushe
PyConJP 2016
September 22, 2016
About me
driller
@patraqushe
derivative trader
1.5 year
2
Agenda
Are you still exhausted from Excel?
Don’t be afraid of time series data
Using Jupyter Notebook
3
Why Python for Finance?
Analyzing data with simple code
Substantial libraries(especially Deep leaning)
pandas
Jupyter Notebook
Cooperating with:
Scraping, crawling
Web frame work
Infrastructure
4
Why Python in my case
Developed trading tools using Excel
Adding new functions and changing rules of exchange became
gradually more difficult
Python is the most manageable(in my opinion)
Pandas is similar to Excel
Drastically cheaper using Jupyter Notebooks
5
Are you still exhausted from
Excel?
~ Migrate from Excel to Python to improve productivity ~
6
Generate stock prices using Monte-Carlo
simulation
Stock price 1,000 yen
time remaining until
expiration
30 days
risk-free interest rate 0.1%
annual volatility of stock price 20%
sample paths 10,000 -> 50,000
7
Case1-1: Implement Monte-Carlo
Simulation in Excel Function
1. Input formula into a Cell to generate random number with
geometric Brownian motion(it satisfies the following stochastic
differential equation)
𝑑𝑆𝑡 = 𝜇𝑆𝑡 𝑑𝑡 + 𝜎𝑆𝑡 𝐵𝑡
2. Copy above formula count of sample paths
3. Classify the result into bins
4. Count the number of each bins then visualize
Sample:
Case1-1_1-2.xlsm
8
It's possible to implement Monte-Carlo
Simulations using only mathematical formula,
however:
To increase cell for increasing sample paths
To correspond existing cell for adding cells
To become heavy and slow by recalculation
9
Case1-2: Implement Monte-Carlo
Simulation in VBA
Very long code(especially histogram)
When changing the layout of an Excel sheet, you have to change all
the addresses of related cells(can handle by "name manager"
partially)
Very slow
10
Sample:
Case1-1_1-2.xlsm
Case1-3: Implement Monte-Carlo
Simulation in Python
Very short code(especially histogram)
No need to consider data storage
Faster than VBA
Sample:
Case1-3.ipynb
11
Excel vs. Python
Lines of Code: 105
Wall Time: 7.89s
More complex
Lines of Code: 10
Wall Time: 0.83s
More simple
12
Though, Excel has the advantage of…
Easy to input
Easy to create a template
A huge number of users(high data compatibility)
13
Python packages to work with Excel files
xlrd
xlwt
XlsxWriter
xlutils
openpyxl
xlwings
ExcelPython
14
There are many packages, but…
pandas.read_excel() will almost always be what I want
Use other packages for the operation that pandas cannot do
Write data to an opened file
Operate Cell
Draw graphs…
15
Case1-4: Relationship Economic indicator
and exchange rate and stock price
Open Economic indicator & Stock price Excel file @ vdata.nikkei.com
through pandas
Economic indicator
Real Gross Domestic Product
Diffusion Index
Currency Pair : USD/JPY
Stock: Nikkei Index
Visualize by seaborn
Sample:
Case1-4.ipynb
16
Case 1-5: Relationship ETF/J-REIT
purchases and stock prices
Open Excel files @ boj.or.jp through pandas
Load TSE REIT index and stock Indices price data from k-db site
Stock Indices
TOPIX
JPX400
Nikkei225
Visualize relationship using seaborn
Sample:
Case1-5.ipynb
17
Use xlwings
Read/Write open Excel files
Supports Numpy and pandas data types
Call Python script from Excel
Write Excel User Defined Functions(UDF) in Python
Use openpyxl for Cell & Chart operation
18
Call Python script from Excel
module
function
19
User Defined Functions(UDF)
You can use custom functions
written in Python!
20
UDF returns multiple values to each cells
Using array formula
(Ctrl + Shift + Enter)
21
Case1-6: Download stock prices and
store in Excel Cells
xlwings features
Calling Python from Excel
Put pandas DataFrame data into Excel Cells
Uses syntax close to VBA
Get stock prices using pandas_datareader
22
Sample:
Case1-6.xlsm
Case 1-7: Create User Defined Functions
using Python, and use it in Excel
Windows only
Install add-in
Call function written in Python like an Excel function
Fetch Excel Range(multiple Cells) as array data(pandas or Numpy) in
UDF function
Input multiple return values into Excel Range(multiple Cells)
23
Sample:
Case1-7.xlsm
Don’t be afraid of time series
data
~Get used to pandas~
24
Why pandas?
Wes McKinney built pandas during his tenure at AQR(a quantitative
investment management firm)
Enable all these things in one place
Data structures with labeled axes supporting automatic or explicit data alignment
 Integrated time series functionality
One data structure to handle both time series and non-time series data
Arithmetic operations and reductions
Flexible handling of missing data
Merge and other relational operations found in popular database databases(SQL
based, for example)
Pandas is developed by financial specialists, so it is well suited to analyse
financial data
25
Case2-1: Use DatetimeIndex
pandas.date_range is very useful to create continuous data
Advantage of DatetimeIndex :
Specify various types when selecting a location
datetime.date, datetime.datetime, datetime.time, str, int and so on…
Able to parse most known formats(similar to parsing by dateutil.parser)
Allows slicing into year, month, etc
Handles missing values
Sample:
Case2-1_2.ipynb
26
Case2-2: Create OHLC data and covert
time range
Not that easy to create OHLC
Convert time-series data into frequencies using the .resample()
method
 .resample() performs resampling operations during frequency
conversion
Daily, Weekly, 30minute, 1hour, Quarter, etc
There are tips to convert between different OHLC data representations
Sample:
Case2-1_2.ipynb
27
Resampling image 1/4
100
99
102
105
102
103
105
106
104
102
Daily
Open High Low Close
100 105 99 102
Weekly
28
Resampling image 2/4
100
99
102
105
102
103
105
106
104
102
Daily
Open High Low Close
100 105 99 102
103 106 102 102
Weekly
29
Resampling image 3/4
100
99
102
105
102
103
105
106
104
102
Daily
Open High Low Close
100 105 99 102
103 106 102 102
101 102 97 98
98 100 107 105
106 110 106 108
109 115 107 112
110 120 110 115
113 117 110 115
110 111 102 103
100 101 94 96
Weekly
Open High Low Close
100 106 97 105
Monthly
30
Resampling image 4/4
100
99
102
105
102
103
105
106
104
102
Daily
Open High Low Close
100 105 99 102
103 106 102 102
101 102 97 98
98 100 107 105
106 110 106 108
109 115 107 112
110 120 110 115
113 117 110 115
110 111 102 103
100 101 94 96
Weekly
Open High Low Close
100 110 97 108
106 120 106 115
Monthly
31
Handling the last trading day of
derivatives
Exchange JPX
Products Futures, Options
last trading day The 2nd Friday of every month
* If the 2nd Friday is holiday, the
day before
http://www.jpx.co.jp/derivatives/r
ules/last-trading-day/
32
Example of last trading day
May 2017 – Aug 2017
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
May 1 2 3 4 5 6 Jul 7/1
7 8 9 19 11 12 13 2 3 4 5 6 7 8
14 15 16 17 18 19 20 9 10 11 12 13 14 15
21 22 23 24 25 26 27 16 17 18 19 20 21 22
28 29 30 31 23 24 25 26 27 28 29
Jun 6/1 2 3 30 31
4 5 6 7 8 9 10 Aug 1 2 3 4 5
11 12 13 14 15 16 17 6 7 8 9 10 11 12
18 19 20 21 22 23 24 13 14 15 16 17 18 19
25 26 27 28 29 30 20 21 22 23 24 25 26
27 28 29 30 31
33
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
May 1 2 3 4 5 6 Jul 7/1
7 8 9 19 11 12 13 2 3 4 5 6 7 8
14 15 16 17 18 19 20 9 10 11 12 13 14 15
21 22 23 24 25 26 27 16 17 18 19 20 21 22
28 29 30 31 23 24 25 26 27 28 29
Jun 6/1 2 3 30 31
4 5 6 7 8 9 10 Aug 1 2 3 4 5
11 12 13 14 15 16 17 6 7 8 9 10 11 12
18 19 20 21 22 23 24 13 14 15 16 17 18 19
25 26 27 28 29 30 20 21 22 23 24 25 26
27 28 29 30 31
Example of last trading day
May 2017 – Aug 2017
34
Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat
May 1 2 3 4 5 6 Jul 7/1
7 8 9 19 11 12 13 2 3 4 5 6 7 8
14 15 16 17 18 19 20 9 10 11 12 13 14 15
21 22 23 24 25 26 27 16 17 18 19 20 21 22
28 29 30 31 23 24 25 26 27 28 29
Jun 6/1 2 3 30 31
4 5 6 7 8 9 10 Aug 1 2 3 4 5
11 12 13 14 15 16 17 6 7 8 9 10 11 12
18 19 20 21 22 23 24 13 14 15 16 17 18 19
25 26 27 28 29 30 20 21 22 23 24 25 26
27 28 29 30 31
Example of last trading day
May 2017 – Aug 2017
35
Issues
Be aware of holidays
Picking the 2nd friday
36
Dealing with Japanese public holidays
pandas.tseries.holiday only supports US holidays(as of Sep 22nd, 2016)
It's possible ot create your own holiday rules by inheriting
AbstractHolidayCalendar, but...
Does not solve holidays such as Spring/Autumn Equinox
Instead, use CustomBusinessDay to implement individual holidays
Implement Japanese holidays in pandas using existing calendar data
37
Case2-3: Compute the last trading day
using the CustomBusinessDay class
Import holiday data from YAML file
Select the 2nd friday of evey month using
pandas.date_range(feq='WOM-2FRI')
Skip holidays using the CustomBusinessDay class
Sample:
Case2-3.ipynb
38
Using Jupyter Notebook
~ Don’t miss useful functions ~
Case3-1: Create own magic command
Search stock price using "line magic", and output it to
IPython.display.Iframe
Paste data in various formats into notebook cells using "cell magic" ,
and convert it into a pandas DataFrame
Save frequently used commands to a file and re-use them
using %load_ext
Sample:
Case3-1.ipynb
40
Case3-2: ipywidgets is the easiest way to
create a UI
Easy to implement a UI using the ipywidgets.interact decorator
Automatically creates UI controls for function arguments
bool: check box
Int: slider
Creates interactive visualization of moving averages and Bollinger-
Bands
Sample:
Case3-2.ipynb
41
Useful Nbextensions
Best installed using jupyter_contrib_nbextensions
https://github.com/ipython-contrib/jupyter_contrib_nbextensions
* Of course, it is possible to install Nbextention individually
Easy to enable/disable indiviudal extensions using the Nbextensions
edit menu
Create your own extensions using Javascript
42
Today’s summary
Python >>> Excel
Suitable for handling time series data
Easy to create commands and UI
43
Sample code and files
Sample code and excel files on Github
https://github.com/drillan/pyconjp2016
Some code is redundant due to:
Python 2/3 support
Offline mode
No license limitations
44
Thank you
See you next year?
45

Más contenido relacionado

Similar a PyCon JP 2016 Talk#024 en

Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri FontainePython and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri FontaineCitus Data
 
A Hacking Toolset for Big Tabular Files (3)
A Hacking Toolset for Big Tabular Files (3)A Hacking Toolset for Big Tabular Files (3)
A Hacking Toolset for Big Tabular Files (3)Toshiyuki Shimono
 
Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018Sam Clifford
 
Multi state churn analysis with a subscription product
Multi state churn analysis with a subscription productMulti state churn analysis with a subscription product
Multi state churn analysis with a subscription productVienna Data Science Group
 
Implementing the Split-Apply-Combine model in Clojure and Incanter
Implementing the Split-Apply-Combine model in Clojure and Incanter Implementing the Split-Apply-Combine model in Clojure and Incanter
Implementing the Split-Apply-Combine model in Clojure and Incanter Tom Faulhaber
 
Herve_Momo-TASS_25SEP2015
Herve_Momo-TASS_25SEP2015Herve_Momo-TASS_25SEP2015
Herve_Momo-TASS_25SEP2015Herve Momo
 
Managing your black Friday logs - CloudConf.IT
Managing your black Friday logs - CloudConf.ITManaging your black Friday logs - CloudConf.IT
Managing your black Friday logs - CloudConf.ITDavid Pilato
 
Advanced Topics in Agile Planning
Advanced Topics in Agile PlanningAdvanced Topics in Agile Planning
Advanced Topics in Agile PlanningMike Cohn
 
Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset
Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset
Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset Microsoft
 
Data Science applications in business
Data Science applications in businessData Science applications in business
Data Science applications in businessVladyslav Yakovenko
 
Analytycs fot iot_hen_we_tv1
Analytycs fot iot_hen_we_tv1Analytycs fot iot_hen_we_tv1
Analytycs fot iot_hen_we_tv1Roy Cecil
 
openTSDB - Metrics for a distributed world
openTSDB - Metrics for a distributed worldopenTSDB - Metrics for a distributed world
openTSDB - Metrics for a distributed worldOliver Hankeln
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.pptArumugam90
 
Revision booklet 6957 2016
Revision booklet 6957 2016Revision booklet 6957 2016
Revision booklet 6957 2016jom1987
 
Date dimension in your data warehouse
Date dimension in your data warehouseDate dimension in your data warehouse
Date dimension in your data warehouseDirk Cludts
 
Advanced Agile Planning - NDC 2014
Advanced Agile Planning - NDC 2014Advanced Agile Planning - NDC 2014
Advanced Agile Planning - NDC 2014Mike Cohn
 
Data Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing ItData Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing Itkanaugust
 
Skillshare - Let's talk about R in Data Journalism
Skillshare - Let's talk about R in Data JournalismSkillshare - Let's talk about R in Data Journalism
Skillshare - Let's talk about R in Data JournalismSchool of Data
 

Similar a PyCon JP 2016 Talk#024 en (20)

Utilizing power of R in financial sector
Utilizing power of R in financial sectorUtilizing power of R in financial sector
Utilizing power of R in financial sector
 
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri FontainePython and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
Python and PostgreSQL: Let's Work Together! | PyConFr 2018 | Dimitri Fontaine
 
A Hacking Toolset for Big Tabular Files (3)
A Hacking Toolset for Big Tabular Files (3)A Hacking Toolset for Big Tabular Files (3)
A Hacking Toolset for Big Tabular Files (3)
 
Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018Classes without Dependencies - UseR 2018
Classes without Dependencies - UseR 2018
 
Multi state churn analysis with a subscription product
Multi state churn analysis with a subscription productMulti state churn analysis with a subscription product
Multi state churn analysis with a subscription product
 
Implementing the Split-Apply-Combine model in Clojure and Incanter
Implementing the Split-Apply-Combine model in Clojure and Incanter Implementing the Split-Apply-Combine model in Clojure and Incanter
Implementing the Split-Apply-Combine model in Clojure and Incanter
 
Herve_Momo-TASS_25SEP2015
Herve_Momo-TASS_25SEP2015Herve_Momo-TASS_25SEP2015
Herve_Momo-TASS_25SEP2015
 
Managing your black Friday logs - CloudConf.IT
Managing your black Friday logs - CloudConf.ITManaging your black Friday logs - CloudConf.IT
Managing your black Friday logs - CloudConf.IT
 
Advanced Topics in Agile Planning
Advanced Topics in Agile PlanningAdvanced Topics in Agile Planning
Advanced Topics in Agile Planning
 
Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset
Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset
Den moderne dataplatform - gør din dataplatform til det mest værdifulde asset
 
Data Science applications in business
Data Science applications in businessData Science applications in business
Data Science applications in business
 
Analytycs fot iot_hen_we_tv1
Analytycs fot iot_hen_we_tv1Analytycs fot iot_hen_we_tv1
Analytycs fot iot_hen_we_tv1
 
openTSDB - Metrics for a distributed world
openTSDB - Metrics for a distributed worldopenTSDB - Metrics for a distributed world
openTSDB - Metrics for a distributed world
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
 
Revision booklet 6957 2016
Revision booklet 6957 2016Revision booklet 6957 2016
Revision booklet 6957 2016
 
Date dimension in your data warehouse
Date dimension in your data warehouseDate dimension in your data warehouse
Date dimension in your data warehouse
 
Advanced Agile Planning - NDC 2014
Advanced Agile Planning - NDC 2014Advanced Agile Planning - NDC 2014
Advanced Agile Planning - NDC 2014
 
Models
ModelsModels
Models
 
Data Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing ItData Wrangling: Working with Date / Time Data and Visualizing It
Data Wrangling: Working with Date / Time Data and Visualizing It
 
Skillshare - Let's talk about R in Data Journalism
Skillshare - Let's talk about R in Data JournalismSkillshare - Let's talk about R in Data Journalism
Skillshare - Let's talk about R in Data Journalism
 

Más de drillan

WindowsでPython
WindowsでPythonWindowsでPython
WindowsでPythondrillan
 
Pynyumon#4lt
Pynyumon#4ltPynyumon#4lt
Pynyumon#4ltdrillan
 
GmailとPythonでイベント管理
GmailとPythonでイベント管理GmailとPythonでイベント管理
GmailとPythonでイベント管理drillan
 
PyData.Tokyo Meetup #11 LT
PyData.Tokyo Meetup #11 LTPyData.Tokyo Meetup #11 LT
PyData.Tokyo Meetup #11 LTdrillan
 
Pyladies tokyo 2nd anniversary LT
Pyladies tokyo 2nd anniversary LTPyladies tokyo 2nd anniversary LT
Pyladies tokyo 2nd anniversary LTdrillan
 
Stapy#17LT
Stapy#17LTStapy#17LT
Stapy#17LTdrillan
 
PyCon JP 2016 Talk#024 ja
 PyCon JP 2016 Talk#024 ja PyCon JP 2016 Talk#024 ja
PyCon JP 2016 Talk#024 jadrillan
 
Pynyumon03 LT
Pynyumon03 LTPynyumon03 LT
Pynyumon03 LTdrillan
 

Más de drillan (8)

WindowsでPython
WindowsでPythonWindowsでPython
WindowsでPython
 
Pynyumon#4lt
Pynyumon#4ltPynyumon#4lt
Pynyumon#4lt
 
GmailとPythonでイベント管理
GmailとPythonでイベント管理GmailとPythonでイベント管理
GmailとPythonでイベント管理
 
PyData.Tokyo Meetup #11 LT
PyData.Tokyo Meetup #11 LTPyData.Tokyo Meetup #11 LT
PyData.Tokyo Meetup #11 LT
 
Pyladies tokyo 2nd anniversary LT
Pyladies tokyo 2nd anniversary LTPyladies tokyo 2nd anniversary LT
Pyladies tokyo 2nd anniversary LT
 
Stapy#17LT
Stapy#17LTStapy#17LT
Stapy#17LT
 
PyCon JP 2016 Talk#024 ja
 PyCon JP 2016 Talk#024 ja PyCon JP 2016 Talk#024 ja
PyCon JP 2016 Talk#024 ja
 
Pynyumon03 LT
Pynyumon03 LTPynyumon03 LT
Pynyumon03 LT
 

Último

(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Pooja 9892124323 : Call Girl in Juhu Escorts Service Free Home Delivery
Pooja 9892124323 : Call Girl in Juhu Escorts Service Free Home DeliveryPooja 9892124323 : Call Girl in Juhu Escorts Service Free Home Delivery
Pooja 9892124323 : Call Girl in Juhu Escorts Service Free Home DeliveryPooja Nehwal
 
00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptx00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptxFinTech Belgium
 
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779Delhi Call girls
 
Call Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...
TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...
TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...ssifa0344
 
Top Rated Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...
Top Rated  Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...Top Rated  Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...
Top Rated Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...Call Girls in Nagpur High Profile
 
The Economic History of the U.S. Lecture 26.pdf
The Economic History of the U.S. Lecture 26.pdfThe Economic History of the U.S. Lecture 26.pdf
The Economic History of the U.S. Lecture 26.pdfGale Pooley
 
Booking open Available Pune Call Girls Wadgaon Sheri 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Wadgaon Sheri  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Wadgaon Sheri  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Wadgaon Sheri 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )Pooja Nehwal
 
The Economic History of the U.S. Lecture 23.pdf
The Economic History of the U.S. Lecture 23.pdfThe Economic History of the U.S. Lecture 23.pdf
The Economic History of the U.S. Lecture 23.pdfGale Pooley
 
Indore Real Estate Market Trends Report.pdf
Indore Real Estate Market Trends Report.pdfIndore Real Estate Market Trends Report.pdf
Indore Real Estate Market Trends Report.pdfSaviRakhecha1
 
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptxFinTech Belgium
 
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...Pooja Nehwal
 
VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...
VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...
VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...Call Girls in Nagpur High Profile
 
The Economic History of the U.S. Lecture 20.pdf
The Economic History of the U.S. Lecture 20.pdfThe Economic History of the U.S. Lecture 20.pdf
The Economic History of the U.S. Lecture 20.pdfGale Pooley
 
The Economic History of the U.S. Lecture 22.pdf
The Economic History of the U.S. Lecture 22.pdfThe Economic History of the U.S. Lecture 22.pdf
The Economic History of the U.S. Lecture 22.pdfGale Pooley
 
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...Pooja Nehwal
 

Último (20)

(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(DIYA) Bhumkar Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Pooja 9892124323 : Call Girl in Juhu Escorts Service Free Home Delivery
Pooja 9892124323 : Call Girl in Juhu Escorts Service Free Home DeliveryPooja 9892124323 : Call Girl in Juhu Escorts Service Free Home Delivery
Pooja 9892124323 : Call Girl in Juhu Escorts Service Free Home Delivery
 
00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptx00_Main ppt_MeetupDORA&CyberSecurity.pptx
00_Main ppt_MeetupDORA&CyberSecurity.pptx
 
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
Best VIP Call Girls Noida Sector 18 Call Me: 8448380779
 
Call Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Maya Call 7001035870 Meet With Nagpur Escorts
 
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANIKA) Budhwar Peth Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...
TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...
TEST BANK For Corporate Finance, 13th Edition By Stephen Ross, Randolph Weste...
 
Top Rated Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...
Top Rated  Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...Top Rated  Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...
Top Rated Pune Call Girls Viman Nagar ⟟ 6297143586 ⟟ Call Me For Genuine Sex...
 
The Economic History of the U.S. Lecture 26.pdf
The Economic History of the U.S. Lecture 26.pdfThe Economic History of the U.S. Lecture 26.pdf
The Economic History of the U.S. Lecture 26.pdf
 
Booking open Available Pune Call Girls Wadgaon Sheri 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Wadgaon Sheri  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Wadgaon Sheri  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Wadgaon Sheri 6297143586 Call Hot Ind...
 
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
Vip Call US 📞 7738631006 ✅Call Girls In Sakinaka ( Mumbai )
 
The Economic History of the U.S. Lecture 23.pdf
The Economic History of the U.S. Lecture 23.pdfThe Economic History of the U.S. Lecture 23.pdf
The Economic History of the U.S. Lecture 23.pdf
 
Indore Real Estate Market Trends Report.pdf
Indore Real Estate Market Trends Report.pdfIndore Real Estate Market Trends Report.pdf
Indore Real Estate Market Trends Report.pdf
 
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
02_Fabio Colombo_Accenture_MeetupDora&Cybersecurity.pptx
 
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
Dharavi Russian callg Girls, { 09892124323 } || Call Girl In Mumbai ...
 
VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...
VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...
VVIP Pune Call Girls Katraj (7001035870) Pune Escorts Nearby with Complete Sa...
 
The Economic History of the U.S. Lecture 20.pdf
The Economic History of the U.S. Lecture 20.pdfThe Economic History of the U.S. Lecture 20.pdf
The Economic History of the U.S. Lecture 20.pdf
 
The Economic History of the U.S. Lecture 22.pdf
The Economic History of the U.S. Lecture 22.pdfThe Economic History of the U.S. Lecture 22.pdf
The Economic History of the U.S. Lecture 22.pdf
 
VIP Independent Call Girls in Bandra West 🌹 9920725232 ( Call Me ) Mumbai Esc...
VIP Independent Call Girls in Bandra West 🌹 9920725232 ( Call Me ) Mumbai Esc...VIP Independent Call Girls in Bandra West 🌹 9920725232 ( Call Me ) Mumbai Esc...
VIP Independent Call Girls in Bandra West 🌹 9920725232 ( Call Me ) Mumbai Esc...
 
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
Independent Call Girl Number in Kurla Mumbai📲 Pooja Nehwal 9892124323 💞 Full ...
 

PyCon JP 2016 Talk#024 en

  • 1. Start hacking finance data with Python driller@patraqushe PyConJP 2016 September 22, 2016
  • 3. Agenda Are you still exhausted from Excel? Don’t be afraid of time series data Using Jupyter Notebook 3
  • 4. Why Python for Finance? Analyzing data with simple code Substantial libraries(especially Deep leaning) pandas Jupyter Notebook Cooperating with: Scraping, crawling Web frame work Infrastructure 4
  • 5. Why Python in my case Developed trading tools using Excel Adding new functions and changing rules of exchange became gradually more difficult Python is the most manageable(in my opinion) Pandas is similar to Excel Drastically cheaper using Jupyter Notebooks 5
  • 6. Are you still exhausted from Excel? ~ Migrate from Excel to Python to improve productivity ~ 6
  • 7. Generate stock prices using Monte-Carlo simulation Stock price 1,000 yen time remaining until expiration 30 days risk-free interest rate 0.1% annual volatility of stock price 20% sample paths 10,000 -> 50,000 7
  • 8. Case1-1: Implement Monte-Carlo Simulation in Excel Function 1. Input formula into a Cell to generate random number with geometric Brownian motion(it satisfies the following stochastic differential equation) 𝑑𝑆𝑡 = 𝜇𝑆𝑡 𝑑𝑡 + 𝜎𝑆𝑡 𝐵𝑡 2. Copy above formula count of sample paths 3. Classify the result into bins 4. Count the number of each bins then visualize Sample: Case1-1_1-2.xlsm 8
  • 9. It's possible to implement Monte-Carlo Simulations using only mathematical formula, however: To increase cell for increasing sample paths To correspond existing cell for adding cells To become heavy and slow by recalculation 9
  • 10. Case1-2: Implement Monte-Carlo Simulation in VBA Very long code(especially histogram) When changing the layout of an Excel sheet, you have to change all the addresses of related cells(can handle by "name manager" partially) Very slow 10 Sample: Case1-1_1-2.xlsm
  • 11. Case1-3: Implement Monte-Carlo Simulation in Python Very short code(especially histogram) No need to consider data storage Faster than VBA Sample: Case1-3.ipynb 11
  • 12. Excel vs. Python Lines of Code: 105 Wall Time: 7.89s More complex Lines of Code: 10 Wall Time: 0.83s More simple 12
  • 13. Though, Excel has the advantage of… Easy to input Easy to create a template A huge number of users(high data compatibility) 13
  • 14. Python packages to work with Excel files xlrd xlwt XlsxWriter xlutils openpyxl xlwings ExcelPython 14
  • 15. There are many packages, but… pandas.read_excel() will almost always be what I want Use other packages for the operation that pandas cannot do Write data to an opened file Operate Cell Draw graphs… 15
  • 16. Case1-4: Relationship Economic indicator and exchange rate and stock price Open Economic indicator & Stock price Excel file @ vdata.nikkei.com through pandas Economic indicator Real Gross Domestic Product Diffusion Index Currency Pair : USD/JPY Stock: Nikkei Index Visualize by seaborn Sample: Case1-4.ipynb 16
  • 17. Case 1-5: Relationship ETF/J-REIT purchases and stock prices Open Excel files @ boj.or.jp through pandas Load TSE REIT index and stock Indices price data from k-db site Stock Indices TOPIX JPX400 Nikkei225 Visualize relationship using seaborn Sample: Case1-5.ipynb 17
  • 18. Use xlwings Read/Write open Excel files Supports Numpy and pandas data types Call Python script from Excel Write Excel User Defined Functions(UDF) in Python Use openpyxl for Cell & Chart operation 18
  • 19. Call Python script from Excel module function 19
  • 20. User Defined Functions(UDF) You can use custom functions written in Python! 20
  • 21. UDF returns multiple values to each cells Using array formula (Ctrl + Shift + Enter) 21
  • 22. Case1-6: Download stock prices and store in Excel Cells xlwings features Calling Python from Excel Put pandas DataFrame data into Excel Cells Uses syntax close to VBA Get stock prices using pandas_datareader 22 Sample: Case1-6.xlsm
  • 23. Case 1-7: Create User Defined Functions using Python, and use it in Excel Windows only Install add-in Call function written in Python like an Excel function Fetch Excel Range(multiple Cells) as array data(pandas or Numpy) in UDF function Input multiple return values into Excel Range(multiple Cells) 23 Sample: Case1-7.xlsm
  • 24. Don’t be afraid of time series data ~Get used to pandas~ 24
  • 25. Why pandas? Wes McKinney built pandas during his tenure at AQR(a quantitative investment management firm) Enable all these things in one place Data structures with labeled axes supporting automatic or explicit data alignment  Integrated time series functionality One data structure to handle both time series and non-time series data Arithmetic operations and reductions Flexible handling of missing data Merge and other relational operations found in popular database databases(SQL based, for example) Pandas is developed by financial specialists, so it is well suited to analyse financial data 25
  • 26. Case2-1: Use DatetimeIndex pandas.date_range is very useful to create continuous data Advantage of DatetimeIndex : Specify various types when selecting a location datetime.date, datetime.datetime, datetime.time, str, int and so on… Able to parse most known formats(similar to parsing by dateutil.parser) Allows slicing into year, month, etc Handles missing values Sample: Case2-1_2.ipynb 26
  • 27. Case2-2: Create OHLC data and covert time range Not that easy to create OHLC Convert time-series data into frequencies using the .resample() method  .resample() performs resampling operations during frequency conversion Daily, Weekly, 30minute, 1hour, Quarter, etc There are tips to convert between different OHLC data representations Sample: Case2-1_2.ipynb 27
  • 29. Resampling image 2/4 100 99 102 105 102 103 105 106 104 102 Daily Open High Low Close 100 105 99 102 103 106 102 102 Weekly 29
  • 30. Resampling image 3/4 100 99 102 105 102 103 105 106 104 102 Daily Open High Low Close 100 105 99 102 103 106 102 102 101 102 97 98 98 100 107 105 106 110 106 108 109 115 107 112 110 120 110 115 113 117 110 115 110 111 102 103 100 101 94 96 Weekly Open High Low Close 100 106 97 105 Monthly 30
  • 31. Resampling image 4/4 100 99 102 105 102 103 105 106 104 102 Daily Open High Low Close 100 105 99 102 103 106 102 102 101 102 97 98 98 100 107 105 106 110 106 108 109 115 107 112 110 120 110 115 113 117 110 115 110 111 102 103 100 101 94 96 Weekly Open High Low Close 100 110 97 108 106 120 106 115 Monthly 31
  • 32. Handling the last trading day of derivatives Exchange JPX Products Futures, Options last trading day The 2nd Friday of every month * If the 2nd Friday is holiday, the day before http://www.jpx.co.jp/derivatives/r ules/last-trading-day/ 32
  • 33. Example of last trading day May 2017 – Aug 2017 Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat May 1 2 3 4 5 6 Jul 7/1 7 8 9 19 11 12 13 2 3 4 5 6 7 8 14 15 16 17 18 19 20 9 10 11 12 13 14 15 21 22 23 24 25 26 27 16 17 18 19 20 21 22 28 29 30 31 23 24 25 26 27 28 29 Jun 6/1 2 3 30 31 4 5 6 7 8 9 10 Aug 1 2 3 4 5 11 12 13 14 15 16 17 6 7 8 9 10 11 12 18 19 20 21 22 23 24 13 14 15 16 17 18 19 25 26 27 28 29 30 20 21 22 23 24 25 26 27 28 29 30 31 33
  • 34. Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat May 1 2 3 4 5 6 Jul 7/1 7 8 9 19 11 12 13 2 3 4 5 6 7 8 14 15 16 17 18 19 20 9 10 11 12 13 14 15 21 22 23 24 25 26 27 16 17 18 19 20 21 22 28 29 30 31 23 24 25 26 27 28 29 Jun 6/1 2 3 30 31 4 5 6 7 8 9 10 Aug 1 2 3 4 5 11 12 13 14 15 16 17 6 7 8 9 10 11 12 18 19 20 21 22 23 24 13 14 15 16 17 18 19 25 26 27 28 29 30 20 21 22 23 24 25 26 27 28 29 30 31 Example of last trading day May 2017 – Aug 2017 34
  • 35. Sun Mon Tue Wed Thu Fri Sat Sun Mon Tue Wed Thu Fri Sat May 1 2 3 4 5 6 Jul 7/1 7 8 9 19 11 12 13 2 3 4 5 6 7 8 14 15 16 17 18 19 20 9 10 11 12 13 14 15 21 22 23 24 25 26 27 16 17 18 19 20 21 22 28 29 30 31 23 24 25 26 27 28 29 Jun 6/1 2 3 30 31 4 5 6 7 8 9 10 Aug 1 2 3 4 5 11 12 13 14 15 16 17 6 7 8 9 10 11 12 18 19 20 21 22 23 24 13 14 15 16 17 18 19 25 26 27 28 29 30 20 21 22 23 24 25 26 27 28 29 30 31 Example of last trading day May 2017 – Aug 2017 35
  • 36. Issues Be aware of holidays Picking the 2nd friday 36
  • 37. Dealing with Japanese public holidays pandas.tseries.holiday only supports US holidays(as of Sep 22nd, 2016) It's possible ot create your own holiday rules by inheriting AbstractHolidayCalendar, but... Does not solve holidays such as Spring/Autumn Equinox Instead, use CustomBusinessDay to implement individual holidays Implement Japanese holidays in pandas using existing calendar data 37
  • 38. Case2-3: Compute the last trading day using the CustomBusinessDay class Import holiday data from YAML file Select the 2nd friday of evey month using pandas.date_range(feq='WOM-2FRI') Skip holidays using the CustomBusinessDay class Sample: Case2-3.ipynb 38
  • 39. Using Jupyter Notebook ~ Don’t miss useful functions ~
  • 40. Case3-1: Create own magic command Search stock price using "line magic", and output it to IPython.display.Iframe Paste data in various formats into notebook cells using "cell magic" , and convert it into a pandas DataFrame Save frequently used commands to a file and re-use them using %load_ext Sample: Case3-1.ipynb 40
  • 41. Case3-2: ipywidgets is the easiest way to create a UI Easy to implement a UI using the ipywidgets.interact decorator Automatically creates UI controls for function arguments bool: check box Int: slider Creates interactive visualization of moving averages and Bollinger- Bands Sample: Case3-2.ipynb 41
  • 42. Useful Nbextensions Best installed using jupyter_contrib_nbextensions https://github.com/ipython-contrib/jupyter_contrib_nbextensions * Of course, it is possible to install Nbextention individually Easy to enable/disable indiviudal extensions using the Nbextensions edit menu Create your own extensions using Javascript 42
  • 43. Today’s summary Python >>> Excel Suitable for handling time series data Easy to create commands and UI 43
  • 44. Sample code and files Sample code and excel files on Github https://github.com/drillan/pyconjp2016 Some code is redundant due to: Python 2/3 support Offline mode No license limitations 44
  • 45. Thank you See you next year? 45