0% found this document useful (0 votes)
89 views

#Big Data Analytics With Numpy in Python

The document outlines the syllabus for a course on Big Data Analytics in Finance using Python, covering topics such as business models of fintech, artificial intelligence in investment analysis, text mining, deep learning, and culminating in a final project presentation. The course is taught over 18 weeks on Thursday evenings at Tamkang University by Assistant Professor Min-Yuh Day and utilizes tools like Numpy, Pandas, Keras and TensorFlow for Python-based financial data analysis.

Uploaded by

Yulius
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

#Big Data Analytics With Numpy in Python

The document outlines the syllabus for a course on Big Data Analytics in Finance using Python, covering topics such as business models of fintech, artificial intelligence in investment analysis, text mining, deep learning, and culminating in a final project presentation. The course is taught over 18 weeks on Thursday evenings at Tamkang University by Assistant Professor Min-Yuh Day and utilizes tools like Numpy, Pandas, Keras and TensorFlow for Python-based financial data analysis.

Uploaded by

Yulius
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 123

Tamkang

財務金融大數據分析 University
Big Data Analytics in Finance
Python Numpy大數據分析
(Big Data Analytics with Numpy
in Python)
1061BDAF07
MIS EMBA (M2322) (8605)
Thu 12,13,14 (19:20-22:10) (D503)
Min-Yuh Day
戴敏育
Assistant Professor
專任助理教授
Dept. of Information Management, Tamkang University
淡江大學 資訊管理學系
http://mail. tku.edu.tw/myday/
2017-11-09 1
課程大綱 (Syllabus)
週次 (Week) 日期 (Date) 內容 (Subject/Topics)
1 2017/09/21 財務金融大數據分析課程介紹
(Course Orientation for Big Data Analytics in Finance)
2 2017/09/28 金融科技商業模式 (Business Models of Fintech)
3 2017/10/05 人工智慧投資分析與機器人理財顧問
(Artificial Intelligence for Investment Analysis and
Robo-Advisors)
4 2017/10/12 金融科技對話式商務與智慧型交談機器人
(Conversational Commerce and
Intelligent Chatbots for Fintech)
5 2017/10/19 事件研究法 (Event Study)
6 2017/10/26 財務金融大數據分析個案研究 I
(Case Study on Big Data Analytics in Finance I)
2
課程大綱 (Syllabus)
週次 (Week) 日期 (Date) 內容 (Subject/Topics)
7 2017/11/02 Python 財務大數據分析基礎
(Foundations of Finance Big Data Analytics in Python)
8 2017/11/09 Python Numpy大數據分析
(Big Data Analytics with Numpy in Python)
9 2017/11/16 Python Pandas 財務大數據分析
(Finance Big Data Analytics with Pandas in Python)
10 2017/11/23 期中報告 (Midterm Project Report)
11 2017/11/30 文字探勘分析技術與自然語言處理
(Text Mining Techniques and
Natural Language Processing)
12 2017/12/07 Python Keras深度學習
(Deep Learning with Keras in Python)
3
課程大綱 (Syllabus)
週次 (Week) 日期 (Date) 內容 (Subject/Topics)
13 2017/12/14 財務金融大數據分析個案研究 II
(Case Study on Big Data Analytics in Finance II)
14 2017/12/21 TensorFlow深度學習
(Deep Learning with TensorFlow)
15 2017/12/28 財務金融大數據深度學習
(Deep Learning for Finance Big Data)
16 2018/01/04 社會網絡分析 (Social Network Analysis)
17 2018/01/11 期末報告 I (Final Project Presentation I)
18 2018/01/18 期末報告 II (Final Project Presentation II)

4
Big Data Analytics
with

Numpy
in Python 5
Numpy
NumPy
Base
N-dimensional array
package
6
NumPy
is the
fundamental package
for
scientific computing
with Python.
Source: http://www.numpy.org/ 7
Wes McKinney (2012),
Python for Data Analysis: Data Wrangling with
Pandas, NumPy, and IPython, O'Reilly Media

