Sharing investing and trading ideas. Helping traders get started.
Advertisement
Showing posts with label Directional Movement. Show all posts
Showing posts with label Directional Movement. Show all posts

Friday, May 4, 2012

Directional Movement - Backtest Results and Implementation Issues

Directional Movement - Backtest Results and Implementation Issues
Discusses basic issues and results when attempting to back-test the Directional Movement formula.

Data: S&P500 Emini Futures
Futures Contract Rollover: Arbitrarily set on second Friday of every Mar, Jun, Sep and Dec
Period: 28 Dec 2008 - 31 Mar 2012 (Hourly Chart)
VBA Code: Method B of Code
Excel Version: 2010

Note: 1) As with the Aroon Indicator, the Directional Movement can be used for other time frames and not necessarily daily data as is the context when I wrote the Directional Movement explanation page.

2) The directional movement indicator refers to an implementation of the directional movement formula. What this page attempts to do is to backtest the directional movement formula, not the directional movement index and also not the directional movement indicator. Return to the technical analysis page for links to the respective indicators, codes and backtest results. 

Click here for background information, formula, calculation steps and the entire VBA code.

There are multiple variations on how to apply an indicator. I seek your understanding that the work done below are experiments and are for information purposes only.

Results Summary
Divergence does not signify that a trend is forming in the hourly charts based on my dataset. I inverted this trading rule and arrived with a strategy that is profitable before transaction costs, as expected. Given the large number of trades generated, the naive trading rule applied will most likely not be profitable after costs. The interesting fact is that this is the second or third indicator I found where the  usual trading rule I read from other sources has to be inverted in order for the strategy to even show before transaction cost profitability.

Procedure in Brief
Pasted the code into Excel with changes made as per comments in code to direct application to ranges with input data (High, Low, Close columns). Screen Shot 1 shows the output (the last four columns).

Screen Shot 1
Detailed Findings and Results

Summary of Trading Rules -

1) Calculate Positive DM Minus Negative DM. Recall that Negative DM is a positive value. Negative refers to the fact that a new low was created in the current bar. 
2) If (Positive DM Minus Negative DM) is greater than 0, Long 
3) If (Positive DM Minus Negative DM) is lower than 0, Short
4) Else maintain long or short position

517 index points of profit (before transaction costs) were generated on a large set of 6,687 trades. Average return is very small at just 0.08 points per trade, which means that such an application will not cover trading costs for most people (I guess?). 59% of trades were profitable. Standard deviation of returns for each trade is 4.93 points. The profit trajectory of this indicator and trading rules is shown in Chart 1. 

Chart 1: Cumulative Profit of Strategy (Before Costs)
Conclusion
Although this application of the DM generated a lot of trades, just like my attempt with the Aroon Indicator, the trading rule in this case did not generate statistically significant profitability based on the student t statistic. The low per trade profitability of this approach negated the statistical effect of a large number of trades in reducing standard error. I end this post at this juncture as we will be using this Directional Movement formula in various forms when backtesting the directional movement index and the directional movement indicator. More can be elaborated then. 

Any suggestions or comments are welcome. 


Like what you have just read? Digg it or Tip'd it.
The objective of Finance4Traders is to help traders get started by bringing them unbiased research and ideas. Since late 2005, I have been developing trading strategies on a personal basis. Not all of these models are suitable for me, but other investors or traders might find them useful. After all, people have different investment/trading goals and habits. Thus, Finance4Traders becomes a convenient platform to disseminate my work...(Read more about Finance4Traders)

Friday, June 19, 2009

Directional Movement

Directional Movement DM
Read on formula, calculation steps and VBA Code. For naive backtest results and findings, visit here

Introduction
The directional movement index is based on the directional movement indicator which is in turn calculated from the directional movement of the period. Directional movement classifies market movements into positve and negative directional movements, represented by +DM and -DM respectively. Users must understand that both +DM and -DM some what like indices that seperately measure the day's upward movement or downward movement. As such, both +DM and -DM are positive numbers. In its current form, the DM serves mainly as an input to the calculation of the directional movement indicator.

+DM = (Hight– Hight-1) OR 0 if either of the following occurs
  1. (Hight-Hight-1) < (Lowt-1-Lowt) - Low fell more than high
  2. Both (Hight-Hight-1) and (Lowt-1-Lowt) are less than 0. - High fell and low rose
-DM = (Lowt-1-Lowt) OR 0 if either of the following occurs
  1. (Hight-Hight-1) > (Lowt-1-Lowt) - High rose more than low
  2. Both (Hight-Hight-1) and (Lowt-1-Lowt) are less than 0. - High fell and low rose
In both Scenario 1 and 2 below, the improvement in high is better than the change in low. Therefore, positive DM, +DM is equal to (Hight – Hight-1), while negative DM, -DM is 0.

In Scenario 3 below, Today’s High is lower than Yesterday’s High and Today’s Low is higher than Yesterday’s Low. Both +DM and –DM are 0.

Most articles do not mention what to do when (Hight – Hight-1) and (Lowt-1 – Lowt) are both positive and of the same value.

Both +DM and -DM are either 0 or positive numbers. They reflect if the market is more bullish and bearish.

Signals are based on divergence between moving averages of the +DM and the -DM. For example, the moving average of the +DM will slope upwards in a bullish market, while the -DM remain flat.

The +DM and -DM is usually used in the computation of the directional index, DI and the average directional movement index, ADX. The +DM and the -DM generally measures ths strength of the upward and downward movements respectively.

VBA Code
Method A uses functions, while Method B uses sub procedures to calculate DM. Method B is faster and more flexible.

Method A
Do note that the DM function is actually an array function. To use it, enter into any cell "=DM([current high], [current low], [previous high], [previous low])". Select the cell and the neigbouring cell right next to it, press F2 and Crtl+Shift+Enter. +DM and -DM will be computed.

'Paste this code into your ThisWorkBook code window in VBA. Right Click This WorkBook in Project Explorer and click View Code.
Private Sub Workbook_Open()
AddUDF
End Sub
'The rest belong to any module

Sub AddUDF()
'Tells Excel to includes these in list of functions, add descriptions to them and create a new 'category called Technical Indicators.
Application.MacroOptions macro:="DM", _
Description:="Returns Positive Directional Movement and Negative Directional Movement" & Chr(10) & Chr(10) & _
"Select current high, current low, last period's high and last period's low.", _
Category:="Technical Indicators"
End Sub

Public Function DM(high, low, highYesterday, lowYesterday)
high0 = high - highYesterday
low0 = lowYesterday - low
Dim result(0, 1 To 2)
If high0 < 0 And low0 < 0 Then
result(0, 1) = 0
result(0, 2) = 0
Else
If high0 > low0 Then
result(0, 1) = high0
result(0, 2) = 0
Else
If high0 < low0 Then
result(0, 1) = 0
result(0, 2) = low0
Else
result(0, 1) = high0
result(0, 2) = low0
End If
End If
End If
DM = result
End Function

To use the DM function, enter into any cell "=DM([current high], [current low], [previous high], [previous low])". Select the cell and the neigbouring cell right next to it, press F2 and Crtl+Shift+Enter. +DM and -DM will be computed.

Method B
Run the DM_1 sub from the Runthis sub.

Sub Runthis()
Dim high As Range, low As Range, close1 As Range
Dim volume As Range, output As Range, n As Long
Dim k As Long
'These are the location of your respective historic data and output
'Setting close and volume is not necessary for the DM_1 sub alone.
Set high = Range("C2:C19632")
Set low = Range("D2:D19632")
Set close1 = Range("E2:E19632")
Set volume = Range("F2:F19632")
Set output = Range("H2:H19632")
DM_1 high, low, output
End Sub

Sub DM_1(high As Range, low As Range, output As Range)
high0 = high(1, 1).Address(False, False)
high1 = high(2, 1).Address(False, False)
low0 = low(1, 1).Address(False, False)
low1 = low(2, 1).Address(False, False)
output(0, 1).Value = "'High-High"
output(0, 2).Value = "'Low-Low"
output(0, 3).Value = "Positive DM"
output(0, 4).Value = "Negative DM"

output(2, 1).Value = "=" & high1 & "-" & high0
output(2, 2).Value = "=" & low0 & "-" & low1

high2 = output(2, 1).Address(False, False)
low2 = output(2, 2).Address(False, False)
output(2, 3).Value = "=IF(OR(" & high2 & "<" & low2 & ",AND(" & high2 & "<0," & low2 & "<0)),0," & high2 & ")"
output(2, 4).Value = "=IF(OR(" & high2 & ">" & low2 & ",AND(" & high2 & "<0," & low2 & "<0)),0," & low2 & ")"
Range(output(2, 1), output(2, 4)).Copy output
Range(output(1, 1), output(1, 4)).ClearContents
End Sub
Other References



Like what you have just read? Digg it or Tip'd it.
The objective of Finance4Traders is to help traders get started by bringing them unbiased research and ideas. Since late 2005, I have been developing trading strategies on a personal basis. Not all of these models are suitable for me, but other investors or traders might find them useful. After all, people have different investment/trading goals and habits. Thus, Finance4Traders becomes a convenient platform to disseminate my work...(Read more about Finance4Traders)