Indicator discrepancy

 
Hi, I am working on implementing strategies developed in MQL4 into python here. The built in ADX function seems to use a different smoothing method than the conventional wilders smoothing method. The values differ by a significant margin(see screenshots). Is there any way to access the source code for the built in iADX() function? I really like the sensitivity of this version of the indicator and would like to implement it in python.
Thanks
Files:
IMG_2503.jpeg  366 kb
IMG_2504.jpeg  252 kb
 
GEORGE E SOLOMOS:
Hi, I am working on implementing strategies developed in MQL4 into python here. The built in ADX function seems to use a different smoothing method than the conventional wilders smoothing method. The values differ by a significant margin(see screenshots). Is there any way to access the source code for the built in iADX() function? I really like the sensitivity of this version of the indicator and would like to implement it in python.
Thanks
Wilders is not conventional. It is called wilders for a reason 
 
Paul Anscombe #:
Wilders is not conventional. It is called wilders for a reason 
Does anyone know the calculation mt4 uses for its iAdx function ?
 
GEORGE E SOLOMOS #:
Does anyone know the calculation mt4 uses for its iAdx function ?

maybe this doc helps https://www.metatrader5.com/en/terminal/help/indicators/trend_indicators/admi

Average Directional Movement Index - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
Average Directional Movement Index Technical Indicator (ADX) helps to determine if there is a price trend. It was developed and described in detail...
 
GEORGE E SOLOMOS #: Does anyone know the calculation mt4 uses for its iAdx function ?

Yes, the calculation it is provided in the official documentation ... ADX = SUM[(+DI-(-DI))/(+DI+(-DI)), N]/N

The example implementation source code is also provided in the CodeBase.

Code Base

Average Directional Movement Index, ADX

MetaQuotes, 2005.12.06 15:10

Average Directional Movement Index Indicator (ADX) helps to determine if there is a price trend.
 

I recently removed a post from this thread. I see now that I misread the original post and that the question is indeed about Python (the reason for the removal).

My apologies to the poster for removing it. It is not possible for me to restore the post, so here is a copy of it.

However, please be aware that it contains some misinformation about the calculation and implementation not being provide by MetaQuotes. It is indeed provide as I indicated in my previous post.

Forum on trading, automated trading systems and testing trading strategies

Deleted comment from "Indicator discrepancy"

Nardus Van Staden, 2024.09.28 23:37

The source code for built-in functions like iADX() in MQL4 is not publicly available. These built-in functions are proprietary to MetaQuotes and are part of the MetaTrader platform's closed-source system. You can attempt to recreate the behavior of the iADX() function by observing how its output differs from the conventional ADX implementation.

Here is an example (long story)

To replicate the behaviour in Python, you could try:

Since you like the sensitivity of iADX() , you can experiment with smoothing methods and parameters in Python to approximate its output. You could manually adjust parameters in the Python ADX implementation until the output matches iADX() as closely as possible. Some third-party libraries or indicators might offer an alternative smoothing method or may have reverse-engineered variations of ADX that resemble iADX() more closely. By running both the iADX() function in MQL4 and your ADX implementation in Python, you can examine where they diverge and adjust the Python code to match the behavior. 

  • +DI (Positive Directional Index): Measures upward movement.
  • -DI (Negative Directional Index): Measures downward movement.
  • ADX (Average Directional Index): Measures the strength of the trend.

For example:

At row 9, the ADX value is 71.95, indicating a strong trend.

To adjust the sensitivity to match the MQL4's iADX() behavior, you can experiment with the smoothing method. One possibility is to adjust the exponential moving average (EMA) by changing the alpha or trying other smoothing techniques.

here is an example where i modify the smoothing method by adjusting the alpha value in the ewm function. The default smoothing uses alpha = 1/period for Wilder's smoothing, but we can tweak this to see if it matches the behavior of the iADX() function in MQL4. This is just an example, hope it clears up your query.


import numpy as np
import pandas as pd

# Sample data (high, low, close prices)
data = {
    'High': [45.09, 46.78, 47.03, 46.30, 46.09, 47.34, 46.50, 45.89, 47.11, 48.23],
    'Low': [44.11, 45.23, 45.89, 45.23, 45.01, 45.87, 45.19, 44.23, 45.98, 47.00],
    'Close': [44.98, 46.00, 46.99, 45.89, 46.00, 47.10, 45.89, 45.50, 47.01, 48.12]
}
df = pd.DataFrame(data)

# Parameters
period = 14

# True Range (TR)
df['High-Low'] = df['High'] - df['Low']
df['High-Close'] = abs(df['High'] - df['Close'].shift(1))
df['Low-Close'] = abs(df['Low'] - df['Close'].shift(1))
df['TR'] = df[['High-Low', 'High-Close', 'Low-Close']].max(axis=1)

# Directional Movement (DM)
df['+DM'] = np.where((df['High'] - df['High'].shift(1)) > (df['Low'].shift(1) - df['Low']), 
                     np.maximum(df['High'] - df['High'].shift(1), 0), 0)
df['-DM'] = np.where((df['Low'].shift(1) - df['Low']) > (df['High'] - df['High'].shift(1)), 
                     np.maximum(df['Low'].shift(1) - df['Low'], 0), 0)

# Modified Smoothing (you can tweak the alpha value here)
def custom_smoothing(series, period, alpha=None):
    # Default alpha (Wilder's method) is 1/period, but we allow custom alpha for sensitivity tuning
    if alpha is None:
        alpha = 1 / period
    return series.ewm(alpha=alpha, adjust=False).mean()

# Applying the smoothing with custom alpha (try different values like 0.2, 0.3, etc.)
alpha_value = 0.3  # Modify this to experiment with different smoothing sensitivity
df['TR_smooth'] = custom_smoothing(df['TR'], period, alpha=alpha_value)
df['+DM_smooth'] = custom_smoothing(df['+DM'], period, alpha=alpha_value)
df['-DM_smooth'] = custom_smoothing(df['-DM'], period, alpha=alpha_value)

# Calculate +DI and -DI
df['+DI'] = 100 * (df['+DM_smooth'] / df['TR_smooth'])
df['-DI'] = 100 * (df['-DM_smooth'] / df['TR_smooth'])

# Calculate DX and ADX
df['DX'] = 100 * abs(df['+DI'] - df['-DI']) / (df['+DI'] + df['-DI'])
df['ADX'] = custom_smoothing(df['DX'], period, alpha=alpha_value)

# Final output with adjusted smoothing
print(df[['High', 'Low', 'Close', '+DI', '-DI', 'ADX']].tail())