magic Number does not work

 

Hi Guys,

I have this EA but the magic Number does not work. I did not program it all myself and I tried to fix the MN for days but I do not get it. Can anyone help me. Everyelse is good so far.

extern int        MagicNumber = 123456;

extern double     Lots=0.01;
extern int        Timeframe=0;
extern int        EMA_PeriodSlow=100;
extern string TradeStartTime ="00:00";
extern string TradeStopTime = "23:59";
bool   Trading_on_off  = true;
double alertTag;
double places;
bool Close_at_EMA_break=true;

//*********************************************************************
int init(){
   if (Digits==3 || Digits==5) places=10.0; else places=1.0;
   return(0);

  // return(0);
}
//*********************************************************************
int start(){
  
   if(TimeCurrent()>StrToTime(TradeStartTime) && TimeCurrent()<StrToTime(TradeStopTime)) Trading_on_off = true;
      
      else Trading_on_off = false;
  
   double free=AccountFreeMargin();
   Comment("Account free margin is ",DoubleToStr(free,2),"\n","Current time is ",TimeToStr(TimeCurrent()));
   double   digit; 
   digit=MarketInfo(Symbol(),MODE_DIGITS);
   
   int cnt, ticket; 
   double EmaSlow=iMA(NULL,Timeframe, EMA_PeriodSlow,0,MODE_EMA, PRICE_CLOSE,1);
   double EmaSlowPrev=iMA(NULL,Timeframe, EMA_PeriodSlow,0,MODE_EMA, PRICE_CLOSE,2);
  
   
   int ordTotal = 0;
   for(int i = 0;i < OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol() == Symbol())
         ordTotal++;
   }
   int total=ordTotal;
   if (total<1){
         if(EmaSlow<Close[1] && EmaSlowPrev > Close[2]){
            if (Trading_on_off && alertTag != Time[0]){ 
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,5*places,0,0,"EMAperiod", MagicNumber,0,Green);
            alertTag=Time[0];
            PlaySound ("Alert2");
            Print("ticket ", ticket, "MagicNumber is ", MagicNumber, "EMA:", EmaSlow);
            
        }}
          if(EmaSlow>Close[1] && EmaSlowPrev <Close[2]){
            if (Trading_on_off && alertTag != Time[0]){
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,5*places,0,0,"EMAperiod", MagicNumber,0,Red);
            alertTag=Time[0];
            PlaySound ("Alert2");
            Print("Account free margin is ", AccountFreeMargin(), "MagicNumber is ", MagicNumber, "EMA:", EmaSlow);
        }}}  
   
   for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
   if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)
         &&  OrderMagicNumber()  == MagicNumber      // my magic number
         &&  OrderSymbol()       == Symbol()          // and my pair.
         &&  OrderType()<=OP_SELL ){                  // need this only if EA opens pending orders.

         if (Close_at_EMA_break){
               if(EmaSlow>Close[1] && EmaSlowPrev<Close[2]){       
                  OrderClose(OrderTicket(),OrderLots(),Bid,5*places,Blue);
                return(0);
         }}
       
       else{
         if (Close_at_EMA_break){
           if(EmaSlow<Close[1] && EmaSlowPrev>Close[2]){
              OrderClose(OrderTicket(),OrderLots(),Ask,5*places,Violet);
              return(0);
      }}}}}
  return(0);
}
 
What exactly do you mean by "the magic Number does not work." ?
 
I use this EA on one symbol with eg tree different EMA Settings and different magic numbers. Then one of the EAs opens a long position and another EA gets a sell signal. So it closes the position and opens a sell.