Download MetaTrader 5

Problem with MT4 strategy tester

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

Hi,

 

I have built an expert advisor thanks to ea builder, it is based on fractals breakout strategy.

I have set up an open long order when price line closes above the upper fractal line with a stop loss located on the lower fractal line. Short orders are opened when the price line closes below the lower fractal line with a stop loss on the upper fractal line.

However if you look below,  a short order was placed by the system, however, it didn't close it when it crosses above the upper fractal line.

 

 

Here are the code lines :

 

   //Open Buy Order, instant signal is tested first
   if(Cross(2, Close[0] > iCustom(NULL, PERIOD_CURRENT, "FractalsLine", 0, 0)) //Candlestick Close crosses above FractalsLine
   && iAO(NULL, PERIOD_CURRENT, 0) <= 0.3 //Awesome Oscillator <= fixed value
   && iAO(NULL, PERIOD_CURRENT, 0) >= -0.3 //Awesome Oscillator >= fixed value
   )
     {
      RefreshRates();
      price = Ask;
      SL = iCustom(NULL, PERIOD_CURRENT, "FractalsLine", 1, 0) + 10; //Stop Loss = FractalsLine + fixed value
      if(!inTimeInterval(TimeCurrent(), TOD_From_Hour, TOD_From_Min, TOD_To_Hour, TOD_To_Min)) return; //open trades only at specific times of the day  
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_BUY, price, TradeSize, "");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "");
      myOrderModify(ticket, SL, 0);
     }
  
   //Open Sell Order, instant signal is tested first
   if(Cross(3, Close[0] < iCustom(NULL, PERIOD_CURRENT, "FractalsLine", 1, 0)) //Candlestick Close crosses below FractalsLine
   && iAO(NULL, PERIOD_CURRENT, 0) <= 0.3 //Awesome Oscillator <= fixed value
   && iAO(NULL, PERIOD_CURRENT, 0) >= -0.3 //Awesome Oscillator >= fixed value
   )
     {
      RefreshRates();
      price = Bid;
      SL = iCustom(NULL, PERIOD_CURRENT, "FractalsLine", 0, 0) + 10; //Stop Loss = FractalsLine + fixed value
      if(!inTimeInterval(TimeCurrent(), TOD_From_Hour, TOD_From_Min, TOD_To_Hour, TOD_To_Min)) return; //open trades only at specific times of the day  
      if(IsTradeAllowed())
        {
         ticket = myOrderSend(OP_SELL, price, TradeSize, "");
         if(ticket <= 0) return;
        }
      else //not autotrading => only send alert
         myAlert("order", "");
      myOrderModify(ticket, SL, 0);
     }

If someone could help me, I would really appreciate it.

Thank you

Roberto Jacobs
48614
Roberto Jacobs  
Naftoil:

Hi,

 

I have built an expert advisor thanks to ea builder, it is based on fractals breakout strategy.

I have set up an open long order when price line closes above the upper fractal line with a stop loss located on the lower fractal line. Short orders are opened when the price line closes below the lower fractal line with a stop loss on the upper fractal line.

However if you look below,  a short order was placed by the system, however, it didn't close it when it crosses above the upper fractal line.

 

 

Here are the code lines :

 


If someone could help me, I would really appreciate it.

Thank you

where the function code myOrderSend?
I see your order does not have a stop loss
myOrderModify code is only active if Not IsTradeAllowed.


Naftoil
15
Naftoil  

Hi,

 

Thank you for your reply. Here is the function code of myOrderSend

