$TYPECHECK ON $APPTYPE GUI $INCLUDE "RAPIDQ.INC" '----- API Constants and Functions CONST GWL_HWNDPARENT = (-8) 'Minimize to task bar 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) 'No resizing 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 ' 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 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 '252 trading days per year 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