Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Something for stock traders
#1
A probability calculator for traders. It answers to questions like 'What is the probability the price exceeds a barrier level' etc.

For example with risk free rate 2% , volatility 20% , stock price 1$, up barrier 1.1$ and time 6 months, the probability after 6 months we are below 1.1$ will be 74.98%.

For this example the inputs must be

Drift (or risk free rate) = 0.02
Volatility =0.2
Time = 0.5
Up barrier = 1.1
Starting Price = 1

Code:
Function snorm(z As Double) as double
   Dim pi as double
   pi=3.14159265358979
   Dim a1 as double,a2 as double,a3 as double,a4 as double,a5 as double,k as double,w as double
   a1 = 0.31938153
   a2 = -0.356563782
   a3 = 1.781477937
   a4 = -1.821255978
   a5 = 1.330274429
   If 0 > z Then w = -1 Else w = 1
   k = 1 / (1 + 0.2316419 * w * z)
   snorm = 0.5 + w * (0.5 - 1 / Sqr(2 * pi) * Exp(-z ^ 2 / 2) * (a1 * k + a2 * k ^ 2 + a3 * k ^ 3 + a4 * k ^ 4 + a5 * k ^ 5))
End Function

' m drift
' s standard deviation
' t time
' H up barrier
' L down barrier
' K1,K2 barriers
' ST starting price

' ProbPTBB probability at time t the price is below the barrier
' ProbPTUB probability at time t the price is above the barrier
' ProbMaxPTUB probability at any time until t max price is crossing above the up barrier
' ProbMaxPTBB probability at any time until t max price is below the barrier
' ProbMinPTBB probability at any time until t min price is crossing bellow the down barrier
' ProbMinPTUB probability at any time until t min price is above the down barrier
' ProbFPBBMaxPTUB probability final price is below K1 and max price is crossing above the up barrier
' ProbFPUBMinPTBB probability final price is above K2 and min price is crossing below the down barrier

Function ProbPTBB(m as double, s as double, t as double, H as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbPTBB=snorm((log(H/ST)-MM*t)/(s*sqr(t)))
End Function

Function ProbPTUB(m as double, s as double, t as double, H as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbPTUB=1-snorm((log(H/ST)-MM*t)/(s*sqr(t)))

End Function

Function ProbMaxPTUB(m as double, s as double, t as double, H as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbMaxPTUB=snorm((-log(H/ST)+MM*t)/(s*sqr(t)))+((H/ST)^(2*MM/s^2))*snorm((-log(H/ST)-MM*t)/(s*sqr(t)))
End Function

Function ProbMaxPTBB(m as double, s as double, t as double, H as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbMaxPTBB=1-(snorm((-log(H/ST)+MM*t)/(s*sqr(t)))+((H/ST)^(2*MM/s^2))*snorm((-log(H/ST)-MM*t)/(s*sqr(t))))
End Function

Function ProbMinPTBB(m as double, s as double, t as double, L as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbMinPTBB=snorm((log(L/ST)-MM*t)/(s*sqr(t)))+((L/ST)^(2*MM/s^2))*snorm((log(L/ST)+MM*t)/(s*sqr(t)))
End Function

Function ProbMinPTUB(m as double, s as double, t as double, L as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbMinPTUB=1-(snorm((log(L/ST)-MM*t)/(s*sqr(t)))+((L/ST)^(2*MM/s^2))*snorm((log(L/ST)+MM*t)/(s*sqr(t))))
End Function

Function ProbFPBBMaxPTUB(m as double, s as double, t as double, H as double, K1 as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbFPBBMaxPTUB=((H/ST)^(2*MM/s^2))*snorm((log(K1/ST)-2*log(H/ST)-MM*t)/(s*sqr(t)))  
End Function

Function ProbFPUBMinPTBB(m as double, s as double, t as double, L as double, K2 as double, ST as double) as double
   Dim MM as double
   MM=m-s^2/2
   ProbFPUBMinPTBB=((L/ST)^(2*MM/s^2))*snorm((-log(K2/ST)+2*log(L/ST)+MM*t)/(s*sqr(t)))  
End Function
10:
cls
Color 10
Print "Geometric Brownian Motion Probability Formulas - (c) 2005 gbosmis@yahoo.com"
Print
Color 15
Print "(1) Probability at time t the price is below the H barrier."
Print "(2) Probability at time t the price is above the H barrier."
Print "(3) Probability at any time until t max price is crossing above the H barrier."
Print "(4) Probability at any time until t max price is below the H barrier."
Print "(5) Probability at any time until t min price is crossing bellow the L barrier."
Print "(6) Probability at any time until t min price is above the L barrier."
Print "(7) Probability final price is below K1 and max price is crossing above the H barrier."
Print "(8) Probability final price is above K2 and min price is crossing below the L barrier."
Print
20:
Dim choise
Input "Your choise (1-8) ... ";choise
' m drift
' s standard deviation
' t time
' H up barrier
' L down barrier
' K1,K2 barriers
' ST starting price
Dim m as double, s as double, t as double, H as double, L as double, K1 as double, K2 as double, ST as double
Print
Select Case choise
Case 1
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Up Barrier H ... ";H
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbPTBB(m,s,t,H,ST)
Case 2
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Up Barrier H ... ";H
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbPTUB(m,s,t,H,ST)  
Case 3
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Up Barrier H ... ";H
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbMaxPTUB(m,s,t,H,ST)      
Case 4
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Up Barrier H ... ";H
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbMaxPTBB(m,s,t,H,ST)    
Case 5
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Down Barrier L ... ";L
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbMinPTBB(m,s,t,L,ST)    
Case 6
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Down Barrier L ... ";L
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbMinPTUB(m,s,t,L,ST)  
Case 7
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Up Barrier H ... ";H
   Input "K1 Barrier ... ";K1
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbFPBBMaxPTUB(m,s,t,H,K1,ST)    
Case 8
   Input "Drift (mean return per year) ... ";m
   Input "Volatility ... ";s
   Input "Time (years) ... ";t
   Input "Down Barrier L ... ";L
   Input "K2 Barrier ... ";K2
   Input "Starting Price ... ";ST
   Print
   Print "prob = ";ProbFPUBMinPTBB(m,s,t,L,K2,ST)    
Case else
   Print "Please enter a choise between 1 - 8 ...."
   goto 20:
End Select

Dim eyn as string
Print
Input "Exit (y/n) ... ";eyn
if eyn="n" then goto 10:
end
Reply
#2
Cool if it works, but instead of repeating all that code in the SELECT CASE, you can just do:
[syntax="freebasic"]
if choise >= 1 and choice <= 8 then
Input "Drift (mean return per year) ... ";m
Input "Volatility ... ";s
Input "Time (years) ... ";t
Input "Up Barrier H ... ";H
Input "Starting Price ... ";ST
Print
Print "prob = ";ProbPTBB(m,s,t,H,ST)
else
Print "Please enter a choise between 1 - 8 ...."
goto 20:
end if
[/syntax]
Reply
#3
Quote:Cool if it works, but instead of repeating all that code in the SELECT CASE, you can just do:

Not quite. He is calling different subs in each case. So only the INPUTs would be identical.

The "Print "prob = ";Prob..." lines would still have to be in separate cases.
ric Carr
Reply
#4
Oh yeah, sorry, I missed that. Nevermind me.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)