Wrong Values

 

Good evening,

I created an EA of Long/Short, that consists in buy and sell from two different symbols that have a good correlation. So, I use an indicator with the ratio, average and bollinger bands. The EA works fine, but the indicator sometimes need to be refreshed to show the correct values. Using the function "GetLastError" I noticed that the problem is the idification of the symbol 2. I thied to use the "CheckLoadHistory" but it also didn't worked. I read and tried a lot of thing to fix it, but I didn't get a good result until now. 

Thank you in advance for any help!!


int OnInit()
  {
//---
   ResetLastError();
   SymbolSelect(symbo1, true);
   SymbolSelect(symbo2, true);  
   Comment(symbo1,"/", symbo1);
}
   
int OnCalculate()
{
double price, price1, price2;  
   
for(int i= MathMin(MathMin(rates_total - period,rates_total - prev_calculated), 750); i>=0; i--)
   {
      //-- PREÇOS DE FECHAMENTO
      Close1_Buffer[i] = iClose(symbo1, Tempo_Grafico, i);
      Close2_Buffer[i] = iClose(symbo2, Tempo_Grafico, i);
      
      //--PREÇO ATIVO 1
      price1 = Close1_Buffer[i];
      
      //--PREÇO ATIVO 2
      price2 = Close2_Buffer[i];
      
      if(price1 > 0 && price2 > 0)
      {
         price = price1 / price2;   
         RatioBuffer [i] = price;
         
         //-- MÉDIA
         double average = 0; 
         for(int j=0; j<period; j++)
         {
            average += RatioBuffer [i+j];
         }
         average /= period;  
         Averege_Buffer[i] = average ;
         
         //--BOLLINGER BANDS
         double StdDev_dTmp=0;
         for(int k=0; k<periodo; k++)
         {
            StdDev_dTmp += MathPow (RatioBuffer[i+k]- Averege_Buffer[i],2);
         }
         
         StdDev_dTmp = MathSqrt(StdDev_dTmp/period);
         DeviBuffer2[i] = StdDev_dTmp;
         //--- upper line
         UpperBuffer[i]=Averege_Buffer[i]+desvio1*DeviBuffer2[i];
         UpperBuffer2[i]=Averege_Buffer[i]+desvio2*DeviBuffer2[i];
         UpperBuffer3[i]=Averege_Buffer[i]+desvio3*DeviBuffer2[i];
         //--- lower line
         LowerBuffer[i]=Averege_Buffer[i]-desvio1*DeviBuffer2[i];
         LowerBuffer2[i]=Averege_Buffer[i]-desvio2*DeviBuffer2[i];
         LowerBuffer3[i]=Averege_Buffer[i]-desvio3*DeviBuffer2[i];
        
         }                    
      } 
   }
}  
 
Luis Eduardo:

I created an EA of Long/Short, that consists in buy and sell from two different symbols that have a good correlation. So, I use an indicator with the ratio, average and bollinger bands. The EA works fine, but the indicator sometimes need to be refreshed to show the correct values. Using the function "GetLastError" I noticed that the problem is the idification of the symbol 2. I thied to use the "CheckLoadHistory" but it also didn't worked. I read and tried a lot of thing to fix it, but I didn't get a good result until now. 

Without full code, nobody knows how you declared/initialized your variables and buffers, and nobody can test run, even if they want to.

 
      //-- PREÇOS DE FECHAMENTO
      Close1_Buffer[i] = iClose(symbo1, Tempo_Grafico, i);
      Close2_Buffer[i] = iClose(symbo2, Tempo_Grafico, i);
  1. You are mixing apples and oranges.

  2. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum

  3.    Comment(symbo1,"/", symbo1);

 
Seng Joo Thio:

Without full code, nobody knows how you declared/initialized your variables and buffers, and nobody can test run, even if they want to.


Thank you Seng Joo. I attached my indicator, so you can check! Sorry for that!!

Files:
 
William Roeder:
  1. You are mixing apples and oranges.

  2. On MT4: Unless the current chart is that specific pair/TF referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4

    The function linked to, opens a hidden chart for the symbol/TF in question (if not already open,) thus updating history, and temporarily placing the symbol on Market Watch (if not already there,) so SymbolInfoDouble(symbol, SYMBOL_BID) or MarketInfo(symbol, MODE_BID) don't also return zero on the first call.

    On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Is it mystical?! It is! - Withdraw - Technical Indicators - MQL5 programming forum
              Timeseries and Indicators Access / Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
              Synchronize Server Data with Terminal Data - Symbols - General - MQL5 programming forum


Thank you William for your reply! 

1. I didn't get that you mean. I need that the information of iClose goes to an array, isn't it the best way to do that? 

2. I read those articles before and this one (https://www.mql5.com/en/code/1251) was the only way to correct the problem, but I would like to understand what's happening, and not just copy and paste.. 

3. That was a litttle mistake when I was transfering the code to post here. Sorry!

Thank you again for your reply, it helped a lot, for sure. I'm gonna read again the articles and keep trying a way to solve the problem. I attached my indicator here, so if you have time to check it and maybe show me a way, i would really appreciate that!!

CheckHistory - Check and load history function
CheckHistory - Check and load history function
  • www.mql5.com
Slightly modified history load function from MetaQuotes.
 
Luis Eduardo:

Thank you Seng Joo. I attached my indicator, so you can check! Sorry for that!!

Hmm... this file does not match the section of code you posted earlier (which looked better than this file, in fact)... but I ran it anyway, and set symbol1 to GBPUSD and symbol2 to EURUSD, and timeframe to M1 so I can see results quickly... and this is what I get:

Red vertical line denotes the time i started running... and looks like all new bars are updated without requiring refresh... am I missing something? What symbols/timeframes did you test on? Also, if you can, how about some screenshots to show what you meant by incorrect values?

 
Luis Eduardo:

Thank you Seng Joo. I attached my indicator, so you can check! Sorry for that!!

Anyway, try this version - I've removed/simplified some parts that seemed redundant/excessive... see if it runs fine (you can find my changes by searching for 'SJT').

Files:
 
Seng Joo Thio:

Anyway, try this version - I've removed/simplified some parts that seemed redundant/excessive... see if it runs fine (you can find my changes by searching for 'SJT').

Thank you Seng Joo! I totally agree you you that this code iswith excessive/redundant loops, thats why I "cleaned" it before post here, hahah. I tried to use these loops to get the right data from the symbols, but I noticed that that was not the problem. The problem was to load the values from Symbol 2. I'll try your version and give you a feedback.

I use it for symbols from the Brazilian Market, with timeframe 1D. 

 
Seng Joo Thio:

Hmm... this file does not match the section of code you posted earlier (which looked better than this file, in fact)... but I ran it anyway, and set symbol1 to GBPUSD and symbol2 to EURUSD, and timeframe to M1 so I can see results quickly... and this is what I get:

Red vertical line denotes the time i started running... and looks like all new bars are updated without requiring refresh... am I missing something? What symbols/timeframes did you test on? Also, if you can, how about some screenshots to show what you meant by incorrect values?

I tried your code and it still didn't work. I did what you said and printed what i was trying to mean. These are before and after refresh.


Files:
10.jpg  308 kb
11.jpg  336 kb
 
Luis Eduardo:

I tried your code and it still didn't work. I did what you said and printed what i was trying to mean. These are before and after refresh.

From the screenshots, it looks like your MediaMovel_Buffer is assigned huge numbers at times... and you may be right to suspect Symbol 2. So perhaps you could add these lines (highlighted) to your code and see if there's any error being printed when the chart looks crazy:

      if (iBars(ativo1,Tempo_Grafico)>i)
      {
         ResetLastError();
         price1 = iClose(ativo1,Tempo_Grafico,i);
         if (_LastError!=0)
            Print ("i=", i, ", price1=", price1, ", price1 error=", _LastError);
      }

      if (iBars(ativo2,Tempo_Grafico)>i)
      {
         ResetLastError();
         price2 = iClose(ativo2,Tempo_Grafico,i);
         if (_LastError!=0)
            Print ("i=", i, ", price2=", price2, ", price2 error=", _LastError);
      }
Reason: