Download MetaTrader 5

Run multiple separate experts at once

To add comments, please log in or register
Copy the deals of successful traders. Subscribe to signals!
Roadkill
25
Roadkill 2015.05.27 16:17 

Dear all,

Can somebody point me in the right direction to help me "split" different expert advisors when running on different charts?

The problem is that I currently use the check "if(PositionsTotal() == 0)" as before proceeding to my "OrderSend" section. But I want to split them, so that I can run multiple trades at once.

So in other words: How do I make an EA check that that specific EA currently has an active position? 

I suspect I have to use something in the "MqlTradeResult" structure to refer to a deal or magic number, but I haven't been able to figure it out.

Help is very much appreciated!

Osama Shaban
1415
Osama Shaban 2015.05.27 18:34  

This is an easy issue for any good programmer. You have to put in mind that when coding any EA don't rely OrdersTotal() command 

to count the opened trades because every EA wants to differentiate between his orders and the others ... 

Instead, use the following function ...

int CountTrades() {
   int cnt = 0;
   for (int i = OrdersTotal() - 1; i >= 0; i--) {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      if (OrderSymbol() != Symbol() || OrderMagicNumber() != Magic) continue;
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
         if (OrderType() == OP_SELL || OrderType() == OP_BUY) cnt++;
   }
   return (cnt);
}   

 Also, an an other example, to check the profit for each EA (magic number) ... use a function such this ...

void SymbolProfit(string smbl){ 
   int cnt, total; 
   BuyProfit = 0;
   SellProfit = 0;
   total = OrdersTotal(); 
   for(cnt=0; cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
         if(OrderSymbol()==smbl && OrderMagicNumber()==Magic){
            if(OrderType()==OP_BUY){
               BuyProfit = BuyProfit + OrderProfit() + OrderCommission() + OrderSwap(); 
            }   
            if(OrderType()==OP_SELL){
               SellProfit = SellProfit + OrderProfit() + OrderCommission() + OrderSwap(); 
            }   
         }      
   }
}

In the second code, we use OrdersTotal() to know the total opened orders in the account then we will check all those orders using

magic number ... so, no harm in that. Actually, we must to do that here. 

The bottom line

ALWAYS use magic number in all functions and try to split your code to as much as separate functions to

do a specific task. This task is easy to use magic number and make your code is easy to read and trace. 

Alain Verleyen
Moderator
30640
Alain Verleyen 2015.05.27 19:55  
Osama Shaban:

This is an easy issue for any good programmer. You have to put in mind that when coding any EA don't rely OrdersTotal() command 

...
Really ? Maybe you can read the post carefully, the question was about mql5.
Alain Verleyen
Moderator
30640
Alain Verleyen 2015.05.27 19:55  
Roadkill:

Dear all,

Can somebody point me in the right direction to help me "split" different expert advisors when running on different charts?

The problem is that I currently use the check "if(PositionsTotal() == 0)" as before proceeding to my "OrderSend" section. But I want to split them, so that I can run multiple trades at once.

So in other words: How do I make an EA check that that specific EA currently has an active position? 

I suspect I have to use something in the "MqlTradeResult" structure to refer to a deal or magic number, but I haven't been able to figure it out.

Help is very much appreciated!

You have to use PositionSelect().

There is only 1 position by symbol with MT5, if you want to work with multiple "trades" you have to work with deals, which is far from trivial.

Roadkill
25
Roadkill 2015.05.27 20:38  

Appreciate the feedback guys,

I tried using this article: https://www.mql5.com/en/articles/211

From what I've understand:

I issue an order --> to make a deal --> that results in a position.

But somewhere my SL and TP are stored. Deals cannot be modified, and a position can consist of multiple orders/deals, because everything combines in 1 position (per symbol).

But from what I see I can only modify PENDING orders, not finished ones, is this correct? 

Is the trick to recall the volume and other parameters from your MQLTradeResult structure and modify PART of a position with for instance a new stoploss? Should that be a direction I'm looking in?

Thanks as always! =) 

Orders, Positions and Deals in MetaTrader 5
Orders, Positions and Deals in MetaTrader 5
  • 2011.02.01
  • MetaQuotes Software Corp.
  • www.mql5.com
Creating a robust trading robot cannot be done without an understanding of the mechanisms of the MetaTrader 5 trading system. The client terminal receives the information about the positions, orders, and deals from the trading server. To handle this data properly using the MQL5, it's necessary to have a good understanding of the interaction between the MQL5-program and the client terminal.
Alain Verleyen
Moderator
30640
Alain Verleyen 2015.05.27 20:45  
Roadkill:

Appreciate the feedback guys,

I tried using this article: https://www.mql5.com/en/articles/211

From what I've understand:

I issue an order --> to make a deal --> that results in a position.

But somewhere my SL and TP are stored. Deals cannot be modified, and a position can consist of multiple orders/deals, because everything combines in 1 position (per symbol).

But from what I see I can only modify PENDING orders, not finished ones, is this correct? 

Is the trick to recall the volume and other parameters from your MQLTradeResult structure and modify PART of a position with for instance a new stoploss? Should that be a direction I'm looking in?

Thanks as always! =) 

What you are trying to do is not clear.
Roadkill
25
Roadkill 2015.05.27 20:51  

I'll try to clarify:

My first EA: EA#1, is creating a position volume EURUSD of 0.5 Lots, with a ST and TP at 20 Pips

Then EA#2 is making it's own trade, let's say 1 Lot EURUSD with a ST and TP at 50 Pips.

What I want to do is make EA#1 run a trailing stop function that modifies the SL of the first 0.5 Lots, without touching the 1Lot that is using the 50Pips value.

Alain Verleyen
Moderator
30640
Alain Verleyen 2015.05.27 20:54  
Roadkill:

I'll try to clarify:

My first EA: EA#1, is creating a position volume EURUSD of 0.5 Lots, with a ST and TP at 20 Pips

Then EA#2 is making it's own trade, let's say 1 Lot EURUSD with a ST and TP at 50 Pips.

What I want to do is make EA#1 run a trailing stop function that modifies the SL of the first 0.5 Lots, without touching the 1Lot that is using the 50Pips value.

It's what I answered you previously :

Forum on trading, automated trading systems and testing trading strategies

Run multiple separate experts at once

Alain Verleyen, 2015.05.27 19:55

There is only 1 position by symbol with MT5, if you want to work with multiple "trades" you have to work with deals, which is far from trivial.

If you want to do that you can't work with Position and SL/TP, you have to work with deals ONLY. And manage your sl/tp inside your EA or with pending orders.
To add comments, please log in or register