indicator refresh problem

 

Hi guys I'm trying to edit an ema-rsi indicator that can show an up or down arrow when 4 ema cross happens and rsi is > or < 50.

My problem is that theese arrows don't refresh for every tick once they appeared, I need to change timeframe if I want to check if conditions are still good to show arrow. Can you tell me where is the problem ? I post the code.

Thank you 

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red

#property indicator_width1 4
#property indicator_width2 4

double CrossUp[];
double CrossDown[];
extern int FasterEMA1     = 6;
extern int SlowerEMA1     = 12;
extern int FasterEMA2     = 7;
extern int SlowerEMA2     = 14;
extern int RSInowPeriod   = 6;
extern int barsBack       = 2000;
extern bool AlertsMessage = true;
extern bool AlertsSound   = true;
extern bool debug         = false;
extern double K           = 1.0 ;

bool EMACrossedUp = false;
bool RSICrossedUp = false;
bool EMACrossedDown = false;
bool RSICrossedDown = false;
int SignalLabeled = 0// 0: initial state; 1: up; 2: down.
int upalert=false,downalert=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0DRAW_ARROWEMPTY);
   SetIndexArrow(0241);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1DRAW_ARROWEMPTY);
   SetIndexArrow(1242);
   SetIndexBuffer(1, CrossDown);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int limit, i, counter;
   double fasterEMA1now, slowerEMA1now, fasterEMA1previous, slowerEMA1previous, fasterEMA2now, slowerEMA2now, fasterEMA2previous, slowerEMA2previous;
   double RSInow;
   double Range, AvgRange;

   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=MathMin(Bars-counted_bars,barsBack);
   
   for(i = limit; i>=0; i--) {
       
      counter=i;
      Range=0;
      AvgRange=0;
      for (counter=i ;counter<=i+9;counter++)
      {
        AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;

      fasterEMA1now = iMA(NULL0, FasterEMA1, 0MODE_EMAPRICE_CLOSE, i);
      fasterEMA1previous = iMA(NULL0, FasterEMA1, 0MODE_EMAPRICE_CLOSE, i+1);
      
      fasterEMA2now = iMA(NULL0, FasterEMA2, 0MODE_EMAPRICE_CLOSE, i);
      fasterEMA2previous = iMA(NULL0, FasterEMA2, 0MODE_EMAPRICE_CLOSE, i+1);
      
      slowerEMA1now = iMA(NULL0, SlowerEMA1, 0MODE_EMAPRICE_CLOSE, i);
      slowerEMA1previous = iMA(NULL0, SlowerEMA1, 0MODE_EMAPRICE_CLOSE, i+1);
      
      slowerEMA2now = iMA(NULL0, SlowerEMA2, 0MODE_EMAPRICE_CLOSE, i);
      slowerEMA2previous = iMA(NULL0, SlowerEMA2, 0MODE_EMAPRICE_CLOSE, i+1);
      
      RSInow=iRSI(NULL,0,RSInowPeriod,PRICE_CLOSE,i);
      
      if (RSInow > 50) {
         if (debug)Print(TimeToStr(Time[i],TIME_DATE)+TimeToStr(Time[i],TIME_SECONDS)+" RSI UP ");
         RSICrossedUp = true;
         RSICrossedDown = false;
      }
      
      if (RSInow < 50) {
         if (debug)Print(TimeToStr(Time[i],TIME_DATE)+TimeToStr(Time[i],TIME_SECONDS)+" RSI DOWN ");
         RSICrossedUp = false;
         RSICrossedDown = true;
      }
      
      if ((fasterEMA1now >= slowerEMA1now) && (fasterEMA1previous < slowerEMA1previous) && (fasterEMA2now >= slowerEMA2now) && (fasterEMA2previous < slowerEMA2previous) ) {
         if (debug)Print(TimeToStr(Time[i],TIME_DATE)+TimeToStr(Time[i],TIME_SECONDS)+" EMA UP ");
         EMACrossedUp = true;
         EMACrossedDown = false;
      }

      if ((fasterEMA1now <= slowerEMA1now) && (fasterEMA1previous > slowerEMA1previous) && (fasterEMA2now <= slowerEMA2now) && (fasterEMA2previous > slowerEMA2previous)) {
         if (debug)Print(TimeToStr(Time[i],TIME_DATE)+TimeToStr(Time[i],TIME_SECONDS)+" EMA DOWN ");
         EMACrossedUp = false;
         EMACrossedDown = true;
      }

      if ((EMACrossedUp == true) && (RSICrossedUp == true) && (SignalLabeled != 1)) {
         CrossUp[i] = Low[i] - K*Range;
         if (debug)Print(TimeToStr(Time[i],TIME_DATE)+TimeToStr(Time[i],TIME_SECONDS)+" SIGNAL UP ");
         if(i<=2 && AlertsMessage && !upalert)
           {
            Alert (Symbol()," ",Period(),"M  BUY SIGNAL ");
            //SendMail("EMA Cross Up on "+Symbol(),"");
            upalert=true;
            downalert=false;
           }           
         if(i<=2 && AlertsSound && !upalert)
           {
            PlaySound("alert.wav");
            upalert=true;
            downalert=false;
           }
         SignalLabeled = 1;
      }

      else if ((EMACrossedDown == true) && (RSICrossedDown == true) && (SignalLabeled != 2)) {
         CrossDown[i] = High[i] + K*Range;
         if (debug)Print(TimeToStr(Time[i],TIME_DATE)+TimeToStr(Time[i],TIME_SECONDS)+" SIGNAL DOWN ");
         if(i<=2 && AlertsMessage && !downalert)
           {
            Alert (Symbol()," ",Period(),"M  SELL SIGNAL ");
            //SendMail("EMA Cross Down on "+Symbol(),"");
            downalert=true;
            upalert=false;
           }
         if(i<=2 && AlertsSound && !downalert)
           {
            PlaySound("alert.wav");
            downalert=true;
            upalert=false;
           }
         SignalLabeled = 2;
      }
   }
   return(0);
}
//end
 

