How to run two EA's at the same time without them conflicting?

 

Hi, I am running two (or more) EA's on separate charts with different magic numbers.  I found that the first EA has its position closed based on the logic in the second EA.  How do I get them to run 100% independently.


Just for example (without attaching the long code).  EA1 closes position based on moving average crossings.  EA2 closes based on a certain time after opening.  I am finding that EA1 closes its position based on the time condition of EA2.

 
dclark24:

Hi, I am running two (or more) EA's on separate charts with different magic numbers.  I found that the first EA has its position closed based on the logic in the second EA.  How do I get them to run 100% independently.


Just for example (without attaching the long code).  EA1 closes position based on moving average crossings.  EA2 closes based on a certain time after opening.  I am finding that EA1 closes its position based on the time condition of EA2.

There will be a problem with the code, just having a magic number isn't enough, each individual EA needs to reference the Magic Number when manipulating trades/orders, these one obvious don't

 

Thanks Rick!,


I have two closing conditions setup.  Can you let me know how to provide the reference to the magic number?


//Here is a snippet of the code execution conditions...can you tell me how to put in the reference to the magic number?

//calls for open and close condition 1 further below)
if (golong == true) {
         openBuy();
      }

//calls for the close based on ma crossings condition 2 below)
     if (close_on_ma_crossing) {
         if (ma_signal_buy<ma_signal_l_buy && ma_presignal_buy>ma_presignal_l_buy)
            closePosition(POSITION_TYPE_BUY);
         if (ma_signal_sell>ma_signal_l_sell && ma_presignal_sell<ma_presignal_l_sell) 
            closePosition(POSITION_TYPE_SELL);
      }

//condition 1...executes order and closes based on stoploss or takeprofit levels.
void openBuy() {
   if (PositionsTotal() > 0) return;
   if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) return;
   ENUM_ORDER_TYPE side = ORDER_TYPE_BUY;
   double SL = 0;
   double TP = 0; 
   global_SL = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + buy_stoploss*4*0.25;
   if (buy_stoploss > 0 && close_loss_in_pips)
      SL = SymbolInfoDouble(Symbol(), SYMBOL_ASK) - buy_stoploss*4*0.25; // here we multiply stoploss/takeprofit to 4 because of point step 0.25
   if (buy_takeprofit > 0 && close_profit_in_pips) 
      TP = SymbolInfoDouble(Symbol(), SYMBOL_ASK) + buy_takeprofit*4*0.25;
   trade.PositionOpen(Symbol(), side, getContracts(), SymbolInfoDouble(Symbol(), SYMBOL_ASK), SL, TP, "");
   return;

//condition2...additional closing condition
void closePosition(ENUM_POSITION_TYPE side) {
   //if(!TerminalInfoInteger(TERMINAL_TRADE_ALLOWED)) return;
   for(int i=0; i<PositionsTotal(); i++) {
      if(!PositionSelect(Symbol())) continue;
      long type=PositionGetInteger(POSITION_TYPE);
      double amount = PositionGetDouble(POSITION_VOLUME);
      if (type == side) {
         ENUM_ORDER_TYPE close_side = ORDER_TYPE_SELL;
         if (type == POSITION_TYPE_SELL) close_side = ORDER_TYPE_BUY;
         //if (type == ORDER_TYPE_BUY) close_side = ORDER_TYPE_SELL;
         //trade.PositionClose(Symbol(),amount,slippage); 
         trade.PositionOpen(Symbol(), close_side, getContracts(), SymbolInfoDouble(Symbol(), SYMBOL_BID), 0, 0, "");
         global_SL = 0;
      }   
   }
}
 

Something like this:

https://www.mql5.com/en/forum/75757

Using OrderMagicNumber() compared to whatever you input Magic Number is called

How to select order by magicnumber?
How to select order by magicnumber?
  • 2016.03.10
  • www.mql5.com
Hello, I would like to assign the EA to order many currency but i don't know how to write...
 

Thanks again.

Slightly off subject but still part of the main issue.  I am using trade.PositionOpen to place the trades but I found a function OrderSend which takes the magic number as an argument.  I am thinking that I can just define the magic number for the EA and pass this to OrderSend, but I wonder What is really the difference between PositionOpen and OrderSend?    I think I can use them interchangeably.

 
dclark24:

Thanks again.

Slightly off subject but still part of the main issue.  I am using trade.PositionOpen to place the trades but I found a function OrderSend which takes the magic number as an argument.  I am thinking that I can just define the magic number for the EA and pass this to OrderSend, but I wonder What is really the difference between PositionOpen and OrderSend?    I think I can use them interchangeably.

PositionOpen is a function from trade class

OrderSend is the native function


PositionOpen uses OrderSend in it

 
Sorry about misspelling the name. The voice recognition on my phone is not so good.
 

Hi Dua,


Do you know if I can pass the magic number as an argument into the positionopen function?  Do you know if there are any examples of this being done?

 
//using PositionOpen
   trade.PositionOpen(Symbol(), side, getContracts(), SymbolInfoDouble(Symbol(), SYMBOL_BID), SL, TP, "");

//using OrderSend instead of PositionOpen
   MqlTradeResult result={0};
   MqlTradeRequest request={0};
   request.symbol = Symbol();
   request.type = side;
   request.volume = getContracts();
   request.price = SymbolInfoDouble(Symbol(), SYMBOL_BID);
   request.sl = SL;
   request.tp = TP;
   request.comment = "";
   OrderSend(request, result);*/
So to include the magic number I will use OrderSend but am trying to make sure I am using it properly.  The two section of codes I thought should do the same thing but the result is different.  Any idea why?
 
dclark24:
So to include the magic number I will use OrderSend but am trying to make sure I am using it properly.  The two section of codes I thought should do the same thing but the result is different.  Any idea why?

request.magic    =EXPERT_MAGIC;

 
Alexander Fedosov:

request.magic    =EXPERT_MAGIC;

Thanks Alex,  but the issue I am having is that the OrderSend function is giving a different profit result compared to the trade.PositionOpen function.  I guess I am not using it correctly and wanted some advice on what might be wrong with how I am using it.  Regarding the magic number, yes I will use it in the OrderSend function once I can get same results between the OrderSend and the trade.PositionOpen function.

Reason: