I have a problem in pricing barrier option using trinomial model. I’ve tried to calculate the barrier option using black scholes and trinomial. For the stock price movements, it’s already convergent to black scholes. But when it comes to the barrier condition, it’s not novergent. Some of the values are not in the right place. For example, the black scholes calculation for call down-in barrier option is 10 and call down-out is 2, it’s considered to be true. But when I tried it with trinomial, the call down-in barrier option will be 5 and call down-out will be 10.
Here below is the code for the stock price movements and also the value of european barrier option using trinomial (already convergent to black scholes).
`# CALL DOWN OPTION
import math
import numpy as np
S0 = 193.02
M = 100
s = 0.02
T = 63/252
dt = T/M
B = 188
E = 185
r = 0.05
q = 0
#Trinomial
u = math.exp(3*s*math.sqrt(dt))
d = math.exp(3*-s*math.sqrt(dt))
mm = math.exp(r*dt)
vv = (mm**2)*(math.exp((s**2)*dt)-1)
pu =(((vv+(mm**2)-mm)*u)-(mm-1))/((u-1)*((u**2)-1))
pd = (((vv+(mm**2)-mm)*(u**2))-(mm-1)*(u**3))/((u-1)*((u**2)-1))
pm = 1-pu-pd
payoff = np.zeros([2*M+1,M+1], dtype=float)
AP = np.zeros([2*M+1,M+1], dtype=float)
stock_price = np.zeros([2*M+1,M+1], dtype=float)
stock_price[0,0] = S0
for i in range(1,M+1):
if M%3==1:
bl = 2*i+1
else:
bl=2*i+2
for j in range(0,bl,3):
if stock_price[j,i-1]!=0:
stock_price[j, i] = stock_price[j,i-1]*u
stock_price[j+1,i] = stock_price[j,i-1]
stock_price[j+2,i] = stock_price[j,i-1]*d
elif stock_price[j-1,j-i]!=0:
stock_price[j, i] = stock_price[j-1,i-1]
stock_price[j+1,i]= stock_price[j-1,i-1]*d
else:
stock_price[j,i]=stock_price[j-2,i-1]*d
#European
for j in range(2*M+1):
if j==M:
payoff[j,M] = max(S0*u*d-E, 0)
elif j<M:
payoff[j,M] = max(S0*u**(M-j)-E, 0)
else:
payoff[j,M] = max(S0*d**(j-M)-E, 0)
for i in range(M-1,-1,-1):
for j in range(0,2*i+1):
payoff[j,i]=math.exp(-r*dt)*((pu*payoff[j,i+1])+(pd*payoff[j+2,i+1])+(pm*payoff[j+1,i+1]))
print (payoff[0,0])`
The problem is on the code below.
`#CALL DOWN IN
# Initialize the option value matrix V
V = np.zeros([2 * M + 1, M + 1], dtype=float)
#baris = 0
# Define the range for i
barrier_hit_at = None
for i in range(0, M + 1): # Iterate from 0 to M (inclusive)
barrier_hit = False # Flag to track if barrier is hit
for j in range(0, 2 * i + 1):
if stock_price[j, i] <= B:
barrier_hit = True
break # Exit the inner loop once barrier is hit for the first time
#baris += 1
if barrier_hit:
barrier_hit_at = f"M-{M - i}" # Calculate the position of i where the loop stops
break # Exit the outer loop if barrier is hit for any i
# Calculate payoff array based on barrier hit position
if barrier_hit_at is not None:
payoff_array = [max(stock_price[j, M] - E, 0) for j in range(2 * M - 2 * int(barrier_hit_at.split("-")[1]), 2 * M + 1)]
# Insert payoff_array into V at the corresponding position
V[2 * M - 2 * int(barrier_hit_at.split("-")[1]):2 * M + 1, M] = payoff_array
# Print the entire V array
# Display the position of i where the loop stops due to barrier hit
print("Position of i where the loop stops:", barrier_hit_at)
print(np.round(V,2))
for i in range(M-1,-1,-1):
for j in range(0, 2 * i + 1):
V[j,i] = math.exp(-r * dt) * ((pu * V[j, i+1]) + (pm * V[j+1, i+1]) + (pd * V[j+2, i+1]))
#baris +=1
V[0,0]`
Anyone can help? I would be very grateful if someone can state where the problem is in the code. Thank you!
Clarissa Hongo is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.