EA to mark sma crossover is not working correctley.

To add comments, please log in or register
Richard Roberts
166
Richard Roberts  
Hi, first time posting in here. Can someone tell me I where I am going wrong on this EA as I cannot figure it out.  This would be a great help to my learning to code.    EA should be marking SMA cross over but is not. I cannot figure it out.
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int SMA_CrossUp ;
int SMA_CrossDown;
//-- Ma CrossOver settinga-----------------------------------------//

extern int    SlowMa                      =50;
extern int    SlowMaShift                 =0;
extern int    SlowMaMethod                =0;        // '0' = simple
extern int    SlowMaAppliedTo             =0;        //on the close

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(IsThisANewCandle())
      {CheckMa ();
      }
      
  }   
//+------------------------------------------------------------------+
//|MA CrossOver funtion for direcrtion.                              |
//+------------------------------------------------------------------+
void CheckMa ()
 {
  
                     
  //-- Slow MA settings-----------------------------------------------                         
  double CurrentSlow   = iMA (NULL,0,SlowMa,SlowMaShift,SlowMaShift,SlowMaAppliedTo,1);  // The candle number one is used so it only draws from the last fully drawn candle.
   
 
  //-- Up arrow-------------------------------------------------------
  int i ;
  for( i=0.0; i<Bars; i++)
  
  if (Close [i] <Open[i] && Open [i] < CurrentSlow && Close [i] > CurrentSlow)                                                           
     {
       SMA_CrossUp= i;
       string ObjectName = "Arrow"+IntegerToString(i);
       ObjectCreate(0,ObjectName,OBJ_ARROW_UP,0,Time[SMA_CrossUp],Low[SMA_CrossUp]);
       Print (" This is an up signal");
        
     }                   
    
    
  //--Down arrow-------------------------------------------------------
 else if (Close [i]<Open [i]&& Open [i] > CurrentSlow && Close [i] < CurrentSlow)                                                           
     {
    
       SMA_CrossDown = i;
       string ObjectName = "Arrow"+IntegerToString(i);
       ObjectCreate(0,ObjectName,OBJ_ARROW_DOWN,0,Time[SMA_CrossDown],High[SMA_CrossDown]);
       Print (" This is an down signal");
       
     }  
    
    
 
  }
//--Is this a new Candle?---------------------------------------------//
bool IsThisANewCandle()
  {
   static int BarsOnChart=100;                                         //-- this will be update on initialization, static will stop it reseting
   if(Bars==BarsOnChart)                                               //-- will update int in GBL with bars on current chart
      return(false);                                                   //-- No new number
   BarsOnChart=Bars;                                                   //-- Is this number higher than the last saved number
   return(true);                                                       //-- This is a new candle

  }
Keith Watford
Moderator
17654
Keith Watford  

Do not double post

Your other topic has been deleted.

Richard Roberts
166
Richard Roberts  
I have update the EA and now it works, the problem is not it will only draw on every arrow, when I try to add a condition such as  if(Close[i]<CurrentSlow && Open[I]>CurrentSlow) it will not do it
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

int SMA_CrossUp;
int SMA_CrossDown;
//-- Ma CrossOver settinga-----------------------------------------//

extern int    SlowMa                      =50;
extern int    SlowMaShift                 =0;
extern int    SlowMaMethod                =0;        // '0' = simple
extern int    SlowMaAppliedTo             =0;        //on the close
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
   if(IsThisANewCandle())
     {
      CheckMa();
     }

  }
//+------------------------------------------------------------------+
//|MA CrossOver funtion for direcrtion.                              |
//+------------------------------------------------------------------+
void CheckMa()
  {



//-- Up arrow-------------------------------------------------------
   int i;
   for(i=0.0; i<Bars; i++)
      
     
      if(Close[i]<Open[i])
        {
         double CurrentSlow=iMA(NULL,0,SlowMa,SlowMaShift,SlowMaShift,SlowMaAppliedTo,i);
         if(Close[i]<CurrentSlow )

               {
               string name="FXX_IchiupArrow"+(string)Time[0];
               if(ObjectFind(0,name)!=0)
                 {
                  ObjectCreate(name,OBJ_ARROW_UP,0,Time[0],Low[0]-(Period()*Point*2));// Time[0] since you are talking about current bar
                  ObjectSet(name,OBJPROP_COLOR,clrRed);
                  ObjectSet(name,OBJPROP_WIDTH,1);
                  ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR_TOP);
                 }
               }
           
         }
         //--Down arrow-------------------------------------------------------
         else if(Close[i]>Open[i])
           {
            double CurrentSlow=iMA(NULL,0,SlowMa,SlowMaShift,SlowMaShift,SlowMaAppliedTo,i);
            if(Close[i]>CurrentSlow)
              {
               string name="FXX_IchiupArrow"+(string)Time[0];
               if(ObjectFind(0,name)!=0)
                 {
                  ObjectCreate(name,OBJ_ARROW_DOWN,0,Time[0],High[0]+(Period()*Point*2));// Time[0] since you are talking about current bar
                  ObjectSet(name,OBJPROP_COLOR,clrRed);
                  ObjectSet(name,OBJPROP_WIDTH,1);
                  ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR_TOP);
                 }
              }

           }


        }
//--Is this a new Candle?---------------------------------------------//
   bool IsThisANewCandle()
     {
      static int BarsOnChart=100;                                         //-- this will be update on initialization, static will stop it reseting
      if(Bars==BarsOnChart)                                               //-- will update int in GBL with bars on current chart
         return(false);                                                   //-- No new number
      BarsOnChart=Bars;                                                   //-- Is this number higher than the last saved number
      return(true);                                                       //-- This is a new candle

     }
//+------------------------------------------------------------------+
Seng Joo Thio
1014
Seng Joo Thio  
Richard Roberts:

Hi, can anybody help me here, I'd very much appreciate it.  This EA is supposed to just identify a candle as it crosses a SMA and mark it. Its not doing that. I've spent a good few days trying and failing to get it to work. 

Code is:

You should make iMA() call for each 'i' (i.e. move your iMA() call into the 'for' loop, and instead of '1', use 'i').

Also, not sure why you chose to do this in EA rather than an indicator... because using EA, you have to name your arrows uniquely, and including time in their names will be more appropriate rather than the bar index 'i' because bar index for the same candle will change over time.

Another issue is your new candle check - don't use bar (won't be reliable especially when Bars will change as your page up/down or change your charts' display number of display bars). Use time instead.

Keith Watford
Moderator
17654
Keith Watford  
The above post was moved from a duplicate topic that has now been deleted.
To add comments, please log in or register