Magic Number?

 

Hello,



i need a little help. I use the code you see below. I use drag this EA on different instruments (currencies) and timeframes. I use the following timeframes: M15, M30, H1, H4. Everything is working perfect. The only thing is, if i have a MA cross on the M15 timeframe the EA closes ALL other open trades on the other timeframes too. The M30, H1 and H4 also get cloesed.

I like that the M15 trades only get closed if on the M15 timeframe is a cross

The M30 trades should ONLY get closed if on the M30 timeframe is a MA cross.

The H1 trades should ONLY get closed if on the H1 timeframe is a MA cross.

And the H4 trades should only get closed, if on the H4 timeframe is a MA cross.


Do i need to set a "magic number" for every chart? So that the EA can different the timeframes?


extern int MagicNumber=100;



100, 101, 102 and so on. Is this the only thing i need to add to my code? Or do i need to add more to my code to make the magic number work?


Really Thanks for you help!


//============================================================================================
//
//
//
//
//
//============================================================================================
extern int    MA1_Period=10;
extern int    MA2_Period=30;
extern int    MA1_Method=0;
extern int    MA2_Method=1;
extern int    MA1_Price=0;
extern int    MA2_Price=4;
extern int    MA1_Shift=0;
extern int    MA2_Shift=0;
extern double Lot = 0.1;
extern int    slippage = 0; 
int New_Bar;
int Time_0;
int PosOpen;
int PosClose;
int total;
double MA1_0;
double MA1_1;
double MA2_0;
double MA2_1;
int orderBuy;
int orderSell; 
//============================================================================================
int init()  
   {

   }  
//============================================================================================
int start()  
   {
   orderBuy=0;
   orderSell=0; 
   double price;  
   int openOrders=0;
   int total=OrdersTotal();
   for(int i=total-1;i>=0;i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderType()==OP_BUY)
            {
            orderBuy=1;
            if(CrossPositionClose()==1)
               {
               price=MarketInfo(Symbol(),MODE_BID);
               OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
               }
            }
         if(OrderType()==OP_SELL)
            {
            orderSell=1;
            if(CrossPositionClose()==2)
               {
               price=MarketInfo(Symbol(),MODE_ASK);
               OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
               }
            }
         }
      }
   
   New_Bar=0;
   if (Time_0 != Time[0])
      {
      New_Bar= 1;
      Time_0 = Time[0];
      } 
   
   MA1_0=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 0);
   MA1_1=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 1);
   MA2_0=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 0);
   MA2_1=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 1);
   
   if (CrossPositionOpen()==1 && New_Bar==1)
      {
      OpenBuy();
      }      
   if (CrossPositionOpen()==2 && New_Bar==1)
      {
      OpenSell();
      }    
   return;
   }  
//============================================================================================
int CrossPositionOpen()
   {
   PosOpen=0;
   if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0))  
      {
      PosOpen=1;
      }                  
   if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0))
      {
      PosOpen=2;
      }             
   return(PosOpen);
   }
//============================================================================================
int CrossPositionClose()
   {
   PosClose=0;
   if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0))
      {
      PosClose=1;
      }                  
   if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0))
      {
      PosClose=2;
      }             
   return(PosClose);
   }
//============================================================================================
int OpenBuy() 
   {
   if (total==1)
      {
      OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_BUY) return;
      }
   OrderSend(Symbol(),OP_BUY, Lot, Ask, slippage, 0, 0, "M15", 1, 0, CLR_NONE);
   return;
   }
//============================================================================================
int OpenSell()
   {
   if (total==1)
      {
      OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_SELL) return;
      }
   OrderSend(Symbol(),OP_SELL, Lot, Bid, slippage, 0, 0, "M15", 2, 0, CLR_NONE);
   return;
   }
//============================================================================================
int MAMethod(int MA_Method)
   {
      switch(MA_Method)
        {
         case 0: return(0);
         case 1: return(1);
         case 2: return(2);
         case 3: return(3);
        }
   }
//============================================================================================
int MAPrice(int MA_Price)
   {
      switch(MA_Price)
        {
         case 0: return(PRICE_CLOSE);       
         case 1: return(PRICE_OPEN);
         case 2: return(PRICE_HIGH);
         case 3: return(PRICE_LOW);
         case 4: return(PRICE_MEDIAN);
         case 5: return(PRICE_TYPICAL);
         case 6: return(PRICE_WEIGHTED);
        }
   }
//============================================================================================
 
Morpheus85:

Hello,



i need a little help. I use the code you see below. I use drag this EA on different instruments (currencies) and timeframes. I use the following timeframes: M15, M30, H1, H4. Everything is working perfect. The only thing is, if i have a MA cross on the M15 timeframe the EA closes ALL other open trades on the other timeframes too. The M30, H1 and H4 also get cloesed.

I like that the M15 trades only get closed if on the M15 timeframe is a cross

The M30 trades should ONLY get closed if on the M30 timeframe is a MA cross.

The H1 trades should ONLY get closed if on the H1 timeframe is a MA cross.

And the H4 trades should only get closed, if on the H4 timeframe is a MA cross.


Do i need to set a "magic number" for every chart? So that the EA can different the timeframes?


extern int MagicNumber=100;

100, 101, 102 and so on. Is this the only thing i need to add to my code? Or do i need to add more to my code to make the magic number work?

You need to add the MagicNumber to your OrderSend() and check it before you execute an OrderClose() . . . while you are at it you should also check the return values from these functions and report any errors if they have failed, read this: What are Function return values ? How do I use them ?
 
RaptorUK:
You need to add the MagicNumber to your OrderSend() and check it before you execute an OrderClose() . . . while you are at it you should also check the return values from these functions and report any errors if they have failed, read this: What are Function return values ? How do I use them ?


Ok, I think I understood your problem. You do not need to create a external parameter for MagicNumber nor change this number for each instance of your EA.

The issue in your code is that you're not checking for the Order magic number or the Symbol(the chart your EA is running on).

In order to correct that, you just need to add some conditions to your IFs in the beginning of the start sub.

Where you have the if(OrderType()==OP_BUY)...

Add: if(OrderType()=OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber()==MagicBuy) ... (remember to do the same for the OP_SELL condition)

 
Morpheus85: Do i need to set a "magic number" for every chart? So that the EA can different the timeframes?
One way or another
 
aquino.rinaldo:


Ok, I think I understood your problem. You do not need to create a external parameter for MagicNumber nor change this number for each instance of your EA.

The issue in your code is that you're not checking for the Order magic number or the Symbol(the chart your EA is running on).

In order to correct that, you just need to add some conditions to your IFs in the beginning of the start sub.

Where you have the if(OrderType()==OP_BUY)...

Add: if(OrderType()=OP_BUY && OrderSymbol() == Symbol() && OrderMagicNumber()==MagicBuy) ... (remember to do the same for the OP_SELL condition)

Hello, so i need to set the external magic number and add this to the buy and sell orders right?
 

Hello i have changed it now to this:



//============================================================================================
//
//
//
//
//
//============================================================================================
extern int    MA1_Period=10;
extern int    MA2_Period=30;
extern int    MA1_Method=0;
extern int    MA2_Method=1;
extern int    MA1_Price=0;
extern int    MA2_Price=4;
extern int    MA1_Shift=0;
extern int    MA2_Shift=0;
extern double Lot = 0.1;
extern int    slippage = 0; 
extern string MagicNumbers="Set different magicnumber for each timeframe of a pair";
extern int    MagicNumber=100;
int New_Bar;
int Time_0;
int PosOpen;
int PosClose;
int total;
double MA1_0;
double MA1_1;
double MA2_0;
double MA2_1;
int orderBuy;
int orderSell; 
//============================================================================================
int init()  
   {

   }  
//============================================================================================
int start()  
   {
   orderBuy=0;
   orderSell=0; 
   double price;  
   int openOrders=0;
   int total=OrdersTotal();
   for(int i=total-1;i>=0;i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicBuy)
            {
            orderBuy=1;
            if(CrossPositionClose()==1)
               {
               price=MarketInfo(Symbol(),MODE_BID);
               OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
               }
            }
         if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderMagicNumber()==MagicSell)
            {
            orderSell=1;
            if(CrossPositionClose()==2)
               {
               price=MarketInfo(Symbol(),MODE_ASK);
               OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
               }
            }
         }
      }
   
   New_Bar=0;
   if (Time_0 != Time[0])
      {
      New_Bar= 1;
      Time_0 = Time[0];
      } 
   
   MA1_0=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 0);
   MA1_1=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 1);
   MA2_0=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 0);
   MA2_1=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 1);
   
   if (CrossPositionOpen()==1 && New_Bar==1)
      {
      OpenBuy();
      }      
   if (CrossPositionOpen()==2 && New_Bar==1)
      {
      OpenSell();
      }    
   return;
   }  
//============================================================================================
int CrossPositionOpen()
   {
   PosOpen=0;
   if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0))  
      {
      PosOpen=1;
      }                  
   if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0))
      {
      PosOpen=2;
      }             
   return(PosOpen);
   }
//============================================================================================
int CrossPositionClose()
   {
   PosClose=0;
   if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0))
      {
      PosClose=1;
      }                  
   if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0))
      {
      PosClose=2;
      }             
   return(PosClose);
   }
//============================================================================================
int OpenBuy() 
   {
   if (total==1)
      {
      OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_BUY) return;
      }
   OrderSend(Symbol(),OP_BUY, Lot, Ask, slippage, 0, 0, "M15", MagicNumber, 1, 0, CLR_NONE);
   return;
   }
//============================================================================================
int OpenSell()
   {
   if (total==1)
      {
      OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_SELL) return;
      }
   OrderSend(Symbol(),OP_SELL, Lot, Bid, slippage, 0, 0, "M15", MagicNumber, 2, 0, CLR_NONE);
   return;
   }
//============================================================================================
int MAMethod(int MA_Method)
   {
      switch(MA_Method)
        {
         case 0: return(0);
         case 1: return(1);
         case 2: return(2);
         case 3: return(3);
        }
   }
//============================================================================================
int MAPrice(int MA_Price)
   {
      switch(MA_Price)
        {
         case 0: return(PRICE_CLOSE);       
         case 1: return(PRICE_OPEN);
         case 2: return(PRICE_HIGH);
         case 3: return(PRICE_LOW);
         case 4: return(PRICE_MEDIAN);
         case 5: return(PRICE_TYPICAL);
         case 6: return(PRICE_WEIGHTED);
        }
   }
//============================================================================================



but now i have the problem that i can´t compile it. MagicBuy and MagicSell variable not defined, please help.


Thanks

 
Morpheus85:

Hello i have changed it now to this:

but now i have the problem that i can´t compile it. MagicBuy and MagicSell variable not defined, please help.

This is why you need to understand what you are doing rather than simply trying to copy and paste code . . . why do you need different Magic Numbers for Buys and Sells ?
 
RaptorUK:
This is why you need to understand what you are doing rather than simply trying to copy and paste code . . . why do you need different Magic Numbers for Buys and Sells ?


Hello,


i think that i finally got it. I changed it now 4 times in the code.

2 times in the "int start" and 2 times in the "ordersend" should this work now?


//============================================================================================
//
//
//
//
//
//============================================================================================
extern int    MA1_Period=10;
extern int    MA2_Period=30;
extern int    MA1_Method=0;
extern int    MA2_Method=1;
extern int    MA1_Price=0;
extern int    MA2_Price=4;
extern int    MA1_Shift=0;
extern int    MA2_Shift=0;
extern double Lot = 0.1;
extern int    slippage = 0; 
extern int    MagicNumber=103;
int New_Bar;
int Time_0;
int PosOpen;
int PosClose;
int total;
double MA1_0;
double MA1_1;
double MA2_0;
double MA2_1;
int orderBuy;
int orderSell; 
//============================================================================================
int init()  
   {

   }  
