# How can I simulate the barrier option call model in Python?

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

$$0 < b< S_0$$,

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

The payoff function : $$C_{T} = \max(S(T)-K) \mathbf{1}_{S(t_{1})> b \cap \dots \cap S(t_k)>b }$$

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$$.

Run a Monte Carlo simulation with n=[100,1000,10000,50000] to estimate the $$\left(\frac{1}{1+r}\right)^{T} E_{\mathbb{Q}}[C_{T}]$$

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

for N in [100, 1000, 10000, 50000]:
barrier_binomial(K,T,S0,b,r,N,u,d,opttype='C')



What is my mistake here ? I can't find out what.Any help ?

<ipython-input-24-eea44e75eec5>:8: RuntimeWarning: overflow encountered in power
S = S0 * d**(np.arange(N,-1,-1)) * u**(np.arange(0,N+1,1))
<ipython-input-24-eea44e75eec5>:21: RuntimeWarning: overflow encountered in power
S = S0 * d**(np.arange(i,-1,-1)) * u**(np.arange(0,i+1,1))
<ipython-input-24-eea44e75eec5>:21: RuntimeWarning: overflow encountered in multiply
S = S0 * d**(np.arange(i,-1,-1)) * u**(np.arange(0,i+1,1))
<ipython-input-24-eea44e75eec5>:8: RuntimeWarning: invalid value encountered in multiply
S = S0 * d**(np.arange(N,-1,-1)) * u**(np.arange(0,N+1,1))
<ipython-input-24-eea44e75eec5>:17: RuntimeWarning: invalid value encountered in greater_equal
C[S >= b] = 0
<ipython-input-24-eea44e75eec5>:21: RuntimeWarning: invalid value encountered in multiply
S = S0 * d**(np.arange(i,-1,-1)) * u**(np.arange(0,i+1,1))
<ipython-input-24-eea44e75eec5>:24: RuntimeWarning: invalid value encountered in greater_equal
C[S >= b] = 0

• Why you think there is a problem with your code is not clearly stated in your post. Oct 20, 2021 at 17:00
• @Alper because it doesn’t run.I receive error with the power of d in no.arange
– user57440
Oct 20, 2021 at 17:06
• It is better if you include that in your post and also include the exact error message you get when you try to run your code. Oct 20, 2021 at 17:08
• @Alper I have added the error that occurs
– user57440
Oct 20, 2021 at 17:13
• You're re-using the number of simluations as the number of tree steps which is not right. Do you understand how tree pricing works? How monte carlo works? Have you debugged it by starting will simple cases (1,2 periods) to see if the logic holds? Oct 20, 2021 at 17:29