Search code examples
pythonfor-loopquantitative-finance

Barrier Option Pricing in Python


We have a barrier call option of European type with strike price K>0 and a barrier value

0 < b< S0 ,

where S_0 is the starting price.According to the contract, the times 0<t_1<...<t_k<T the price must be checked S(t_k)>b for every k.

Assuming the S(t) is described with the binomial option model with u=1.1 and d = 0.9,r=0.05,T=10, and t_1=2,t_2=4 and t-3=7 the times that the asset must be checked.Also consider the S_0=100,K=125 and the barrier b=60.

My attempt is the following :

# Initialise parameters
S0 = 100      # initial stock price
K = 125       # strike price
T = 10        # time to maturity in years
b = 60        # up-and-out barrier price/value
r = 0.05      # annual risk-free rate
N = 4         # number of time steps
u = 1.1       # up-factor in binomial models
d = 0.9       # ensure recombining tree
opttype = 'C' # Option Type 'C' or 'P'

def barrier_binomial(K,T,S0,b,r,N,u,d,opttype='C'):
    #precompute values
    dt = T/N
    q = (1+r - d)/(u-d)
    disc = np.exp(-r*dt)
    
    # initialise asset prices at maturity
    S = S0 * d**(np.arange(N,-1,-1)) * u**(np.arange(0,N+1,1))
        
    # option payoff
    if opttype == 'C':
        C = np.maximum( S - K, 0 )
    else:
        C = np.maximum( K - S, 0 )
            
    # check terminal condition payoff
    C[S >= b] = 0
            
    # backward recursion through the tree
    for i in np.arange(N-1,-1,-1):
        S = S0 * d**(np.arange(i,-1,-1)) * u**(np.arange(0,i+1,1))
        C[:i+1] = disc * ( q * C[1:i+2] + (1-q) * C[0:i+1] )
        C = C[:-1]
        C[S >= H] = 0
    return C[0]

barrier_binomial(K,T,S0,b,r,N,u,d,opttype='C')


I receive nothing because something is wrong and I don’t know what

But is it a simulation ?

Any help from someone ?


Solution

  • In your loop you are using C[S >= H] = 0, but your barrier param is defined as b. Also you are filling the array C with 0s only, so check the payoff condition. In general, I find it much easier looping through matrices when working with tree models.