//============================================================================================
int start()  
   {
   orderBuy=0;
   orderSell=0; 
   double price;  
   int openOrders=0;
   int total=OrdersTotal();
   for(int i=total-1;i>=0;i--)
      {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true)
         {
         if(OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderMagicNumber())
            {
            orderBuy=1;
            if(CrossPositionClose()==1)
               {
               price=MarketInfo(Symbol(),MODE_BID);
               OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
               }
            }
         if(OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderMagicNumber())
            {
            orderSell=1;
            if(CrossPositionClose()==2)
               {
               price=MarketInfo(Symbol(),MODE_ASK);
               OrderClose(OrderTicket(),OrderLots(),price,slippage,CLR_NONE);
               }
            }
         }
      }
   
   New_Bar=0;
   if (Time_0 != Time[0])
      {
      New_Bar= 1;
      Time_0 = Time[0];
      } 
   
   MA1_0=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 0);
   MA1_1=iMA(NULL,0, MA1_Period, MA1_Shift,MAMethod(MA1_Method), MAPrice(MA1_Price), 1);
   MA2_0=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 0);
   MA2_1=iMA(NULL,0, MA2_Period, MA2_Shift,MAMethod(MA2_Method), MAPrice(MA2_Price), 1);
   
   if (CrossPositionOpen()==1 && New_Bar==1)
      {
      OpenBuy();
      }      
   if (CrossPositionOpen()==2 && New_Bar==1)
      {
      OpenSell();
      }    
   return;
   }  
//============================================================================================
int CrossPositionOpen()
   {
   PosOpen=0;
   if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0))  
      {
      PosOpen=1;
      }                  
   if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0))
      {
      PosOpen=2;
      }             
   return(PosOpen);
   }
//============================================================================================
int CrossPositionClose()
   {
   PosClose=0;
   if ((MA1_1>=MA2_0 && MA1_0<MA2_0) || (MA1_1>MA2_0 && MA1_0<=MA2_0))
      {
      PosClose=1;
      }                  
   if ((MA1_1<=MA2_0 && MA1_0>MA2_0) || (MA1_1<MA2_0 && MA1_0>=MA2_0))
      {
      PosClose=2;
      }             
   return(PosClose);
   }
//============================================================================================
int OpenBuy() 
   {
   if (total==1)
      {
      OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_BUY&&OrderSymbol()==Symbol()&&OrderMagicNumber()) return;
      }
   OrderSend(Symbol(),OP_BUY, Lot, Ask, slippage, 0, 0, "M15", 1, 0, CLR_NONE);
   return;
   }
//============================================================================================
int OpenSell()
   {
   if (total==1)
      {
      OrderSelect(0, SELECT_BY_POS,MODE_TRADES);
      if (OrderType()==OP_SELL&&OrderSymbol()==Symbol()&&OrderMagicNumber()) return;
      }
   OrderSend(Symbol(),OP_SELL, Lot, Bid, slippage, 0, 0, "M15", 2, 0, CLR_NONE);
   return;
   }
//============================================================================================
int MAMethod(int MA_Method)
   {
      switch(MA_Method)
        {
         case 0: return(0);
         case 1: return(1);
         case 2: return(2);
         case 3: return(3);
        }
   }
//============================================================================================
int MAPrice(int MA_Price)
   {
      switch(MA_Price)
        {
         case 0: return(PRICE_CLOSE);       
         case 1: return(PRICE_OPEN);
         case 2: return(PRICE_HIGH);
         case 3: return(PRICE_LOW);
         case 4: return(PRICE_MEDIAN);
         case 5: return(PRICE_TYPICAL);
         case 6: return(PRICE_WEIGHTED);
        }
   }
//============================================================================================
Reason: