$TYPECHECK ON
$APPTYPE GUI
$INCLUDE "RAPIDQ.INC"
CONST GWL_HWNDPARENT = (-8)
CONST HWND_DESKTOP = 0
DECLARE FUNCTION SetWindowLongAPI LIB "user32" ALIAS "SetWindowLongA" (BYVAL hwnd AS LONG, BYVAL nIndex AS LONG, BYVAL dwNewLong AS LONG) AS LONG
DIM BFont AS QFONT
BFont.Name = "Arial"
BFont.AddStyles(fsBold)
BFont.Size=10
DECLARE SUB ButtonClick
CREATE Form AS QFORM
CAPTION = "Probability Calculator"
Width = 300
Height = 260
DelBorderIcons(2)
Center
CREATE Button1 AS QBUTTON
CAPTION = "Execute"
Left = 5
Top = 205
width=100
OnClick=ButtonClick
END CREATE
CREATE Edit1 AS QEDIT
Text = "11"
Left = 170
Top = 10
Width = 100
TabOrder = 1
END CREATE
CREATE Edit2 AS QEDIT
Text = "10"
Left = 60
Top = 40
Width = 100
TabOrder = 2
END CREATE
CREATE Edit3 AS QEDIT
Text = "9"
Left = 170
Top = 70
Width = 100
TabOrder = 3
END CREATE
CREATE Edit4 AS QEDIT
Text = "25"
Left = 170
Top = 120
Width = 100
TabOrder = 4
END CREATE
CREATE Edit5 AS QEDIT
Text = "2"
Left = 170
Top = 145
Width = 100
TabOrder = 5
END CREATE
CREATE Edit6 AS QEDIT
Text = "63"
Left = 170
Top = 170
Width = 100
TabOrder = 6
END CREATE
CREATE Label1 AS QLABEL
Left=50
Top=15
CAPTION="Up Barrier"
Font=BFont
END CREATE
CREATE Label2 AS QLABEL
Left=10
Top=45
CAPTION="Price"
Font=BFont
END CREATE
CREATE Label3 AS QLABEL
Left=50
Top=75
CAPTION="Down Barrier"
Font=BFont
END CREATE
CREATE Label4 AS QLABEL
Left=10
Top=125
CAPTION="Volatility (%)"
Font=BFont
END CREATE
CREATE Label5 AS QLABEL
Left=10
Top=150
CAPTION="Risk Free Rate (%)"
Font=BFont
END CREATE
CREATE Label6 AS QLABEL
Left=10
Top=175
CAPTION="Trading Days"
Font=BFont
END CREATE
END CREATE
SetWindowLongAPI(Form.Handle, GWL_HWNDPARENT, 0)
SetWindowLongAPI(Application.Handle, GWL_HWNDPARENT, Form.Handle)
Form.SHOWMODAL
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
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
SUB ButtonClick
DIM Pr AS DOUBLE
DIM H AS DOUBLE
DIM L AS DOUBLE
DIM V AS DOUBLE
DIM R AS DOUBLE
DIM T AS DOUBLE
Pr=VAL(Edit2.text)
H=VAL(Edit1.text)
L=VAL(Edit3.text)
V=VAL(Edit4.text)/100
R=VAL(Edit5.text)/100
T=VAL(Edit6.text)/252
DIM ans$ AS STRING
ans$=""
ans$="Probability at the end of time period we are above "+Edit1.text+" is "+STR$(ProbPTUB(R,V,T,H,Pr))_
+CHR$(13)+_
"Probability at the end of time period we are below "+Edit3.text+" is "+STR$(ProbPTBB(R,V,T,L,Pr))_
+CHR$(13)+_
"Probability at any time until the end of time period max_price is crossing above "+Edit1.text+" is "+STR$(ProbMaxPTUB(R,V,T,H,Pr))_
+CHR$(13)+_
"Probability at any time until the end of time period min_price is crossing below "+Edit3.text+" is "+STR$(ProbMinPTBB(R,V,T,L,Pr))_
+CHR$(13)
SHOWMESSAGE ans$
END SUB
|
|