Delta Calculator


Public Sub McDelta(Fprice As Double, Days As Double, Strike As Double, Cp As String,  Prem As Double, Vol As Double, Delta As Double)    ' CALCULATES ,delta etc using Black / Scholes algorithm
    '   FPRICE    - futures price
    '   DAYS      - number of days to expiry
    '   STRIKE    - strike price
    '   CP        -  'C'all OR 'P'ut
    '   PREM      - premium   
   '   DELTA     - set = -99999 to indicate want PREM/DELTA to be calculated..
    ' Returns:
    '   VOL   - implied volatility
    '   DELTA - delta factor
    ' Returns:
    '   PREM  - premium
    '   DELTA - delta factor
    Dim OptPrem As Boolean
    Dim W As Double
    Dim T As Double
    Dim TT As Double
    Dim Temp9 As Double
    Dim K As Double
    Dim X As Double
    Dim M As Double
    Dim N1 As Double
    Dim N2 As Double
    Dim QQ As Double
    Dim CallPrem As Double
    Dim PutPrem As Double
    Dim Vvol As Double
    Vol = 0    Delta = 0
    OptPrem = False
    If Delta = -99999 Then OptPrem = -1
    Delta = 0
    If OptPrem = False Then Vol = 0
    If OptPrem = True Then Prem = 0
    If Fprice <= 0 Then Exit Sub
    If Strike <= 0 Then Exit Sub
    If Days <= 0 Then Exit Sub
    If Vol <= 0 And OptPrem = True Then Exit Sub
    If Cp <> "C" And Cp <> "P" Then Exit Sub
    If Cp = "C" Then W = 0
    If Cp = "P" Then W = 1
    T = Days        ' do not clobber passed data
40  T = T / 365
    TT = Sqr(T)
    If OptPrem = True Then Vol = Vol / 100
    If OptPrem = True Then GoTo 50
    Temp9 = Strike * TT
    If Cp = "C" Then    
      Vol = 0
      If Temp9 <> 0 Then Vol = 2.5 * (Prem - 0.5 * (Fprice - Strike)) / Temp9
    Else                    ' for a PUT
      Vol = 0
      If Temp9 <> 0 Then Vol = 2.5 * (Prem + 0.5 * (Fprice - Strike)) / Temp9
      K = 0
    End If
        ' VOLATILITY is ZERO for a CALL if the difference between the strike and futures price
        '                    is twice the premium when the strike is lower than futures prices
        ' VOLATILITY is ZERO for a PUT if the difference between the strike and futures price
        '                    is twice the premium when the strike is higher than futures prices
    If Vol = 0 Then
      Delta = 0
      Exit Sub
    End If
50  Vvol = Vol * TT
    X = 0
    If Vvol <> 0 Then X = Log(Fprice / Strike) / Vvol + 0.5 * Vvol
    M = 0.39894228082 * Exp(-X ^ 2 / 2)
    If M = 0 Then
      Vol = 0
      Delta = 0
      Exit Sub
    End If
60  If X >= 0 Then
        N1 = FNN(X)
     Else
       N1 = 1 - FNN(-X)
     End If
70  If X >= Vvol Then
       N2 = FNN(X - Vvol)
    Else
      N2 = 1 - FNN(Vvol - X)
    End If
    If OptPrem = True Then GoTo 180
80  QQ = Fprice * N1 - Strike * N2
    QQ = QQ + W * (Strike - Fprice)
    If Abs(Prem - QQ) < 0.001 Then GoTo 100
90  Temp9 = (Fprice * TT * M)
    If Temp9 <> 0 Then Vol = Vol + (Prem - QQ) / Temp9
    If Vol > 0.01 And Vol < 10 Then
      GoTo 50
    Else
      Vol = 0
      Delta = 0
      Exit Sub
    End If
100 Vol = 100 * Vol
    Delta = N1
    If W = 1 Then Delta = -1 * N1        ' for a PUT
    If W = 1 Then Delta = (1 - Abs(Delta)) * -1  
    GoTo 210
180 ' Mode for Option Premiums
    CallPrem = Fprice * N1 - Strike * N2
    Delta = N1
    If W = 1 Then Delta = (1 - Abs(Delta)) * -1
    PutPrem = CallPrem + Strike – Fprice
    Prem = CallPrem
    If W = 1 Then Prem = PutPrem
210 Exit Sub

Function FNN(X As Double) As Double
       FNN = 1 - 0.39894228082 * Exp(-X ^ 2 / 2) * (0.31938153 _
                    / (1 + 0.2316419 * X) - 0.356563782 _
                    / (1 + 0.2316419 * X) ^ 2 + 1.781477937 _
                    / (1 + 0.2316419 * X) ^ 3 - 1.821255978 _
                    / (1 + 0.2316419 * X) ^ 4 + 1.330274429 _
                    / (1 + 0.2319419 * X) ^ 5)
    End Function