EA programming help with ADX

To add comments, please log in or register
biqut2
14
biqut2  
I'm new to forex trading but have a background in programming. I have a strategy that has been working reasonable for me but it involves a lot of staring at the screen waiting for just the right time to open a position. I'm working on an EA to automate this but I need to get the values of D+ &D- that are plotted as part of the ADX chart so that I can compare the two. As far as I have been able to find in the documentation the ADX function only provides the value of the ADX line. Any help here would be greatly appreciated.
rv
461
rv  
biqut2 wrote >>
I'm new to forex trading but have a background in programming. I have a strategy that has been working reasonable for me but it involves a lot of staring at the screen waiting for just the right time to open a position. I'm working on an EA to automate this but I need to get the values of D+ &D- that are plotted as part of the ADX chart so that I can compare the two. As far as I have been able to find in the documentation the ADX function only provides the value of the ADX line. Any help here would be greatly appreciated.


https://docs.mql4.com/indicators/iADX
https://docs.mql4.com/constants/lines
William Roeder
22674
William Roeder  
Or you can incorporatate the ADX code into your EA
int init() {
    // I use Exponential averaging for simplicity, 'real' ATR uses simple.
    ATR.EMA         = 2.0/(ATR.Period+1);
    ADX.EMA         = 2.0/(ADX.Period+1);
    for(int shift=3.45*MathMax(ADX.Period, ATR.Period)+1; shift > 0; shift--) {
        CheckNewBar(shift); // Initialize indicators DMI+, DMI-, ADX, and ATR
    }
//...

//+------------------------------------------------------------------+
//| Handle adjustments on the start of a new bar                     |
//+------------------------------------------------------------------+
bool    newBar,                 // Export to start and modifyStops.
        closeAllOrders;         // Export to modifyStops
int     ADX.open.bars,          // Export to Comment/openNew
        tradeThisBar,           // \
        spikeDelay;             //  > Export to openNow
double  DId,                    // /
        ATR,                    // Export to Comment/SetDir
        ADXd,   pDMId,  nDMId;  // Export to Comment only
void    CheckNewBar(int shift=0) {
    static datetime Time0;  newBar = Time[shift] > Time0;   if (!newBar) return;
    Time0 = Time[shift];
    
    double  TR  = MathMax(High[shift+1], Close[shift+2])            // True
                - MathMin( Low[shift+1], Close[shift+2]);           // range.

    // I use EMA for simplicity, the 'real' ATR uses a SMA.
    ATR += ATR.EMA*(TR - ATR);

    if (TR > 0) {                                                       double
        pdm = MathMax(0, High[shift+1] - High[shift+2]),        // Directional
        ndm = MathMax(0,  Low[shift+2] -  Low[shift+1]);        // movement.
        if      (pdm<ndm) pdm=0;    // Only the largest
        else if (ndm<pdm) ndm=0;    // Movement is counted.

        // The ADX and DMI use a 0-100 range. I use 0-1.00 for efficiency
        pDMId += ADX.EMA*(pdm/TR - pDMId);
        nDMId += ADX.EMA*(ndm/TR - nDMId);

        // Average Directional Index averages from DMI
        DId   = pDMId-nDMId;    double div = pDMId+nDMId;           if (div>0)
        ADXd += ADX.EMA*(MathAbs(DId)/div - ADXd);
//...
I use DId and ADXd (0-1) rather than (0-100) ADX=100*ADXd, DI=100*DId
kasinath
168
kasinath  
William Roeder:
Or you can incorporatate the ADX code into your EA I use DId and ADXd (0-1) rather than (0-100) ADX=100*ADXd, DI=100*DId

Hi @William Roeder,

Do you know where can I find the full ADX code that you mention here?

If it is not available, can you please direct me to where I can find an MQL implementation of the ADX?

Thanks.

William Roeder
22674
William Roeder  
kasinath: Do you know where can I find the full ADX code that you mention here?\
If you had used this you would have found this.
          How can I search for indicators and other elements in this forum? - MQL5 programming forum
kasinath
168
kasinath  
William Roeder:
If you had used this you would have found this.
          How can I search for indicators and other elements in this forum? - MQL5 programming forum

Haha, yes. I found it shortly after leaving this reply!

Thank you!

To add comments, please log in or register