Option Explicit

'A Windows API function, used to rename progress graphs.
Private Declare Function MoveFile Lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String) As Long

'How long between updates of the progress graphs
Private Const PROGRESS_GRAPH_UPDATE_INTERVAL% = 50

'This variable allows use to receive events from the RDK library:
Dim WithEvents RDKApp As RDKApplication

'Variables that control the real-time progress graph:
Dim m_ShowProgressGraph As Boolean
Dim m_RealJPGFile As String
Dim m_TempJPGFile As String

'Model Parameters
Dim m_CurrentPrice#
Dim m_PutExercisePrice#
Dim m_PutDuration#
Dim m_RiskFreeRate#
Dim m_ActualGrowthRate#
Dim m_Volatility#
Dim m_PutPrice#

'This function is used to "connect" an instance of this class module to the
'RDKX library, so it can receive RDKX events:
Public Sub Attach(x As RDKApplication)
   Set RDKApp = x
End Sub

'On shutdown of your application, call this function to "disconnect" the
'class module from the RDKX library:
Public Sub Detach()
   Set RDKApp = Nothing
End Sub

'This function initializes this module for the display of the live updating progress graph:
Public Sub InitializeProgressGraph(showProgressGraph As Boolean, outputDir As String)
   m_ShowProgressGraph = showProgressGraph
      
   If showProgressGraph Then
      'save the names of the "temp" and "real" .jpg file destinations.  See the iteration
      'event comments for more information...
      m_TempJPGFile = outputDir & "\temp.jpg"
      m_RealJPGFile = outputDir & "\prog.jpg"
      
      With RDKApp.GraphDefaults
         .RevertToDefaultSettings
         .Destination = RDKJPGFile
         .DestinationFile = m_TempJPGFile
         .PictureHeight = 5000
         .PictureWidth = 6000
         .DisplayMean = True
      End With
   End If
End Sub

'This routine copies the variants passed in from the caller, into a set of module global variable
'which are accessed during a simulation:
Public Sub InitializeModelParameters(currentPrice, putExercisePrice, putDuration, riskFreeRate, actualGrowthRate, volatility, putPrice)
   m_CurrentPrice = CDbl(currentPrice)
   m_PutExercisePrice = CDbl(putExercisePrice)
   m_PutDuration = CDbl(putDuration) / 365
   m_RiskFreeRate = CDbl(riskFreeRate) / 100
   m_ActualGrowthRate = CDbl(actualGrowthRate) / 100
   m_Volatility = CDbl(volatility) / 100
   m_PutPrice = CDbl(putPrice)
End Sub

'This is an RDKX event which is called each iteration of an RDK simulation.
'Each iteration, the two outputs of the model are recalculated.  Also, every
'PROGRESS_GRAPH_UPDATE_INTERVAL iterations, a progress graph is generated.
Private Sub RDKApp_Iteration(whichSim As Long, whichIter As Long, cancel As Boolean)
   Dim dellAtExpiration As Double
   Dim putAtExpiration As Double
   Dim normalDistribution As Double
   Dim exponent As Double
   Dim overlays() As RDKResult
    
   With RDKApp
      'Calculate the value with and without the put
      exponent = (m_ActualGrowthRate - 0.5 * m_Volatility ^ 2) * m_PutDuration + (.Inputs(1)) * m_Volatility * Sqr(m_PutDuration)
      dellAtExpiration = m_CurrentPrice * Exp(exponent)
      putAtExpiration = IIf((dellAtExpiration > m_PutExercisePrice), 0, m_PutExercisePrice - dellAtExpiration)
      .Outputs(1) = 100 * ((dellAtExpiration - m_CurrentPrice) / m_CurrentPrice)
      .Outputs(2) = 100 * ((dellAtExpiration + putAtExpiration - m_CurrentPrice - m_PutPrice) / (m_CurrentPrice + m_PutPrice))
      
      'If necessary, update the progress graph:
      If m_ShowProgressGraph And (whichIter Mod PROGRESS_GRAPH_UPDATE_INTERVAL% = 0) Then
         .GraphDefaults.MainTitle = "Percent Return" & vbLf & whichIter & " Iterations"
         .Outputs(1).Results(whichSim).Graph RDKResultCurveTypeCumulativeAscending, .Outputs(2).Results(whichSim)
         
         'To make sure the progress graph update smoothly on the web, the graph is written to
         'a "temp" .jpg file and then renamed to the "real" .jpg file that a web browser can read.
         'This keeps us from accidentally writing to a file which the web server is in the
         'middle of delivering to a client.  We make use of the Windows API function "MoveFile"
         'to do this.  If the web server is in the middle of serving up the \prog.jpg file,
         'the MoveFile call will fail, and will not interrupt the web server.
         On Error Resume Next
         Kill m_RealJPGFile
         MoveFile m_TempJPGFile, m_RealJPGFile
         On Error GoTo 0
      End If
   End With
End Sub


VB Source Code Publisher R2

© Palisade Corporation, eDecisionTools.com, 2001