Download MetaTrader 5

EA only opens trades 1 by 1

To add comments, please log in or register
Angel Dickinson
91
Angel Dickinson  
So I built an EA only for entries nothing else. But for some reason for example when there is a setup on AUDUSD and another on GBPNZD at the same time the robot only take 1 of those trades. Why? What can I do to fix this problem. Im new to coding so please bear with me. I know I said it was only for entries but I had to add Take Profit and Stop Loss because I kept getting Ordersend error 130. Btw the MagicNumbers for each were different.
extern    int MagicNumber = 12345;
extern    double lots = 0.10;
extern    int stopLoss = 1000;
extern    int takeProfit = 2000;
void OnTick()
  {
  
  if (OrdersTotal() > 0) {
        return;
    }
     
    double ema = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
    double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);    
    
     
    if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }


    if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70) ) {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print("Sell order succeeded!");
        }
    }
}
//---
   
  
//+------------------------------------------------------------------+
whroeder1
16555
whroeder1  

Angel Dickinson:
But for some reason for example when there is a setup on AUDUSD and another on GBPNZD at the same time the robot only take 1 of those trades. Why? What can I do to fix this problem.

I kept getting Ordersend error 130.

Btw the MagicNumbers for each were different.

  1. if (OrdersTotal() > 0) return;
    Because that is what you coded it to do. If a trade is open, do nothing more.
    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

  2. How should we know, when you don't show the code that generates the error? (Not your modified code.)

  3. Irrelevant. Read the link provided above.
  4. if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
       Print("Buy order succeeded!");
    }
    OrderSend does not return a boolean.

  5. Check your return codes for errors and report them. What are Function return values ? How do I use them ? - MQL4 forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  6. You buy at the Ask and sell at the Bid. So your buy order's TP/SL, will be triggered when the Bid reaches it. Your SL is short by the spread, your TP is longer by the spread.

  7. Are you adjusting (SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs?) points == pips? - Trading Systems - MQL5 programming forum - Page 5

Angel Dickinson
91
Angel Dickinson  
whroeder1:
  1. Because that is what you coded it to do. If a trade is open, do nothing more.
    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

  2. How should we know, when you don't show the code that generates the error? (Not your modified code.)

  3. Irrelevant. Read the link provided above.
  4. if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
       Print("Buy order succeeded!");
    }
    OrderSend does not return a boolean.

  5. Check your return codes for errors and report them. What are Function return values ? How do I use them ? - MQL4 forum
    Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  6. You buy at the Ask and sell at the Bid. So your buy order's TP/SL, will be triggered when the Bid reaches it. Your SL is short by the spread, your TP is longer by the spread.

  7. Are you adjusting (SL, TP, and slippage; for 4/5 digit brokers and for JPY pairs?) points == pips? - Trading Systems - MQL5 programming forum - Page 5

Hey thanks for the Help and the things you have highlighted are those what you mean by irrelevant? And that should be removed? So the links you provided should help me fix the issue correct? I will read up on it later I'm very interested in learning mql programming its just a little confusing since I'm new to it. And I'm not sure what you mean by #7
whroeder1
16555
whroeder1  
Angel Dickinson:

what you mean by irrelevant?

I'm not sure what you mean by #7

  1. I posted your three questions above and said "irrelevant" in item 3. Nothing more.
  2. Did you read the link provided. What part of "your code breaks if you switch brokers 4/5 and you don't adjust the value(s,)" is unclear?
Osama Shaban
1589
Osama Shaban  

In my opinion, or actually what I used to do is the following ...

1- Put the EA on each chart you want to trade ...

2- Never ever use OrdersTotal() if you want to treat pairs separately ...

So, you need to tweak the OrdersTotal() and use a custom function instead 

to count total trades of each pair (Not counting all pairs together) ...

So, your code should be like the following ...

extern    int MagicNumber = 12345;
extern    double lots = 0.10;
extern    int stopLoss = 1000;
extern    int takeProfit = 2000;
void OnTick()
  {
  
  if (TotalNoOfOrders() > 0) {
        return;
    }
     
    double ema = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
    double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);    
    
     
    if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Ask - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }


    if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70) ) {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Bid + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print("Sell order succeeded!");
        }
    }
}
//---
   
  
//+------------------------------------------------------------------+



int TotalNoOfOrders(){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
         i++; 
   }
   return(i);
}

I wish there is no mistakes (minor) as I didn't reviewed your original code. But diffidently you need the TotalNoOfOrders() instead of OrdersTotal() ...

Good luck ...

Angel Dickinson
91
Angel Dickinson  
Osama Shaban:

In my opinion, or actually what I used to do is the following ...

1- Put the EA on each chart you want to trade ...

2- Never ever use OrdersTotal() if you want to treat pairs separately ...

So, you need to tweak the OrdersTotal() and use a custom function instead 

to count total trades of each pair (Not counting all pairs together) ...

So, your code should be like the following ...

I wish there is no mistakes (minor) as I didn't reviewed your original code. But diffidently you need the TotalNoOfOrders() instead of OrdersTotal() ...

Good luck ...


Hey I appreciate you kind reply and hospitality as I stated above I'm new to coding and smart comments like the guys gave me above was not helpful at all. Thank you for your help now is there a way to switch TP to act as 100 (for 100 pips) on the extern handle instead of me using 1000( for 100 pips). And so I only use OrdersTotal() for one at a time activation of trades correct?

Osama Shaban
1589
Osama Shaban  
extern    int MagicNumber = 12345;
extern    double lots = 0.10;
extern    int stopLoss = 1000;
extern    int takeProfit = 2000;

void OnTick()
  {
  
  if (TotalNoOfOrders(Symbol()) > 0) {
        return;
    }
     
    double ema = iMA(NULL, 0, 400, 0, MODE_EMA, PRICE_CLOSE, 0);
    double ema2 = iMA(NULL, 0, 200, 0, MODE_EMA, PRICE_CLOSE, 0);
    double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0);    
    
     
    if ((Bid > ema)&&(Bid < ema2)&&(rsi < 30) ) {
        if (OrderSend(Symbol(), OP_BUY, lots, Ask, 3, Bid - stopLoss * Point, Ask + takeProfit * Point, "BossPips", MagicNumber, 0, Blue)) {
            Print("Buy order succeeded!");
        }
    }


    if ((Bid < ema)&&(Bid > ema2)&&(rsi > 70) ) {
        if (OrderSend(Symbol(), OP_SELL, lots, Bid, 3, Ask + stopLoss * Point, Bid - takeProfit * Point, "BossPips", MagicNumber,0, Red)) {
            Print("Sell order succeeded!");
        }
    }
}
//---
   
  
//+------------------------------------------------------------------+

int TotalNoOfOrders(string symb){ 
   int cnt, total; 
   int i = 0;
   total = OrdersTotal(); 
   for(cnt=0;cnt<total; cnt++){ 
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
      if(OrderSymbol()==symb && OrderMagicNumber()==MagicNumber)
         i++; 
   }
   return(i);
}

I backtested it ... I even changed the TotalNoOfOrders() > 0 to different numbers and found it works.

Anyway, I see also you have a mistake in Ask and Bid for Stoploss for both Buy and Sell SendOrder function ...

I corrected everything and also added a little change which has no effect, it is sending the symbol name as an argument to TotalNoOfOrders( ) function ...

Above is the final code ... (or download the attached file) It works ... Open only one order per each currency pair at a time ...

Note: No need to change the MagicNumber ... Just use the default one for all pairs.

Files:
test-del.mq4 2 kb
Angel Dickinson
91
Angel Dickinson  
Osama Shaban:

I backtested it ... I even changed the TotalNoOfOrders() > 0 to different numbers and found it works.

Anyway, I see also you have a mistake in Ask and Bid for Stoploss for both Buy and Sell SendOrder function ...

I corrected everything and also added a little change which has no effect, it is sending the symbol name as an argument to TotalNoOfOrders( ) function ...

Above is the final code ... (or download the attached file) It works ... Open only one order per each currency pair at a time ...

Note: No need to change the MagicNumber ... Just use the default one for all pairs.


Thank you

Angel Dickinson
91
Angel Dickinson  
Osama Shaban:

I backtested it ... I even changed the TotalNoOfOrders() > 0 to different numbers and found it works.

Anyway, I see also you have a mistake in Ask and Bid for Stoploss for both Buy and Sell SendOrder function ...

I corrected everything and also added a little change which has no effect, it is sending the symbol name as an argument to TotalNoOfOrders( ) function ...

Above is the final code ... (or download the attached file) It works ... Open only one order per each currency pair at a time ...

Note: No need to change the MagicNumber ... Just use the default one for all pairs.


I was playing around with the EA and would like to know if you can help me with coding a Moving average crossover for exit. I have been googling how to do this but couldn't find anything can you please help? I would really appreciate this also how to add ATR as my SL feature?

To add comments, please log in or register