Results 1 to 6 of 6

Thread: Error #Value!, vba code trinomial model, Option Put American

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,887
    Location
    Can't help with math, but in
    For State = 0 To Index
    I think the (state + 2) caused it to run off the edge

    I added a - 2 to the For and it at least runs and doesn't give an error

    Capture.JPG

    Option Explicit
    
    Function Mon_AmericanPut_Trinomial(S As Double, X As Double, T As Double, rf As Double, sigma As Double, n As Long) As Double
    Dim Prix_t_plus_1() As Double, delta_t As Double
    Dim up As Double, down As Double, r As Double
    Dim Prix_t() As Double
    Dim Pu As Double, Pd As Double, Pm As Double
    Dim State As Long, Index As Long
    ReDim Prix_t_plus_1(n)
    delta_t = T / n
    up = Exp(sigma * Sqr(3 * delta_t))
    down = 1 / up
    r = Exp(rf * delta_t)
    Pu = (r - down) / (up - down)
    Pd = (up - r) / (up - down)
    Pm = 1 - Pu - Pd
    For State = 0 To n
         Prix_t_plus_1(State) = Application.Max(X - S * _
         up ^ State * down ^ (n - State), 0)
    Next State
    For Index = n - 1 To 0 Step -1
         ReDim Prix_t(Index)
         For State = 0 To Index - 2      '   <<<<<<<<<<<<<<<<<<<<<
              Prix_t(State) = Application.Max(X - S * up ^ State * down ^ (Index - State), _
              (r) ^ (-1) * ((Pd) * Prix_t_plus_1(State) + (Pm) * Prix_t_plus_1(State + 1) + (Pu) * Prix_t_plus_1(State + 2)))
         Next State
         ReDim Prix_t_plus_1(Index)
         For State = 0 To Index
             Prix_t_plus_1(State) = Prix_t(State)
        Next State
    Next Index
    Mon_AmericanPut_Trinomial = Prix_t(0)
    End Function
    Attached Files Attached Files
    Last edited by Aussiebear; 04-16-2023 at 04:21 PM. Reason: Reduced the whitespace
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •