How coulde make indicator only show signal on the first bar?

 

Dear coder,

I am writing an indicator (checking signal from MACD + MACD of highger timflame)

But I could not find the method only add one arrow on the first bar when the signal appear

So the chart as image below.

Could you please check and give me some advice to revise my code.

Thank you so much.

#property copyright "Copyright © 2005, Mr.Sun"
#property link      "mailto:mrsunftu@gmail.com"

#property indicator_separate_window
#property indicator_buffers 6
#property indicator_color1 Blue
#property indicator_color2 Coral
#property indicator_color3 Red
#property indicator_color4 Magenta
#property indicator_color5 clrChocolate
#property indicator_color6 clrSilver


#property indicator_level1     0
#property indicator_levelcolor clrSilver
#property indicator_levelstyle STYLE_SOLID
//#include <kholuutru.mqh>

//---- input parameters
extern int        Fast     =12;
extern int        Slow     =26;
extern int        Signal   =9;
extern int        Fast_TF1     =12;
extern int        Slow_TF1     =26;
extern int        Signal_TF1   =9;

//---- buffers
double MACDLineBuffer[], SignalLineBuffer[], main_TF1[], sign_TF1[];
double SignalDown[];
double SignalUp[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   IndicatorShortName("Check Only one Signal");
   SetIndexBuffer(0,MACDLineBuffer);   SetIndexStyle(0,DRAW_LINE,1,2,Blue);      SetIndexDrawBegin (0,Slow);   
   SetIndexBuffer(1,SignalLineBuffer); SetIndexStyle(1,DRAW_LINE,1,2,clrCoral);  SetIndexDrawBegin (1,Slow+Signal); 
//Arrow 

   SetIndexBuffer(2, SignalDown);      SetIndexStyle(2,DRAW_ARROW,0,1);       SetIndexDrawBegin (2,Slow+Signal);  SetIndexArrow(2, 234);
   SetIndexBuffer(3, SignalUp);        SetIndexStyle(3,DRAW_ARROW,0,1);       SetIndexDrawBegin (3,Slow+Signal);  SetIndexArrow(3,233); 
//Check main + sign off TF1
   SetIndexBuffer(4,main_TF1);         SetIndexStyle(4,DRAW_LINE,2,0,clrBlue);   SetIndexDrawBegin (4,Slow+Signal);
   SetIndexBuffer(5,sign_TF1);         SetIndexStyle(5,DRAW_LINE,2,0,clrCoral);  SetIndexDrawBegin (5,Slow+Signal);
   return(0);
}

int TF1()
   {
      int TF=0;
      switch (Period())
      {
      case 1:       TF= 15;       break;
      case 5:       TF= 30;      break;         
      case 15:      TF= 60;      break;
      case 30:      TF= 240;     break;
      case 60:       TF= 240;    break;
      case 240:      TF= 1440;      break;
      case 1440:     TF= PERIOD_W1; break;
      case 10080:    TF= PERIOD_MN1;break;
      case 43200:    TF= 43200;     break;
      }
   return(TF);
   }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
  int counted_bars = IndicatorCounted();
  if(counted_bars < 0) return(0);
  if(counted_bars > 0) counted_bars--;
  int limit = fmin(Bars - counted_bars,Bars-1);
    
  for(int i=limit; i>=0; i--)
   {
      int shift = iBarShift(Symbol(),TF1(), Time[i], true);
      if (shift == -1) continue;
      {
         main_TF1[i]=      iMACD(Symbol(), TF1(), Fast_TF1, Slow_TF1, 2,PRICE_CLOSE,MODE_SIGNAL,shift);
         sign_TF1[i]=    iMACD(Symbol(), TF1(), Fast_TF1, Slow_TF1, Signal_TF1,PRICE_CLOSE,MODE_SIGNAL,shift);
         if(main_TF1[i] > sign_TF1[i])      int tin_hieu_TF1 =0;
         else                                   tin_hieu_TF1 =1;
      }
      MACDLineBuffer[i] = iMACD(Symbol(),0,Fast,Slow,2,PRICE_CLOSE,MODE_SIGNAL,i);
      SignalLineBuffer[i] = iMACD(Symbol(),0,Fast,Slow,Signal,PRICE_CLOSE,MODE_SIGNAL,i);       

      //SELL signal
      if (MACDLineBuffer[i]<SignalLineBuffer[i])     SignalDown[i]= SignalLineBuffer[i] ;
      
      //BUY signal
      if (MACDLineBuffer[i]>SignalLineBuffer[i])    SignalUp[i+1]= MACDLineBuffer[i] ;    
   }
   return(0);
}







How coulde make indicator only show signal on the first bar?

 
mrsunftu: But I could not find the method only add one arrow on the first bar when the signal appear

When in doubt, think. Before you add an arrow, see if there is already one on the previous bar.

 

I already add the global variable "Count_signal" 
then if signal appear, it's auto counting +1 & mix with condition same like that, but it do not work.

 
int Count_signal=0;
...
int start()
        ...
        //SELL signal
      if (MACDLineBuffer[i]<SignalLineBuffer[i]&& Count_signal<1)
      { 
         SignalDown[i]= SignalLineBuffer[i] ;
         Count_signal++;
      }
      //BUY signal
      if (MACDLineBuffer[i]>SignalLineBuffer[i]&& Count_signal<1)    
      {
         SignalUp[i]= MACDLineBuffer[i] ;    
         Count_signal++;
      }
 

I think I could check signal on the last bar i+1

But it still do not work well, 

      //SELL signal
      if (MACDLineBuffer[i]<SignalLineBuffer[i])
      { 
             // check signal on i+1
             if (SignalDown[i + 1] != EMPTY_VALUE ) continue;  
             else SignalDown[i]= SignalLineBuffer[i] ;            
       }

    
      //BUY signal
      if (MACDLineBuffer[i]>SignalLineBuffer[i])    
      {
             if (SignalUp[i + 1] != EMPTY_VALUE ) continue;  
             else SignalUp[i]= MACDLineBuffer[i] ;    
      }


 
mrsunftu:

Dear coder,

I am writing an indicator (checking signal from MACD + MACD of highger timflame)

But I could not find the method only add one arrow on the first bar when the signal appear

So the chart as image below.

Could you please check and give me some advice to revise my code.

Thank you so much.







Your indicator looks into the future in the historic bars.

For instance, your main TF is on M1 and you are using M15 .

For any bar that has both M1 and M15 concluded in the past , it's form represents the final closed state .

But you are requesting it earlier.

For instance at 15:00 you are requesting the M15 bar which concludes at 15:14:59 and this is the shape it has in the history.