Excel VBA code (technical index for trading system)

Excel VBA Source Code --- Moving Average

Technical code for trading system.

Sub MovingAverage()

'''-----http://www.samurai-logic.com/
'''-----Project of Trading System Development
'''------------------------------------------------------------------
''----B列(日付)、C列(始値)、D列(高値)、E列(安値)、F列(終値)
''----
''----TextBox1に移動平均の期間、TextBox2にずらす日数、
''----OptionButtonは高値、安値、終値の選択
''--------------------------------------------------------------------

Dim length1%, length2%, length3%, LastRow&, i&

Application.ScreenUpdating = False

    Worksheets("MA").Activate
    
    LastRow = (Range("B4").End(xlDown).Row)
        
    Range("H5:H5000").ClearContents

    length1 = CInt(ActiveSheet.TextBox1.Value)  'period of moving average
    length2 = CInt(ActiveSheet.TextBox2.Value)  'Move Forword
    
  If (ActiveSheet.OptionButton1 = True) Then
    length3 = 2             '1 = ClosingPrice, 2 = HightPrice, 3 = LowPrice
  ElseIf (ActiveSheet.OptionButton2.Value = True) Then
    length3 = 3
  Else: length3 = 1
  End If
    Range("H3") = "MA(" & length1 & ":" & length2 & ")"
   
  For i = length1 + 4 To LastRow
  
    If length3 = 1 Then     'ClosingPrice
        Cells(i + (length2), 8).Value = _
            WorksheetFunction.Average(Range("F" & i - (length1) + 1, "F" & i))
            
    ElseIf length3 = 2 Then  'HighPrice
        Cells(i + (length2), 8).Value = _
            WorksheetFunction.Average(Range("D" & i - (length1) + 1, "D" & i))
            
    ElseIf length3 = 3 Then 'LowPrice
        Cells(i + (length2), 8).Value = _
            WorksheetFunction.Average(Range("E" & i - (length1) + 1, "E" & i))
    End If

  Next

    Range("H5", "H" & (LastRow + length2)).NumberFormatLocal = "0"
Application.ScreenUpdating = True
End Sub