Try setting the buffer to EMPTY_VALUE

ie.



   for(i = limit; i>=0; i--) {
      CrossUp[i] = EMPTY_VALUE;
      CrossDown[i] = EMPTY_VALUE;
 
  1.    int counted_bars=IndicatorCounted();
    //---- check for possible errors
       if(counted_bars<0) return(-1);
    //---- last counted bar will be recounted
       if(counted_bars>0) counted_bars--;

       limit=MathMin(Bars-counted_bars,barsBack);
      
       for(i = limit; i>=0; i--) {
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  2. Do your lookbacks correctly.
    extern int FasterEMA1     = 6;
    extern int SlowerEMA1     = 12;
    extern int FasterEMA2     = 7;
    extern int SlowerEMA2     = 14;
    extern int RSInowPeriod   = 6;      
    for (counter=i ;counter<=i+9;counter++)  // LB=9
  3. No need for barsBack. See #2
  4.       AvgRange=0;
          for (counter=i ;counter<=i+9;counter++)
          {
            AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
          }
          Range=AvgRange/10;
    Could have use iATR
  5. danizani95: My problem is that theese arrows don't refresh for every tick once they appeared
        else if ((EMACrossedDown == true) && (RSICrossedDown == true) && (SignalLabeled != 2)) {
             CrossDown[i] = High[i] + K*Range;
    :
    SignalLabeled = 2;
    You set buffers when first half is true but never unset them when it is false. Set them to EMPTY_VALUE as Keith Watford suggests but also #6
  6. Your SignalLabeled, upalert, downAlert are also wrong. Save the previous value of your global variables. Process the loop. Then after the loop do you alerts if needed. Remove their test inside the loop.
  7.       if ((fasterEMA1now >= slowerEMA1now) && (fasterEMA1previous < slowerEMA1previous) && (fasterEMA2now >= slowerEMA2now) && (fasterEMA2previous < slowerEMA2previous) ) {
             EMACrossedUp = true;
             EMACrossedDown = false;
          }

          if ((fasterEMA1now <= slowerEMA1now) && (fasterEMA1previous > slowerEMA1previous) && (fasterEMA2now <= slowerEMA2now) && (fasterEMA2previous > slowerEMA2previous)) {
             EMACrossedUp = false;
             EMACrossedDown = true;
          }
    What are the value of those variables if there is no cross on that bar? You never unset the variables.
    EMACrossedUp   = fasterEMA1now >= slowerEMA1now && fasterEMA1previous < slowerEMA1previous
                  && fasterEMA2now >= slowerEMA2now && fasterEMA2previous < slowerEMA2previous;
    EMACrossedDown = fasterEMA1now <= slowerEMA1now && fasterEMA1previous >
    slowerEMA1previous
                  && fasterEMA2now <= slowerEMA2now && fasterEMA2previous > slowerEMA2previous;
    Those tests mean both moving average must cross in the same bar. If you don't want that you must test for a change in conditions
    bool risingCurr = fasterEMA1now      >= slowerEMA1now      && fasterEMA2now      >= slowerEMA2now;
    bool risingPrev = fasterEMA1previous >= slowerEMA1previous && fasterEMA2previous >= slowerEMA2previous;

    EMACrossedUp    = risingCurr && !risingPrev;

  8. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So Don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled. Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
    if(EMACrossedDown && RSICrossedDown) is better.
 
Thank you guys tomorrow I will try to do some changes :) !
 
I tried to do the changes that you suggested but now the indicator shows the arrow only when the current candle has closed and the arrow appear/disapper considering the next candle movements.. Could you help me ? maybe post a correct version of the code ? Thanks so much
 
danizani95:
I tried to do the changes that you suggested but now the indicator shows the arrow only when the current candle has closed and the arrow appear/disapper considering the next candle movements.. Could you help me ? maybe post a correct version of the code ? Thanks so much

We can't guess what changes you have made.

You need to show the modifications.

 
I have already deleted the edit because I thought it was all wrong... could you show me the right code ?
 

Have you done what I suggested earlier?

If so what effect did it have?

 
Keith Watford:

Have you done what I suggested earlier?

If so what effect did it have?

yes plus or minus... i did what I could because I am not very experienced in mql4. in particular, when you say that I do not need any lines of code, you mean that those lines do not let my indicator to work properly or simply that they are useless ? thank you for your support keith
 
Keith Watford:

Have you done what I suggested earlier?

If so what effect did it have?

sorry keith i confused you with whroeder1, so i did what you said but now the indicator shows the arrow only when the current candle has closed and the arrow appear/disapper considering the next candle movements..
 
danizani95:
sorry keith i confused you with whroeder1, so i did what you said but now the indicator shows the arrow only when the current candle has closed and the arrow appear/disapper considering the next candle movements..
I don't see why that should happen. I'm working on a job at the moment, if I have time later, I may have a look at your code.
Reason: