Multiple EAs on same symbol, order modify conflict

 

Hey guys,

I'm trying to run two independent EAs on EURUSD, and I'm having issues with making them managing their stoplosses independently.


I'm pretty new to this, but have done my due diligence and couldn't find a solution to my issue. Any help is appreciated!

Yes, I am running the EAs on 2 separate charts.

I took care of them working independently by using different magic numbers, for opening orders as well as managing the opened orders.

My current exit code looks like this: (this is same logic I use for opening new orders, to make sure that no open orders with the same magic number exist. That works flawlessly.)

// EXIT / UPDATE TRAILING STOP
// ---------------------------
for(i = 0; i < OrdersTotal(); i++)
  {
   if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      continue;
   if(OrderType() <= OP_SELL && OrderMagicNumber() == botID)
     {
      // For Buy Position
      if(OrderType() == OP_BUY)
        {
         OrderModify(xxxx);
        }
      
      // For Sell Position
      else
        {
         OrderModify(yyyy);
        }
     }
  }

Once the EAs open their respective orders, the issue happens when updating the respective stoplosses. One bot seems to be hijacking the stoploss of the other.

You see the image attached? That's 2 buy orders, with a common stoploss (you can see overlapping text in the stoploss). That's not coincidence, that's a combined stoploss for both orders that is now controlled by the EA that opened the last order.

I do not want my trend following order get stopped out by my scalping EA!!


I need the stoploss updates to happen independently of each other, as dictated by their individual strategies. Is there any obvious mistake I am making?

Thanks in advance!

Files:
 

It's not possible unless you use the same magicnumber in

botID
 
Marco vd Heijden:

It's not possible unless you use the same magicnumber in


I'm pretty sure I'm using different magic numbers :) That is how the 2 buy orders got created by the individual bots. So if the entry logic works for the EAs independently, not sure why the exit logic doesn't work the same way.


One other possibility. I came across global variables as I'm researching.

I have declared a few input variables at the beginning of the code (outside of the main void OnTick() function) as follows:

input int      minRiskPoints  = 50;
input int      maxRiskPoints  = 500;

input int maSlopeInput = 30;

Now, these have some overlap. They have the same name in both EAs, but different values.

Is that causing my interference? Again, apologies if this is basic, I'm just getting started :)

 

If your ordermagicnumbers are different then the robots and the orders are separated by it and they can not interfere with each other by this rule.

 if(OrderType() <= OP_SELL && OrderMagicNumber() == botID)
 
for(i = 0; i < OrdersTotal(); i++)
  {
   if(!OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
      continue;
   if(OrderType() <= OP_SELL && OrderMagicNumber() == botID)
  1. Either A) you are using a different MN in your OrderSend, or B) one doesn't check MN in it's modification section, or C) The same number in both.
    You should also check _Symbol.
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
    2. For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.
 

Thanks a lot for your inputs. No issues with the magic numbers for sure.


So I just checked the functionality manually.

In a demo account, I opened 2 different buy orders on the same symbol and tried to set different stop losses for each. MT4 didn't let me do it :)

Looks like I can only have a single common stop loss order, no matter how many orders are open. Is this something controlled by regulations, and such modifications are not permitted?


That's what I'm seeing, no issues with the EAs themselves, they are working perfectly.

I might just have to recode the EAs to not update stoplosses, but directly close the orders if conditions are met. This way I will have independent control :)

 
Naveen Asaithambi:

Thanks a lot for your inputs. No issues with the magic numbers for sure.

So I just checked the functionality manually.

In a demo account, I opened 2 different buy orders on the same symbol and tried to set different stop losses for each. MT4 didn't let me do it :)

Looks like I can only have a single common stop loss order, no matter how many orders are open. Is this something controlled by regulations, and such modifications are not permitted?

That's what I'm seeing, no issues with the EAs themselves, they are working perfectly.

I might just have to recode the EAs to not update stoplosses, but directly close the orders if conditions are met. This way I will have independent control :)

Are you perhaps using a USA broker where FIFO rules apply? If so, then do some research on how FIFO works so as to make your EA compliant with FIFO.


Forum on trading, automated trading systems and testing trading strategies

When changing TakeProfit for 1 out of 3 tickets opened via script all 3 are changed

whroeder1, 2013.03.20 02:04

In the US, NAFTA rules now state that the oldest order must be closed first.

Some brokers (like IBFX) implemented this rule in the back office side, so accounting statements don't match the mql4 statements (P/L correct when all are closed) but the mql4 model wasn't broken.

Others implemented this rule by forcing the oldest order to have the closest TP/SL of all orders so that oldest order is closed first, per the rule. This breaks the mql4 model but accounting matches.

Forum on trading, automated trading systems and testing trading strategies

When changing TakeProfit for 1 out of 3 tickets opened via script all 3 are changed

GreenMoney, 2013.03.20 02:28

Your broker may be enforcing the FIFO (First In, First Out) rule.  I live in the US and use a broker that operates both in the US and internationally.  US brokers are (unfortunately) required by US law to enforce the FIFO rule.  The behavior you are describing is how my broker enforces the FIFO rule.  If I open two trades on the same symbol in the same direction, they will have the same stoploss and takeprofit values.  Example: if I buy eur/usd at 1.30000 and set the stoploss at 1.29000 and set the takeprofit at 1.31000, and then I execute a second trade that buys the eur/usd at 1.30500 and set the stoploss at 1.30200 and takeprofit at 1.31500, both trades with have the same stoploss and takeprofit values once the second trade executes.  The first trade inherits the second trade's stoploss and takeprofit values.  If I change one, both trade's values change.  I sent an email to my broker asking why (even though I figured it was due to FIFO), and my broker confirmed that the behavior was due to their enforcement of FIFO rules.  The broker you mentioned has a US office and international offices, so you probably need to contact it to ask why you are experiencing that behavior, especially if you don't live in the US or aren't a US citizen.

EDIT #1: WHRoeder beat me to the possible answer (I was writing mine while he posted his).  :)

 
Fernando Carreiro:

Are you perhaps using a USA broker where FIFO rules apply? If so, then do some research on how FIFO works so as to make your EA compliant with FIFO.



Yes, I'm with a US broker that enforces FIFO!

Read up on that yesterday, and I learned that:

  • FIFO: When I try to close any position, the oldest open position will be closed first
  • No hedging: I can't hold trades in opposite directions on the same symbol simultaneously (if I have multiple trades, all of them have to be either buy or sell)

New learning: multiple positions on a symbol will always share the same take profit / stop loss setting. The broker will not permit you to have different stop losses for different orders.

This will interfere with the FIFO rule (it won't be possible for a newer order to have a tighter stop loss, as this will violate FIFO)


Thanks for the reference discussions! Solves all my questions.

Reason: