def up_call(s0,x,T,r,sigma,n_simulation,barrier): """ Objective: output up_and_out_call and up_and_in_call """ import scipy as sp import p4f n_steps=100. dt=T/n_steps inTotal=0 outTotal=0 for j in range(0, n_simulation): sT=s0 inStatus=False outStatus=True for i in range(0,int(n_steps)): e=sp.random.normal() sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt)) if sT>barrier: outStatus=False inStatus=True #print 'sT=',sT #print 'j=',j ,'out=',out if outStatus==True: outTotal+=p4f.bs_call(s0,x,T,r,sigma) else: inTotal+=p4f.bs_call(s0,x,T,r,sigma) return outTotal/n_simulation, inTotal/n_simulation
def up_call(s0, x, T, r, sigma, n_simulation, barrier): import scipy as sp import p4f n_steps = 100. dt = T / n_steps inTotal = 0 outTotal = 0 for j in range(0, n_simulation): sT = s0 inStatus = False outStatus = True for i in range(0, int(n_steps)): e = sp.random.normal() sT *= sp.exp((r - 0.5 * sigma * sigma) * dt + sigma * e * sp.sqrt(dt)) if (sT > barrier): outStatus = False inStatus = True #print 'sT=',sT #print 'j=', j, 'out=',out if outStatus == True: outTotal += p4f.bs_call(s0, x, T, r, sigma) else: inTotal += p4f.bs_call(s0, x, T, r, sigma) return outTotal / n_simulation, inTotal / n_simulation
def upCall(s, x, T, r, sigma, nSimulation, barrier): import scipy as sp import p4f n_steps = 100 dt = T / n_steps inTotal = 0 outTotal = 0 for j in range(0, nSimulation): sT = s inStatus = False outStatus = True for i in range(0, int(n_steps)): e = sp.random.normal() sT *= sp.exp((r - 0.5 * sigma * sigma) * dt + sigma * e * sp.sqrt(dt)) if sT > barrier: outStatus = False inStatus = True if outStatus == True: outTotal += p4f.bs_call(s, x, T, r, sigma) else: inTotal += p4f.bs_call(s, x, T, r, sigma) return outTotal / nSimulation, inTotal / nSimulation
def up_and_out_call(s0,x,T,r,sigma,n_simulation,barrier): n_steps=100. dt=T/n_steps total=0 for j in range(0, n_simulation): sT=s0 out=False for i in range(0,int(n_steps)): e=sp.random.normal() sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt)) if sT>barrier: out=True if out==False: total+=p4f.bs_call(s0,x,T,r,sigma) return total/n_simulation
def up_and_out_call(s0,x,T,r,sigma,n_simulation,barrier): n_steps=100. dt=T/n_steps total=0 for j in range(0,n_simulation): sT=s0 out=False for i in range(0,int(n_steps)): e=sp.random.normal() sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt)) if ssT>barrier: out=True if out ==False: total+=p4f.bs_call(s0,x,T,r,sigma) return total/n_simulation
def lookback_min_price_as_strike(s,T,r,sigma,n_simulation): n_steps=100. dt=T/n_steps total=0 for j in range(n_simulation): min_price=100000. sT=s for i in range(int(n_steps)): e=sp.random.normal() sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt)) if sT<min_price: min_price=sT #print 'j=',j,'i=',i,'total=',total total+=p4f.bs_call(s,min_price,T,r,sigma) return total/n_simulation
def lookback_min_price_as_strike(s, T, r, sigma, n_simulation): n_steps = 100. dt = T / n_steps total = 0 for j in range(n_simulation): min_price = 100. sT = s for i in range(int(n_steps)): e = sp.random.normal() sT *= sp.exp((r - 0.5 * sigma * sigma) * dt + sigma * e * sp.sqrt(dt)) if sT < min_price: min_price = sT #print 'j=',j,'i=',i,'total=',total total += p4f.bs_call(s, min_price, T, r, sigma) return total / n_simulation
import matplotlib.pyplot as plt # s = 9.25 # stock price at time zero x = 9.10 # exercise price barrier = 10.5 # barrier T = 0.5 # maturity date (in years) n_steps = 30 # number of steps r = 0.05 # expected annual return sigma = 0.2 # volatility (annualized) sp.random.seed(125) # seed() n_simulation = 5 # number of simulations # dt = T / n_steps S = sp.zeros([n_steps], dtype=float) time_ = range(0, int(n_steps), 1) c = p4f.bs_call(s, x, T, r, sigma) sp.random.seed(124) outTotal, inTotal = 0., 0. n_out, n_in = 0, 0 for j in range(0, n_simulation): S[0] = s inStatus = False outStatus = True for i in time_[:-1]: e = sp.random.normal() S[i + 1] = S[i] * sp.exp((r - 0.5 * pow(sigma, 2)) * dt + sigma * sp.sqrt(dt) * e) if S[i + 1] > barrier: outStatus = False inStatus = True
for j in range(0, n_simulation): sT = s0 inStatus = False outStatus = True for i in range(0, int(n_steps)): e = sp.random.normal() sT *= sp.exp((r - 0.5 * sigma * sigma) * dt + sigma * e * sp.sqrt(dt)) if (sT > barrier): outStatus = False inStatus = True #print 'sT=',sT #print 'j=', j, 'out=',out if outStatus == True: outTotal += p4f.bs_call(s0, x, T, r, sigma) else: inTotal += p4f.bs_call(s0, x, T, r, sigma) return outTotal / n_simulation, inTotal / n_simulation s0 = 40. x = 40. barrier = 42 T = 0.5 r = 0.05 sigma = 0.2 n_simulation = 100 upoutCall, upInCall = up_call(s0, x, T, r, sigma, n_simulation, barrier) print 'upOutCall=', round(upoutCall, 2), 'upinCall=', round(upInCall, 2) print 'Black-Scholes call', round(p4f.bs_call(s0, x, T, r, sigma), 2)
# -*- coding: utf-8 -*- """ Name : c10_31_plot_call_vs_one_inut.py Book : Python for Finance (2nd ed.) Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 6/6/2017 email : [email protected] [email protected] """ import numpy as np import p4f as pf import matplotlib.pyplot as plt s0 = 30 T0 = 0.5 sigma0 = 0.2 r0 = 0.05 x0 = 30 sigma = np.arange(0.05, 0.8, 0.05) T = np.arange(0.5, 2.0, 0.5) call_0 = pf.bs_call(s0, x0, T0, r0, sigma0) call_sigma = pf.bs_call(s0, x0, T0, r0, sigma) call_T = pf.bs_call(s0, x0, T, r0, sigma0) plt.plot(sigma, call_sigma, 'b') plt.plot(T, call_T) plt.show()
import p4f sT = arange(20,70,5) s=40;x=40;T1=0.5;T2=1;sigma=0.3;r=0.05 payoff=(abs(sT-x)+sT-x)/2 call_01 = p4f.bs_call(s,x,T1,r,sigma) #short call_02=pdf.bs_call(s,x,T2,r,sigma) # long profit_01=payoff-call_01 call_03=pdf.bs_call(sT,x,(T2-T1),r,sigm) calender_spread=call_03-payoff+call_01 -call_02 y0=zeros(len(sT)) ylim(-20,20) xlim(20,60) plot(sT,call_03,'b-.') plot(sT,call_02-call_01-payoff,'b-.') plot(sT,calender_spread,'r') plot([x,x],[-20,-15]) title("Calender spread with calls") xlabel('Stock price at maturity (sT)') ylabel('Profit (loss)') plt.annotate('Buy a call with T1 and sell a call with T2', xy=(25,16)) plt.annotate('where T1<T2', xy=(25,14)) plt.annotate('Calender spread', xy=(25,-3), xytext=(22,-15), arrowprops=dict(facecolor='red',shrink=0.01),) plt.annotate('Value of the call (T2) at maturity', xy=(45,7), xytest=(25,10), arrowprops=dict(facecolor='red',shrink=0.01),) plt.annotate('Profit/loss with call 1 only', xy=(50,-10), xytext(30,-10), arrowprops=dict(facecolor='blue',shrink=0.01),) plt.annotate('Excercise price', xy=(x+0.5,-20+0.5)) show()
Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import matplotlib.pyplot as plt import p4f sT = arange(20,70,5) s=40 x=40 T1=0.5 T2=1 sigma=0.3 r=0.05 payoff=(abs(sT-x)+sT-x)/2 call_01=p4f.bs_call(s,x,T1,r,sigma) # short call_02=p4f.bs_call(s,x,T2,r,sigma) # long profit_01=payoff-call_01 call_03=p4f.bs_call(sT,x,(T2-T1),r,sigma) calendar_spread=call_03-payoff+call_01 -call_02 y0=zeros(len(sT)) ylim(-20,20) xlim(20,60) plot(sT,call_03,'b-.') plot(sT,call_02-call_01-payoff,'b-.') plot(sT,calendar_spread,'r') plot([x,x],[-20,-15]) title("Calendar spread with calls") xlabel('Stock price at maturity (sT)') ylabel('Profit (loss)') plt.annotate('Buy a call with T1 and sell a call with T2', xy=(25,16))
k = 190 # Strike price of the May 26th 2015 and July 28th 2015 Call Option cs = 7.50 # Premium of the Short call on May 2nd, 2015 cl = 20.70 # Premium of the Long call on May 2nd, 2015 shares = 100 # Shares per lot sT = np.arange(0, 2 * s0, 5) # Stock Price at expiration of the Call sigma = 0.4 # Historical Volatility r = 0.08 # Risk-free interest rate t = datetime(2015, 7, 28) - datetime(2015, 5, 26) T = t.days / 365 # Payoff from the May 26th 2015 Short Call Option at Expiration y1 = np.where(sT > k, ((k - sT) + cs) * shares, cs * shares) # Value of the July 28th 2015 long Call Option on May 26th 2015 lc_value = p4f.bs_call(sT, k, T, r, sigma) * shares # Payoff from the Calendar Call y2 = y1 + (lc_value - cl) # Create a plot using matplotlib fig, ax = plt.subplots() ax.spines['top'].set_visible(False) # Top border removed ax.spines['right'].set_visible(False) # Right border removed ax.spines['bottom'].set_position('zero') # Sets the X-axis in the center ax.tick_params(top=False, right=False) # Removes the tick-marks on the RHS plt.plot(sT, y1, lw=1.5, label='Short Call') plt.plot(sT, lc_value, lw=1.5, label='Long Call') plt.plot(sT, y2, lw=1.5, label='Calendar Call') plt.title('Calendar Call') plt.xlabel('Stock Prices')
from math import exp import matplotlib.pyplot as pl S0 = 9.15 x = 9.15 barrier = 10.15 T = 0.5 n_steps = 30. r = 0.05 sigma = 0.2 sp.random.seed(125) n_simulation = 5 dt = T / n_steps S = sp.zeros([n_steps], dtype=float) time_ = range(0, int(n_steps), 1) c = p4f.bs_call(S0, x, T, r, sigma) sp.random.seed(124) outTotal, inTotal = 0., 0. n_out, n_in = 0, 0 for j in range(0, n_simulation): S[0] = S0 inStatus = False outStatus = True for i in time_[:-1]: e = sp.random.normal() S[i + 1] = S[i] * exp((r - 0.5 * pow(sigma, 2)) * dt + sigma * sp.sqrt(dt) * e) if S[i + 1] > barrier: outStatus = False inStatus = True pl.plot(time_, S)
for j in range(0, n_simulation): sT=s0 inStatus=False outStatus=True for i in range(0,int(n_steps)): e=sp.random.normal() sT*=sp.exp((r-0.5*sigma*sigma)*dt+sigma*e*sp.sqrt(dt)) if (sT>barrier) : outStatus=False inStatus=True #print 'sT=',sT #print 'j=', j, 'out=',out if outStatus==True: outTotal+=p4f.bs_call(s0,x,T,r,sigma) else: inTotal+=p4f.bs_call(s0,x,T,r,sigma) return outTotal/n_simulation, inTotal/n_simulation s0=40. x=40. barrier=42 T=0.5 r=0.05 sigma=0.2 n_simulation=100 upoutCall,upInCall=up_call(s0,x,T,r,sigma,n_simulation,barrier) print 'upOutCall=', round(upoutCall,2),'upinCall=',round(upInCall,2) print 'Black-Scholes call', round(p4f.bs_call(s0,x,T,r,sigma),2)
plt.annotate('x1 = 50, x2 =55, x3=60', xy=(45,12)) plt.annotate('c1=10, c2= 7, c3=5', xy=(45,10)) plt.show() ## 입력값과 옵션값의 관계 -> 변동성과 c, p 가격 차이 ## #변동성이 커지면 옵션값은 올라간다# #만기가 증가하면 K가 낮아서 콜증가, 풋감소 + 변동성 증가의 가능성을 증가시켜 옵션가격 증가시킨다# s0 = 30 T0 = 0.5 sigma0 = 0.2 r0= 0.05 x0 = 30 sigma = np.arange(0.05, 0.8, 0.05) T = np.arange(0.5 , 2.0 , 0.5) call_0 = p4f.bs_call(s0,x0,T0,r0, sigma0) call_sigma = p4f.bs_call(s0,x0,T0,r0,sigma) call_T = p4f.bs_call(s0,x0,T,r0, sigma0) plt.title("Relationship between sigma and call, T, and call") plt.plot(sigma, call_sigma,'b') plt.plot(T, call_T, 'r') plt.annotate('x=Sigma, y=call price', xy=(0.6,5), xytext=(1,6), arrowprops=dict(facecolor = 'blue', shrink = 0.01),) plt.annotate('x=T(maturity), y=call price',xy =(1,3), xytext=(0.8,1), arrowprops=dict(facecolor = 'red', shrink = 0.01),) plt.ylabel("Call premium") plt.xlabel("Volatiltiy or T") plt.show() #### 풋콜패러티 #### x = 10
""" Name : 4375OS_09_14_sigma_against_call.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import numpy as np import p4f as pf s0 = 30 T0 = 0.5 r0 = 0.05 x0 = 30 sigma = np.arange(0.05, 0.8, 0.05) call = pf.bs_call(s0, x0, T0, r0, sigma) plot(sigma, call, 'b') show()
import numpy as np import p4f as pf s0=30;T0=0.5;sigma0=0.2;r0=0.05;x0=30 sigma=np.arange(0.5,2.0,0.5) call_0=pf.bs_call(s0,x0,T0,r0,sigma) call_sigma=pf.bs_call(s0,x0,T0,r0,sigma) call_T=pf.bs_call(s0,x0,Tr0,sigma0) plot(sigma,call_sigma,'b') plot(T,call_T)
""" Name : 4375OS_10_15_for_loop_implied_vol_p4f.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import p4f S=40 K=40 T=0.5 r=0.05 c=3.30 for i in range(200): sigma=0.005*(i+1) diff=c-p4f.bs_call(S,K,T,r,sigma) if abs(diff)<=0.01: print(i,sigma, diff)
import numpy as np from datetime import datetime import matplotlib.pyplot as plt s0 = 187 # Initial stock price on May 2nd, 2015 k = 190 # Strike price of the May 26th 2015 and July 28th 2015 Call Option cs = 7.50; # Premium of the Short call on May 2nd, 2015 cl = 20.70; # Premium of the Long call on May 2nd, 2015 shares = 100 # Shares per lot sT = np.arange(0,2*s0,5) # Stock Price at expiration of the Call sigma = 0.4 # Historical Volatility r = 0.08 # Risk-free interest rate t = datetime(2015,7,28) - datetime(2015,5,26) ; T = t.days / 365; # Payoff from the May 26th 2015 Short Call Option at Expiration y1 = np.where(sT > k,((k - sT) + cs) * shares, cs * shares) # Value of the July 28th 2015 long Call Option on May 26th 2015 lc_value = p4f.bs_call(sT,k,T,r,sigma) * shares # Payoff from the Calendar Call y2 = y1 + (lc_value - cl) # Create a plot using matplotlib fig, ax = plt.subplots() ax.spines['top'].set_visible(False) # Top border removed ax.spines['right'].set_visible(False) # Right border removed ax.spines['bottom'].set_position('zero') # Sets the X-axis in the center ax.tick_params(top=False, right=False) # Removes the tick-marks on the RHS plt.plot(sT,y1,lw=1.5,label='Short Call') plt.plot(sT,lc_value,lw=1.5,label='Long Call') plt.plot(sT,y2,lw=1.5,label='Calendar Call') plt.title('Calendar Call') plt.xlabel('Stock Prices') plt.ylabel('Profit/loss') plt.grid(True)
email : [email protected] [email protected] """ import p4f import numpy as np import matplotlib.pyplot as plt sT = np.arange(20, 70, 5) s = 40 x = 40 T1 = 0.5 T2 = 1 sigma = 0.3 r = 0.05 payoff = (abs(sT - x) + sT - x) / 2 call_01 = p4f.bs_call(s, x, T1, r, sigma) # short call_02 = p4f.bs_call(s, x, T2, r, sigma) # long profit_01 = payoff - call_01 call_03 = p4f.bs_call(sT, x, (T2 - T1), r, sigma) calendar_spread = call_03 - payoff + call_01 - call_02 y0 = np.zeros(len(sT)) plt.ylim(-20, 20) plt.xlim(20, 60) plt.plot(sT, call_03, 'b-.') plt.plot(sT, call_02 - call_01 - payoff, 'b-.') plt.plot(sT, calendar_spread, 'r') plt.plot([x, x], [-20, -15]) plt.title("Calendar spread with calls") plt.xlabel('Stock price at maturity (sT)') plt.ylabel('Profit (loss)')
""" Name : 4375OS_09_14_sigma_against_call.py Book : Python for Finance Publisher: Packt Publishing Ltd. Author : Yuxing Yan Date : 12/26/2013 email : [email protected] [email protected] """ import numpy as np import p4f as pf s0 = 30 T0 = 0.5 r0 = 0.05 x0 = 30 sigma = np.arange(0.05, 0.8, 0.05) call = pf.bs_call(s0, x0, T0, r0, sigma) plot(sigma, call, "b") show()