Sub EMA()
'''-----http://www.samurai-logic.com/
'''-----Project of Trading System Development
'''------------------------------------------------------------------
''----B列(日付)、C列(始値)、D列(高値)、E列(安値)、F列(終値)
''----
''----TextBox1に期間、TextBox2にずらす日数、
''--------------------------------------------------------------------
Dim length1%, length2%, length3%, LastRow&, i&
Application.ScreenUpdating = False
Worksheets("EMA").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
Range("H3") = "EMA(" & length1 & ":" & length2 & ")"
For i = length1 + 4 To LastRow
If i = length1 + 4 Then
Cells(i + (length2), 8).Value = _
WorksheetFunction.Average(Range("F" & i - (length1) + 1, "F" & i))
Else
Cells(i + (length2), 8).Value = Cells(i + (length2) - 1, 8).Value + _
((Cells(i, 6).Value - Cells(i + (length2) - 1, 8)) * 2 / (1 + length1))
End If
Next
Range("H5", "H" & LastRow + (length2)).NumberFormatLocal = "0"
End Sub