Problem with E.A. Sometimes dont open a trade

 

Hello, I use this E.A, and in strategy tester work good, but when I put it on a real account, when it have to open two trades, sometimes (only sometimes), it open just one, so I cant testing in real.

Could exist a code for an expert to review and correct if dont open a trade, every second?

Sorry for my english, thank you very much

I add the code (its no mine, I m only add a trailing stop code)

//+------------------------------------------------------------------+
//| mo_bidir_v0_1.mq4                                                |
//|                                                                  |
//| - Works best in 5M timeframe                                     |
//| - Bug fix to stop_loss in line 22 2010.04.07                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010 - Monu Ogbe"

#define MAGIC  0
#define IDENT  "mo_bidir_v0_1"

extern bool trail = True;
extern double TrailingStop    = 50;
extern double  lots           = 1.0;
extern double  stop_loss      = 600;   // (8 pips) optimise 50-2000
extern double  take_profit    = 10000;  // (75 pips) optimise 50-2000

int            last_bar       = 0;
void trail() 
{
for (int i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if ( OrderSymbol()==Symbol() ) 
{
if (OrderType() == OP_BUY) {
if (Bid - OrderOpenPrice() > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if (OrderStopLoss() < Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
OrderModify(OrderTicket(), OrderOpenPrice(), Bid - TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red);
}
}
} else if (OrderType() == OP_SELL) {
if (OrderOpenPrice() - Ask > TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) {
if ((OrderStopLoss() > Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT)) || 
(OrderStopLoss() == 0)) {
OrderModify(OrderTicket(), OrderOpenPrice(),
Ask + TrailingStop * MarketInfo(OrderSymbol(), MODE_POINT), OrderTakeProfit(), Red); 
}
}
}
}
}
}

int start(){
   if (trail) trail();
   
   if (last_bar == Bars) return(0);
   last_bar = Bars;
   if (OrdersTotal() == 0){
         OrderSend(Symbol(), OP_BUY, lots ,Ask, 3, Ask - stop_loss * Point, Bid + take_profit * Point, IDENT, MAGIC, 0, Blue);
         OrderSend(Symbol(), OP_SELL, lots ,Bid, 3, Bid + stop_loss * Point, Ask - take_profit * Point, IDENT, MAGIC, 0, Red);
   } 
   return(0);
}
 
gabrejos:

Hello, I use this E.A, and in strategy tester work good, but when I put it on a real account, when it have to open two trades, sometimes (only sometimes), it open just one, so I cant testing in real.

Could exist a code for an expert to review and correct if dont open a trade, every second?

Sorry for my english, thank you very much

I add the code (its no mine, I m only add a trailing stop code)

Check your Trading Function return values,  print the error and other relevant information if they fail . . .  then when they do you will have all the information you need to diagnose the problem. 


What are Function return values ? How do I use them ?

 

Don't use Bars,  it isn't a reliable method . . .   use Time[1]  instead . . . 

 

Even this wasn't your question, after adding the proposed error handling you should also think about your trail() function:

- No filter for magic number (and MAGIC defined as 0): Makes it incompatible with other EAs and manual trading.
- No error handling/Print() statements for OrderModify(): Makes it hard to find any erros.
- You should always count down orders: https://www.mql5.com/en/forum/139654

 
RaptorUK:

Check your Trading Function return values,  print the error and other relevant information if they fail . . .  then when they do you will have all the information you need to diagnose the problem. 


What are Function return values ? How do I use them ?

 

Don't use Bars,  it isn't a reliable method . . .   use Time[1]  instead . . . 


Thank you for your answer Raptor. Just dont open, no error.

You have more experience than me in forex, Raptor, could you test it in strategy tester H1, H4, or Daily, with any pair with less than 2 pips spread, and a broker with 5 digits, and tell me what you think?

Change the stop loss to 800 (80 pips) 

I really dont trust in strategy tester. Is posible that performance?

Thank you very much 

 
gabrejos:


Thank you for your answer Raptor. Just dont open, no error.

You have more experience than me in forex, Raptor, could you test it in strategy tester H1, H4, or Daily, with any pair with less than 2 pips spread, and a broker with 5 digits, and tell me what you think?

Change the stop loss to 800 (80 pips) 

I really dont trust in strategy tester. Is posible that performance?

Thank you very much 

You're funny guy. The purpose is finding error is to find what is the cause of your EA not sending order ;). I add error Print and run your EA and find several error - like there's some value in your EA that is too small.

#include <stdlib.mqh>
// Print ("An error has occured ",ErrorDescription(GetLastError());