├── README.md ├── twosigma ├── README.md ├── question5.py ├── question4.py ├── question4.csv └── question5.csv └── callOptions └── European_call_option.py /README.md: -------------------------------------------------------------------------------- 1 | # Finance Concepts in Python 2 | A collection of Python scripts to illustrate concepts from MGMT 656: Energy Derivatives. 3 | 4 | ### Pricing a Call Option 5 | European_call_option.py 6 | 7 | ### Time Series 8 | 9 | ### Monte Carlo Simulations 10 | 11 | ### Volatility 12 | -------------------------------------------------------------------------------- /twosigma/README.md: -------------------------------------------------------------------------------- 1 | # Questions for Data Analysis 2 | 3 | How do you filter based on date, type of transaction, etc.? 4 | How do you sum a given field for a particular date? 5 | How do you determine max and minimum values given NA or empty strings? 6 | etc., etc. 7 | 8 | ### More beginner Python Finance examples to follow in other folders. 9 | 10 | 11 | Last update: 2/19/2015 12 | -------------------------------------------------------------------------------- /twosigma/question5.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | 3 | import csv 4 | 5 | stocks = open('question5.csv') 6 | csv_stocks = csv.reader(stocks) 7 | 8 | d = {} 9 | 10 | for row in csv_stocks: 11 | if row[6] != '' and row[6] != 'Volume': 12 | d.setdefault(row[1], []).append(int(row[6])) 13 | 14 | 15 | for key,value in d.iteritems(): 16 | if sum(value) > 0: 17 | print key + ",", sum(value) 18 | -------------------------------------------------------------------------------- /twosigma/question4.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | 3 | import csv 4 | 5 | stocks = open('question4.csv') 6 | csv_stocks = csv.reader(stocks) 7 | 8 | asks = [] 9 | bids = [] 10 | 11 | for row in csv_stocks: 12 | if row[4] == "Quote": 13 | if row[1] == "07-APR-2014": 14 | asks.append(float(row[9])) 15 | bids.append(float(row[7])) 16 | 17 | print "%.2f" % min(asks) , "\n" , "%.2f" % max(bids) 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /twosigma/question4.csv: -------------------------------------------------------------------------------- 1 | #Ticker,Date,Time,GMT Offset,Type,Price,Volume,Bid Price,Bid Size,Ask Price,Ask Size 2 | IBM,04-APR-2014,13:30:04.599,-4,Trade,193.01,49042,,,, 3 | IBM,04-APR-2014,13:30:04.599,-4,Quote,,,193,50,193.03,1 4 | IBM,04-APR-2014,13:30:04.599,-4,Quote,,,193,50,193.03,1 5 | IBM,04-APR-2014,13:30:04.599,-4,Trade,193.03,100,,,, 6 | IBM,04-APR-2014,13:30:04.599,-4,Trade,193.03,50,,,, 7 | IBM,04-APR-2014,13:30:04.599,-4,Quote,,,193,50,193.18,2 8 | IBM,04-APR-2014,13:30:04.608,-4,Trade,193.07,1,,,, 9 | IBM,04-APR-2014,13:30:04.629,-4,Trade,193.02,2,,,, 10 | IBM,04-APR-2014,13:30:04.639,-4,Trade,193.02,25,,,, 11 | IBM,04-APR-2014,13:30:05.708,-4,Quote,,,192.82,1,193.18,2 12 | IBM,04-APR-2014,13:30:05.708,-4,Trade,193,40,,,, 13 | IBM,04-APR-2014,13:30:05.818,-4,Quote,,,192.82,1,193.23,1 14 | IBM,04-APR-2014,13:30:06.328,-4,Quote,,,192.82,1,193.23,1 15 | IBM,04-APR-2014,13:30:09.078,-4,Trade,193.1,19,,,, 16 | -------------------------------------------------------------------------------- /twosigma/question5.csv: -------------------------------------------------------------------------------- 1 | #Ticker,Date,Time,GMT Offset,Type,Price,Volume,Bid Price,Bid Size,Ask Price,Ask Size 2 | IBM,04-APR-2014,13:30:04.599,-4,Trade,193.01,49042,,,, 3 | IBM,04-APR-2014,13:30:04.599,-4,Quote,,,193,50,193.03,1 4 | IBM,04-APR-2014,13:30:04.599,-4,Quote,,,193,50,193.03,1 5 | IBM,04-APR-2014,13:30:04.599,-4,Trade,193.03,100,,,, 6 | IBM,04-APR-2014,13:30:04.599,-4,Trade,193.03,50,,,, 7 | IBM,04-APR-2014,13:30:04.599,-4,Quote,,,193,50,193.18,2 8 | IBM,04-APR-2014,13:30:04.608,-4,Trade,193.07,1,,,, 9 | IBM,04-APR-2014,13:30:04.629,-4,Trade,193.02,2,,,, 10 | IBM,04-APR-2014,13:30:04.639,-4,Trade,193.02,25,,,, 11 | IBM,04-APR-2014,13:30:05.708,-4,Quote,,,192.82,1,193.18,2 12 | IBM,04-APR-2014,13:30:05.708,-4,Trade,193,40,,,, 13 | IBM,04-APR-2014,13:30:05.818,-4,Quote,,,192.82,1,193.23,1 14 | IBM,04-APR-2014,13:30:06.328,-4,Quote,,,192.82,1,193.23,1 15 | IBM,04-APR-2014,13:30:09.078,-4,Trade,193.1,19,,,, 16 | -------------------------------------------------------------------------------- /callOptions/European_call_option.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python3.4 2 | 3 | # Pricing a European Call Option 4 | # Paige Bailey 5 | # Saturday, February 14, 2015 6 | 7 | from math import * 8 | 9 | def CND(X): 10 | ''' Cumulative Stanard Normal Distribution''' 11 | (a1, a2, a3, a4, a5) = (0.31938153,-0.356563782,1.781477937,-1.821255978,1.330274429) 12 | L = abs(X) 13 | K = 1.0 / (1.0+0.2316419 * L) 14 | w = 1.0 - 1.0 / sqrt(2 * pi) * exp(-L * L/ 2.) * (a1 * K + a2 * K * K + a3 * pow(K, 3) + a4 * pow(K, 4) + a5 * pow(K, 5)) 15 | if X < 0: 16 | w = 1.0 - w 17 | return w 18 | 19 | def euro_call(S, X, T, r, sigma): 20 | ''' 21 | S = current stock price 22 | X = exercise price (fixed) 23 | T = maturity (in years) 24 | r = continuously compounded risk-free rate 25 | sigma = volatility of the underlying 26 | ''' 27 | 28 | d1 = (log(S/X) + (r+sigma*sigma/2.0)*T) / (sigma * sqrt(T)) 29 | d2 = d1 - sigma*sqrt(T) 30 | return S * CND(d1) - X*exp(-r*T) * CND(d2) 31 | 32 | 33 | 34 | print(euro_call(40, 42, 0.5, 0.1, 0.2)) 35 | 36 | --------------------------------------------------------------------------------