Excel VBA code (technical index for trading system)

Excel VBA Source Code --- DMI

Technical code for trading system.

Sub DMI()

'''-----http://www.samurai-logic.com/
'''-----Project of Trading System Development
'''------------------------------------------------------------------
''----B列(日付)、C列(始値)、D列(高値)、E列(安値)、F列(終値)
''----TextBox1に+&-&TRの期間、TextBox2にDXの期間
''----TextBox3に移動平均の期間
''--------------------------------------------------------------------
Dim length1%, length2%, length3%, LastRow&, i&, j%, j2&
Dim P!, TempP!, M!, TempM!, TR!, TempTR!
Dim SDX!, TempDX!, Temp!

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

LastRow = Range("B4").End(xlDown).Row

Range("H5:K5000").ClearContents      'シートをクリーンアップ
    
length1 = CInt(ActiveSheet.TextBox1.Value)     '+&-&TRの期間
length2 = CInt(ActiveSheet.TextBox2.Value)      'DXの期間
length3 = CInt(ActiveSheet.TextBox3.Value)      'Aveの期間

Range("H3") = "+DI(" & length1 & ":" & length2 & ")"
Range("I3") = "-DI(" & length1 & ":" & length2 & ")"
Range("J3") = "SDX(" & length3 & ")"
Range("K3") = "SDXR(" & length3 & ")"
    
    
  For i = (length1) + 5 To LastRow
   TR = 0: P = 0: M = 0
   For j = 1 To length1
      TempTR = _
      WorksheetFunction.Max(Cells(i - (length1) + j, 4).Value - _
      Cells(i - (length1) + j - 1, 6).Value, _
      Cells(i - (length1) + j - 1, 6).Value - _
      Cells(i - (length1) + j, 5).Value, _
      Cells(i - (length1) + j, 4).Value - Cells(i - (length1) + j, 5).Value)
      
      TR = TR + TempTR
      
     If (Cells(i - (length1) + j, 4).Value - _
        Cells(i - (length1) + j - 1, 4).Value < _
        0 And Cells(i - (length1) + j - 1, 5).Value - _
        Cells(i - (length1) + j, 5).Value < 0) Or _
        (Cells(i - (length1) + j, 4).Value - _
        Cells(i - (length1) + j - 1, 4).Value = _
        Cells(i - (length1) + j - 1, 5).Value - _
        Cells(i - (length1) + j, 5).Value) Then
        
      P = P + 0
      
     ElseIf (Cells(i - (length1) + j, 4).Value - _
             Cells(i - (length1) + j - 1, 4).Value > _
             Cells(i - (length1) + j - 1, 5).Value - _
             Cells(i - (length1) + j, 5).Value) Then
             
      TempP = Cells(i - (length1) + j, 4).Value - _
              Cells(i - (length1) + j - 1, 4).Value
              
      P = P + TempP
      
     ElseIf (Cells(i - (length1) + j, 4).Value - _
             Cells(i - (length1) + j - 1, 4).Value < _
             Cells(i - (length1) + j - 1, 5).Value - _
             Cells(i - (length1) + j, 5).Value) Then
             
      TempM = Cells(i - (length1) + j - 1, 5).Value - _
              Cells(i - (length1) + j, 5).Value
              
      M = M + TempM
     Else
      TempP = Cells(i - (length1) + j, 4).Value - _
              Cells(i - (length1) + j - 1, 4).Value
      P = P + TempP
      TempM = Cells(i - (length1) + j - 1, 5).Value - _
              Cells(i - (length1) + j, 5).Value
      M = M + TempM
     End If
   Next j
   Cells(i, 8).Value = P / TR * 100
   Cells(i, 9).Value = M / TR * 100
   SDX = 0
  If i > (length1) + (length2 + 6) Then
   For j = 1 To length2
    TempDX = Abs(Cells(i - (length2) + j, 8).Value - _
            Cells(i - (length2) + j, 9).Value) / (Cells(i - _
            (length2) + j, 8).Value + Cells(i - _
            (length2) + j, 9).Value)
            
    SDX = SDX + TempDX
    
   Next j
   
   Cells(i, 10).Value = SDX * 100 / length2
   
  End If
  
  If i > (length1 + (length2 + (length3 + 6))) Then
   Cells(i, 11).Value = (Cells(i, 10).Value + _
                         Cells(i - (length3), 10).Value) / 2

  End If

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



End Sub