Download MetaTrader 5

Running EA on several charts without interference, maybe SelectOrder()?

To add comments, please log in or register
Johan Strandberg
107
Johan Strandberg  

Hi, 

 I have a simple MA-crossover EA running on asset class 1 (let's call it that). Now the problem is when I run the same EA on asset class 2 simultaneously they intefer with each other. 

  

Example, EA on asset class 1 says buy and opens a buy order, it closes right away because EA on asset class 2 says there is no buy-condition and closes the open order. 
Basically I would like to keep these two EA's apart but I presume the OrderSelect() doesn't differentiate based on Asset Class (symbol), I tried changing name on the EAs but that didn't help so now I need help with the OrderSelect()

 

Looked through old posts but did not get any wiser. Somebody said select each order by itself (no idea how to do this). Could somebody please help me with a way to tell my EA that it's supposed to look at asset symbols separately? Not mix buy and sell signals for different asset classes?

 

//+------------------------------------------------------------------+
//|                                         LiveProjektetTrading.mq4 |
//|                                                 Johan Strandberg |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Johan Strandberg"
#property link      ""
#property version   "1.00"
#property strict
//--- input parameters

//En massa saker för att göra iMA:orna
string symbol = NULL;
int timeFrame = 0;
int period = 2;
int period2 = 5;
int maShift = 0;
int lastBarShift = 0;
int antalOpenOrders;

double imaTest;
double imaTest2;


//Nu en massa saker för att göra bool:en för order select funktionen.
int indexPlats = 0;
//int hurValja = SELECT_BY_POS;
//int enTillParameter = MODE_TRADES;

//bool = finnsOrder;

//bool OrderSelect(int = 0, int = SELECT_BY_POS, int pool=MODE_TRADES);
int order;
color Color = CLR_NONE;


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void init()
  {
//---
   
//---
   Alert("Function init startade");
  }
//+------------------------------------------------------------------+
 
 
//+------------------------------------------------------------------+
//| start function                                                   |
//+------------------------------------------------------------------+
void start()
  {
//---
imaTest = iMA(symbol,timeFrame,period, maShift,MODE_SMA,PRICE_MEDIAN,lastBarShift);

imaTest2 = iMA(symbol,timeFrame,period2, maShift,MODE_SMA,PRICE_MEDIAN,lastBarShift);

Alert("iMA 1:",imaTest, "iMA 2:", imaTest2);

antalOpenOrders = OrdersTotal();
Alert (antalOpenOrders);


//Nu ska jag göra trade criteria och öppna och stänga ordrar med nedan if-sats.
//Eftersom det är long-only så stänger jag ordern på ett annat ställe än där jag senare öppnar en ny order.
if((imaTest-imaTest2) > 0 && (antalOpenOrders == 0))
{
   //Öppna ny order här (jag har med en sl samt har satt tp till NULL, vet inte om denna NULL-grej funkar för TP, då
   //jag vill stänga den manuellt med ett MA-cross.
   OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, Bid-200*Point, NULL);
}
else if ((imaTest-imaTest2) < 0 && (antalOpenOrders > 0))
{
   //Stäng aktiv köpposition här
   //Förs skall vi hitta den öppna orderns parametrar.
  bool finnsOrder = OrderSelect(indexPlats, SELECT_BY_POS, MODE_TRADES);
  //Sedan ska vi stänga den.
  //if (finnsOrder = true)
  //{
    order=OrderTicket();
    Alert("Order ", order, " komer nu att stängas");
    OrderClose(order, 0.01, Bid, 3);
    
  //}
  //else
  //{
   //Alert("Elsen på bool finnsOrder");
  //}

}
else
{return;}

//Sleep(100000);
     
//---
   
  }
//+------------------------------------------------------------------+
 
 
 
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void deinit()
  {
//---

//---
      Alert("Function deinit startade");
  }
//+------------------------------------------------------------------+

 

 

Don't have SRC button on the metaeditor, would also be grateful if I could get help with how to post my code in a forum-correct way. Tried to go to view to get any sort of SRC button on my toolbar but nothing. See attached file for both toolbar and the actual code. 

 

Big Thanks to this community. 

honest_knave
Moderator
2293
honest_knave  

Make sure your EA has a Magic Number (which it adds to orders that it opens, and only manages trades with that magic number).

When you add the EA to asset class 1, use one magic number