Source: http://www.amazon.com/Python-Data-Analysis-Wrangling-IPython/dp/1449319793/ 8
The Quant Finance PyData Stack

Source: http://nbviewer.jupyter.org/format/slides/github/quantopian/pyfolio/blob/master/pyfolio/examples/overview_slides.ipynb#/5 9
NumPy

http://www.numpy.org/ 10
Source: https://www.python.org/community/logos/ 11
Python
12
Anaconda-Navigator

Launchpad

13
Anaconda-Navigator

14
Jupyter Notebook

15
Jupyter Notebook
New Python 3

16
print(“hello, world”)

17
Text input and output
print("Hello World")

print("Hello World\nThis is a message")

x = 3
print(x)

x = 2
y = 3
print(x, ' ', y)

name = input("Enter a name: ")

x = int(input("What is x? "))

x = float(input("Write a number "))


Source: http://pythonprogramminglanguage.com/text-input-and-output/ 18
Text input and output

Source: http://pythonprogramminglanguage.com/text-input-and-output/ 19
Variables
x = 2
price = 2.5
word = 'Hello'
word = 'Hello'
word = "Hello"
word = '''Hello'''

x = 2
x = x + 1
x = 5
Source: http://pythonprogramminglanguage.com/ 20
Python Basic Operators
print('7 + 2 =', 7 + 2)
print('7 - 2 =', 7 - 2)
print('7 * 2 =', 7 * 2)
print('7 / 2 =', 7 / 2)
print('7 // 2 =', 7 // 2)
print('7 % 2 =', 7 % 2)
print('7 ** 2 =', 7 ** 2)

21
BMI Calculator in Python

height_cm = float(input("Enter your height in cm: "))


weight_kg = float(input("Enter your weight in kg: "))

height_m = height_cm/100
BMI = (weight_kg/(height_m**2))

print("Your BMI is: " + str(round(BMI,1)))

Source: http://code.activestate.com/recipes/580615-bmi-code/ 22
BMI Calculator in Python

Source: http://code.activestate.com/recipes/580615-bmi-code/ 23
Future value
of a specified
principal amount,
rate of interest, and
a number of years
Source: https://www.w3resource.com/python-exercises/python-basic-exercise-39.php 24
Future Value (FV)

# How much is your $100 worth after 7 years?

print(100 * 1.1 ** 7)
# output = 194.87

Source: https://www.w3resource.com/python-exercises/python-basic-exercise-39.php 25
Future Value (FV)
pv = 100
r = 0.1
n = 7

fv = pv * ((1 + (r)) ** n)
print(round(fv, 2))

26
Future Value (FV)
amount = 100
interest = 10 #10% = 0.01 * 10
years = 7

future_value = amount * ((1 + (0.01 * interest)) ** years)


print(round(future_value, 2))

Source: https://www.w3resource.com/python-exercises/python-basic-exercise-39.php 27
if statements
> greater than
< smaller than
== equals
!= is not

score = 80
if score >=60 :
print("Pass")
else:
print("Fail")
Source: http://pythonprogramminglanguage.com/ 28
if elif else
score = 90 A
grade = ""
if score >=90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"
elif score >= 60:
grade = "D"
else:
grade = "E"
print(grade)
# grade = ”A”
http://pythontutor.com/visualize.html
https://goo.gl/E6w5ph
Source: http://pythonprogramminglanguage.com/ 29
for loops
for i in range(1,11):
print(i)
1
2
3
4
5
6
7
8
9
10
Source: http://pythonprogramminglanguage.com/ 30
for loops
for i in range(1,10):
for j in range(1,10):
print(i, ' * ' , j , ' = ', i*j)
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81

Source: http://pythonprogramminglanguage.com/ 31
while loops
age = 10 10
11
while age < 20: 12
13
print(age) 14
age = age + 1 15
16
17
18
19

Source: https://learnpython.trinket.io/learn-python-part-8-loops#/while-loops/about-while-loops 32
Functions
def convertCMtoM(xcm):
m = xcm/100
return m

cm = 180
m = convertCMtoM(cm)
print(str(m))
1.8
33
Lists
x = [60, 70, 80, 90]
print(len(x))
print(x[0])
print(x[1])
print(x[-1])
4
60
70
90
34
Tuples
A tuple in Python is a collection that
cannot be modified.
A tuple is defined using parenthesis.
x = (10, 20, 30, 40, 50)
print(x[0]) 10
print(x[1]) 20
print(x[2]) 30
print(x[-1]) 50
Source: http://pythonprogramminglanguage.com/tuples/ 35
Dictionary
k = { 'EN':'English', 'FR':'French' }
print(k['EN'])

English

Source: http://pythonprogramminglanguage.com/dictionary/ 36
Sets
animals = {'cat', 'dog'}

animals = {'cat', 'dog'}


print('cat' in animals)
print('fish' in animals)
animals.add('fish')
print('fish' in animals)
print(len(animals))
animals.add('cat')
print(len(animals))
animals.remove('cat')
print(len(animals))

Source: http://cs231n.github.io/python-numpy-tutorial/ 37
File Input / Output
with open('myfile.txt', 'w') as file:
file.write('Hello World\nThis is Python File Input )
Output'

with open('myfile.txt', 'r') as file:


text = file.read()
print(text)

Source: https://github.com/TiesdeKok/LearnPythonforResearch/blob/master/0_python_basics.ipynb 38
File Input / Output
with open('myfile.txt', 'a+') as file:
file.write('\n' + 'New line')

with open('myfile.txt', 'r') as file:


text = file.read()
print(text)

Source: https://github.com/TiesdeKok/LearnPythonforResearch/blob/master/0_python_basics.ipynb 39
Numpy
NumPy
Base
N-dimensional array
package
40
NumPy
NumPy
• NumPy provides a
multidimensional array object
to store homogenous or
heterogeneous data;
it also provides
optimized functions/methods to
operate on this array object.
Source: Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly 41
NumPy ndarray
One-dimensional Array
NumPy

(1-D Array)
0 1 n-1

1 2 3 4 5

Two-dimensional Array
(2-D Array)
0 1 n-1
0 1 2 3 4 5
1 6 7 8 9 10
11 12 13 14 15
m-1 16 17 18 19 20
42
NumPy
NumPy

v = range(1, 6)
print(v)
2 * v
import numpy as np
v = np.arange(1, 6)
v
2 * v
Source: Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly 43
NumPy
Base
N-dimensional
array package

44
NumPy
NumPy Create Array
import numpy as np
a = np.array([1, 2, 3])
b = np.array([4, 5, 6])
c = a * b
c

Source: Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly 45
NumPy
NumPy

Source: http://cs231n.github.io/python-numpy-tutorial/ 46
Numpy Quickstart Tutorial

https://docs.scipy.org/doc/numpy-dev/user/quickstart.html 47
import numpy as np
a = np.arange(15).reshape(3, 5)

a.shape
a.ndim
a.dtype.name

Source: https://docs.scipy.org/doc/numpy-dev/user/quickstart.html 48
Matrix

Source: https://simple.wikipedia.org/wiki/Matrix_(mathematics) 49
NumPy ndarray:
Multidimensional Array Object

50
NumPy ndarray
One-dimensional Array
(1-D Array)
0 1 n-1

1 2 3 4 5

Two-dimensional Array
(2-D Array)
0 1 n-1
0 1 2 3 4 5
1 6 7 8 9 10
11 12 13 14 15
m-1 16 17 18 19 20
51
import numpy as np
a = np.array([1,2,3,4,5])
One-dimensional Array
(1-D Array)
0 1 n-1

1 2 3 4 5

52
a = np.array([[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15],[16,17,18,19,20]])

Two-dimensional Array
(2-D Array)
0 1 n-1
0 1 2 3 4 5
1 6 7 8 9 10
11 12 13 14 15
m-1 16 17 18 19 20

53
import numpy as np
a = np.array([[0, 1, 2, 3],
[10, 11, 12, 13],
[20, 21, 22, 23]])
a
0 1 2 3
10 11 12 13
20 21 22 23
54
a = np.array ([[0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23]])

0 1 2 3
10 11 12 13
20 21 22 23
55
NumPy Basics:
Arrays and Vectorized
Computation

Source: https://www.safaribooksonline.com/library/view/python-for-data/9781449323592/ch04.html 56
NumPy Array

Source: https://www.safaribooksonline.com/library/view/python-for-data/9781449323592/ch04.html 57
Numpy Array

Source: https://www.safaribooksonline.com/library/view/python-for-data/9781449323592/ch04.html 58
Wes McKinney (2017), "Python for Data Analysis: Data Wrangling
with Pandas, NumPy, and IPython", 2nd Edition, O'Reilly Media.

https://github.com/wesm/pydata-book 59
Wes McKinney (2017), "Python for Data Analysis: Data Wrangling
with Pandas, NumPy, and IPython", 2nd Edition, O'Reilly Media.

Source: https://github.com/wesm/pydata-book/blob/2nd-edition/ch04.ipynb 60
Python
Pandas for
Finance 61
pandas

http://pandas.pydata.org/ 62
pandas
Python Data Analysis
Library
providing high-performance, easy-to-use
data structures and data analysis tools
for the Python programming language.
Source: http://pandas.pydata.org/ 63
Jupyter Notebook New Python 3

64
Creating pd.DataFrame

a b c
1 4 7 10
2 5 8 11
3 6 9 12

df = pd.DataFrame({"a": [4, 5, 6],


"b": [7, 8, 9],
"c": [10, 11, 12]},
index = [1, 2, 3])

Source: https://github.com/pandas-dev/pandas/blob/master/doc/cheatsheet/Pandas_Cheat_Sheet.pdf
65
Pandas DataFrame

type(df)

66
conda install pandas-datareader

67
Jupyter Notebook New Python 3

68
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
print('Hello Pandas')

s = pd.Series([1,3,5,np.nan,6,8])
s

dates = pd.date_range('20170301',
periods=6)
dates
Source: http://pandas.pydata.org/pandas-docs/stable/10min.html 69
70
df = pd.DataFrame(np.random.randn(6,4),
index=dates, columns=list('ABCD'))
df

71
df = pd.DataFrame(np.random.randn(4,6),
index=['student1','student2','student3',
'student4'], columns=list('ABCDEF'))
df

72
df2 = pd.DataFrame({ 'A' : 1.,
'B' : pd.Timestamp('20170322'),
'C' : pd.Series(2.5,index=list(range(4)),dtype='float32'),
'D' : np.array([3] * 4,dtype='int32'),
'E' : pd.Categorical(["test","train","test","train"]),
'F' : 'foo' })
df2

73
df2.dtypes

74
Yahoo Finance Symbols: AAPL
Apple Inc. (AAPL)

http://finance.yahoo.com/q?s=AAPL 75
Apple Inc. (AAPL) -NasdaqGS

http://finance.yahoo.com/quote/AAPL?p=AAPL 76
Yahoo Finance Charts: Apple Inc. (AAPL)

http://finance.yahoo.com/chart/AAPL 77
Apple Inc. (AAPL) Historical Data

http://finance.yahoo.com/q/hp?s=AAPL+Historical+Prices 78
Yahoo Finance Historical Prices
Apple Inc. (AAPL)

http://finance.yahoo.com/quote/AAPL/history 79
Yahoo Finance Historical Prices
Apple Inc. (AAPL)

http://finance.yahoo.com/quote/AAPL/history?period1=345398400&period2=1490112000&interval=1d&filter=history&frequency=1d 80
Yahoo Finance Historical Prices
Apple Inc. (AAPL)

http://finance.yahoo.com/quote/AAPL/history?period1=345398400&period2=1490112000&interval=1d&filter=history&frequency=1d 81
Yahoo Finance Historical Prices
http://ichart.finance.yahoo.com/table.csv?s=AAPL

table.csv Date,Open,High,Low,Close,Volume,Adj Close


2017-03-21,142.110001,142.800003,139.729996,139.839996,39116800,139.839996
2017-03-20,140.399994,141.50,140.229996,141.460007,20213100,141.460007
2017-03-17,141.00,141.00,139.889999,139.990005,43597400,139.990005
2017-03-16,140.720001,141.020004,140.259995,140.690002,19132500,140.690002
2017-03-15,139.410004,140.75,139.029999,140.460007,25566800,140.460007
2017-03-14,139.300003,139.649994,138.839996,138.990005,15189700,138.990005
2017-03-13,138.850006,139.429993,138.820007,139.199997,17042400,139.199997
2017-03-10,139.25,139.360001,138.639999,139.139999,19488000,139.139999
2017-03-09,138.740005,138.789993,137.050003,138.679993,22065200,138.679993
2017-03-08,138.949997,139.800003,138.820007,139.00,18681800,139.00
2017-03-07,139.059998,139.979996,138.789993,139.520004,17267500,139.520004
2017-03-06,139.369995,139.770004,138.600006,139.339996,21155300,139.339996
2017-03-03,138.779999,139.830002,138.589996,139.779999,21108100,139.779999
2017-03-02,140.00,140.279999,138.759995,138.960007,26153300,138.960007
2017-03-01,137.889999,140.149994,137.600006,139.789993,36272400,139.789993
2017-02-28,137.080002,137.440002,136.699997,136.990005,23403500,136.990005
2017-02-27,137.139999,137.440002,136.279999,136.929993,20196400,136.929993
2017-02-24,135.910004,136.660004,135.279999,136.660004,21690900,136.660004
2017-02-23,137.380005,137.479996,136.300003,136.529999,20704100,136.529999
2017-02-22,136.429993,137.119995,136.110001,137.110001,20745300,137.110001

82
Yahoo Finance Charts
Alphabet Inc. (GOOG)

Alphabet Inc. (GOOG)

http://finance.yahoo.com/echarts?s=GOOG+Interactive#{"showArea":false,"showLine":false,"showCandle":true,"lineType":"candle","range":"5y","allowChartStacking":true} 83
Dow Jones Industrial Average
(^DJI)

http://finance.yahoo.com/chart/^DJI 84
TSEC weighted index (^TWII) -
Taiwan

http://finance.yahoo.com/chart/^DJI 85
Taiwan Semiconductor Manufacturing Company Limited
(2330.TW)

http://finance.yahoo.com/q?s=2330.TW 86
Yahoo Finance Charts
TSMC (2330.TW)

87
import pandas as pd
import pandas_datareader.data as web
df = web.DataReader('AAPL', data_source='yahoo',
start='1/1/2010', end='3/21/2017')
df.to_csv('AAPL.csv')
df.tail()

88
df = web.DataReader('GOOG',
data_source='yahoo', start='1/1/1980',
end='3/21/2017')
df.head(10)

89
df.tail(10)

90
df.count()

91
df.ix['2015-12-31']

92
df.to_csv('2330.TW.Yahoo.Finance.Data.csv')

93
Python Pandas
for Finance
Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 94
Python Pandas for Finance
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
%matplotlib inline

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 95
Python Pandas for Finance
#Read Stock Data from Yahoo Finance
end = dt.datetime.now()
#start = dt.datetime(end.year-2, end.month, end.day)
start = dt.datetime(2015, 1, 1)
df = web.DataReader("AAPL", 'yahoo', start, end)
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 96
Finance Data from Quandl
import quandl
df = quandl.get("WIKI/AAPL", start_date="2015-01-01", end_date="2017-10-31" )
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()

Source: https://www.quandl.com/tools/python 97
Python Pandas for Finance
df['Adj Close'].plot(legend=True,
figsize=(12, 8), title='AAPL', label='Adj
Close')

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 98
Python Pandas for Finance
plt.figure(figsize=(12,9))
top = plt.subplot2grid((12,9), (0, 0),
rowspan=10, colspan=9)
bottom = plt.subplot2grid((12,9), (10,0),
rowspan=2, colspan=9)
top.plot(df.index, df['Adj Close'],
color='blue') #df.index gives the dates
bottom.bar(df.index, df['Volume'])

# set the labels


top.axes.get_xaxis().set_visible(False)
top.set_title('AAPL')
top.set_ylabel('Adj Close')
bottom.set_ylabel('Volume')
Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 99
Python Pandas for Finance

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 100


Python Pandas for Finance

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 101


Python Pandas for Finance
plt.figure(figsize=(12,9))
sns.distplot(df['Adj Close'].dropna(),
bins=50, color='purple')

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 102


Python Pandas for Finance
# simple moving averages
df['MA05'] = df['Adj Close'].rolling(5).mean()
df['MA20'] = df['Adj Close'].rolling(20).mean() #20 days
df['MA60'] = df['Adj Close'].rolling(60).mean() #60 days

df2 = pd.DataFrame({'Adj Close': df['Adj


Close'],'MA05': df['MA05'],'MA20':
df['MA20'], 'MA60': df['MA60']})
df2.plot(figsize=(12, 9), legend=True,
title='AAPL')
df2.to_csv('AAPL_MA.csv')
fig = plt.gcf()
fig.set_size_inches(12, 9)
fig.savefig(’AAPL_plot.png', dpi=300)
plt.show()

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 103


Python Pandas for Finance

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 104


Python Pandas for Finance

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 105


import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
%matplotlib inline

#Read Stock Data from Yahoo Finance


end = dt.datetime.now()
#start = dt.datetime(end.year-2, end.month, end.day)
start = dt.datetime(2015, 1, 1)
df = web.DataReader("AAPL", 'yahoo', start, end)
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()

df['Adj Close'].plot(legend=True, figsize=(12, 8), title='AAPL', label='Adj Close')


plt.figure(figsize=(12,9))
top = plt.subplot2grid((12,9), (0, 0), rowspan=10, colspan=9)
bottom = plt.subplot2grid((12,9), (10,0), rowspan=2, colspan=9)
top.plot(df.index, df['Adj Close'], color='blue') #df.index gives the dates
bottom.bar(df.index, df['Volume'])

# set the labels


top.axes.get_xaxis().set_visible(False)
top.set_title('AAPL')
top.set_ylabel('Adj Close')
bottom.set_ylabel('Volume')

plt.figure(figsize=(12,9))
sns.distplot(df['Adj Close'].dropna(), bins=50, color='purple')

# simple moving averages


df['MA05'] = df['Adj Close'].rolling(5).mean() #5 days
df['MA20'] = df['Adj Close'].rolling(20).mean() #20 days
df['MA60'] = df['Adj Close'].rolling(60).mean() #60 days
df2 = pd.DataFrame({'Adj Close': df['Adj Close'],'MA05': df['MA05'],'MA20': df['MA20'], 'MA60': df['MA60']})
df2.plot(figsize=(12, 9), legend=True, title='AAPL')
df2.to_csv('AAPL_MA.csv')
fig = plt.gcf()
fig.set_size_inches(12, 9)
fig.savefig(’AAPL_plot.png', dpi=300)
plt.show()
Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 106
Examples:
Python Pandas
for Finance
107
108
sSymbol = "AAPL"
#sSymbol = "GOOG"
#sSymbol = "IBM"
#sSymbol = "MSFT"
#sSymbol = "^TWII"
#sSymbol = "000001.SS"
#sSymbol = "2330.TW"
#sSymbol = "2317.TW"

# sURL = "http://ichart.finance.yahoo.com/table.csv?s=AAPL"
# sBaseURL = "http://ichart.finance.yahoo.com/table.csv?s="
sURL = "http://ichart.finance.yahoo.com/table.csv?s=" + sSymbol
#req = requests.get("http://ichart.finance.yahoo.com/table.csv?s=2330.TW")
#req = requests.get("http://ichart.finance.yahoo.com/table.csv?s=AAPL")
req = requests.get(sURL)

sText = req.text
#print(sText)
#df = web.DataReader(sSymbol, 'yahoo', starttime, endtime)
#df = web.DataReader("2330.TW", 'yahoo')

sPath = "data/"
sPathFilename = sPath + sSymbol + ".csv"
print(sPathFilename)

f = open(sPathFilename, 'w')
f.write(sText)
f.close()
sIOdata = io.StringIO(sText)
df = pd.DataFrame.from_csv(sIOdata)
df.head(5)
109
110
df.tail(5)

111
sSymbol = "AAPL”

# sURL = "http://ichart.finance.yahoo.com/table.csv?s=AAPL"
sURL = "http://ichart.finance.yahoo.com/table.csv?s=" + sSymbol
#req = requests.get("http://ichart.finance.yahoo.com/table.csv?s=AAPL")

req = requests.get(sURL)
sText = req.text
#print(sText)

sPath = "data/"
sPathFilename = sPath + sSymbol + ".csv"
print(sPathFilename)

f = open(sPathFilename, 'w')
f.write(sText)
f.close()
sIOdata = io.StringIO(sText)
df = pd.DataFrame.from_csv(sIOdata)
df.head(5)

112
113
114
def getYahooFinanceData(sSymbol, starttime, endtime, sDir):
#GetMarketFinanceData_From_YahooFinance
#"^TWII"
#"000001.SS"
#"AAPL"
#SHA:000016"
#"600000.SS"
#"2330.TW"
#sSymbol = "^TWII"
starttime = datetime.datetime(2000, 1, 1)
endtime = datetime.datetime(2015, 12, 31)
sPath = sDir
#sPath = "data/financedata/"
df_YahooFinance = web.DataReader(sSymbol, 'yahoo', starttime, endtime)
#df_01 = web.DataReader("2330.TW", 'yahoo')
sSymbol = sSymbol.replace(":","_")
sSymbol = sSymbol.replace("^","_")
sPathFilename = sPath + sSymbol + "_Yahoo_Finance.csv"
df_YahooFinance.to_csv(sPathFilename)
#df_YahooFinance.head(5)
return sPathFilename
#End def getYahooFinanceData(sSymbol, starttime, endtime, sDir):

115
116
sSymbol = "AAPL”
starttime = datetime.datetime(2000, 1, 1)
endtime = datetime.datetime(2015, 12, 31)
sDir = "data/financedata/"

sPathFilename = getYahooFinanceData(sSymbol, starttime, endtime,


sDir)
print(sPathFilename)

117
import pandas as pd
import pandas_datareader.data as web
import matplotlib.pyplot as plt
import seaborn as sns
import datetime as dt
%matplotlib inline

#Read Stock Data from Yahoo Finance


end = dt.datetime.now()
#start = dt.datetime(end.year-2, end.month, end.day)
start = dt.datetime(2015, 1, 1)
df = web.DataReader("AAPL", 'yahoo', start, end)
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()

df['Adj Close'].plot(legend=True, figsize=(12, 8), title='AAPL', label='Adj Close')


plt.figure(figsize=(12,9))
top = plt.subplot2grid((12,9), (0, 0), rowspan=10, colspan=9)
bottom = plt.subplot2grid((12,9), (10,0), rowspan=2, colspan=9)
top.plot(df.index, df['Adj Close'], color='blue') #df.index gives the dates
bottom.bar(df.index, df['Volume'])

# set the labels


top.axes.get_xaxis().set_visible(False)
top.set_title('AAPL')
top.set_ylabel('Adj Close')
bottom.set_ylabel('Volume')

plt.figure(figsize=(12,9))
sns.distplot(df['Adj Close'].dropna(), bins=50, color='purple')

# simple moving averages


df['MA05'] = df['Adj Close'].rolling(5).mean() #5 days
df['MA20'] = df['Adj Close'].rolling(20).mean() #20 days
df['MA60'] = df['Adj Close'].rolling(60).mean() #60 days
df2 = pd.DataFrame({'Adj Close': df['Adj Close'],'MA05': df['MA05'],'MA20': df['MA20'], 'MA60': df['MA60']})
df2.plot(figsize=(12, 9), legend=True, title='AAPL')
df2.to_csv('AAPL_MA.csv')
fig = plt.gcf()
fig.set_size_inches(12, 9)
fig.savefig(’AAPL_plot.png', dpi=300)
plt.show()
Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 118
Python Pandas for Finance

Source: https://mapattack.wordpress.com/2017/02/12/using-python-for-stocks-1/ 119


The Quant Finance PyData Stack

Source: http://nbviewer.jupyter.org/format/slides/github/quantopian/pyfolio/blob/master/pyfolio/examples/overview_slides.ipynb#/5 120


Quantopian

https://www.quantopian.com/ 121
References
• Wes McKinney (2012), Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython,
O'Reilly Media
• Yves Hilpisch (2014), Python for Finance: Analyze Big Financial Data, O'Reilly
• Yves Hilpisch (2015), Derivatives Analytics with Python: Data Analysis, Models, Simulation,
Calibration and Hedging, Wiley
• Michael Heydt (2015) , Mastering Pandas for Finance, Packt Publishing
• Michael Heydt (2015), Learning Pandas - Python Data Discovery and Analysis Made Easy, Packt
Publishing
• James Ma Weiming (2015), Mastering Python for Finance, Packt Publishing
• Fabio Nelli (2015), Python Data Analytics: Data Analysis and Science using PANDAs, matplotlib and
the Python Programming Language, Apress
• Wes McKinney (2013), 10-minute tour of pandas, https://vimeo.com/59324550
• Jason Wirth (2015), A Visual Guide To Pandas, https://www.youtube.com/watch?v=9d5-Ti6onew
• Edward Schofield (2013), Modern scientific computing and big data analytics in Python, PyCon
Australia, https://www.youtube.com/watch?v=hqOsfS3dP9w
• Python Programming, https://pythonprogramming.net/
• Python, https://www.python.org/
• Python Programming Language, http://pythonprogramminglanguage.com/
• Numpy, http://www.numpy.org/
• Pandas, http://pandas.pydata.org/
122
References
• Wes McKinney (2017), "Python for Data Analysis: Data Wrangling with
Pandas, NumPy, and IPython", 2nd Edition, O'Reilly Media.
https://github.com/wesm/pydata-book
• Avinash Jain (2017), Introduction To Python Programming, Udemy,
https://www.udemy.com/pythonforbeginnersintro/
• Alfred Essa (2015), Awesome Data Science: 1.0 Jupyter Notebook Tour,
https://www.youtube.com/watch?v=e9cSF3eVQv0
• Ties de Kok (2017), Learn Python for Research,
https://github.com/TiesdeKok/LearnPythonforResearch
• Ivan Idris (2015), Numpy Beginner's Guide, Third Edition, Packt Publishing
• Numpy Tutorial, https://docs.scipy.org/doc/numpy-dev/user/quickstart.html

123

You might also like