Problem calling an indicator from another indicator

To add comments, please log in or register
kbtech
17
kbtech  
I'm trying to get the values from the ATR to use with another indicator unfortunately I'm getting incorrect data (I'm unsure what I'm doing wrong). I've attached a snippet of the indicators code. Thanks.
Files:
Capture.PNG 45 kb
kbtech
17
kbtech  
The ATR values I'm getting back are randomized like 1.2684997926117756E+290, then 3.0494336517580969E-195. I have also tried different indicators, they are all giving the same weird values back.
Vladimir Karputov
Moderator
160423
Vladimir Karputov  
kbtech:
I'm trying to get the values from the ATR to use with another indicator unfortunately I'm getting incorrect data (I'm unsure what I'm doing wrong). I've attached a snippet of the indicators code. Thanks.

Please insert the code correctly.

Reference: how to insert code

The  Code button of the object-insertion menu is intended to insert the source code into the text of the message. An empty window in which to paste the code appears as soon as you click this button. To finish adding the code, click the  Insert button. To cancel the operation, you should click the Cancel button.

Insert source code to a message


Now I will show an example of how to get ATR values from a custom indicator ...

Vladimir Karputov
Moderator
160423
Vladimir Karputov  

Get iATR:

//+------------------------------------------------------------------+
//|                                                     Get iATR.mq5 |
//|                              Copyright © 2019, Vladimir Karputov |
//|                                           http://wmua.ru/slesar/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2019, Vladimir Karputov"
#property link      "http://wmua.ru/slesar/"
#property version   "1.000"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots   1
//--- plot ATRfromATR
#property indicator_label1  "From iATR"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrLime
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int   Inp_ATR_ma_period = 14; // ATR: averaging period
//--- indicator buffers
double      BufferFromiATR[];
double      BufferiATR[];
//---
int         handle_iATR;            // variable for storing the handle of the iATR indicator
int         bars_calculated   = 0;  //  we will keep the number of values in the iATR indicator
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,BufferFromiATR,INDICATOR_DATA);
   SetIndexBuffer(1,BufferiATR,INDICATOR_CALCULATIONS);
//--- set the accuracy of values to be displayed in the Data Window
   IndicatorSetInteger(INDICATOR_DIGITS,Digits());
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,Inp_ATR_ma_period);
//--- name for DataWindow and indicator subwindow label
   string short_name="From iATR("+string(Inp_ATR_ma_period)+")";
   IndicatorSetString(INDICATOR_SHORTNAME,short_name);
//--- an empty value
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0);
//--- create handle of the indicator iATR
   handle_iATR=iATR(Symbol(),Period(),Inp_ATR_ma_period);
//--- if the handle is not created
   if(handle_iATR==INVALID_HANDLE)
     {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Period()),
                  GetLastError());
      //--- the indicator is stopped early
      return(INIT_FAILED);
     }
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//--- number of values copied from the iMA indicator
   int values_to_copy;
//--- determine the number of values calculated in the indicator
   int calculated=BarsCalculated(handle_iATR);
   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 iATR 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 iMABuffer array is greater than the number of values in the iATR indicator for symbol/period, then we don't copy everything
      //--- otherwise, we copy less than the size of indicator buffers
      if(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 iMABuffer array with values of the Moving Average indicator
//--- if FillArrayFromBuffer returns false, it means the information is nor ready yet, quit operation
   if(!FillArrayFromBuffer(BufferFromiATR,handle_iATR,values_to_copy))
      return(0);
//--- memorize the number of values in the Moving Average indicator
   bars_calculated=calculated;
//---
   /*
   main loop
   */
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//| Filling indicator buffers from the MA indicator                  |
//+------------------------------------------------------------------+
bool FillArrayFromBuffer(double &values[],   // indicator buffer of Moving Average values
                         int ind_handle,     // handle of the iMA indicator
                         int amount          // number of copied values
                        )
  {
//--- reset error code
   ResetLastError();
//--- fill a part of the BufferFromiATR array with values from the indicator buffer that has 0 index
   if(CopyBuffer(ind_handle,0,0,amount,values)<0)
     {
      //--- if the copying fails, tell the error code
      PrintFormat("Failed to copy data from the iATR indicator, error code %d",GetLastError());
      //--- quit with zero result - it means that the indicator is considered as not calculated
      return(false);
     }
//--- everything is fine
   return(true);
  }
//+------------------------------------------------------------------+


Result:

Get iATR

Files:
Get_iATR.mq5 12 kb
To add comments, please log in or register