Clear indicator value on bar 0 (bar[0])

 
I've an indicator using iCustom() returning a double value which in turn generates true/false value stored into a variable (named signal).

On each tick the generated value might change.

Loginc is: when signal's value is true indicator value is set and an arraow is drawn. When value is false indicator values must be set to 0 (zero) and the arrow must disappear.
The code is quite simple:
bool signal;
// signal is set by iCustom() to true or false

if( signal ) buffer[i] = High[i];
 // for bar[0] remove indicator value if signals disappears
if( !signal && i == 0 ) buffer[i] = 0;


The problem is that the arrow does not disappear. What is wrong with the code/logic?

Thank you

I

 
Use in init() section function SetIndicatorEmpty(IndexOfBuffer,0) - "MQL4: SetIndexEmptyValue"

and in start() function add temp variable:
bool signal;
// signal is set by iCustom() to true or false

val=0; // temp variable
if( signal ) val= High[i];
 // for bar[0] remove indicator value if signals disappears
 buffer[i] = val;




 
Thank you Rosh
Use in init() section function SetIndicatorEmpty(IndexOfBuffer,0) - "MQL4: SetIndexEmptyValue"

and in start() function add temp variable:
bool signal;
// signal is set by iCustom() to true or false

val=0; // temp variable
if( signal ) val= High[i];
 // for bar[0] remove indicator value if signals disappears
 buffer[i] = val;




All the sugested are done. value is set to 0 (zero) and signals are working fine.

I'll try to explain the problem another way:

let's use timeframe H4 (other timeframs are also ok)
* chart is initialized ok.
* next tick we have set limit ( Bars-IndicatorCounted() ) to 1 on bar[0] (last bar recalculated)
* signal is triggered and buffer[0] is set to appropriate value.
* last bar is 62562

All these work fine.

* last bar is still 62562
* next tick comes - conditions have changed and there is no more valid signal, so variable is set to false
* because it is false the value of buffer[0] must be set to 0(zero) /buffer[0] =0;/

Problem is that arrow does not disappear until I use menu Chart -> Refresh

Sorry for these long writings...

All the best
I
 
it's very strange. Let publish your code if it's possible. Do you use DRAW_ARROW in SetIndexStyle() - "MQL4: SetIndexStyle" ?
 
it's very strange. Let publish your code if it's possible. Do you use DRAW_ARROW in SetIndexStyle() - "MQL4: SetIndexStyle" ?


Here is
#include <stderror.mqh>
#include <stdlib.mqh>
#property link      ""
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Magenta

extern int    period      = 4;
extern double volfac      = 1.5;
extern string T3MA_Name   = "T3MA";
extern int    arrowSize   = 2;
double        upbuffer[];
double        dnbuffer[];
double        ax[];
int           sep = 10;
int           ba,sa;
bool          _up, _dn;

//+------------------------------------------------------------------+
int init()
{
  if(arrowSize < 1)
    arrowSize = 1;
  if(arrowSize > 5)
    arrowSize = 5;
//---- 1 additional buffer is used for T3MA.
  IndicatorBuffers(3);

//---- drawing settings
  IndicatorDigits(Digits);
  SetIndexStyle(0,DRAW_ARROW,3,2);
  SetIndexArrow(0,233);//This is buy signal
  SetIndexStyle(1,DRAW_ARROW,3,2);
  SetIndexArrow(1,234);
  SetIndexStyle(2, DRAW_NONE);

//---- 3 indicator buffers mapping
  SetIndexBuffer(0, upbuffer);
  SetIndexBuffer(1, dnbuffer);
  SetIndexBuffer(2, ax);

//---- name for DataWindow and indicator subwindow label
  IndicatorShortName("STOCH Signal");
  SetIndexLabel(0,"Buy ");
  SetIndexLabel(1,"Sell ");
  SetIndexLabel(2,NULL);

//---- if not values
  SetIndexEmptyValue(0,0.0);
  SetIndexEmptyValue(1,0.0);
  SetIndexEmptyValue(2,0.0);

  return(0);
}
int deinit()   {     return(0);   }
//+------------------------------------------------------------------+
int start()
{ 
  int i, counted_bars=IndicatorCounted();
  if(counted_bars<0)  { return(-1);  }
  if(counted_bars>0) { counted_bars--; }
  int limit=Bars-counted_bars;

  for(i=0; i<limit; i++) 
    ax[i]=iCustom(NULL,0,T3MA_Name,period,volfac,0,i);  // this is the external indicator I have it as a ex4 - it computes sort of MA

  for(i=0; i<limit; i++)
  {

    if(ax[i]-ax[i+1]<0 && ax[i+1]-ax[i+2]>0)     { _dn=true; } else { _dn=false; }
    if((ax[i]-ax[i+1])>0 && (ax[i+1]-ax[i+2])<0) { _up=true; } else { _up=false; }

//---- sell signal
    if( _dn && dnbuffer[i] == 0 )   {  dnbuffer[i] = High[i]+(sep*Point);  }    /* srote value only of is not has been stored already */
    if( !_dn && i==0 && dnbuffer[0] != 0 )   { dnbuffer[i] = 0;  } /* remove false signal */
//---- buy signal
    if( _up && upbuffer[i] == 0)           {  upbuffer[i] = Low[i]-(sep*Point);   }
    if( !_up && i==0 && upbuffer[i] != 0 )          { upbuffer[i] = 0;  }
  } // end for
  return(0);
}

 
Try it
#include <stderror.mqh>
#include <stdlib.mqh>
#property link      ""
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Magenta

extern int    period      = 4;
extern double volfac      = 1.5;
extern string T3MA_Name   = "T3MA";
extern int    arrowSize   = 2;
double        upbuffer[];
double        dnbuffer[];
double        ax[];
int           sep = 10;
int           ba,sa;
bool          _up, _dn;

//+------------------------------------------------------------------+
int init()
{
  if(arrowSize < 1)
    arrowSize = 1;
  if(arrowSize > 5)
    arrowSize = 5;
//---- 1 additional buffer is used for T3MA.
  IndicatorBuffers(3);

//---- drawing settings
  IndicatorDigits(Digits);
  SetIndexStyle(0,DRAW_ARROW,3,2);
  SetIndexArrow(0,233);//This is buy signal
  SetIndexStyle(1,DRAW_ARROW,3,2);
  SetIndexArrow(1,234);
  SetIndexStyle(2, DRAW_NONE);

//---- 3 indicator buffers mapping
  SetIndexBuffer(0, upbuffer);
  SetIndexBuffer(1, dnbuffer);
  SetIndexBuffer(2, ax);

//---- name for DataWindow and indicator subwindow label
  IndicatorShortName("STOCH Signal");
  SetIndexLabel(0,"Buy ");
  SetIndexLabel(1,"Sell ");
  //SetIndexLabel(2,NULL);

//---- if not values
  SetIndexEmptyValue(0,0.0);
  SetIndexEmptyValue(1,0.0);
  SetIndexEmptyValue(2,0.0);

  return(0);
}
int deinit()   {     return(0);   }
//+------------------------------------------------------------------+
int start()
{ 
  int i, counted_bars=IndicatorCounted();
  if(counted_bars<0)  { return(-1);  }
  if(counted_bars>0) { counted_bars--; }
  if(counted_bars==0) { counted_bars=period; }

  int limit=Bars-counted_bars;
  double val1,val2;
  for(i=limit; i>=0; i--) 
    ax[i]=iCustom(NULL,0,T3MA_Name,period,volfac,0,i);  // this is the external indicator I have it as a ex4 - it computes sort of MA

  for(i=limit; i>=0; i--)
  {
    _dn=false;
    _up=false;
    if(ax[i]-ax[i+1]<0 && ax[i+1]-ax[i+2]>0)    _dn=true;
    if((ax[i]-ax[i+1])>0 && (ax[i+1]-ax[i+2])<0)  _up=true;

//---- sell signal
    if( _dn  )  val1= High[i]+(sep*Point);    /* srote value only of is not has been stored already */
//---- buy signal
    if( _up )          val2= Low[i]-(sep*Point); 
    upbuffer[i]=val1;
    dnbuffer[i]=val2;
  } // end for
  return(0);
}
 
Thank you Rosh, I will and let you know the result.

Thank you

I
 
Hello Rosh

Unfortunately still the same. After signal is generated and the arrow is drawn and
if the move turn to opposite direction (.i.e. signal disappear and is not more valid)
arrow does not disappear as it must.

It does only after:
* I apply the indicator to the chart again;
* edit indicator and press enter
* apply a new template with indicator

Continue looking for a decision...
 
Well, I'll see it as soon as possible.
 
Sorry :) I forgot null-initialization on each loop.
//+------------------------------------------------------------------+
//|                                                          ivo.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                            http://www.metaquotes.net/forum/3002/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/forum/3002/"

#include <stderror.mqh>
#include <stdlib.mqh>
#property link      ""
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Yellow
#property indicator_color2 Magenta

extern int    period      = 4;
extern double volfac      = 1.5;
extern string T3MA_Name   = "T3MA";
extern int    arrowSize   = 2;
double        upbuffer[];
double        dnbuffer[];
double        ax[];
int           sep = 10;
int           ba,sa;
bool          _up, _dn;

//+------------------------------------------------------------------+
int init()
{
  if(arrowSize < 1)
    arrowSize = 1;
  if(arrowSize > 5)
    arrowSize = 5;
//---- 1 additional buffer is used for T3MA.
  IndicatorBuffers(3);

//---- drawing settings
  IndicatorDigits(Digits);
  SetIndexStyle(0,DRAW_ARROW,3,2);
  SetIndexArrow(0,233);//This is buy signal
  SetIndexStyle(1,DRAW_ARROW,3,2);
  SetIndexArrow(1,234);
  SetIndexStyle(2, DRAW_NONE);

//---- 3 indicator buffers mapping
  SetIndexBuffer(0, upbuffer);
  SetIndexBuffer(1, dnbuffer);
  SetIndexBuffer(2, ax);

//---- name for DataWindow and indicator subwindow label
  IndicatorShortName("STOCH Signal");
  SetIndexLabel(0,"Buy ");
  SetIndexLabel(1,"Sell ");
  //SetIndexLabel(2,NULL);

//---- if not values
  SetIndexEmptyValue(0,0.0);
  SetIndexEmptyValue(1,0.0);
  SetIndexEmptyValue(2,0.0);

  return(0);
}
int deinit()   {     return(0);   }
//+------------------------------------------------------------------+
int start()
{ 
  int i, counted_bars=IndicatorCounted();
  if(counted_bars<0)  { return(-1);  }
  if(counted_bars>0) { counted_bars--; }
  if(counted_bars==0) { counted_bars=period; }

  int limit=Bars-counted_bars;
  double val1,val2;
  for(i=limit; i>=0; i--) 
    //ax[i]=iCustom(NULL,0,T3MA_Name,period,volfac,0,i);  // this is the external indicator I have it as a ex4 - it computes sort of MA
     ax[i]=iMA(NULL,0,period,0,MODE_SMA,PRICE_CLOSE,i);  // this is simple MA instead of absent T3MA indicator
  for(i=limit; +i>=0; i--)
  {
    _dn=false;
    _up=false;
    val1=0;
    val2=0;
    if(ax[i]-ax[i+1]<0 && ax[i+1]-ax[i+2]>0)    _dn=true;
    if((ax[i]-ax[i+1])>0 && (ax[i+1]-ax[i+2])<0)  _up=true;

//---- sell signal
    if( _dn  )  val1= High[i]+(sep*Point);    /* srote value only of is not has been stored already */
//---- buy signal
    if( _up )          val2= Low[i]-(sep*Point); 
    upbuffer[i]=val1;
    dnbuffer[i]=val2;
  } // end for
  return(0);
}


Now it works properly.

 
Thank you for your replay

Sorry :) I forgot null-initialization on each loop.

No problem I did it, but this does not resolve the problem.

Try it on a lower time frame i.e. M5 or M15

Look on the current bar (bar[0]) especially after arrow appears and move goes against the arrow...

I'm testing it all the time and the only thing that comes in my mind is that ticks are too often and sceen cannot redraw accordingly.
When I force it (applying indicator or template) it puts everything on the right place.

All the best

I
Reason: