0% found this document useful (0 votes)
71 views253 pages

02 Phan Tich Dau Tu Nang Cao - Co So Ve Lap Trinh Python

The document discusses how Python has become hugely popular in finance and why it is well suited for financial applications and analysis. It covers several key areas where Python is used in finance, including trading systems, hedge funds, quant finance, startups, and more. It also provides an overview of the Python ecosystem for data science and financial applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views253 pages

02 Phan Tich Dau Tu Nang Cao - Co So Ve Lap Trinh Python

The document discusses how Python has become hugely popular in finance and why it is well suited for financial applications and analysis. It covers several key areas where Python is used in finance, including trading systems, hedge funds, quant finance, startups, and more. It also provides an overview of the Python ecosystem for data science and financial applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 253

Bài 2 – Phân tích và Quản lý đầu tư nâng cao

Cơ sở lập trình Python trong phân tích


đầu tư

TS. Đỗ Như Tài


Đại Học Kinh Tế TP.HCM
[email protected]
NỘI DUNG

Why Python is huge in Finance? 1

Python Programming 2
Foundations of Investment Analysis in Python 3
3

Why Python is huge in


Finance?
Finance is …

Asset
Banking Insurance
Management

https://www.youtube.com/watch?v=kBwOy-6CtAQ
5

Finance is Big larg part of economy impacts your life

S&P 500 - a stock market index tracking the stock performance of 500 of the largest companies listed
on stock exchanges in the United States
6

Why should I care about finance?

In 2004-2014, it accounted
• Lost of interesting jobs for 12.5% of all employment
income and 26.1% of all
corporare profits

• B2B customers
Worldwide spending on
information technology (IT) by
financial services, $440 billion
• Your personal finances in 2018. And growing…
7

Areas where Python is used


8

Trading Systems
Finance Information System
• Front Arena
• Huge & complex – multi
• Python embedded since 1999
million line, PhD Math
• Used for business logic and
customization
• Expensive, $2000 • Courses to certify 1000+ in
USD/user & month range Python based development

• Need to be customized
9

Hedge Funds / Quants

Advanced trading strategies

Workflow
• Generate ideas
• Evaluate
• Execute

Very, very data heavy


10

Ways to be fast
11

You can try out the quant life


Crowd sourced algo trading

Both Python powered


12

Open Bank
We use Python to quickly develop new
Machine Learning models that power our
data-driven products
• Optimize for fast experimentation and
model development, Python enables
very fast iteration cycles
• Offer most support for ML purposes
• Enable our data scientists to build
complex model while providing a low
entry barrier
• Share code between training and
serving ML models in production.
13

Startups
We need to…
All your Investments,
Connect and glue data
Savings and Pensions Python + Requestes +
in one app ‘batteries’

Manage complex data model


PostgreSQL + Django

Server APIs
Django + Channels / Sanic

Fast Risk Calculations


Numpy + Pandas
Celery
Python is …
• Fast to learn
• Designed to be easy to read and learn
• Early competition was, C++/Java/VB & Perl
• Now, great education resources. It is ok to expect people to ‘pick it up’

• Batteries & Glue


• Easy to just get tings done
• Perfect Glue language (Banks need a lot of glue…)

• Native libs / embedding


• C++ traditionaly used for speed sensitive code: dead simple to drop into
Python, Speed behind Numpy etc.
• Embed Python in other applications: Like VBA for your C++ monolith
enterprise application
• Now: Cython, Numba
15

The Quant Finance PyData Stack

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

Yves Hilpisch (2020), Artificial Intelligence in Finance: A Python-Based Guide, O’Reilly

Source: https://github.com/yhilpisch/aiif

https://github.com/yhilpisch/aiif
17

Yves Hilpisch (2020), Artificial Intelligence in Finance: A Python-Based Guide, O’Reilly

Source: https://github.com/yhilpisch/aiif/tree/main/code

https://github.com/yhilpisch/aiif/tree/main/code
18

Python in Google Colab


19

Python in Google Colab


20

Python in Google Colab


21

Python in Google Colab


22

Python in Google Colab


23

Python in Google Colab


24

Python in Google Colab


25

Python in Google Colab


26

Python in Google Colab


27

Python in Google Colab


28

Python in Google Colab


29

Python in Google Colab


30

Python in Google Colab


31

Python in Google Colab


32

Python in Google Colab


33

Python in Google Colab

33
34

Python in Google Colab

34
35

Python in Google Colab


36

Python Programming
37

Top Programming Languages

https://spectrum.ieee.org/the-top-programming-languages-2023
38

Python is an
interpreted,
object-oriented,
high-level
programming language
with
dynamic semantics.
Source: https://www.python.org/doc/essays/blurb/
39

Python Ecosystem for Data Science

Source: https://medium.com/pyfinance/why-python-is-best-choice-for-financial-data-modeling-in-2019-c0d0d1858c45
40

Python Ecosystem for Data Science

Source:https://duchesnay.github.io/pystatsml/introduction/python_ecosystem.html
41

The Quant Finance PyData Stack

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

Numpy
NumPy
Base
N-dimensional array
package
43

Python
matplotlib

Source: https://matplotlib.org/
44

Python
Pandas

http://pandas.pydata.org/
45

W3Schools Python

https://www.w3schools.com/python/
46

W3Schools Python

https://www.w3schools.com/python/
47

W3Schools Python

https://www.w3schools.com/python/
48

W3Schools Python: Try Python

https://www.w3schools.com/python/trypython.asp?filename=demo_default
49

LearnPython.org

https://www.learnpython.org/
50

Google’s Python Class

https://developers.google.com/edu/python
51

Google Colab

https://colab.research.google.com/notebooks/welcome.ipynb
52

Connect Google Colab in Google Drive


53

Google Colab
54

Google Colab
55

Connect Colaboratory to Google Drive


56

Google Colab
57

Google Colab
58

Google Colab
59

Run Jupyter Notebook

Python3 GPU
Google Colab
60

Google Colab Python Hello World

print('Hello World')
61

Python in Google Colab


62

Python
Programming
63

Python Hello World

print("Hello World")

print("Hello World")

63
64

Python Syntax

# comment

# comment

64
65

Python Syntax

Indentation
the spaces at the beginning of a code line
4 spaces

score = 80
if score >=60 :
print("Pass")

65
66

Python Variables

# Python Variables
x = 2
price = 2.5
word = 'Hello'

word = 'Hello'
word = "Hello"
word = '''Hello'''
67

Python Variables

x = 2
y = x + 1

67
68

python_version()

# comment
from platform import python_version
print("Python Version:", python_version())

Python Version: 3.10.12


69

Python Data Types

x = "Hello World" #str


x = 2 #int
x = 2.5 #float
x = 7j #complex

69
70

Python Data Types

x = ["apple", "banana", "cherry"] #list


x = ("apple", "banana", "cherry") #tuple
x = range(6) #range
x = {"name" : "Tom", "age" : 20} #dict
x = {"apple", "banana", "cherry"} #set
x =
frozenset({"apple", "banana", "cherry"})
#frozenset

70
71

Python Data Types

x = True #bool
x = b"Hello" #bytes
x = bytearray(5) #bytearray
x = memoryview(bytes(5)) #memoryview
x = None #NoneType

71
72

Python Casting

x = str(3) # x will be '3'


y = int(3) # y will be 3
z = float(3) # z will be 3.0
print(x, type(x))
print(y, type(y))
print(z, type(z))
3 <class 'str’>
3 <class 'int’>
3.0 <class 'float'>

72
73

Python Numbers

x = 2 # int
y = 3.4 # float
z = 7j #complex
print(x, type(x))
print(y, type(y))
print(z, type(z))
2 <class 'int’>
3.4 <class 'float’>
7j <class 'complex'>

73
74

Python Arithmetic Operators

Operator Name Example


+ Addition 7 + 2 = 9
- Subtraction 7 - 2 = 5
* Multiplication 7 * 2 = 14
/ Division 7 / 2 = 3.5
// Floor division 7 // 2 = 3 (Quotient)
% Modulus 7 % 2 = 1 (Remainder)
** Exponentiation 7 ** 2 = 49

74
75

Python Basic Operators

print('7 + 2 =', 7 + 2) 7 + 2 = 9
print('7 - 2 =', 7 - 2) 7 - 2 = 5
print('7 * 2 =', 7 * 2) 7 * 2 = 14
print('7 / 2 =', 7 / 2) 7 / 2 = 3.5
print('7 // 2 =', 7 // 2) 7 // 2 = 3
print('7 % 2 =', 7 % 2) 7 % 2 = 1
print('7 ** 2 =', 7 ** 2) 7 ** 2 = 49

75
76

Python Booleans: True or False

# Python Booleans: True or False


print(3 > 2)
print(3 == 2)
print(3 < 2)

76
77

Python BMI Calculator

# BMI Calculator in Python


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

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

Your BMI is: 20.8

77
78

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
79

How much is your $100 worth after 7 years?

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


fv = 100 * 1.1 ** 7
print('fv = ', round(fv, 2))
# output = 194.87

fv = 194.87

79
80

Future Value

# Future Value
pv = 100
r = 0.1
n = 7
fv = pv * ((1 + (r)) ** n)
print(round(fv, 2))

194.87

80
81

Future Value

# Future Value
amount = 100
interest = 10 #10% = 0.01 * 10
years = 7

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


print(round(future_value, 2))
194.87

81
82

Summary
• Python Syntax
• Python Comments
• Python Variables
• Python Data Types
• Python Numbers
• Python Casting
• Python Strings
• Python Operators
• Python Booleans
83

Python
Data Structures
84

Python Data Types

x = ["apple", "banana", "cherry"] #list


x = ("apple", "banana", "cherry") #tuple
x = {"name" : "Tom", "age" : 20} #dict
x = {"apple", "banana", "cherry"} #set

84
85

Python Collections
• There are four collection data types in the Python programming
language
• List []
• a collection which is ordered and changeable. Allows duplicate
members.
• Tuple ()
• a collection which is ordered and unchangeable. Allows duplicate
members.
• Set {}
• a collection which is unordered, unchangeable, and unindexed. No
duplicate members.
• Dictionary {k:v}
• a collection which is ordered and changeable. No duplicate
members.

85
86

Python Dictionaries {k:v}


• As of Python version 3.7, dictionaries are ordered.
• In Python 3.6 and earlier, dictionaries are unordered.
87

Lists []

x = [60, 70, 80, 90]


print(len(x)) 4
print(x[0]) 60
print(x[1]) 70
print(x[-1]) 90
88

Lists []
• len(): how many items
• type(): data type
• list() constructor: creating a new list
89

Python List Methods


• Method Description
• append() Adds an element at the end of the list
• clear() Removes all the elements from the list
• copy() Returns a copy of the list
• count() Returns the number of elements with the specified value
• extend() Add the elements of a list (or any iterable), to the end of the
current list
• index() Returns the index of the first element with the specified value
• insert() Adds an element at the specified position
• pop() Removes the element at the specified position
• remove() Removes the item with the specified value
• reverse() Reverses the order of the list
• sort() Sorts the list
https://www.w3schools.com/python/python_lists_methods.asp
90

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/
91

Sets {}

animals = {'cat', 'dog'}


print('cat' in animals) True
print('fish' in animals) False
animals.add('fish')
print('fish' in animals) True
print(len(animals)) 3
animals.add('cat’)
print(len(animals)) 3
animals.remove('cat')
print(len(animals)) 2
Source: http://cs231n.github.io/python-numpy-tutorial/
92

Dictionary {key : value}

Python Dictionary
Key  Value
'EN’  'English’
'FR’  'French'

k = { 'EN':'English', 'FR':'French' }
print(k['EN'])
English

Source: http://pythonprogramminglanguage.com/dictionary/
93

Python Data Structures

fruits = ["apple", "banana", "cherry"] #lists []


colors = ("red", "green", "blue") #tuples ()
animals = {'cat', 'dog'} #sets {}
person = {"name" : "Tom", "age" : 20} #dictionaries {}
94

Python for Finance Applications

# Python Lists
expenses = [72.50, 80.75, 50.00, 90.25]
total_expenses = sum(expenses)
print("Total expenses:", total_expenses)
Total expenses: 293.5
95

Python for Finance Applications

# Python Tuples
accounts = (("Cash", 1001), ("Accounts Receivable", 1002),
("Inventory", 1003))
for account in accounts:
print("Account name:", account[0], "Account number:", account[1])

Account name: Cash Account number: 1001


Account name: Accounts Receivable Account number: 1002
Account name: Inventory Account number: 1003
96

Python for Finance Applications

# Python Sets
account_numbers = {1001, 1002, 1003}
new_account_number = 1004
if new_account_number not in account_numbers:
print("Account number", new_account_number, "is not in use.")

Account number 1004 is not in use.


97

Python for Finance Applications

# Python Dictionaries
accounts = {"1001": {"name": "Cash", "balance": 500.00, "type": "Asset"},
"1002": {"name": "Accounts Receivable", "balance": 1000.00, "type": "Asset"},
"2001": {"name": "Accounts Payable", "balance": 750.00, "type": "Liability"}}
for account_number, account_info in accounts.items():
print("Account number:", account_number)
print("Account name:", account_info["name"])
print("Account balance:", account_info["balance"])
print("Account type:", account_info["type"])
Account number: 1001
Account name: Cash Account number: 2001
Account balance: 500.0
Account type: Asset Account name: Accounts Payable
Account
Account
number: 1002
name: Accounts Receivable
Account balance: 750.0
Account
Account
balance: 1000.0
type: Asset
Account type: Liability
98

Summary
• Python Data Structures
• Python Lists []

• Python Tuples ()

• Python Sets {}

• Python Dictionaries {k:v}


99

Python
Control Logic
and
Loops
100

Python Control Logic and Loops


• Python if else
• if elif else
• Booleans: True, False
• Operators: ==, !=, >, <, >=, <=, and, or, not
• Python for Loops
• for
• Python while Loops
• While
• break
• continue
101

Python if...else
• Python if...else
• if elif else
• Booleans: True, False
• Operators: ==, !=, >, <, >=, <=, and, or, not
102

Python Conditions and If statements


• Python supports the usual logical conditions from mathematics:
• Equals: a == b
• Not Equals: a != b
• Less than: a < b
• Less than or equal to: a <= b
• Greater than: a > b
• Greater than or equal to: a >= b
103

Python Comparison Operators


Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x>y
< Less than x<y
>= Greater than or equal to x >= y
<= Less than or equal to x <= y

Source: https://www.w3schools.com/python/python_operators.asp
104

Python Logical Operators


Operator Description Example
Returns True if both
and x < 5 and x < 10
statements are true

Returns True if one of the


or x < 5 or x < 4
statements is true

Reverse the result, returns


not not(x < 5 and x < 10)
False if the result is true

Source: https://www.w3schools.com/python/python_operators.asp
105

Python if

# Python if
score = 80
if score >=60 :
print("Pass")
106

Python if else

# Python if else
score = 80
if score >=60 :
print("Pass")
else:
print("Fail")
107

Python if elif else

score = 95
if score >= 90 :
print("A")
elif score >=60 :
print("Pass")
else:
print("Fail")
108

Python if elif else

# Python if elif else


score = 90
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)
109

Python for Loops

for i in range(1,6):
print(i)
1
2
3
4
5
110

Python for loops

# for loops
for i in range(1,10):
for j in range(1,10):
print(i, ' * ' , j , ' = ', i*j)
111

Python while Loops


• while
• break
• continue
112

Python while loops

# while loops
age = 10
while age < 20:
print(age)
age = age + 1
113

Summary
• Python if else
• if elif else
• Booleans: True, False
• Operators: ==, !=, >, <, >=, <=, and, or, not
• Python for Loops
• for
• Python while Loops
• while
• break
• continue
114

Python
Functions
115

Python Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
• Creating a Function
• In Python a function is defined using the def keyword:
116

Python Function def

# Python Function def


# indentation for blocks. four spaces
def getfv(pv, r, n):
fv = pv * ((1 + (r)) ** n)
return fv
fv = getfv(100, 0.1, 7)
print(round(fv, 2))
194.87
117

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
118

How much is your $100 worth after 7 years?

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


fv = 100 * 1.1 ** 7
print('fv = ', round(fv, 2))
# output = 194.87
fv = 194.87
119

Future Value

# Future Value
pv = 100
r = 0.1
n = 7
fv = pv * ((1 + (r)) ** n)
print(round(fv, 2))

194.87
120

Future Value

# Future Value
amount = 100
interest = 10 #10% = 0.01 * 10
years = 7

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


print(round(future_value, 2))
194.87
121

Python Function

def getfv() define get future value function

# Python Function def


# indentation for blocks. four spaces
def getfv(pv, r, n):
fv = pv * ((1 + (r)) ** n)
return fv
fv = getfv(100, 0.1, 7)
print(round(fv, 2))
194.87
122

Python
Classes/Objects
class MyClass:
123

Python Classes/Objects
• Python is an object oriented programming language.
• Almost everything in Python is an object, with its properties and methods.
• A Class is like an object constructor, or a "blueprint" for creating objects.
• Create a Class:
• To create a class, use the keyword class:
Python Classes/Objects
124

class MyClass:

# Python class
class MyClass:
x = 5

c1 = MyClass()
print(c1.x)

https://www.w3schools.com/python/python_classes.asp
125

Python Classes/Objects

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

p1 = Person("Alan", 20)

print(p1.name) Alan
print(p1.age) 20
126

Python Classes/Objects

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)

p1 = Person("Alan", 20)
p1.myfunc()
127

Python Classes/Objects

class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def myfunc(self):
print("Hello my name is " + self.name)

p1 = Person("Alan", 20)
p1.myfunc() Hello my name is Alan
print(p1.name) Alan
print(p1.age) 20
128

Python Classes and Obects

class Vehicle:
name = ""
kind = "car"
color = ""
value = 100.00
def description(self):
desc_str = "%s is a %s %s worth $%.2f." %
(self.name, self.color, self.kind, self.value)
return desc_str
129

Python Classes and Objects

car1 = Vehicle() class Vehicle:


car1.name = "Fer" name = ""
car1.color = "red" kind = "car"
car1.kind = "convertible" color = ""
value = 100.00
car1.value = 60000.00
def description(self):
desc_str = "%s is a %s %s
car2 = Vehicle() worth $%.2f." % (self.name, self.color,
car2.name = "Jump" self.kind, self.value)
car2.color = "blue" return desc_str
car2.kind = "van"
car2.value = 10000.00

print(car1.description()) Fer is a red convertible worth $60000.00.


print(car1.name) Fer
Jump is a blue van worth $10000.00.
print(car2.description()) Jump
print(car2.name)
https://www.learnpython.org/en/Classes_and_Objects
130

Python
Modules
131

Python Modules
• Consider a module to be the same as a code library.
• A file containing a set of functions you want to include in your application.
• Create a Module
• To create a module just save the code you want in a file with the file extension .py:
• Use a Module
• import module
132

Python Modules

# mymodule.py
def greeting(name):
print("Hello, " + name)

import mymodule
mymodule.greeting("Alan")
mymodule.py
def greeting(name):
print("Hello, " + name)
133

Python File Input / Output

# Python 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)

Hello World This is Python File Input Output


134

Python File Input / Output

# Python File Input / Output


filename = 'mymodule.py'
with open(filename, 'w') as file:
text = '''def greeting(name):
print("Hello, " + name)
'''
file.write(text)

with open(filename, 'r') as file:


text = file.read()
print(filename)
print(text)
mymodule.py
def greeting(name):
print("Hello, " + name)
135

Python Modules

import mymodule

# mymodule.py
def greeting(name):
print("Hello, " + name)

import mymodule
mymodule.greeting("Alan")

Hello, Alan
136

Python main() function

#Python main() function


def main():
print("Hello World!")

if __name__ == "__main__":
main()
137

Summary
• Python Functions
• def myfunction():
• Python Classes/Objects
• class MyClass:
• Python Modules
• mymodule.py
• import mymodule
138

Files
and
Exception Handling
139

Files and Exception Handling


• Python Files (File Handling)
• open()

• f = open("myfile.txt")

• Python Try Except (Exception Handling)


• try:
except:
else:
finally:
140

File Handling
• The key function for working with files in Python is the open() function.
• The open() function takes two parameters; filename, and mode.
• There are four different methods (modes) for opening a file:
• "r" - Read - Default value. Opens a file for reading, error if the file does not exist
• "a" - Append - Opens a file for appending, creates the file if it does not exist
• "w" - Write - Opens a file for writing, creates the file if it does not exist
• "x" - Create - Creates the specified file, returns an error if the file exists
141

Python Files (File Handling)

f = open("myfile.txt", "w")
f.write("Hello World")
f.close()

f = open("myfile.txt", "r")
text = f.read()
print(text)
f.close()
Hello World
142

Python Files (File Handling)

# Python File Input / Output


with open('myfile.txt', 'w') as file:
file.write('Hello World')

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


text = file.read()
print(text)

Hello World
143

Python Files

# Python File Input / Output


with open('myfile.txt', 'w') as file:
file.write('Hello World\nPython File IO')

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


text = file.read()
print(text)
Hello World
Python File IO
144

Python Files

# Python 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)
Hello World
Python File IO
New line
145

Python Files

# !ls list files


!ls

myfile.txt sample_data
146

Python OS, IO, files, and Google Drive

import os

cwd = os.getcwd()
print(cwd)

/content
147

os.listdir()

os.listdir(cwd)

['.config',
'myfile.txt',
'sample_data']
148

os.path.join()

path = os.path.join(cwd, 'sample_data')


print(path)
os.listdir(path)

/content/sample_data
['README.md', 'anscombe.json',
'mnist_train_small.csv',
'mnist_test.csv',
'california_housing_train.csv',
'california_housing_test.csv']
149

from google.colab import files

from google.colab import files

with open('io_file_myday.txt', 'w') as f:


f.write('Google Colab File Write Text some content Myday')

import time
time.sleep(1) # time sleep 1 second

files.download('io_file_myday.txt')
print('downloaded')

downloaded
150

Python Files

from google.colab import files


uploaded = files.upload()

for fn in uploaded.keys():
print('User uploaded file "{name}"
with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))

User uploaded file "io_file_myday2.txt" with length 47 bytes


151

os.remove()

import os
if os.path.exists("myfile.txt"):
os.remove("myfile.txt")
print("myfile.txt removed")
else:
print("The file does not exist")

myfile.txt removed
152

os.mkdir("myfolder1")
os.rmdir("myfolder1")
import os
os.listdir()
os.mkdir("myfolder1")
os.listdir()
os.rmdir("myfolder1")
os.listdir()
153

Python Try Except


• The try block lets you test a block of code for errors.
• The except block lets you handle the error.
• The else block lets you execute code when there is no error.
• The finally block lets you execute code, regardless of the
result of the try- and except blocks.
154

Python Try Except (Exception Handling)

try: except:

#Python try except


try:
print(x)
except:
print("Exception Error")
155

Python try: except: finally:

#Python try except finally


try:
print("Hello")
except:
print("Exception Error")
finally:
print("Finally process")
Hello
Finally process
156

Python try: except: else:

#Python try except else


try:
print("Hello")
except:
print("Exception Error")
else:
print("No exception")
Hello
No exception
157

Python try: except: else: finally:

try:
print("Hello")
except:
print("Exception Error")
else:
print("No exception")
finally:
print("Finally process")
Hello
No exception
Finally process
158

Python try: except: else: finally:

try:
price = float(input("Enter the price of the stock (e.g. 10):"))
shares = int(input("Enter the number of shares (e.g. 2):"))
total = price * shares
except Exception as e:
print("Exception error:", str(e))
else:
print("The total value of the shares is:", total)
finally:
print("Thank you.")
Enter the price of the stock (e.g. 10):10
Enter the number of shares (e.g. 2):2
The total value of the shares is: 20.0
Thank you.
159

Python try: except: else: finally:

try:
file = open("myfile.txt")
file.write("Python write file")
print("file saved")
except:
print("Exception file Error")

Exception file Error


160

Python try: except: else: finally:

try:
file = open("myfile.txt")
file.write("Python write file")
print("file saved")
except:
print("Exception file Error")
finally:
file.close()
print("Finally process")
Exception file Error
Finally process
161

Python try: except: else: finally:

try:
file = open("myfile.txt", 'w’)
file.write("Python write file")
print("file saved")
except:
print("Exception file Error")
finally:
file.close()
print("Finally process")
file saved
Finally process
162

Summary
• Python Files (File Handling)
• open()

• f = open("myfile.txt")

• Python Try Except (Exception Handling)


• try:
except:
else:
finally:
163

Foundations of
Investment Analysis
in Python
Outline
164

• Numpy
•Scientific computing
• Pandas
• Data structures and data analysis
tools
165
The Quant Finance PyData Stack

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

Python is an
interpreted,
object-oriented,
high-level
programming language
with
dynamic semantics.
Source: https://www.python.org/doc/essays/blurb/
169

Google Colab

https://colab.research.google.com/notebooks/welcome.ipynb
Python in Google Colab (Python101) 170

https://colab.research.google.com/drive/1FEG6DnGvwfUbeo4zJ1zTunjMqf2RkCrT

https://tinyurl.com/imtkupython101
Numpy
171

NumPy
Base
N-dimensional array
package
Python
172

matplotlib

Source: https://matplotlib.org/
173

Python
Pandas

http://pandas.pydata.org/
Iris flower data set
174

setosa versicolor virginica

Source: https://en.wikipedia.org/wiki/Iris_flower_data_set
Source: http://suruchifialoke.com/2016-10-13-machine-learning-tutorial-iris-classification/
Iris Classfication
175

Source: http://suruchifialoke.com/2016-10-13-machine-learning-tutorial-iris-classification/
176
iris.data
https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data
5.1,3.5,1.4,0.2,Iris-setosa setosa
4.9,3.0,1.4,0.2,Iris-setosa
4.7,3.2,1.3,0.2,Iris-setosa
4.6,3.1,1.5,0.2,Iris-setosa
5.0,3.6,1.4,0.2,Iris-setosa
5.4,3.9,1.7,0.4,Iris-setosa
4.6,3.4,1.4,0.3,Iris-setosa
5.0,3.4,1.5,0.2,Iris-setosa
4.4,2.9,1.4,0.2,Iris-setosa
4.9,3.1,1.5,0.1,Iris-setosa
5.4,3.7,1.5,0.2,Iris-setosa virginica
4.8,3.4,1.6,0.2,Iris-setosa
4.8,3.0,1.4,0.1,Iris-setosa
4.3,3.0,1.1,0.1,Iris-setosa
5.8,4.0,1.2,0.2,Iris-setosa
5.7,4.4,1.5,0.4,Iris-setosa
5.4,3.9,1.3,0.4,Iris-setosa
5.1,3.5,1.4,0.3,Iris-setosa
5.7,3.8,1.7,0.3,Iris-setosa versicolor
5.1,3.8,1.5,0.3,Iris-setosa
5.4,3.4,1.7,0.2,Iris-setosa
5.1,3.7,1.5,0.4,Iris-setosa
4.6,3.6,1.0,0.2,Iris-setosa
5.1,3.3,1.7,0.5,Iris-setosa
4.8,3.4,1.9,0.2,Iris-setosa
5.0,3.0,1.6,0.2,Iris-setosa
177
Iris Data Visualization

Source: https://seaborn.pydata.org/generated/seaborn.pairplot.html
178
Connect Google Colab in Google Drive
179
Google Colab
180
Google Colab
181
Connect Colaboratory to Google Drive
182
Google Colab
183
Google Colab
184
Google Colab
185

Run Jupyter Notebook


Python3 GPU
Google Colab
186

Google Colab Python Hello World


print('Hello World')
187
Data Visualization in Google Colab
https://colab.research.google.com/drive/1KRqtEUd2Hg4dM2au9bfVQKrxWnWN3O9-

Source: https://seaborn.pydata.org/generated/seaborn.pairplot.html
import seaborn as sns 188
sns.set(style="ticks", color_codes=True)
iris = sns.load_dataset("iris")
g = sns.pairplot(iris, hue="species")

Source: https://seaborn.pydata.org/generated/seaborn.pairplot.html
https://colab.research.google.com/drive/1KRqtEUd2Hg4dM2au9bfVQKrxWnWN3O9- 189

import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.plotting import scatter_matrix

# Load dataset
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
df = pd.read_csv(url, names=names)

print(df.head(10))
print(df.tail(10))
print(df.describe())
print(df.info())
print(df.shape)
print(df.groupby('class').size())

plt.rcParams["figure.figsize"] = (10,8)
df.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
plt.show()

df.hist()
plt.show()

scatter_matrix(df)
plt.show()

sns.pairplot(df, hue="class", size=2)


Source: https://machinelearningmastery.com/machine-learning-in-python-step-by-step/
190
import numpy as np
import pandas as pd
%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
from pandas.plotting import scatter_matrix
191

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'class']
df = pd.read_csv(url, names=names)
print(df.head(10))
df.tail(10)
192
df.describe()
193
194

print(df.info())
print(df.shape)
195

df.groupby('class').size()
196
plt.rcParams["figure.figsize"] = (10,8)
df.plot(kind='box', subplots=True, layout=(2,2), sharex=False, sharey=False)
plt.show()
df.hist()
197

plt.show()
scatter_matrix(df)
198

plt.show()
199
sns.pairplot(df, hue="class", size=2)
200

Anaconda
The Most Popular
Python
Data Science Platform
Source: https://www.anaconda.com/
201

Download Anaconda

https://www.anaconda.com/download
202

HelloWorld
Anaconda-Navigator
203

Launchpad
Anaconda Navigator
204
Jupyter Notebook
205
Jupyter Notebook 206

New Python 3
207

print("hello, world")
208

from platform import python_version


print("Python Version:", python_version())
209

Big Data Analytics


with
Numpy
in Python
Numpy
210

NumPy
Base
N-dimensional array
package
211

NumPy
is the
fundamental package
for
scientific computing
with Python.
Source: http://www.numpy.org/
212

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
213

NumPy

http://www.numpy.org/
214
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
215

NumPy
NumPy

v = list(range(1, 6))
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
216

NumPy
Base
N-dimensional
array package
217

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
218

NumPy
NumPy

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

https://docs.scipy.org/doc/numpy-dev/user/quickstart.html
import numpy as np
220

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
221

Matrix

Source: https://simple.wikipedia.org/wiki/Matrix_(mathematics)
222

NumPy ndarray:
Multidimensional Array Object
223
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
import numpy as np
224

a = np.array([1,2,3,4,5])
One-dimensional Array
(1-D Array)
0 1 n-1

1 2 3 4 5
225

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
226

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
227

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
228

NumPy Basics:
Arrays and Vectorized
Computation

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

NumPy Array

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

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

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

Source: https://github.com/wesm/pydata-book/blob/2nd-edition/ch04.ipynb
233

Python
Pandas
234

Python Pandas
for Finance

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

! pip install pandas_datareader


236

conda install pandas-datareader


237
238

Finance Data from Yahoo Finance

# !pip install pandas_datareader


import pandas_datareader.data as web
import datetime as dt
#Read Stock Data from Yahoo Finance
end = dt.datetime(2017, 12, 31)
start = dt.datetime(2016, 1, 1)
df = web.DataReader("AAPL", 'yahoo', start, end)
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()
239

# !pip install pandas_datareader


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(2016, 1, 1)
df = web.DataReader("AAPL", 'yahoo', start, end)
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()
240
df['Adj Close'].plot(legend=True, figsize=(12,
8), title='AAPL’, label='Adj Close')
241
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'])
242
243

# 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')
244
245

# 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()
246
247
# !pip install pandas_datareader
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(2016, 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()
248
Finance Data from Quandl
249

# ! pip install quandl


import quandl
# quandl.ApiConfig.api_key = "YOURAPIKEY"
df = quandl.get("WIKI/AAPL", start_date="2016-01-
01", end_date="2017-12-31")
df.to_csv('AAPL.csv')
df.from_csv('AAPL.csv')
df.tail()

Source: https://www.quandl.com/tools/python
250
The Quant Finance PyData Stack

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

• Foundations of
AI Investment Analysis in Python
• Python
• Programming language
• Numpy
• Scientific computing
• Pandas
• Data structures and data analysis
tools
252

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
• Ties de Kok (2017), Learn Python for Research,
https://github.com/TiesdeKok/LearnPythonforResearch
• Avinash Jain (2017), Introduction To Python Programming, Udemy,
https://www.udemy.com/pythonforbeginnersintro/
• 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/
• Skikit-learn, http://scikit-learn.org/
• Data School (2015), Machine learning in Python with scikit-learn,
https://www.youtube.com/playlist?list=PL5-
da3qGB5ICeMbQuqbbCOQWcS6OYBr5A
• Jason Brownlee (2016), Your First Machine Learning Project in Python Step-By-Step,
https://machinelearningmastery.com/machine-learning-in-python-step-by-step/
253

CÁM ƠN ĐÃ LẮNG
NGHE!

You might also like