When you add the EA to asset class 2, use a different magic number.

Keith Watford
Moderator
10541
Keith Watford  

I have added your code to your post.

The SRC button is here in the forum toolbar, not in the compiler.

Personally, I prefer to copy the code that I want to post. Hold down the Ctrl key and press 5 and then v. T don't get stuck in the box then as can happen with the SRC button.

whroeder1
16574
whroeder1  
  1. Don't paste code
    Play video
    Please edit your post.
    For large amounts of code, attach it

  2. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.) Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
Johan Strandberg
107
Johan Strandberg  

Getting closer to figure it out but need some additional help. 

 

1) When I do my trade criteria test I use the following code: 

I've earlier declared         antalOpenOrders = OrdersTotal();    

Then  I see if the MA's are in buy position, then look if there are any open orders. Below I guess I have to change my criteria to look if there are any other open orders for that particular asset instead of looking on all asset classes with the OrdersTotal. How do I do this? Otherwise I can only have 1 order open at a time disregardless of asset class?

if((imaTest-imaTest2) > 0 && (antalOpenOrders == 0)) 


 

2) When that is done  I changed my open position code and included a variable called "magic" as the magic numer (int magic = 1; as well as Expert_ID = 1; is declared globally earlier) on orders for asset1 and did the same for an EA I named differently only changing the numbers to 2.

OrderSend(Symbol(), OP_BUY, 0.01, Ask, 3, Bid-200*Point, NULL,NULL, 1); 

 

 

3) Lastly when I want to close my positon I'm struggling with the logic of it all.

I presume I need to loop over all open orders I need to loop over OrdersTotal() and do an if-statment in the loop, something like this:

for ( int x = _OrdersTotal; x < OrdersTotal - 1; x++ ) 

if ( OrderMagicNumber() != Expert_ID ) continue;

else{   bool finnsOrder = OrderSelect(indexPlats, SELECT_BY_POS, MODE_TRADES);

order=OrderTicket();

OrderClose(order, 0.01, Bid, 3); 




All the help is really appreciated, and excuse me if I am a bit lost here. Am not getting too much sleep with newly born twins here :)

 

Thanks/JoBo 

honest_knave
Moderator
2293
honest_knave  
JoBo:

Am not getting too much sleep with newly born twins here :)

Congratulations!

I am presuming you are applying the same EA to multiple different charts (rather than trading all asset classes from one chart).

If so, have an input parameter for the magic number. Manually set this when you add the EA to the chart.

Inside your EA, have a small function that checks how many orders are open with the correct magic number (and/or correct symbol). Here is an example.

That is it.

Don't try to manage 2 separate magic numbers from within the same EA.

Roberto Jacobs
48879
Roberto Jacobs  
honest_knave:

Congratulations!

I am presuming you are applying the same EA to multiple different charts (rather than trading all asset classes from one chart).

If so, have an input parameter for the magic number. Manually set this when you add the EA to the chart.

Inside your EA, have a small function that checks how many orders are open with the correct magic number (and/or correct symbol). Here is an example.

That is it.

Don't try to manage 2 separate magic numbers from within the same EA.

But it may, manage the unique magic number for each pair in one EA (EA for multiple pairs). I have the EA for MT4, but is untested for MT5.
honest_knave
Moderator
2293
honest_knave  
Roberto Jacobs:
But it may, manage the unique magic number for each pair in one EA (EA for multiple pairs). I have the EA for MT4, but is untested for MT5.

Of course, but my advice was based upon:

honest_knave:

I am presuming you are applying the same EA to multiple different charts (rather than trading all asset classes from one chart). 

So no need to complicate it.
Roberto Jacobs
48879
Roberto Jacobs  
honest_knave:

Of course, but my advice was based upon:

So no need to complicate it.
I just wish that Jobo open his mind
honest_knave
Moderator
2293
honest_knave  
Roberto Jacobs:
I just wish that Jobo open his mind

OK, perhaps my comment could be misinterpreted that it is not possible. So I should qualify my statement:

honest_knave:

Don't try to manage 2 separate magic numbers from within the same EA unless you have to.

Roberto Jacobs
48879
Roberto Jacobs  
honest_knave:

OK, perhaps my comment could be misinterpreted that it is not possible. So I should qualify my statement:

it is good advice... b0212-smile.gif
123
To add comments, please log in or register