Sub V()
'''-----http://www.samurai-logic.com/
'''-----Project of Trading System Development
'''------------------------------------------------------------------
''----B列(日付)、C列(始値)、D列(高値)、E列(安値)、F列(終値)
''----TextBox1に期間1、TextBox2に期間2
''--------------------------------------------------------------------
Dim length1%, length2%, length3%, LastRow&, i&, j%, j2&, Temp!
Application.ScreenUpdating = False
Worksheets("ボラティリティ").Activate
Range("H5:I5000").ClearContents
LastRow = Range("B4").End(xlDown).Row
length1 = CInt(ActiveSheet.TextBox1.Value)
length2 = CInt(ActiveSheet.TextBox2.Value)
Range("H3") = "Volatility:" & length1
Range("I3") = "Volatility:" & length2
For i = length1 + 5 To LastRow
Temp = (WorksheetFunction.Max(CSng(Range("D" & i).Value) - _
CSng(Range("F" & i - 1).Value), _
CSng(Range("F" & i - 1).Value) - _
CSng(Range("E" & i).Value), _
CSng(Range("D" & i).Value) - _
CSng(Range("E" & i).Value))) * 100
If i = length1 + 5 Then
Cells(i, 8).Value = _
(Temp / WorksheetFunction.Average(Range("D" & i, "F" & i).Value))
Else
Cells(i, 8).Value = _
CSng(Cells(i - 1, 8).Value) + _
((Temp / CSng(WorksheetFunction.Average(Range("D" & i, "F" & i).Value)) - _
CSng(Cells(i - 1, 8).Value)) * 2 / (1 + length1))
End If
If i = length2 + 5 Then
Cells(i, 9).Value = _
(Temp / CSng(WorksheetFunction.Average(Range("D" & i, "F" & i).Value)))
ElseIf i > (length2) + 5 Then
Cells(i, 9).Value = CSng(Cells(i - 1, 9).Value) + _
((Temp / CSng(WorksheetFunction.Average(Range("D" & i, "F" & i).Value)) - _
CSng(Cells(i - 1, 9).Value)) * 2 / (1 + length2))
End If
Next
Range("H5", "I" & LastRow).NumberFormatLocal = "0.00"
Application.ScreenUpdating = True
End Sub