3 sma cross over clitch

 
//+------------------------------------------------------------------+
#property version   "1.00"
#property strict
#property indicator_chart_window

#property indicator_buffers 5


//---Indicator Colours
#property indicator_color1 clrGreen//up arrow
#property indicator_color2 clrRed//down arrow
#property indicator_color3 clrCrimson//SMA10
#property indicator_color4 clrNavy//SMA25
#property indicator_color5 clrWhite//SMA100


//---Indicator Width
#property indicator_width1 2
#property indicator_width2 2

#include <WinUser32.mqh>
extern bool show=true;//Show Indicators?

double UpArrow[];
double DownArrow[];
double SMA10[];
double SMA25[];
double SMA100[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

//--- indicator buffers mapping
   SetIndexBuffer(0,UpArrow);
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,233);
   SetIndexLabel(0,"Buy Signal.");
//---
   SetIndexBuffer(1,DownArrow);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,234);
   SetIndexLabel(1,"Sell Signal.");
//---
   SetIndexBuffer(2,SMA10);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexLabel(2,"SMA10.");
//---
   SetIndexBuffer(3,SMA25);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexLabel(3,"SMA25.");
//---
   SetIndexBuffer(4,SMA100);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexLabel(4,"SMA100.");

   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---for loop
   int counted_bars=IndicatorCounted();
   int limit= Bars-counted_bars;
   for(int i=1;i<limit;i++)
     {
      double fasterMAnow=iMA(NULL,0,10,0,MODE_SMA,PRICE_MEDIAN,i+1);
      double fasterMAprevious=iMA(NULL,0,10,0,MODE_SMA,PRICE_MEDIAN,i+2);
      double fasterMAafter=iMA(NULL,0,10,0,MODE_SMA,PRICE_MEDIAN,i-1);
      //---
      double mediumMAnow=iMA(NULL,0,25,0,MODE_SMA,PRICE_MEDIAN,i+1);
      double mediumMAprevious=iMA(NULL,0,25,0,MODE_SMA,PRICE_MEDIAN,i+2);
      double mediumMAafter=iMA(NULL,0,25,0,MODE_SMA,PRICE_MEDIAN,i-1);
      //---
      double slowerMAnow=iMA(NULL,0,100,0,MODE_SMA,PRICE_MEDIAN,i+1);
      double slowerMAprevious=iMA(NULL,0,100,0,MODE_SMA,PRICE_MEDIAN,i+2);
      double slowerMAafter=iMA(NULL,0,100,0,MODE_SMA,PRICE_MEDIAN,-1);
      //---
      if(show==true)
        {
         SMA10[i]=fasterMAnow;
         SMA25[i]=mediumMAnow;
         SMA100[i]=slowerMAnow;
        }
      if((fasterMAnow>slowerMAnow && fasterMAprevious<=slowerMAprevious && fasterMAafter>slowerMAafter && mediumMAnow>slowerMAnow) || (fasterMAnow>slowerMAnow && mediumMAnow>slowerMAnow && mediumMAprevious<=slowerMAprevious && mediumMAafter>slowerMAafter))
         //if(ma10>ma25 && ma25>ma100 && ma10>ma100)       
        {
         UpArrow[i]=Open[i];
        }
      if((fasterMAnow<slowerMAnow && fasterMAprevious>=slowerMAprevious && fasterMAafter<slowerMAafter && mediumMAnow<slowerMAnow) || (fasterMAnow<slowerMAnow && mediumMAnow<slowerMAnow && mediumMAprevious>=slowerMAprevious && mediumMAafter<slowerMAafter))
          //if(ma10<ma25 && ma25<ma100 && ma10<ma100)
           {
            DownArrow[i]=Open[i];
           }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

Hi, I hope some one can help fix the coding error here? It draws the green up arrow OK but not the red a down arrow.

I need a fresh pair of eyes to fix the fault!! Hope some one can solve the riddle THANKS

 
  1. double slowerMAafter=iMA(NULL,0,100,0,MODE_SMA,PRICE_MEDIAN,-1);
    Bogus call.
  2. Drop all your "after" things. The comparison of now vs after will be done only once, on the first tick of bar zero, and they will essentially be the same so your comparison is bogus.
  3. Now is i, previous is i+1. Don't just set your buffer[i], you must unset it (EMPTY_VALUE) if the condition becomes false for bar zero.
  4. Do your lookbacks correctly.
  5. Are your books one column but two feet wide? No because that is unreadable. They are 6 inches, sometimes two columns, so you can read it easily. So should be your code. I'm not going to go scrolling (or moving my eyes) back and forth trying to read it.
  6. Write self documenting code
    // if((fasterMAnow>slowerMAnow && fasterMAprevious<=slowerMAprevious && fasterMAafter>slowerMAafter && mediumMAnow>slowerMAnow)
    // || (fasterMAnow>slowerMAnow && mediumMAnow>slowerMAnow && mediumMAprevious<=slowerMAprevious && mediumMAafter>slowerMAafter))
    
    bool  isFastUp     = fasterMAnow>slowerMAnow;
    bool wasFastUp     = fasterMAprevious>slowerMAprevious;
    bool fastCrossed   = isFastUp && !wasFastUp;
    
    bool  isMediUp     = mediumMAnow>slowerMAnow;
    bool wasMediUp     = mediumMAprevious>slowerMAprevious;
    bool mediCrossed   = isMediUp && !wasMediUp;
    
    if(isFastUp && isMediUp && (fastCrossed || mediCrossed) ) ...
    
 

Hi!

      double slowerMAafter=iMA(NULL,0,100,0,MODE_SMA,PRICE_MEDIAN,-1);

Shall ther be an additional i.

Split your if statement into simpler statements to find your error.

 
thanks guys will take your helpful comments on board :)
Reason: