Excel VBA code (technical index for trading system)

Excel VBA Source Code --- ストキャスティックRSI

Technical code for trading system.

Sub StchRSI()

'''-----http://www.samurai-logic.com/
'''-----Project of Trading System Development
'''------------------------------------------------------------------
''----B列(日付)、C列(始値)、D列(高値)、E列(安値)、F列(終値)
''----TextBox1にRSIの計算期間、TextBox2にRSIの高値&安値の期間
''----TextBox3にのAverage
''----
''--------------------------------------------------------------------
Dim length1%, length2%, length3%, LastRow&, i&, j%, j2&
Dim P2!, M2!, Temp!, P!, M!, RSIp!, RSIm!

Application.ScreenUpdating = False
 
Worksheets("StchRSI").Activate

LastRow = Range("B4").End(xlDown).Row
    
Range("H5:I5000").ClearContents
  
length1 = CInt(ActiveSheet.TextBox1.Value) 'RSIの計算期間
length2 = CInt(ActiveSheet.TextBox2.Value) 'RSIの高値&安値の期間
length3 = CInt(ActiveSheet.TextBox3.Value) 'Average
  
Range("H3") = "StchRSI:" & length1
Range("I3") = "StchRSI:" & length2
    
  For i = length1 + 5 To LastRow
  
   P = 0: M = 0: RSIp = 0: RSIm = 0
   
   For j = 1 To length1
   
    Temp = Cells(i - (length1) + j, 6).Value - _
           Cells(i - (length1) + j - 1, 6).Value
    
    If Temp > 0 Then
     P = P + Temp
    Else: M = M + Temp
    End If
    
   Next j
   
   If P - M <> 0 Then  'P=0 & M=0
     Cells(i, 9).Value = (P * 100) / (P - M) '52=AZ
    Else: Cells(i, 9).Value = 100
    End If
    
   If i > (length1 + (length2 + 4)) Then
   
    RSIp = _
    WorksheetFunction.Max(Range("I" & i - (length2) + 1, "I" & i))
    RSIm = _
    WorksheetFunction.Min(Range("I" & i - (length2) + 1, "I" & i))
    
     If RSIm <> RSIp Then
       Cells(i, 8).Value = _
               ((Cells(i, 9).Value - RSIm) * 100) / (RSIp - RSIm)
     Else: Cells(i, 8).Value = 50
     End If
     
   End If

  Next i
  
  Range("I5:I" & LastRow).ClearContents
  
  For i = (length1 + (length2 + (4 + length3))) To LastRow
   Cells(i, 9).Value = WorksheetFunction.Average(Range("H" & i), _
                       Range("H" & i - (length3) + 1))

  Next i
  
Range("H5", "I" & LastRow).NumberFormatLocal = "0.00"
  
Application.ScreenUpdating = True
End Sub