Download MetaTrader 5

Different values for the same iClose(...) of another symbol. Why?

To add comments, please log in or register
Theo998
6
Theo998  
Hello to everyone,

My first question on this forum is about accessing close values of a symbol different than Symbol().

I don't understand how it works since when adding multiple identical indicators (identical input parameters) in the same chart (even on simulation) which uses another symbols values (using iClose(...)) values from iClose differs on each indicator instance.

How does it work? Why, on the same tick/bar, with identical parameters, one instance of an indicator gets X value from iClose(...) and another instance of the same indicator gets a value different from X of the exact same iClose(...)? Does anyone know something about it?

Thanks in advance.
whroeder1
16105
whroeder1  
Theo998:

My first question on this forum is about accessing close values of a symbol different than Symbol().

I don't understand how it works since when adding multiple identical indicators (identical input parameters) in the same chart (even on simulation) which uses another symbols values (using iClose(...)) values from iClose differs on each indicator instance.

How does it work? Why, on the same tick/bar, with identical parameters, one instance of an indicator gets X value from iClose(...) and another instance of the same indicator gets a value different from X of the exact same iClose(...)?
  1. If the symbol/TF isn't already open, a hidden chart is created, and the value retrieved. You must handle 4066/4073 errors. See Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum If it isn't accessed for 10 minutes, it will be removed.
  2. Probably 4073 or assume that Time[i] == iTime(otherPair,tf, i) always use iBarShift.
  3. Same question, previously answered.
Theo998
6
Theo998  

Hi whroeder1,

I'm afraid I don't understand where is the problem or what am I doing wrong.

I'm testing it with a much more simple code, using a custom RSI and adding it to another Symbol chart. I'm opening manually the other Symbol chart to avoid any error. Still it doesn't work. What's wrong with this code? :? Thanks in advance.


EDIT: I'm not getting any error; just wrong values.


//+------------------------------------------------------------------+
//|                                                          RSI.mq4 |
//|                   Copyright 2005-2014, MetaQuotes Software Corp. |
//|                                              https://www.mql4.com |
//+------------------------------------------------------------------+
#property copyright   "2005-2014, MetaQuotes Software Corp."
#property link        "https://www.mql4.com"
#property description "Relative Strength Index"
#property strict

#property indicator_separate_window
#property indicator_minimum    0
#property indicator_maximum    100
#property indicator_buffers    1
#property indicator_color1     DodgerBlue
#property indicator_level1     30.0
#property indicator_level2     70.0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_DOT
//--- input parameters
input string InpSymbol="USDCAD"; // SYMBOL
input int InpRSIPeriod=2; // RSI Period
//--- buffers
double ExtRSIBuffer[];
double ExtPosBuffer[];
double ExtNegBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   string short_name;
//--- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
   SetIndexBuffer(1,ExtPosBuffer);
   SetIndexBuffer(2,ExtNegBuffer);
//--- indicator line
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtRSIBuffer);
//--- name for DataWindow and indicator subwindow label
   short_name="RSI("+string(InpRSIPeriod)+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,short_name);
//--- check for input
   if(InpRSIPeriod<2)
     {
      Print("Incorrect value for input variable InpRSIPeriod = ",InpRSIPeriod);
      return(INIT_FAILED);
     }
//---
   SetIndexDrawBegin(0,InpRSIPeriod);
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Relative Strength Index                                          |
//+------------------------------------------------------------------+
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[])
  {
   int    i,pos;
   double diff;
   
   int Myindex0 = iBarShift(InpSymbol, PERIOD_M1, TimeCurrent(), false);
   int MyRatesTotal = iBars(InpSymbol, PERIOD_M1);
   
//---
   if(Bars<=InpRSIPeriod || InpRSIPeriod<2)
      return(0);
//--- counting from 0 to rates_total
   ArraySetAsSeries(ExtRSIBuffer,false);
   ArraySetAsSeries(ExtPosBuffer,false);
   ArraySetAsSeries(ExtNegBuffer,false);
   ArraySetAsSeries(close,false);
//--- preliminary calculations
   pos=prev_calculated-1;
   if(pos<=InpRSIPeriod)
     {
      //--- first RSIPeriod values of the indicator are not calculated
      ExtRSIBuffer[0]=0.0;
      ExtPosBuffer[0]=0.0;
      ExtNegBuffer[0]=0.0;
      double sump=0.0;
      double sumn=0.0;
      for(i=1; i<=InpRSIPeriod; i++)
        {
         ExtRSIBuffer[i]=0.0;
         ExtPosBuffer[i]=0.0;
         ExtNegBuffer[i]=0.0;
         //diff=close[i]-close[i-1];
         diff=iClose(InpSymbol, PERIOD_M1,i)-iClose(InpSymbol, PERIOD_M1,i-1); //###########################> MODIFIED
         if(diff>0)
            sump+=diff;
         else
            sumn-=diff;
        }
      //--- calculate first visible value
      ExtPosBuffer[InpRSIPeriod]=sump/InpRSIPeriod;
      ExtNegBuffer[InpRSIPeriod]=sumn/InpRSIPeriod;
      if(ExtNegBuffer[InpRSIPeriod]!=0.0)
         ExtRSIBuffer[InpRSIPeriod]=100.0-(100.0/(1.0+ExtPosBuffer[InpRSIPeriod]/ExtNegBuffer[InpRSIPeriod]));
      else
        {
         if(ExtPosBuffer[InpRSIPeriod]!=0.0)
            ExtRSIBuffer[InpRSIPeriod]=100.0;
         else
            ExtRSIBuffer[InpRSIPeriod]=50.0;
        }
      //--- prepare the position value for main calculation
      pos=InpRSIPeriod+1;
     }
//--- the main loop of calculations
   //for(i=pos; i<rates_total && !IsStopped(); i++)
   for(i=pos; i<MyRatesTotal && !IsStopped(); i++) //###########################> MODIFIED
     {
      //diff=close[i]-close[i-1];
      diff=iClose(InpSymbol, PERIOD_M1,i)-iClose(InpSymbol, PERIOD_M1,i-1); //###########################> MODIFIED
      ExtPosBuffer[i]=(ExtPosBuffer[i-1]*(InpRSIPeriod-1)+(diff>0.0?diff:0.0))/InpRSIPeriod;
      ExtNegBuffer[i]=(ExtNegBuffer[i-1]*(InpRSIPeriod-1)+(diff<0.0?-diff:0.0))/InpRSIPeriod;
      if(ExtNegBuffer[i]!=0.0)
         ExtRSIBuffer[i]=100.0-100.0/(1+ExtPosBuffer[i]/ExtNegBuffer[i]);
      else
        {
         if(ExtPosBuffer[i]!=0.0)
            ExtRSIBuffer[i]=100.0;
         else
            ExtRSIBuffer[i]=50.0;
        }
     }
//---
   return(MyRatesTotal); //###########################> MODIFIED
  }
//+------------------------------------------------------------------+
nicholishen
946
nicholishen  
Theo998:

Hi whroeder1,

I'm afraid I don't understand where is the problem or what am I doing wrong.

I'm testing it with a much more simple code, using a custom RSI and adding it to another Symbol chart. I'm opening manually the other Symbol chart to avoid any error. Still it doesn't work. What's wrong with this code? :? Thanks in advance.


EDIT: I'm not getting any error; just wrong values.



I would try using a MqlRates array with CopyTime to see if that fixes your problem.

To add comments, please log in or register