Custom indicator returning values for signals that don't exist

 

Greetings again,

My recent efforts at scripting EAs has turned to the use of custom indicators. I have used a number of them without any major problem though these have all been vector based indicators.

My most recent dabbling have been focused on an EMA crossover indicator which paints an object on the chart at the bar where the 'signal' occurs. After knocking up a rough EA I found the indicator (attached below) was calling a signal where there appeared to be none (based on the appearance of a signal and also visual inspection of the EMAs the signal is based on).

After a bit of searching I found a similar thread initiated by Tradingjunky (https://forum.mql4.com/47490/page2). I took a test script from this thread and modified it appropriately (please see below). To my surprise, the indicator was indeed generating a signal (based on output of the below EA) without either painting an object or actually fitting signal requirements.

So I guess the question is, what on earth is going on here? The below script reports when a signal is generated (time, date and price) and which it is (GreenArrow or RedArrow) many of which, as already mentioned, do not align with what I can see on the chart.

Could someone please take a look at this and lend their expertise on the above issues.

Cheers.

int start()
  {
//----
 
//trade once per bar
static datetime Time0;  
bool newBar=Time0 < Time[0];
if (!newBar) return(0);                   
Time0 = Time[0];

 
 double GreenArrow= iCustom(NULL,0,"MA Crossover Alert",false,false,1,5,0,1,8,0,0,1); //Up arrow
 double RedArrow= iCustom(NULL,0,"MA Crossover Alert",false,false,1,5,0,1,8,0,1,1);   //Down arrow
 
 if(GreenArrow!=2147483647)
 Print ("GreenArrow", GreenArrow," @ ", TimeToStr(TimeCurrent(),TIME_DATE)," ",
   TimeToStr(TimeCurrent(),TIME_MINUTES)," on Symbol=",Symbol()," in Period=",Period());
 
 if(RedArrow!=2147483647)
 Print ("RedArrow", RedArrow," @ ", TimeToStr(TimeCurrent(),TIME_DATE)," ",
   TimeToStr(TimeCurrent(),TIME_MINUTES)," on Symbol=",Symbol()," in Period=",Period());

//----
   return(0);
  }


Files:
 

As a side note the value "2147483647" is generated when there is no signal to return or object to paint. Though this value is not mentioned anywhere in the custom indicator I can only assume it is the EMPTY_VALUE.

Also, for those with a keen eye, the "j" and "g" in the indicator file name are added by the form server due to white space in the file name.

Cheers again :)

 
SteepCurve: a signal where there appeared to be none
  1. if ((fastMAnow > slowMAnow) && (fastMAprevious < slowMAprevious)){
       CrossUp[i] = Low[i] - Range*0.75;
    }
    else if ((fastMAnow < slowMAnow) && (fastMAprevious > slowMAprevious)){
       CrossDown[i] = High[i] + Range*0.75;
    }
    
    Bar 0 can easily contain both up and down, since it never clears the other.
  2. On a period change, new history, etc., Build 5xx- didn't clear the existing buffers (but set IndicatorCounted==0) resulting in conflicting buffers in some cases (as it never clears the buffers.) Don't know about 6xx.
  3. No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  4. If you recompile it with B600+ you will have to handle your look back correctly. MA(x) has x lookback.
    Access beyond timeseries (no longer tolerated)
    Lookback handled
       if(counted_bars>0) counted_bars--;
       limit=Bars-counted_bars;
       for(i = 0; i <= limit; i++) {
    
    // Lookback is the largest of Fast/SlowMA_Period
       if(counted_bars < SlowMA_Period) counted_bars = SlowMA_Period;
       for(i = Bars -1 -counted_bars; i >= 0; i--) {
    

 

WHRoeder:


On a period change, new history, etc., Build 5xx- didn't clear the existing buffers (but set IndicatorCounted==0) resulting in conflicting buffers in some cases (as it never clears the buffers.) Don't know about 6xx.


Drawing Buffers with gaps ? That is the case with Build 6xx too. It is still neccessary to code it to clear the buffers in such cases as you mentioned.

As for the indicator in question I've seen some of those jnr indicators before, they were badly coded to start with.

 
WHRoeder:
  1. Bar 0 can easily contain both up and down, since it never clears the other.
  2. On a period change, new history, etc., Build 5xx- didn't clear the existing buffers (but set IndicatorCounted==0) resulting in conflicting buffers in some cases (as it never clears the buffers.) Don't know about 6xx.
  3. No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  4. If you recompile it with B600+ you will have to handle your look back correctly. MA(x) has x lookback.
    Access beyond timeseries (no longer tolerated)
    Lookback handled



SDC:


Drawing Buffers with gaps ? That is the case with Build 6xx too. It is still neccessary to code it to clear the buffers in such cases as you mentioned.

As for the indicator in question I've seen some of those jnr indicators before, they were badly coded to start with.


HI WHRoeder and SDC,


Thank you for your replies.

So I have rectified the bar 0 issue, which I was a little suspicious about to start off with. I mean we've all seen MAs dancing around on bar0 as the price changes.

And I changed the look back functino to that as was suggested. After looking at the link that was forwarded I'm not entirely sure I still understand the brevity of the situation but was rather amused when I got the the last post and saw this after hurting my 'I've been awake for 5 minutes and already trying to comprehend things' brain:

"With the advent of Build 6xx and OnCalculate() for indicators this thread and the function is deprecated. See https://www.mql5.com/en/forum/149695"

I am indeed using B600+ (MT$ B610 and ME5 B887). So this begs the question, where in the indicator would I check what the current buffer values are. In a previous build of Test_Shell the print functions were called as:

static datetime Time0;  
bool newBar=Time0 < Time[0];
if (!newBar) return(0);                   
Time0 = Time[0];

 
 double GreenArrow= iCustom(NULL,0,"MA Crossover Alert",false,false,1,5,0,1,8,0,0,1); //Up arrow
 double RedArrow= iCustom(NULL,0,"MA Crossover Alert",false,false,1,5,0,1,8,0,1,1);   //Down arrow
 
 Print ("GreenArrow", GreenArrow," @ ", TimeToStr(TimeCurrent(),TIME_DATE)," ",
   TimeToStr(TimeCurrent(),TIME_MINUTES)," on Symbol=",Symbol()," in Period=",Period());
 
 Print ("RedArrow", RedArrow," @ ", TimeToStr(TimeCurrent(),TIME_DATE)," ",
   TimeToStr(TimeCurrent(),TIME_MINUTES)," on Symbol=",Symbol()," in Period=",Period());


This resulted in constant printing of "2147483647" until a signal was received. Am I correct in assuming that this values indicates they are not being cleared or is this the EMPTY_VALUE as I previously suggested.

In regard to the Jr Indicator, I cannot take credit for it. Writing indicators are still beyond me due to my Jnr-ness in MQL being quite extreme. I've only been playing with it for the past 6 months or so whilst also churning through a molecular biology PhD so please forgive me if I'm sounding like a complete and utter noob (because I am haha).

With the above changes Test_Shell buffer calls and objects drawn on the chart meet up. So thank you very much.

Could someone please address the question regarding buffer clearing?

 

I have just noticed something else a little odd.

When running the Test_Shell back test, it finds an object at the very beginning (within 3 bars) of the backtest period that isn't there. It does this regardless of the timeframe used. This is the only error I can find at present.

The chart goes back another year or two so I don't think it's an error in the chart. Could it be that there is not enough data in the MA arrays (since indicator start) and thus creating erroneous objects. Should a function similar to that shown below be added to rectify this issue (or would it even rectify it for that matter)

if(Bars<SlowMAPeriod)
return(0);
 
SteepCurve:

This resulted in constant printing of "2147483647" until a signal was received. Am I correct in assuming that this values indicates they are not being cleared or is this the EMPTY_VALUE as I previously suggested.

In regard to the Jr Indicator, I cannot take credit for it. Writing indicators are still beyond me due to my Jnr-ness in MQL being quite extreme. I've only been playing with it for the past 6 months or so whilst also churning through a molecular biology PhD so please forgive me if I'm sounding like a complete and utter noob (because I am haha).

With the above changes Test_Shell buffer calls and objects drawn on the chart meet up. So thank you very much.

Could someone please address the question regarding buffer clearing?


The default value of unused buffer indexes is 2147483647.

The clearing of buffers issue is another issue. I wouldnt worry about that for now. It can affect the way the indicator displays its historical values, it doesnt affect the live prices and alerts generated by them..

Read the headers in the indicator it has the authors name and his website, did you try contacting them ?

 

Thanks for the heads up on the buffer issue. What do you mean by historical prices? Do you mean those displayed in the journal? Or do indicators store their generated data in particular log files somewhere (I'm really starting to show my lack of knowledge here haha)

I haven't contacted the person. I figured it was a little pointless. I've tried contacting others regarding EAs on codebase or indicators from various forums with very little success.

I guess it can't hurt to try though right?

All things aside though the indicator seems to be behaving itself now; in regard to the values returned from the test script anyway :)

Reason: