Magic number help please

 

Hi all, i am running the same ea on four charts and each chart has a different currency, at present the ea trades on one chart only for the period of that trade and will not open trades on any other chart should the conditions be met.

Could i give the ea a different name and (4 to be precise) and use them on each chart ?, if not could anyone help with this problem.

If anyone is willing, they could code it in the MACD ea that is standard in MT4 as my ea is similar, well the orders, thanks anyone willing to help is this matter,

Robbo

 
If someone could help it would be appreciated.
 

I think it really depends on your code.

Perhaps you need to make sure that when you use OrderSelect() it includes the currency pair as well..

Renaming EA will not help at all

 
//check for correct syntax of this pseudocode:

int MAGIC_OF_THIS_EA=999;

// somewhere in your ea
OrderSend(........Lots,Stop, Profit, "comment", MAGIC_OF_THIS_EA, 0);
//,,,


// somehere else in your EA, for trades management
int my_stop=50; // 
for(int i=OrdersTotal()-1; i>=0; i--)
{
   if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      continue;
   if(OrderSymbol()!=Symbol()) // if order's currency does not meet chart's currency, continue with next order  
      continue;
   if(OrderMagicNumber()!= MAGIC_OF_THIS_EA)  // see whether trade belongs to this EA, otherwhise go to the next
        continue;
   //this just illustrative rule 
   if(OrderType()==OP_BUY && Bid-my_stop*Point > OrderOpenPrice() && Bid-my_stop*Point > OrderStopLoss())
       OrderModify(OrderTicket(),OrderOpenPrice(),Bid-my_stop*Point,OrderTakeProfit(),0,White);

   // remaining orders management rules....
} 

 

J,and AM thanks for the input, in short, if i used 4 charts with 4 currencies and 4 different EA's should i get trades open at the same time if the conditions are met at the same time ?

my assuption is that each charts EA should act on that chart,regardless of whats happening elswhere,

EDIT

If I use the currency, were do I place the symbol, for example if i use the eur/usd

Thanks Robbo

If i send you the EA would you look at it for me ?

 

robo,

a suggestion?

get ea to generate it's own unique random EA ID# or Magic#

once sorted out, you'll never have to bother with the damn things again as any ea you run does it automatically...

regards

 

FBJ good suggestion,probably even better if i knew how to do it !!

any clues as to how?

 
//+------------------------------------------------------------------+
//| Function..: SeqNum                                               |
//| Parameters: None.                                                |
//| Purpose...: Generate a sequential number.                        |
//| Returns...: dSeqNum - next sequence number or -1 if an error     |
//|                       occured, which can be determined by        |
//|                       calling GetLastError().                    |
//| Notes.....: MT4 keeps the value of the global variable at the    |
//|             client terminal for 4 weeks since the last access.   |                        
//|             Use SeqNum() to generate a unique identity for each  |
//|             order (and passed via parameter <magic> number, or   |
//|             converted to a string and passed via parameter       |
//|             <comment> to the OrderSend() function) as the trade  |
//|             servers of some brokers do modify the ticket number  |
//|             of a pending order when it changes to a market order.|
//|             The same sequence number could, for example, be used |
//|             to identify the two positions of a straddle order.   |
//+------------------------------------------------------------------+
double SeqNum() {
  double dSeqNum=1;
  string sName="SequenceNumber";
  
  if(GlobalVariableCheck(sName)) {
    dSeqNum=GlobalVariableGet(sName)+1;
    if(dSeqNum==1) dSeqNum=-1;
  }
  if((dSeqNum>0) && (GlobalVariableSet(sName,dSeqNum) == 0)) dSeqNum=-1;
  return(dSeqNum);
}
 

Ok for single EA usage but health hazard for multiple EA usage. SequenceNumber gvar access by many EAs (especially on terminal startup) = no warranty that unique value will be obtained.

Suggest use single access guard semaphore globalVar.

Each instance of function on entry can acquire atomic access via GlobalVariableSetOnCondition() and retry loop etc; do it's biz; on exit release semaphore.

 

I found this in my search,thanks Barrowboy, would giving a different number in #define MAGICMACD 999998 to each EA solve the problem ?

Just to confirm, i have not used the full code but extracted some of the "guts" mainly the parts I have put a **// next to

Robbo


#define MAGICMACD 999998 **//

extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;

//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MacdCurrent, MacdPrevious, SignalCurrent;
double SignalPrevious, MaCurrent, MaPrevious;
int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external
// variables (Lots, StopLoss, TakeProfit,
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
if(Bars<100)
{
Print("bars less than 100");
return(0);
}
if(TakeProfit<10)
{
Print("TakeProfit less than 10");
return(0); // check TakeProfit
}
// to simplify the coding and speed up access
// data are put into internal variables
MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);


total=ActiveTradesForMagicNumber(Symbol(), YourMagicNumber); **//
if(total<1)

{
// no opened orders identified
if(AccountFreeMargin()<(1000*Lots))
{
Print("We have no money. Free Margin = ", AccountFreeMargin());
return(0);
}
// check for long position (BUY) possibility
if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious &&
MathAbs(MacdCurrent)>(MACDOpenLevel*Point) && MaCurrent>MaPrevious)
{
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// check for short position (SELL) possibility
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDOpenLevel*Point) && MaCurrent<MaPrevious)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,Bid-TakeProfit*Point,"macd sample",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);
}
return(0);
}
// it is important to enter the market correctly,
// but it is more important to exit it correctly...
for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if(OrderType()<=OP_SELL && // check for opened position
OrderSymbol()==Symbol()) // check for symbol
{
if(OrderType()==OP_BUY) // long position is opened
{
// should it be closed?
if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious &&
MacdCurrent>(MACDCloseLevel*Point))
{
OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if(Bid-OrderOpenPrice()>Point*TrailingStop)
{
if(OrderStopLoss()<Bid-Point*TrailingStop)
{
OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*TrailingStop,OrderTakeProfit(),0,Green);
return(0);
}
}
}
}
else // go to short position
{
// should it be closed?
if(MacdCurrent<0 && MacdCurrent>SignalCurrent &&
MacdPrevious<SignalPrevious && MathAbs(MacdCurrent)>(MACDCloseLevel*Point))
{
OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
return(0); // exit
}
// check for trailing stop
if(TrailingStop>0)
{
if((OrderOpenPrice()-Ask)>(Point*TrailingStop))
{
if((OrderStopLoss()>(Ask+Point*TrailingStop)) || (OrderStopLoss()==0))
{
OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*TrailingStop,OrderTakeProfit(),0,Red);
return(0);
}
}
}
}
}
}
return(0);
}
int ActiveTradesForMagicNumber(string SymbolToCheck, int MagicNumberToCheck) **// all code from here on down
{
int icnt, itotal, retval;

retval=0;
itotal=OrdersTotal();

for(icnt=0;icnt<itotal;icnt++)
{
OrderSelect(icnt, SELECT_BY_POS, MODE_TRADES);
// check for opened position, symbol & MagicNumber
if(OrderType()<=OP_SELL && OrderSymbol()==SymbolToCheck && OrderMagicNumber()==MagicNumberToCheck)
{

retval++;

//Print("Orders opened : ",retval);

}
}

return(retval);

}


// the end.

 
??
Reason: