EA getting different readings of indicator between Visual and Optimizer simulations

 

Hi,

I'm facing a strange problem. First, I developed an indicator that has nothing special to it: 4 buffers, two with data and two with colors, that plot buy and sell arrows in the chart. The following picture is the indicator attached to a chart of the WDO$N symbol (Brazil) in 2019:

First

As you can see, no problems. The same indicator works perfectly well in the Strategy Tester, producing the same signals in the same places.

Then I have an EA which loads this indicator and starts operations based in the reading of the arrows: if a strong color is found, starts the trade; if none or light color is found, do nothing. I developed this EA in the last 5 years and have done multiple loading of indicators and tested many strategies, so the system of loading indicators is already very well tested. Nonetheless, the following is the code where the indicator's buy and sell color data are loaded to buffers:

   if (CopyBuffer(tfHandles.IHVal_S0_Test[GlobalIndicatorHandles::IH_S0_Test_Indicator1]
                        ,MW_MRMA_B_BuyIndicationColor
                        ,0
                        ,4
                        ,setupData.IDVal_S0_Test[GlobalSetupsData::ID_S0_Test_Data1].buffer) == -1)
   {
      Print(MagicNumber,": Error while accessing setup ",getSetupName(AS_0_Test)
                                ," first indicator values (",GetLastError(),")");
      
                string errorPos = CODE_PLACE;
                
      glErrors.processCopyBufferError(true,CODE_PLACE);
      
      return false;
   }
   else
      glErrors.processCopyBufferError(false);
      
   if (CopyBuffer(tfHandles.IHVal_S0_Test[GlobalIndicatorHandles::IH_S0_Test_Indicator1]
                        ,MW_MRMA_B_SellIndicationColor
                        ,0
                        ,4
                        ,setupData.IDVal_S0_Test[GlobalSetupsData::ID_S0_Test_Data2].buffer) == -1)
   {
      Print(MagicNumber,": Error while accessing setup ",getSetupName(AS_0_Test)
                                ," first indicator values (",GetLastError(),")");
      
                string errorPos = CODE_PLACE;
                
      glErrors.processCopyBufferError(true,CODE_PLACE);
      
      return false;
   }
   else
      glErrors.processCopyBufferError(false);

And here is where the buffers are read to see if a new operation should start:

         const int ind1Buff1Value1 = int(setupData.IDVal_S0_Test[GlobalSetupsData::ID_S0_Test_Data1].buffer[1]);
         const int ind1Buff2Value1 = int(setupData.IDVal_S0_Test[GlobalSetupsData::ID_S0_Test_Data2].buffer[1]);
         
         Print("ind1Buff1Value1: ",ind1Buff1Value1,"; ind1Buff2Value1: ",ind1Buff2Value1);
         
         if (ind1Buff1Value1 == MW_MRMA_C_ReturnOK)
         {
            Print("Positive");
            return NOD_Positive;
         }
         else if (ind1Buff2Value1 == MW_MRMA_C_ReturnOK)
         {
            Print("Negative");
            return NOD_Negative;
         }

Note: "MW_MRMA_C_ReturnOK" is an enum value for the signal color (strong green or strong red).

So now comes the problem: if I run my EA in the backtesting tool, looking for all history, it starts well and goes well until the date 2019/02/06, from which it stops completely til present day:

image 2

Focusing in the day where the problem happens, 2019-02-06, if I put the optimizer to run between day 05 to day 20, it does a last operation day 05 and nothing more from 06 ownwards:

image 3

When I go for the log, I notice that both the light buy arrow of day 06 and the strong red sell arrow of day 12 where not properly red:

image 4  image 5

This is a strange occurrance: after all, as show above, the code reads the indicator's buffers fine until the mentioned day 06, from which "out of nowhere" it stops working. But here is what makes it more interesting: first, if I put optimizer to start not from day 05, but from day 06, then it runs fine!

image 6

imagem 7  image 8

Furthermore, if I put to start again from day 05 and run the Visual Tester, everything works fine!

imagem 9  img 10

So in summary: for whatever reason, when I run my indicator in the Chart or Visual Tester, everything works fine; when I run the EA loading the indicator in visual mode, everything fine; if I put the EA in optimizer starting at day 2019-02-06, fine; but if I put it to run before that day, 05 or before, then it stops reading the indicator's buffers properly at day 06. 

When trying to find an answer, I though maybe it could be some code that was tester/optimizer only, which I indeed have. But this can't be: all the code is either irrelevant to indicator reading, or happens after an operation already started. Moreover, if there where some bugs there, it doesn't make sense it would only appear in this specific date. I also checked the code between different dates and there was nothing there that could affect indicator reading. I checked the code for calls to "2019", "06" and "6" and found no code that could be leading to problems in this specific day. So now I'm lost and I can't figure out what could be happening :T History quality is 100%, btw.

So, does anyone have a clue on what could be happening? In case more data is needed, just ask, and thanks for any help.

 
I would try to log the data (OHLC or whatever is needed) to a Textfile and do a comparison, to see if the issue is given by the Terminal/API.

Maybe it's a corrupted tick database file?

Any other symbols with such effects?
 

Since I see your logs only have prints at round hours, I guess you're using Open Prices Only OR you're checking the indicator only after a candle opens

The issue should be in the indicator, if it's not a repainter (which I suppose) then I would bet the issue is that the indicator ONLY updates the last candle except in the initialization: when a new candle is created (rates_total is 1 more than prev_calculated) you need to recalculate the last candle too, not only the new candle.

If you're using Open Prices, in visual mode any indicator that is used by your EA gets updated 4 times (OHLC), in non-visual mode it would get updated only at OPEN, so if it's not recalculated at the start of the next candle it would stay with that value. More so, if you don't call the indicator in the EA (CopyBuffer) for 10 candles for example, it would not trigger its OnCalculate event at any point in between (so rates_total COULD be equal to prev_calculated +10 the next time it's called, be careful if your indicator doesn't take that into account: prev_calculated can be more values than exactly 0, rates_total or rates_total-1)


If that's not exactly the issue, it's also very easy that it's something similar (like a CopyBuffer which also copies everything or the last candle only)

 

Hi!

First, thanks for all the replies!

Regarding my problem, I think I fixed it. I decided to revise my indicator's code because of your comments, I noticed a missing line in it: whenever a new bar is formed, a function is called that passes a temporary value to a fixed one which saves what was the last state of the indicator's calculations. Normally this temporary value is put to null before the call of the function that do the calculations, but this time I forgot to put that. The interesting thing is that the addition of that line didn't make any difference in the indicator's results when plotted in a chart, but when running the optimizer, the difference appeared! I guess this is because of the way the OHLC information is feed to the indicator between loading it into a chart and when the optimizer and the visual tester are called. 

Anyway, thanks for the help!