Guidance
指路人
g.yi.org
software / rapidq / Examples / Algorithm & Maths / ProbabilityCalc.bas

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
     $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
掌柜推荐
 
 
¥738.00 ·
 
 
¥1,480.00 ·
 
 
¥397.00 ·
 
 
¥950.00 ·
 
 
¥264.00 ·
 
 
¥1,005.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2009-07-25 17:30:54