Registered User Joined: 3/4/2005 Posts: 40
|
Hi, I'm trying to duplicate TCNet's calculation of ADX and am getting inconsistant results. I benchmarked my calculation against TCNet's by setting the ADX period to 2 (just for testing, of course) and recording the value it produced for Google (GOOG) on 4/29. These are my results:
GOOG calculations for ADX, DX period 2, ADX average period 2
Open High Low Close 1:00 219.28, 219.43, 219.25, 219.43 1:01 219.41, 219.44, 219.27, 219.28 1:02 219.29, 219.38, 219.15, 219.15 1:03 219.15, 219.34, 219.14, 219.29 1:04 219.40, 219.00, 219.40, 219.00
+DM -DM CL True Range 1:00 219.43 219.43 219.43 219.43 1:01 0.01 -0.02 -0.15 0.01 1:02 0.06 -0.08 -0.13 0.06 1:03 -0.04 -0.01 0.14 0.14 1:04 -0.34 0.00 -0.29 0.00
Calculations for +DI, -DI, DX +DI -DI DX ADX TCNet ADX 1:00 N/A N/A N/A N/A 1:01 N/A N/A N/A N/A 1:02 1.0 1.0 0 N/A 1:03 1.0 -0.14 133.0 66.5 78.28 1.04 1.0 -0.05 111.0 122.00 78.26
Note that ADX values from TCNet and mine are not even close!
I followed Wilder's formula and cannot get results to match TCNet's.
Suggestions please.
G
|
|
Registered User Joined: 3/4/2005 Posts: 40
|
As an adjunct to the post just prior to this one, this is the algorithm I used:
Step 1: Calculate direction movement +DM = HIGH_TODAY - HIGH_YESTERDAY -DM = LOW_TODAY - LOW_YESTERDAY
Step 2: Calculate CL variable CL=CLOSE_TODAY-CLOSE_YESTERDAY
Step 3: Calculate true range TRUE_RANGE=MAX( +DM, -DM, CL )
Step 4: Sum +DM, -DM, TRUE_RANGE, and compute +DI, -DI +DI=(SUM(+DM)for i=1 to n) / (SUM(TRUE_RANGE) for i=1 to n) -DI=(SUM(-DM)for i=1 to n) / (SUM(TRUE_RANGE) for i=1 to n)
Step 5: Calculate DX DX = 100 * ((+DI - -DI) / (+DI + -DI))
Step 6: Calculate ADX ADX=(SUM(DX)for i=1 to n)/n
+DM = HIGH_TODAY - HIGH_YESTERDAY -DM = LOW_TODAY - LOW_YESTERDAY CL = CLOSE_TODAY - CLOSE_YESTERDAY
Let: n = number of periods TRUE_RANGE = max of +DM, -DM, and CL. HIGH_TODAY = current high HIGH_YESTERDAY = previous high LOW_TODAY = current low LOW_YESTERDAY = previous low CLOSE_TODAY = current close CLOSE_YESTERDAY = previous close
G
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Your algorithm doesn't seem to match Wilder's formula for ADX. True Range is the largest of Today's High minus Today's Low, Today's High minus Yesterday's Close or Yesterday's Close minus Today's Low. Your algorithm also uses simple smoothing throughout while Wilder's formula actually uses Wilder's Smoothing.
Here are some forum topics relating to ADX:
Sorting by the ADX indicator PCF for Wilder's Directional Movement DMI-crossovers PCF for Wilder's DX, and/or a CI for Wilder's ADX Average True Range (ATR) & Stop Losses
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 1/1/2005 Posts: 2,645
|
garp,
Just to start you off correctly, the first three steps are:
Step 1: Calculate direction movement +DM = (H0>H1)*(H0-H1>L1-L0)*(H0-H1) -DM = (L1>L0)*(H0-H1<L1-L0)*(L1-L0)
Step 2: Calculate CL variable Not applicable.
Step 3: Calculate true range TR = (H0-L0+ABS(H0-C1)+ABS(L0-C1))/2
Thanks, Jim Murphy
|
|
Registered User Joined: 3/4/2005 Posts: 40
|
Thanks Bruce, As per your comments, I recomputed my ADX and still not even close to what TC-Net gives me. I appreciate the links but they contain no info as to how ADX is actually calculated. Anyway, my steps:
ADX Calculation
Step 1: Calculate direction movement +DM = HIGH_TODAY - HIGH_YESTERDAY -DM = LOW_TODAY - LOW_YESTERDAY
ALso CL = CLOSE_TODAY - CLOSE_YESTERDAY
Step 2: Calculate CL variable CL=CLOSE_TODAY-CLOSE_YESTERDAY
Step 3: Calculate true range TRUE_RANGE=MAX of( HIGH_TODAY-LOW_TODAY, HIGH_TODAY-CLOSE_YESTERDAY, CLOSE_YESTERDAY-LOW_TODAY )
Step 4: Sum +DM, -DM, TRUE_RANGE, and compute +DI, -DI +DI=(SUM(+DM)for i=1 to n) / (SUM(TRUE_RANGE) for i=1 to n) -DI=(SUM(-DM)for i=1 to n) / (SUM(TRUE_RANGE) for i=1 to n)
Step 5: Calculate DX DX = 100 * ((+DI - -DI) / (+DI + -DI))
Step 6: Calculate ADX as ADX=(SUM(DX)for i=1 to n)/n
Let: n = number of periods TRUE_RANGE = max of +DM, -DM, and CL. HIGH_TODAY = current high HIGH_YESTERDAY = previous high LOW_TODAY = current low LOW_YESTERDAY = previous low CLOSE_TODAY = current close CLOSE_YESTERDAY = previous close
GOOG calculations for ADX, DX period 2, ADX average period 2
Open High Low Close 1:00 219.28 219.43 219.25 219.43 1:01 219.41 219.44 219.27 219.28 1:02 219.29 219.38 219.15 219.15 1:03 219.15 219.34 219.14 219.29 1:04 219.29 219.44 219.29 219.44
Intermediate calculations:
+DM -DM True Range 1:00 219.43 219.43 219.43 1:01 0.01 -0.02 0.17 1:02 0.06 -0.08 0.23 1:03 -0.04 -0.01 0.20 1:04 -0.34 0.00 0.15
Calculations for +DI, -DI, DX +DI -DI DX ADX ADX from TCNet 1:00 N/A N/A N/A N/A N/A 1:01 N/A N/A N/A N/A N/A 1:02 1.0 1.0 0 N/A N/A 1:03 0.18 -0.25 -614 N/A 78.28 1.04 -1.09 -0.22 -66 -33.0 78.26
ADX, calculated as: ADX i = [(ADX (i-1) * (n - 1)) + DX i] / n where n = Smoothing Period
G
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The first link I gave is an alternative to using Personal Criteria Formulas. The second, third and fourth links I gave go into considerable detail on how to calculate ADX, +DI, -DI and ATR using PCF language. Bustermu gave examples of how to calulate +DM, -DM and TR using PCF language in this very thread. I've used these methods and I know they match TeleChart's implementation of ADX.
You seem to want to calculate these indicators by hand however. If this is what you want, and you want to match TeleChart's results, more than n days are required for your calculation. I think 5*(2*n-1) days should produce a decent approximation.
This is not a list of PCFs. You will want to read the earlier links for that. This is just a basic list of the steps to calculate ADX by hand.
n = Period TR = MAX(H-L,H-C1,C1-L) ATRn = (Yesterday_ATRn*(n-1)+TR)/n +DM = IF H>H1 AND H-H1>L1-L THEN H-H1 ELSE 0 -DM = IF L1>L AND L1-L>H-H1 THEN L1-L ELSE 0 +DMIn = (Yesterday_+DMIn*(n-1)+(+DM))/n -DMIn = (Yesterday_-DMIn*(n-1)+(-DM))/n +DIn = 100*(+DMIn)/ATRn -DIn = 100*(-DMIn)/ATRn DXn = 100*ABS((+DIn)-(-DIn))/((+DIn)+(-DIn)) ADXn = (Yesterday_ADXn*(n-1)+DXn)/n ADXRn = (ADXn+nDaysAgo_ADXn)/2
You may alternately calculate ATRn, +DMIn, -DMIn and ADXn as follows:
ATRn = XAVG(TR,(2*n-1)) +DMIn = XAVG(+DM,(2*n-1)) -DMIn = XAVG(-DM,(2*n-1)) ADXn = XAVG(DXn,(2*n-1))
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 3/4/2005 Posts: 40
|
Thanks for the info. You guys rock! I'm trying to do something a bit unusual here, which I'm forced to do because TC doesn't have a native ADX function callable from from a CI. Yeah, you've got that big PCF cited in the link with the caveat that it runs super slow, so it's no use to me as I need to generate ADX values in real time. So I need to calc ADX outside TC, which I'm trying to do in C++ and Java. The problem I'm having is getting the ADX values to match TC's - thus the questions. BTW: so If I specify an average period of 2 (for testing), and a DX of 2, why do I need 5*(2*n-1) bars of data? G
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Wilder’s and Exponential smoothing theoretically use all of the historical data for calculation. Older data has a much smaller influence than newer data on the results however, so you can usually limit the data used and get a reasonably good approximation. 5*(2*n-1) bars is to some extent arbitrary, but TeleChart normally uses at least five times as many bars for calculating an Exponential Moving Average as its period and an n-Period Wilder’s smoothing is the same as an Exponential smoothing with a period of 2*n-1.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 3/4/2005 Posts: 40
|
Thanks Bruce. Clears up the mystery.
There's a typo in the ADX help file. In the line: DX Period - Period used in computing the DX, and it's component +DI, and -DI values.
The "it's" should be "its"
G
|
|
Registered User Joined: 3/4/2005 Posts: 40
|
A question, or confirmation that I'm understanding this right. In Bruce's previous post, the formula:
ATRn = (Yesterday_ATRn*(n-1)+TR)/n
implies that the calculation of the Wilder sum, has as one of its inputs, the sum from the prior sum calculation. Thus, rather than being an isolated sliding window of some period n, the term "Yesterday_ATRn" is the prior calculation. For example, given an abitrary series { 4, 5, 6, 7}, and period 3, the first iteration for computing the wilder sum of 5,6,7 needs as one of its inputs, a value for Yesterday_ATRn, which was obtained when computing the wilder sum for the series 4,5,6.
Confusing, no?
G
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Your understanding appears to be correct.
ATRn = (Yesterday_ATRn*(n-1)+TR)/n
Means, take yesterday's Average True Range and multiply it by one less than the period, then add today's True Range and divide the entire result by the period. This is the easier of the two methods to use if you are doing the calculations by hand (even easier than calculating a Simple Moving Average).
If an Exponential Moving Average function is available, you should probably use the alternate method of calculating ATRn, +DMIn, -DMIn and ADXn listed at the bottom of the post.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 3/4/2005 Posts: 40
|
If there is no yesterday, what is the value for yesterday's average?
G
If you can look into the seeds of time, And say which grain will grow and which will not, Speak then to me
Macbeth WS
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Wilder seeded things with a short Simple Moving Average. 5 Periods I think, but I'm not sure about the term (I'm sure somebody will pipe in with the correct number if it isn't five). Realistically, if you use enough days for the calculations, it doesn't matter if you seed it with the TR for yesterday, a 5-Period Simple Moving Average of TR or even zero, because it should be long enough ago to only marginally affect the result.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 4/7/2010 Posts: 12
|
Bruce,
Can you plz help with 14 period DMI difference formula . The long one kind.
Thanks,
Sid.
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The PCF for Wilder's DX, and/or a CI for Wilder's ADX topic contains a formula for this and the PCF for Wilder's Directional Movement DMI-crossovers topic explores a similar technique and also uses a 14-Period ADX as the basis of its formulas.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Guest-1 |