[in] The symbol name of the security, the data of which should be used to calculate the indicator. The NULL value means the current symbol.

period

[in] The value of the period can be one of the ENUM_TIMEFRAMES values, 0 means the current timeframe.

adx_period

[in] Period to calculate the index.

Return Value

Returns the handle of a specified technical indicator, in case of failure returns INVALID_HANDLE. The computer memory can be freed from an indicator that is no more utilized, using the IndicatorRelease() function, to which the indicator handle is passed.

//+------------------------------------------------------------------+//| Demo_iADX.mq5 |//| Copyright 2011, MetaQuotes Software Corp. |//| https://www.mql5.com |//+------------------------------------------------------------------+#propertycopyright"Copyright 2011, MetaQuotes Software Corp."#propertylink"https://www.mql5.com"#propertyversion"1.00"#propertydescription"The indicator demonstrates how to obtain data"#propertydescription"of indicator buffers for the iADX technical indicator."#propertydescription"A symbol and timeframe used for calculation of the indicator,"#propertydescription"are set by the symbol and period parameters."#propertydescription"The method of creation of the handle is set through the 'type' parameter (function type)."#propertyindicator_separate_window#propertyindicator_buffers 3#propertyindicator_plots 3//--- plot ADX#property indicator_label1 "ADX"#property indicator_type1 DRAW_LINE#property indicator_color1 clrLightSeaGreen#property indicator_style1 STYLE_SOLID#property indicator_width1 1//--- plot DI_plus#property indicator_label2 "DI_plus"#property indicator_type2 DRAW_LINE#property indicator_color2 clrYellowGreen#property indicator_style2 STYLE_SOLID#property indicator_width2 1//--- plot DI_minus#property indicator_label3 "DI_minus"#property indicator_type3 DRAW_LINE#property indicator_color3 clrWheat#property indicator_style3 STYLE_SOLID#property indicator_width3 1//+------------------------------------------------------------------+//| Enumeration of the methods of handle creation |//+------------------------------------------------------------------+enum Creation { Call_iADX, // use iADX Call_IndicatorCreate // use IndicatorCreate };//--- input parametersinput Creation type=Call_iADX; // type of the function inputint adx_period=14; // period of calculationinputstringsymbol=" "; // symbolinputENUM_TIMEFRAMESperiod=PERIOD_CURRENT; // timeframe//--- indicator buffersdouble ADXBuffer[];double DI_plusBuffer[];double DI_minusBuffer[];//--- variable for storing the handle of the iADX indicatorint handle;//--- variable for storingstring name=symbol;//--- name of the indicator on a chartstring short_name;//--- we will keep the number of values in the Average Directional Movement Index indicatorint bars_calculated=0;//+------------------------------------------------------------------+//| Custom indicator initialization function |//+------------------------------------------------------------------+intOnInit() {//--- assignment of arrays to indicator buffersSetIndexBuffer(0,ADXBuffer,INDICATOR_DATA);SetIndexBuffer(1,DI_plusBuffer,INDICATOR_DATA);SetIndexBuffer(2,DI_minusBuffer,INDICATOR_DATA);//--- determine the symbol the indicator is drawn for name=symbol;//--- delete spaces to the right and to the leftStringTrimRight(name);StringTrimLeft(name);//--- if it results in zero length of the 'name' stringif(StringLen(name)==0) {//--- take the symbol of the chart the indicator is attached to name=_Symbol; }//--- create handle of the indicatorif(type==Call_iADX) handle=iADX(name,period,adx_period);else {//--- fill the structure with parameters of the indicator MqlParam pars[1]; pars[0].type=TYPE_INT; pars[0].integer_value=adx_period; handle=IndicatorCreate(name,period,IND_ADX,1,pars); }//--- if the handle is not createdif(handle==INVALID_HANDLE) {//--- tell about the failure and output the error codePrintFormat("Failed to create handle of the iADX indicator for the symbol %s/%s, error code %d", name,EnumToString(period),GetLastError());//--- the indicator is stopped earlyreturn(INIT_FAILED); }//--- show the symbol/timeframe the Average Directional Movement Index indicator is calculated for short_name=StringFormat("iADX(%s/%s period=%d)",name,EnumToString(period),adx_period);IndicatorSetString(INDICATOR_SHORTNAME,short_name);//--- normal initialization of the indicator return(INIT_SUCCEEDED); }//+------------------------------------------------------------------+//| Custom indicator iteration function |//+------------------------------------------------------------------+intOnCalculate(constint rates_total,constint prev_calculated,constdatetime &time[],constdouble &open[],constdouble &high[],constdouble &low[],constdouble &close[],constlong &tick_volume[],constlong &volume[],constint &spread[]) {//--- number of values copied from the iADX indicatorint values_to_copy;//--- determine the number of values calculated in the indicatorint calculated=BarsCalculated(handle);if(calculated<=0) {PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError());return(0); }//--- if it is the first start of calculation of the indicator or if the number of values in the iADX indicator changed//---or if it is necessary to calculated the indicator for two or more bars (it means something has changed in the price history)if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) {//--- if the iADXBuffer array is greater than the number of values in the iADX indicator for symbol/period, then we don't copy everything //--- otherwise, we copy less than the size of indicator buffersif(calculated>rates_total) values_to_copy=rates_total;else values_to_copy=calculated; }else {//--- it means that it's not the first time of the indicator calculation, and since the last call of OnCalculate()//--- for calculation not more than one bar is added values_to_copy=(rates_total-prev_calculated)+1; }//--- fill the array with values of the Average Directional Movement Index indicator//--- if FillArraysFromBuffer returns false, it means the information is nor ready yet, quit operationif(!FillArraysFromBuffers(ADXBuffer,DI_plusBuffer,DI_minusBuffer,handle,values_to_copy)) return(0);//--- form the messagestring comm=StringFormat("%s ==> Updated value in the indicator %s: %d",TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), short_name, values_to_copy);//--- display the service message on the chartComment(comm);//--- memorize the number of values in the Average Directional Movement Index indicator bars_calculated=calculated;//--- return the prev_calculated value for the next callreturn(rates_total); }//+------------------------------------------------------------------+//| Filling indicator buffers from the iADX indicator |//+------------------------------------------------------------------+bool FillArraysFromBuffers(double &adx_values[], // indicator buffer of the ADX linedouble &DIplus_values[], // indicator buffer for DI+double &DIminus_values[], // indicator buffer for DI-int ind_handle, // handle of the iADX indicatorint amount // number of copied values ) {//--- reset error codeResetLastError();//--- fill a part of the iADXBuffer array with values from the indicator buffer that has 0 indexif(CopyBuffer(ind_handle,0,0,amount,adx_values)<0) {//--- if the copying fails, tell the error codePrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false); }//--- fill a part of the DI_plusBuffer array with values from the indicator buffer that has index 1if(CopyBuffer(ind_handle,1,0,amount,DIplus_values)<0) {//--- if the copying fails, tell the error codePrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false); }//--- fill a part of the DI_minusBuffer array with values from the indicator buffer that has index 2if(CopyBuffer(ind_handle,2,0,amount,DIminus_values)<0) {//--- if the copying fails, tell the error codePrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError());//--- quit with zero result - it means that the indicator is considered as not calculatedreturn(false); }//--- everything is finereturn(true); }//+------------------------------------------------------------------+//| Indicator deinitialization function |//+------------------------------------------------------------------+voidOnDeinit(constint reason) {if(handle!=INVALID_HANDLE)IndicatorRelease(handle);//--- clear the chart after deleting the indicatorComment(""); }