EA printing values that shouldn't be there

 

Ok, I threw the following code together just to look at the output I get from my indicator.

It should print 0.0 at all times unless it sees a trade signal from the indicators buffers. However, it's printing out values at all times and never 0.0.

There are no errors in the compiler or in MT5.

I've been playing with this for days, I've printed out every buffer of the indicator just to be on the same side, and I never get the values I'm looking for.

//+------------------------------------------------------------------+
//|                                                Phoenix Trade.mq5 |
//|                              Copyright 2022, Phoenix Investments |
//|                               https://www.phoenixinvestments.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2022, Phoenix Investments"
#property link      "https://www.phoenixinvestments.com"
#property version   "1.00"

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick(){      
      
      int phoenixHandle = iCustom(_Symbol, _Period, "Phoenix Trend");
      double phoenixSell[], phoenixBuy[];
      CopyBuffer( phoenixHandle, 5, 0, 1, phoenixBuy );
      CopyBuffer( phoenixHandle, 6, 0, 1, phoenixSell );
      ArraySetAsSeries(phoenixBuy, true);
      ArraySetAsSeries(phoenixSell, true);
      
      Print("Buy: ", phoenixBuy[0]);
      Print("Sell: ", phoenixSell[0]); 
}

Here is a SS:

Both the Buy and Sell buffers are empty. Set to 0.0 in the indicator, yet my code is returning a price value.

 

You have a huge and very serious mistake: you create a new indicator handle on every tick.

Remember: according to the MQL5 style, the indicator handle MUST BE CREATED ONCE and done in OnInit.

 
//+------------------------------------------------------------------+
void OnTick(){      
      
      int phoenixHandle = iCustom(_Symbol, _Period, "Phoenix Trend");

Perhaps you should read the manual, especially the examples.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.

They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
          Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
          Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
          How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
          How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
          How to call indicators in MQL5 - MQL5 Articles (2010)

 
Vladimir Karputov #:

You have a huge and very serious mistake: you create a new indicator handle on every tick.

Remember: according to the MQL5 style, the indicator handle MUST BE CREATED ONCE and done in OnInit.

Ah, that's what I get for following tutorials. . .

Ok, I changed that.

   int phoenixHandle;
   
   int OnInit(){
   
      phoenixHandle = iCustom(_Symbol, _Period, "Phoenix Trend");
      
      return (0);   
   }
   

However, it seems like it's giving me the same results.

 
Tristen Shaw #: Ah, that's what I get for following tutorials. . . Ok, I changed that. However, it seems like it's giving me the same results.

Do you remember our previous session?

Do you remember me explaining about checking your Indicator handles?

Do you remember me explaining about checking your CopyBuffer results?

Also, your Data Window is not showing "Last Data" and only showing values under the current mouse position, so be careful where you have your mouse pointer.

One more thing, data in the Data Window is restricted to the number of digits specified by the indicator, but when you retrieve buffer information it has full precision with many more digits.

EDIT: You should also consider if the Indicator itself is generating the correct values or not.

 
Fernando Carreiro #:

Do you remember our previous session?

Do you remember me explaining about checking your Indicator handles?

Do you remember me explaining about checking your CopyBuffer results?

Also, your Data Window is not showing "Last Data" and only showing values under the current mouse position, so be careful where you have your mouse pointer.

One more thing, data in the Data Window is restricted to the number of digits specified by the indicator, but when you retrieve buffer information it has full precision with many more digits.

I do remember our last session. I just thought I could slap something together fast to look at the data. It wasn't meant to be my actual usable code.

However, I will go in and add that now. I guess taking shortcuts is never a good thing.

 
Fernando Carreiro #:

Do you remember our previous session?

Do you remember me explaining about checking your Indicator handles?

Do you remember me explaining about checking your CopyBuffer results?

Also, your Data Window is not showing "Last Data" and only showing values under the current mouse position, so be careful where you have your mouse pointer.

One more thing, data in the Data Window is restricted to the number of digits specified by the indicator, but when you retrieve buffer information it has full precision with many more digits.

EDIT: You should also consider if the Indicator itself is generating the correct values or not.

As for the indicator giving the right values. The red dot below is the buffer that I'm looking at, and it's in the right location. However, the dot itself doesn't show up unless I refresh the chart. Which is confusing. (See code snippet below)

This is the code that sets that buffer. The Alert fires immediately however the buffer doesn't show without a refresh.

if( gDidWaeChange ){         
         buyBuffer[iIndex] = ( barColorBuffer[iIndex] < 1 && (close[iIndex] > amaValueBuffer[iIndex]) ) ? low[iIndex] : 0.0;
         sellBuffer[iIndex] = ( barColorBuffer[iIndex] > 0 && (close[iIndex] < amaValueBuffer[iIndex]) ) ? high[iIndex] : 0.0;
         if(isNewBar())
            Alert("WAE Trend Change");
      }
 
Tristen Shaw #: I do remember our last session. I just thought I could slap something together fast to look at the data. It wasn't meant to be my actual usable code. However, I will go in and add that now. I guess taking shortcuts is never a good thing.

Shortcuts are for those that already know how things work and are aware of the consequences. Always do things the correct way without shortcuts. It will be easier to then spot the bugs for what really matters.

At the moment, the bug could be something else, but since the code is not properly done, we may not see it because our focus is drawn to the obvious stuff.

 
Tristen Shaw #: As for the indicator giving the right values. The red dot below is the buffer that I'm looking at, and it's in the right location. However, the dot itself doesn't show up unless I refresh the chart. Which is confusing. (See code snippet below) This is the code that sets that buffer. The Alert fires immediately however the buffer doesn't show without a refresh.

NO! Your EA code is outputting data for the current most recent data point. The red dot is obviously not at the current bar.

If the red dot is not appearing if you don't refresh the chart then your Indicator is faulty. Fix your indicator first, and test the Indicator completely before attempting the EA.

 
Fernando Carreiro #:

NO! Your EA code is outputting data for the current most recent data point. The red dot is obviously not at the current bar.

If the red dot is not appearing if you don't refresh the chart then your Indicator is faulty. Fix your indicator first, and test the Indicator completely before attempting the EA.

I know that the EA is looking at the most recent data. I was only showing that red dot as an example that the buffer did get printed, and that particular dot was in the correct location. There are no dots in the most current data which is why I was expecting a return of 0.

As for fixing the indicator, that is actually what I was trying to do. I simply wanted to see if the EA would see the data in the indicators buffer to see if it was there but not showing up for some reason which is why I was trying to do it quickly, though I can see that that was not the way to do it.

I'm currently at a loss. I've been staring at my code for days, took a couple days off to look at it with fresh eyes. I just don't understand why it's doing it. The alert is after setting the buffer and it works immediately but the buffer doesn't.

 
Tristen Shaw #:

I know that the EA is looking at the most recent data. I was only showing that red dot as an example that the buffer did get printed, and that particular dot was in the correct location. There are no dots in the most current data which is why I was expecting a return of 0.

As for fixing the indicator, that is actually what I was trying to do. I simply wanted to see if the EA would see the data in the indicators buffer to see if it was there but not showing up for some reason which is why I was trying to do it quickly, though I can see that that was not the way to do it.

I'm currently at a loss. I've been staring at my code for days, took a couple days off to look at it with fresh eyes. I just don't understand why it's doing it. The alert is after setting the buffer and it works immediately but the buffer doesn't.

If your aim is to get the Indicator to work properly, then post that instead of the EA, and explain what the difficulties you are having with it.

When, coding, build it in layers. Never advance to the next step until the previous one is working flawlessly. Don't even attempt "Alerts" (and before that not even the "Buy/Sell" signals) until the underlaying functionality is working exactly as expected with no flaws.

If you attempt to solve to many things at the same time, your focus gets distracted. You get lost and waste time. Focus on one step at the time and only advance to the next one, when it is correct.

Reason: