Python For Trading

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

PYTHON for TRADING

2
PYTHON for TRADING

Contents
Part – 1 PYTHON CONCEPTS .................................................................................................................. 5
................................................................................................................................. 6
................................................................................................................................ 7
........................................................................................................................................ 8
........................................................................................................................................ 9
......................................................................................................................... 9
............................................................................................................................ 10
......................................................................................................................... 10
................................................................................................................................... 11
................................................................................................................................ 11
....................................................... 13
.......................................................................................................................................... 14
......................................................................................................................................... 16
................................................................................................................................. 16
........................................................................................................ 18
.................................................................................................................................... 18
...................................................................................................................................... 21
Part -2 DATA STRUCTURE ..................................................................................................................... 22
............................................................................................................................... 23
................................................................................................................................ 23
......................................................................................................................................... 23
2.4 Dictionaries { } ............................................................................................................................. 25
...................................................................................................................................... 26
.............................................................................................................................................. 28
.................................................................................................................................. 29
.................................................................................................................................... 29
Part - 3................................................................................................................................................... 30
................................................................................................................................ 31
................................................................................................................................ 31
............................................................................................................... 31
..................................................................................................................................... 31
........................................................................................................................... 32
........................................................................................................... 34

3
PYTHON for TRADING

..................................................................................................................................... 35
..................................................................................................................... 35
......................................................................................................... 36
........................................................................................................................................ 39
............................................................................................................................... 39
........................................................................................................... 40
......................................................................................................................... 40
................................................................................................................................... 40
............................................................................................................................ 40
.......................................................................................................................... 41
.................................................................................................. 41
................................................................. 41
........................................................................................................................ 41
…..…………………..……………………………………………………………………... 44

4
PYTHON for TRADING

Part – 1 PYTHON CONCEPTS

5
PYTHON for TRADING

6
PYTHON for TRADING

7
PYTHON for TRADING

length = 2
breadth = 3
area = length*breadth
print(area)

Output
>>>
6

# defining a function
def ex_loop():
#The loop starts with an indentation
for i in [1, ‘Gold’, ‘Silver’]:
# The statement in the loop starts with an indentation as
well
print i

# calling the function


ex_loop()

Output
1
Gold
Silver

a = 3.25

b = 3.20

8
PYTHON for TRADING

#defining a function to calculate the bid-ask spread


def calc_spread(bid,ask): #function takes bid and ask prices as
input arguments
spd = ?ask - bid
return spd #function returns spd (bid-ask
spread)

#defining a function to calculate mid price


def calc_midPrice(?,?): bid,ask
mid_price = (bid+ask)/2
return ?mid_price

#defining a function to print values, takes no input arguments


? print_all():def
#name "s" refers to the function to calculate bid-ask spread
s = calc_spread(b,a)
p = ? calc_midPrice(b,a)
for i in [s,p]: #using for loop to print spread and mid-
price
print ? i

#calling the print function


print_all()

9
PYTHON for TRADING



10
PYTHON for TRADING



11
PYTHON for TRADING

x = 10
x = {1,2,3}
x = ‘Hello QuantInsti’

def func():
print(‘Buy Gold’)

x = func
x()

output
>>>
Buy Gold

12
PYTHON for TRADING

 o
o
o
o

o o

o o
o o

o o

o o
o o

13
PYTHON for TRADING

Input

i=1
def foo():

14
PYTHON for TRADING

i=5
print(i, 'in foo()')
print(i, 'global')
foo()

Output

1 global
5 in foo()



15
PYTHON for TRADING

<statement-1>

. . .

<statement-N>

16
PYTHON for TRADING

17
PYTHON for TRADING

18
PYTHON for TRADING

19
PYTHON for TRADING

20
PYTHON for TRADING

21
PYTHON for TRADING

Part -2 Data Structure

22
PYTHON for TRADING

MUTABLE IMMUTABLE
Lists Strings
Sets Tuples
Frozen Sets

Let us now look at each in detail:

List a mutable object, is like a one dimensional array. There are several methods associated
with, some of them are listed below:

append(): Items can be added to the list simply by using append()

23
PYTHON for TRADING

remove(): Items can be deleted from the list using remove()

for loop: Iterate through the list using “for” loop.

If: Searching for an item in a list is also very simple, you can check if an item is present in a
list by using the keyword “if” as shown.

list[:] Lists can be sliced using the below command

list[first index:last index:step]

Index(): searches for an element in the list and returns the “index”

24
PYTHON for TRADING

Insert(): inserts an element at the desired position/index in a list

Pop(): removes the last element of the list

Similarly you can try out functions like ‘sort’, ‘count’, ‘reverse’ etc. and see the results.

2.4 Dictionaries { }

25
PYTHON for TRADING

26
PYTHON for TRADING

Use len(), to find the “length” of a tuple

27
PYTHON for TRADING

28
PYTHON for TRADING

29
PYTHON for TRADING

Part - 3

30
PYTHON for TRADING

Data Analysis (handling & manipulation) Libraries

31
PYTHON for TRADING

s = pd.Series(data, index=index)







32
PYTHON for TRADING

Viewing data frames:

Transposing values:

33
PYTHON for TRADING

Sorting by value/index:

# drops rows with NA

34
PYTHON for TRADING

# fills NA with a particular value, ‘2’ in this case

Code

35
PYTHON for TRADING

Output

36
PYTHON for TRADING

37
PYTHON for TRADING

38
PYTHON for TRADING



39
PYTHON for TRADING

40
PYTHON for TRADING

41
PYTHON for TRADING

42
PYTHON for TRADING

43
PYTHON for TRADING

44
PYTHON for TRADING

45
PYTHON for TRADING

46
PYTHON for TRADING

47

You might also like