int myOrderSend(int type, double price, double volume, string ordername) //send order, return ticket ("price" is irrelevant for market orders)
  {
   if(!IsTradeAllowed()) return(-1);
   int ticket = -1;
   int retries = 0;
   int err;
   int long_trades = TradesCount(OP_BUY);
   int short_trades = TradesCount(OP_SELL);
   int long_pending = TradesCount(OP_BUYLIMIT) + TradesCount(OP_BUYSTOP);
   int short_pending = TradesCount(OP_SELLLIMIT) + TradesCount(OP_SELLSTOP);
   string ordername_ = ordername;
   if(ordername != "")
      ordername_ = "("+ordername+")";
   //test Hedging
   if(!Hedging && ((type % 2 == 0 && short_trades + short_pending > 0) || (type % 2 == 1 && long_trades + long_pending > 0)))
     {
      myAlert("print", "Order"+ordername_+" not sent, hedging not allowed");
      return(-1);
     }
   //test maximum trades
   if((type % 2 == 0 && long_trades >= MaxLongTrades)
   || (type % 2 == 1 && short_trades >= MaxShortTrades)
   || (long_trades + short_trades >= MaxOpenTrades)
   || (type > 1 && long_pending + short_pending >= MaxPendingOrders))
     {
      myAlert("print", "Order"+ordername_+" not sent, maximum reached");
      return(-1);
     }
   //prepare to send order
   while(IsTradeContextBusy()) Sleep(100);
   RefreshRates();
   if(type == OP_BUY)
      price = Ask;
   else if(type == OP_SELL)
      price = Bid;
   else if(price < 0) //invalid price for pending order
     {
      myAlert("order", "Order"+ordername_+" not sent, invalid price for pending order");
          return(-1);
     }
   int clr = (type % 2 == 1) ? clrRed : clrBlue;
   while(ticket < 0 && retries < OrderRetry+1)
     {
      ticket = OrderSend(Symbol(), type, NormalizeDouble(volume, LotDigits), NormalizeDouble(price, Digits()), MaxSlippage, 0, 0, ordername, MagicNumber, 0, clr);
      if(ticket < 0)
        {
         err = GetLastError();
         myAlert("print", "OrderSend"+ordername_+" error #"+err+" "+ErrorDescription(err));
         Sleep(OrderWait*1000);
        }
      retries++;
     }
   if(ticket < 0)
     {
      myAlert("error", "OrderSend"+ordername_+" failed "+(OrderRetry+1)+" times; error #"+err+" "+ErrorDescription(err));
      return(-1);
     }
   string typestr[6] = {"Buy", "Sell", "Buy Limit", "Sell Limit", "Buy Stop", "Sell Stop"};
   myAlert("order", "Order sent"+ordername_+": "+typestr[type]+" "+Symbol()+" Magic #"+MagicNumber);
   return(ticket);
  }

  




 

Roberto Jacobs
48614
Roberto Jacobs  
Naftoil:

Hi,

 

Thank you for your reply. Here is the function code of myOrderSend


  




 

ticket = OrderSend(Symbol(), type, NormalizeDouble(volume, LotDigits), NormalizeDouble(price, Digits()), MaxSlippage, 0, 0, ordername, MagicNumber, 0, clr);

// after MaxSlippage, 0, 0, // it is mean stop loss = 0,  and take profit = 0

Yes, I see your myOrderSend function does not place a stop loss and take profit.

In your code, when IsTradeAllowed, then EA is only open orders without tp and sl.
and myOrderModify function will not be active, because you only activate myOrderModify, if NOT IsTradeAllowed (you use on else)
You should know, if NOT IsTradeAllowed, meaning that EA is not active, then the line
// Not autotrading => only send alerts and below etc.. etc., is irrational logic.
OrderSend(), OrderClose(), OrderCloseBy(), OrderModify(), OrderDelete() trading functions changing the state of a trading account can be called only if trading by Expert Advisors is allowed (the "Allow live trading" checkbox is enabled in the Expert Advisor or script properties).

Your logic will work, if you add a boolean variable to enable or disable EA through a button click on the chart.


Naftoil
15
Naftoil  
it's working now. Thank you very much for your help.
Roberto Jacobs
48614
Roberto Jacobs  
Naftoil:
it's working now. Thank you very much for your help.
You're welcome ^_^
To add comments, please log in or register