3

I'm a noob at MQL4 and I am writing my first EA.

My goal is to get the variables of the +DI and -DI of the ADX Indicator.
I used the iADX() function as shown here:

double a;

int OnInit() {

    a = iADX( NULL, 0, 0, PRICE_CLOSE, MODE_PLUSDI, 0 );

    Alert( a );
}

But it keeps printing 0.0

Did I enter the parameters wrong on the iADX() function?

I'd just like to pull the values of +DI and -DI so I can use them in my code.

  • Hi Chance. When you get a moment, please review the usage guidelines for Stack Overflow, and in particular note that other people may edit your posts. We tend to edit out salutations, thanks in advance, explicit requests for help, etc - they're "fluff" that don't add anything other than more words to read. Would you wind it back, please? – halfer Oct 07 '16 at 07:33
  • 1
    @halfer thanks, I didn't know. Thanks for the info! – Chance Joseph Watkins Oct 08 '16 at 02:32

2 Answers2

1

Syntax first:

double iADX( string symbol,        // BEST AS: _Symbol
             int    timeframe,     // BEST AS: one of {}-ENUMs ~ PERIOD_CURRENT
             int    period,        //          averaging period 
             int    applied_price, // BEST AS: one of {}-ENUMs ~ PRICE_CLOSE
             int    mode,          // BEST AS: one of {}-ENUMs ~ MODE_PLUSDI
             int    shift          //          shift 
             );

Why 0.0?

Once we read into the calling interface, requirement to average the selected sequence of PRICE_CLOSE records, kept for the current Symbol() ( NULL ) seems fair, but just notice, that doing that for zero-consecutive bars instructs to do nothing, instead of taking some reasonable calculus of SUM( Close[i..j] )/period to allow for any meaningful processing.

Experiment with non-zero periods and you are back on the rails, aiming towards your goals.

double DI_plus,
       DI_minus;
int    ADX_PERIOD = 8;

int    OnInit() {
       ObjectCreate( ChartID(), "GUI-SHOW+DI", ... );               // LABEL for +DI
       ObjectCreate( ChartID(), "GUI-SHOW-DI", ... );               // LABEL for -DI
       }

int    OnTick() {

       DI_plus  = iADX( _Symbol,
                        PERIOD_CURRENT,
                        ADX_PERIOD,
                        PRICE_CLOSE,
                        MODE_PLUSDI,
                        0
                        );
       DI_minus = iADX( _Symbol,
                        PERIOD_CURRENT,
                        ADX_PERIOD,
                        PRICE_CLOSE,
                        MODE_MINUSDI,
                        0
                        );
       ObjectSetString( Chart_ID(),
                        "GUI-SHOW+DI",
                        OBJPROP_TEXT,
                        StringFormat("+DI %12.6f", DI_plus )
                        );
       ObjectSetString( Chart_ID(),
                        "GUI-SHOW-DI",
                        OBJPROP_TEXT,
                        StringFormat("-DI %12.6f", DI_minus )
                        );
       }
halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92
0

ADX has one parameter - its period. and you use indicator with period = 0, in such case it returns zeros...

double a;
int period = 14;

int OnInit() {
   a = iADX( NULL, 0, period, PRICE_CLOSE, MODE_PLUSDI, 0 );
   Alert( a );
}

Also I am not sure it is a good idea to call indicator in OnInit() - sometimes you do not have bars already loaded and sometimes you have, maybe it is fixed but I remember several months ago my client had such problem especially when changing timeframes.

Daniel Kniaz
  • 4,603
  • 2
  • 14
  • 20