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

Python for Quantitative Finance Basics

Mechanics of Futures Markets

Alexandre Landi

IBM, Skema Business School

June 8, 2024

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 1 / 12
Problem

A trader enters into a short forward contract to sell 100,000 British


pounds for U.S. dollars at an exchange rate of 1.2700 USD per pound.
How much does the investor gain or lose (in USD) if the exchange rate at
the end of the contract is (a) 1.2600 and (b) 1.2900?

Source: Hull, J. C. (2021). Options, Futures, and Other Derivatives (11th global ed.).
Pearson. p. 42, practice question 1.2

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 2 / 12
Solution

1 position = -1
2 size = 100000
3 initial_price = 1.2700
4 final_price_a = 1.2600
5 final_price_b = 1.2900
6
7 pnl_a = position * ( final_price_a - initial_price ) * size
8 pnl_b = position * ( final_price_b - initial_price ) * size
9
10 print ( f " pnl_a : { round ( pnl_a , 2) } " )
11 print ( f " pnl_b : { round ( pnl_b , 2) } " )

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 3 / 12
Problem

Now let’s say that we have another problem to solve and we want a more
general solution:

A trader enters into a short cotton futures contract when the futures price
is 75 cents per pound. The contract is for the delivery of 50,000 pounds.
How much does the trader gain or lose if the cotton price at the end of the
contract is (a) 73.20 cents per pound and (b) 76.30 cents per pound?

Source: Hull, J. C. (2021). Options, Futures, and Other Derivatives (11th global ed.).
Pearson. p. 42, practice question 1.3

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 4 / 12
Solution

1 def compute_pnl ( position , size , initial_price , final_price ) :


2 pnl = position * ( final_price - initial_price ) * size
3 pnl = round ( pnl , 2)
4 print ( f " pnl : { pnl } " )
5 return pnl
6
7 # solve exercise 1.2
8 compute_pnl ( -1 , 100000 , 1.2700 , 1.2600)
9 compute_pnl ( -1 , 100000 , 1.2700 , 1.2900)
10
11 # solve exercise 1.3
12 compute_pnl ( -1 , 50000 , 0.75 , 0.732)
13 compute_pnl ( -1 , 50000 , 0.75 , 0.763)

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 5 / 12
Problem

Now, let’s say that we need to compute the PnL not just for 1 or 2 trades,
but 10. How could we do that in an efficient way?

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 6 / 12
Problem

1 i m p o r t p a n d a s a s pd
2
3
4 p a t h = ” h t t p s : / / raw . g i t h u b u s e r c o n t e n t . com/ Q u a n t L a n d i / python−f o r −quant−f i n a n c e / main / ”
5 filename = ” trades . csv ”
6 t r a d e s d f = pd . r e a d c s v ( p a t h+f i l e n a m e , i n d e x c o l =0)
7
8 print ( trades df )

Date Position Contracts Commodity Contract Size Initial Price Final Price
2023-08-06 -1 1 Soybeans 5000 1060.20 1178.00
2023-08-19 -1 2 Cotton 50000 93.79 85.21
2023-12-01 1 50 Silver 5000 25.48 23.17
2023-12-19 1 1 Cocoa 10000 2530.00 2700.00
2023-12-27 -1 2 Wheat 5000 543.55 603.94
2023-12-30 -1 20 Gold 100 1844.88 2040.23
2024-01-29 1 60 Crude Oil 1000 47.72 53.02
2024-05-08 1 5 Wheat 5000 603.36 670.40
2024-05-13 -1 20 Gold 100 2315.84 2571.49
2024-05-14 -1 1 Cocoa 10000 2770.56 2700.51

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 7 / 12
Solution

1 def compute pnl ( t r a d e s d f ) :


2 position = trades df [” position ”] ∗ trades df [” n contracts ”]
3 price diff = trades df [” final price ”] − trades df [” i n i t i a l p r i c e ”]
4 trades df [ ” pnl ” ] = p os it io n ∗ p r i c e d i f f ∗ trades df [ ” s i z e ” ]
5 return trades df
6
7 s o l u t i o n = compute pnl ( t r a d e s d f )
8 print ( solution )

Date Position Contracts Commodity Size Initial Price Final Price PnL
2023-08-06 -1 1 Soybeans 5000 1060.20 1178.00 -589000.00
2023-08-19 -1 2 Cotton 50000 93.79 85.21 858000.00
2023-12-01 1 50 Silver 5000 25.48 23.17 -577500.00
2023-12-19 1 1 Cocoa 10000 2530.00 2700.00 1700000.00
2023-12-27 -1 2 Wheat 5000 543.55 603.94 -603900.00
2023-12-30 -1 20 Gold 100 1844.88 2040.23 -390700.00
2024-01-29 1 60 Crude Oil 1000 47.72 53.02 318000.00
2024-05-08 1 5 Wheat 5000 603.36 670.40 1676000.00
2024-05-13 -1 20 Gold 100 2315.84 2571.49 -511300.00
2024-05-14 -1 1 Cocoa 10000 2770.56 2700.51 700500.00

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 8 / 12
PnL Bar Plot

1 import m a t p l o t l i b . pyplot as p l t
2
3
4 c o l o r s = t r a d e s d f [ ’ p n l ’ ] . a p p l y ( lambda p n l : ’ darkgreen ’ i f pnl > 0 e l s e ’ darkred ’ )
5
6 # P l o t t h e PnL column
7 f i g , ax = p l t . s u b p l o t s ( f i g s i z e =(12 , 8 ) )
8 ax . b a r ( t r a d e s d f . i n d e x , t r a d e s d f [ ’ p n l ’ ] , c o l o r=c o l o r s , e d g e c o l o r= ’ b l a c k ’ )
9 ax . s e t t i t l e ( ’ PnL f o r Each Trade ’ , f o n t s i z e =16)
10 ax . s e t x l a b e l ( ’ Date ’ , f o n t s i z e =14)
11 ax . s e t y l a b e l ( ’ PnL i n M i l l i o n s ’ , f o n t s i z e =14)
12 ax . g r i d ( a x i s= ’ y ’ , l i n e s t y l e = ’−− ’ , a l p h a =0.7)
13 p l t . x t i c k s ( r o t a t i o n =45 , ha= ’ r i g h t ’ )
14 plt . tight layout ()
15 p l t . show ( )

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 9 / 12
PnL Bar Plot

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 10 / 12
Cumulative PnL Line Plot

1 # C a l c u l a t e t h e c u m u l a t i v e PnL
2 t r a d e s d f [ ’ c u m p n l ’ ] = t r a d e s d f [ ’ p n l ’ ] . cumsum ( )
3
4 # P l o t t h e C u m u l a t i v e PnL
5 f i g , ax = p l t . s u b p l o t s ( f i g s i z e =(12 , 8 ) )
6 ax . p l o t ( t r a d e s d f . i n d e x , t r a d e s d f [ ’ c u m p n l ’ ] , m a r k e r= ’ o ’ , l i n e s t y l e = ’− ’ , c o l o r= ’ b ’ )
7 ax . a x h s p a n ( ymin=min ( t r a d e s d f [ ’ c u m p n l ’ ] ) ∗ 1 . 5 , ymax=0 , c o l o r= ’ l i g h t b l u e ’ , a l p h a =0.5)
8 ax . s e t t i t l e ( ’ C u m u l a t i v e PnL Over Time ’ , f o n t s i z e =16)
9 ax . s e t x l a b e l ( ’ Date ’ , f o n t s i z e =14)
10 ax . s e t y l a b e l ( ’ C u m u l a t i v e PnL i n M i l l i o n s ’ , f o n t s i z e =14)
11 ax . g r i d ( True , l i n e s t y l e = ’−− ’ , a l p h a =0.7)
12 ax . s e t y l i m ( min ( t r a d e s d f [ ’ c u m p n l ’ ] ) ∗ 1 . 5 )
13 p l t . x t i c k s ( r o t a t i o n =45 , ha= ’ r i g h t ’ )
14 plt . tight layout ()
15 p l t . show ( )

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 11 / 12
Cumulative PnL Line Plot

Alexandre Landi (IBM, Skema) Python for Quantitative Finance Basics June 8, 2024 12 / 12

You might also like