Problem with Magic Number

 
extern int        MagicNumber = 123456;

extern double     Lots=0.01;
extern int        Timeframe=0;
extern int        EMA_PeriodSlow=100;
extern int        EMA_PeriodFast=0;
extern double     SL_EMA_Buyfactor=0;
extern double     TP_EMA_Buyfactor=0;
extern double     SL_EMA_Sellfactor=0;
extern double     TP_EMA_Sellfactor=0;

extern string TradeStartTime ="00:00";
extern string TradeStopTime = "23:59";
string MagicNum;
bool   Trading_on_off  = true;
double SLbuy;
double TPbuy;
double SLsell;
double TPsell;
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);

    
   MagicNum = MagicNumber;
   
   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]){ 
            SLbuy=EmaSlow*SL_EMA_Buyfactor+0;
            TPbuy=EmaSlow*TP_EMA_Buyfactor+0; 
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,5*places,SLbuy,TPbuy,"EMAperiod", MagicNumber,0,Green);

            alertTag=Time[0];
            PlaySound ("Alert2");
            Print("Account free margin is ", AccountFreeMargin(), "MagicNumber is ", MagicNumber, "EMA:", EmaSlow);
           
         
        }}
            
         if(EmaSlow>Close[1] && EmaSlowPrev <Close[2]){
            if (Trading_on_off && alertTag != Time[0]){
            SLsell=EmaSlow*SL_EMA_Sellfactor;
            TPsell=EmaSlow*TP_EMA_Sellfactor; 
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,5*places,SLsell,TPsell,"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--){
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if( (OrderType()<=OP_SELL) && (OrderSymbol()==Symbol()) ){
        if(OrderType()==OP_BUY && OrderComment()== "EMAperiod"){
           if (Close_at_EMA_break){
               if(EmaSlow>Close[1] && EmaSlowPrev<Close[2]){       
                  OrderClose(OrderTicket(),OrderLots(),Bid,5*places,Violet);
                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);
}
Hi,

I have copy and pasted my EA together. I am not an expert on programming but I understand the basics. Now, my EA works great, but if I run the same EA on the same Pair with different settings it closes orders what have been opened by another EA. How can I stop it?

 

when you start your ea, you have the ability to change the magic number. use 101 for the first EA's magic number and 201 for the second instance of the EA.

also, you have a little nugget of potential disaster:

if( (OrderType()<=OP_SELL) && (OrderSymbol()==Symbol()) ){

should be as below. Anytime, although unlikely, the value for OP_SELL could change and so you should be more exact and say equal rather than less than or equal

if( (OrderType()==OP_SELL) && (OrderSymbol()==Symbol()) ){

How well does this EA perform? Are you getting good results?

sn

 
Also, your init() function has an extra return(0) in it. Doesn't hurt anything, but its just weird! ;)
 
serpentsnoir:

when you start your ea, you have the ability to change the magic number. use 101 for the first EA's magic number and 201 for the second instance of the EA.

also, you have a little nugget of potential disaster:

should be as below. Anytime, although unlikely, the value for OP_SELL could change and so you should be more exact and say equal rather than less than or equal

How well does this EA perform? Are you getting good results?

sn


Hi,

even if I change the EA's Magic Number it does not work. I can not really simulate this problem on backtesting, but when I go live it is there.

eg:

1st EA opens on position long

2nd EA gets a signal to open long: nothing happens (because long is open already)

3rd EA gets a signal to open short: closes long from 1st EA and opens short

Is it possilbe that the

OrderClose(OrderTicket(),OrderLots(),Bid,5*places,Violet);

function does not respect the Magic Number?


Well perfomance. Lets say I am a bit disapointed, but I think when it works on longer timeframes and you are able to lauch different settings to avoid losing your account when one setting goes bad, it can be worth trying.

 
Anomalous:
Also, your init() function has an extra return(0) in it. Doesn't hurt anything, but its just weird! ;)

To be honest, i have no idea what it makes. init and deinit works well, so I am happy. I will change it and try!
 
int ordTotal = 0;
   for(int i = 0;i < OrdersTotal();i++){
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol() == Symbol())
         ordTotal++;
   }
for(cnt=OrdersTotal()-1;cnt>=0;cnt--){
      OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES);
      if( (OrderType()<=OP_SELL) && (OrderSymbol()==Symbol()) ){
You are not filtering by magic number and pair. Always test return codes.
 for(pos=OrdersTotal()-1;pos>=0;pos--) if(
      OrderSelect(pos,SELECT_BY_POS)           // Only my orders w/
  &&  OrderMagicNumber()  == magic.number      // my magic number
  &&  OrderSymbol()       == Symbol()          // and my pair.
  &&  OrderType()<=OP_SELL // need this only if EA opens pending orders.
){
 
WHRoeder:
You are not filtering by magic number and pair. Always test return codes.


Okay, I have changed the code as you said. Did I do it right?

But there is another problem. I want that the same EA runs on the same pair with different settings. So I open 6 charts, switch on the same EA six times with different settings. But the EA opens only one position. Now the EA does not close an position what was opened by another EA anymore, but also does not open a second position?


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("Account free margin is ", AccountFreeMargin(), "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(OrderType()==OP_BUY && OrderComment()== "EMAperiod"){
         if (Close_at_EMA_break){
               if(EmaSlow>Close[1] && EmaSlowPrev<Close[2]){       
                  OrderClose(OrderTicket(),OrderLots(),Bid,5*places,Violet);
                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);
}
Reason: