choubix
06-25-2008, 03:35 AM
hello,
I have the following code that generates random prices. (code is not mine)
I don't exactly get how the array works (it's my first time with an array in vba)
it seems that the array "Frequency" is initially created.
then it is dimensioned to get 100 values.
then the array is set from 1 to 100 (still empty)
then I don't get he portion of code "For i = 1 to iteration..."
because in the end the array returns in "Example3Result" the correct value of FinalPrice (when it was manipulated in the loop quiet a few time...)
hope someone can explain that to me.
thx!
Dim objSim As Object
Dim CurrentPrice As Double, Volatility As Double, ExpectedReturn As Double
Dim Days As Integer
Dim Iterations As Double, FirstPrice As Double, FinalPrice As Double
Dim Frequency()
Dim i As Single, Gap As Single
Const num = 100
ReDim Frequency(num)
CurrentPrice = Range("Example3Inputs").Cells(1)
Volatility = Range("Example3Inputs").Cells(2)
Days = Range("Example3Inputs").Cells(3)
ExpectedReturn = Range("Example3Inputs").Cells(4)
Iterations = Range("Example3Inputs").Cells(5)
Gap = Range("Gap")
FirstPrice = Range("Example3Results")
For i = 1 To num
Frequency(i) = 0
Next
Set objSim = CreateSimulationObject
For i = 1 To Iterations
FinalPrice = objSim.NextPrice(CurrentPrice, Volatility, ExpectedReturn, Days, 0)
FinalPrice = FinalPrice - FirstPrice + 1
FinalPrice = ((FinalPrice / Gap)) + 1
If FinalPrice >= 1 And FinalPrice < num Then
Frequency(FinalPrice) = Frequency(FinalPrice) + 1
End If
Next
For i = 1 To num
Range("Example3Results").Cells(i, 2) = Frequency(i)
Next
End Sub
I have the following code that generates random prices. (code is not mine)
I don't exactly get how the array works (it's my first time with an array in vba)
it seems that the array "Frequency" is initially created.
then it is dimensioned to get 100 values.
then the array is set from 1 to 100 (still empty)
then I don't get he portion of code "For i = 1 to iteration..."
because in the end the array returns in "Example3Result" the correct value of FinalPrice (when it was manipulated in the loop quiet a few time...)
hope someone can explain that to me.
thx!
Dim objSim As Object
Dim CurrentPrice As Double, Volatility As Double, ExpectedReturn As Double
Dim Days As Integer
Dim Iterations As Double, FirstPrice As Double, FinalPrice As Double
Dim Frequency()
Dim i As Single, Gap As Single
Const num = 100
ReDim Frequency(num)
CurrentPrice = Range("Example3Inputs").Cells(1)
Volatility = Range("Example3Inputs").Cells(2)
Days = Range("Example3Inputs").Cells(3)
ExpectedReturn = Range("Example3Inputs").Cells(4)
Iterations = Range("Example3Inputs").Cells(5)
Gap = Range("Gap")
FirstPrice = Range("Example3Results")
For i = 1 To num
Frequency(i) = 0
Next
Set objSim = CreateSimulationObject
For i = 1 To Iterations
FinalPrice = objSim.NextPrice(CurrentPrice, Volatility, ExpectedReturn, Days, 0)
FinalPrice = FinalPrice - FirstPrice + 1
FinalPrice = ((FinalPrice / Gap)) + 1
If FinalPrice >= 1 And FinalPrice < num Then
Frequency(FinalPrice) = Frequency(FinalPrice) + 1
End If
Next
For i = 1 To num
Range("Example3Results").Cells(i, 2) = Frequency(i)
Next
End Sub