SlideShare uma empresa 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

Mais conteúdo relacionado

Semelhante a PyCon JP 2016 Talk#024 en

Herve_Momo-TASS_25SEP2015
Herve_Momo-TASS_25SEP2015Herve_Momo-TASS_25SEP2015
Herve_Momo-TASS_25SEP2015
Herve Momo
 
CS3114_09212011.ppt
CS3114_09212011.pptCS3114_09212011.ppt
CS3114_09212011.ppt
Arumugam90
 

Semelhante 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
 

Mais 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

Último (20)

Collecting banker, Capacity of collecting Banker, conditions under section 13...
Collecting banker, Capacity of collecting Banker, conditions under section 13...Collecting banker, Capacity of collecting Banker, conditions under section 13...
Collecting banker, Capacity of collecting Banker, conditions under section 13...
 
Dubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai Multiple
Dubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai MultipleDubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai Multiple
Dubai Call Girls Deira O525547819 Dubai Call Girls Bur Dubai Multiple
 
cost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxcost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptx
 
Vip Call Girls Rasulgada😉 Bhubaneswar 9777949614 Housewife Call Girls Servic...
Vip Call Girls Rasulgada😉  Bhubaneswar 9777949614 Housewife Call Girls Servic...Vip Call Girls Rasulgada😉  Bhubaneswar 9777949614 Housewife Call Girls Servic...
Vip Call Girls Rasulgada😉 Bhubaneswar 9777949614 Housewife Call Girls Servic...
 
Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024
 
20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...
20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...
20240419-SMC-submission-Annual-Superannuation-Performance-Test-–-design-optio...
 
Business Principles, Tools, and Techniques in Participating in Various Types...
Business Principles, Tools, and Techniques  in Participating in Various Types...Business Principles, Tools, and Techniques  in Participating in Various Types...
Business Principles, Tools, and Techniques in Participating in Various Types...
 
W.D. Gann Theory Complete Information.pdf
W.D. Gann Theory Complete Information.pdfW.D. Gann Theory Complete Information.pdf
W.D. Gann Theory Complete Information.pdf
 
7 steps to achieve financial freedom.pdf
7 steps to achieve financial freedom.pdf7 steps to achieve financial freedom.pdf
7 steps to achieve financial freedom.pdf
 
Pension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdfPension dashboards forum 1 May 2024 (1).pdf
Pension dashboards forum 1 May 2024 (1).pdf
 
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call GirlsKurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
Kurla Capable Call Girls ,07506202331, Sion Affordable Call Girls
 
Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...
Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...
Virar Best Sex Call Girls Number-📞📞9833754194-Poorbi Nalasopara Housewife Cal...
 
GIFT City Overview India's Gateway to Global Finance
GIFT City Overview  India's Gateway to Global FinanceGIFT City Overview  India's Gateway to Global Finance
GIFT City Overview India's Gateway to Global Finance
 
logistics industry development power point ppt.pdf
logistics industry development power point ppt.pdflogistics industry development power point ppt.pdf
logistics industry development power point ppt.pdf
 
Explore Dual Citizenship in Africa | Citizenship Benefits & Requirements
Explore Dual Citizenship in Africa | Citizenship Benefits & RequirementsExplore Dual Citizenship in Africa | Citizenship Benefits & Requirements
Explore Dual Citizenship in Africa | Citizenship Benefits & Requirements
 
✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
✂️ 👅 Independent Bhubaneswar Escorts Odisha Call Girls With Room Bhubaneswar ...
 
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
 
Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...
Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...
Famous No1 Amil Baba Love marriage Astrologer Specialist Expert In Pakistan a...
 
falcon-invoice-discounting-unlocking-prime-investment-opportunities
falcon-invoice-discounting-unlocking-prime-investment-opportunitiesfalcon-invoice-discounting-unlocking-prime-investment-opportunities
falcon-invoice-discounting-unlocking-prime-investment-opportunities
 
Strategic Resources May 2024 Corporate Presentation
Strategic Resources May 2024 Corporate PresentationStrategic Resources May 2024 Corporate Presentation
Strategic Resources May 2024 Corporate Presentation
 

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