Download MetaTrader 5

How to use magic number in order to manage several EA

To add comments, please log in or register
patagonia2015
15
patagonia2015  

Hello guys,

I want to use 4 different EA made by me. I don't want that any of the EA interfer with each other and that only if a EA meet the criteria and doesn't have an order (for that particular EA), then open a trade.

That problem kill me for 3 days, I look online but don't have an answer. If you can help me with a short and simple code for these I really apreciate. I made these but doesn't work, it open several trades for a simple EA. Remember that I need only one open trade (if meet he criteria) per EA, only ONE. I want to use a simple code in each EA for manage these problem. Here is the code intended for these problem.

 

VARIABLES

for ( int i = 0; i <= OrdersTotal(); i++ )

{

if(OrderSelect( i, SELECT_BY_POS, MODE_TRADES )==true)

{if(OrderMagicNumber() != MagicNumber ) continue;

}           

if((FIRST FILTER) && OrderMagicNumber() != MagicNumber)

// REST OF THE PROGRAM 

 

Many thanks and sorry but english is not my first language. 

Carl Schreiber
7362
Carl Schreiber  

1) Each EA loops through the open trades (like you do - use SRCF-button to show code here) and count the number of its MagicNumber() if > 0 skip the next trade.

2) Each EA bears in mind the ticket-number if it has opened a position other wise this variable is set to -1 => skip if != -1

patagonia2015
15
patagonia2015  

gooly,

 

Thanks for your response. But I don't understand how to do it in these situation. My problem is that: I want each particular EA open or close a trade (if meet the criteria) only if doesn't have any particular trade opened, and don't care if other EA have already one open trade.

Please help me, I don't know yet how to do it.

Many thanks! 

Inovance
2582
Inovance  
patagonia2015:

gooly,

 

Thanks for your response. But I don't understand how to do it in these situation. My problem is that: I want each particular EA open or close a trade (if meet the criteria) only if doesn't have any particular trade opened, and don't care if other EA have already one open trade.

Please help me, I don't know yet how to do it.

Many thanks! 

If all you're looking to do is check to see if a particular EA already has a trade open, you can use something like this:

bool isPositionOpen() {
    for (int i = 0; i < OrdersTotal(); i++) {
        if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) && OrderMagicNumber() == magicNumber && OrderSymbol() == Symbol()) { 
            return true;
       }
    }
    return false;

and make sure you have defined a unique magicNumber for each EA.


Hope that helps.

To add comments, please log in or register