alec_djinn
alec_djinn

Reputation: 10799

ExpertMACD raise [invalid stops] if Stop Loss is not 0

Please consider that I am just starting with MQL5.

Just for learning, I ran the EA optimization of MT5 on ExpertMACD (preinstalled in MT5) on BTCUSD H1 pair on an Alpari-MT5-Demo account.

All ok until July 2017 data, after which the Advisor does not evolve anymore, it places 0 trades over and over again (screenshot). The same is true in case of backtesting, with data from July 2017 it does not place any trade.

In the testing log, it appears the error [invalid stops]. If I deselect Stop Loss level from the input to be optimized and I set the default value to 0, then everything works again, although the stop loss is not used anymore.

Can you please explain what is going on? Why an Advisor that works on the whole history stop working from July 2017? (I checked the tick data, all looks ok) Why removing the Stop Loss would make it place trade again?

enter image description here

P.S.

I have noticed that the Advisor break down when (suddenly) the spread for BTCUSD went from 2 to 13000 (yes, without the comma) this, of course, is messing up the advisor and, in general, doesn't make sense. How is that possible? What shall I do in this case? I have checked other brokers and they all show the same increase in spread appearing somewhere in July 2017.

Digging into the Alpari website I found that the average spread value for BTCUSD is indeed that high. Again, how is this possible? Why should it be the case? (Maybe related to the fork? It makes no sense to me)

Finally, how would you modify ExpertMACD in order to properly place orders, including reasonable stop-loss, in this case?

The code of ExpertMACD is the following:

//+------------------------------------------------------------------+
//|                                                   ExpertMACD.mq5 |
//|                   Copyright 2009-2017, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "2009-2017, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
#include <Expert\Signal\SignalMACD.mqh>
#include <Expert\Trailing\TrailingNone.mqh>
#include <Expert\Money\MoneyNone.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
//--- inputs for expert
input string Inp_Expert_Title            ="ExpertMACD";
int          Expert_MagicNumber          =10981;
bool         Expert_EveryTick            =false;
//--- inputs for signal
input int    Inp_Signal_MACD_PeriodFast  =12;
input int    Inp_Signal_MACD_PeriodSlow  =24;
input int    Inp_Signal_MACD_PeriodSignal=9;
input int    Inp_Signal_MACD_TakeProfit  =50;
input int    Inp_Signal_MACD_StopLoss    =20;
//+------------------------------------------------------------------+
//| Global expert object                                             |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialization function of the expert                            |
//+------------------------------------------------------------------+
int OnInit(void)
  {
//--- Initializing expert
   if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing expert");
      ExtExpert.Deinit();
      return(-1);
     }
//--- Creation of signal object
   CSignalMACD *signal=new CSignalMACD;
   if(signal==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating signal");
      ExtExpert.Deinit();
      return(-2);
     }
//--- Add signal to expert (will be deleted automatically))
   if(!ExtExpert.InitSignal(signal))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing signal");
      ExtExpert.Deinit();
      return(-3);
     }
//--- Set signal parameters
   signal.PeriodFast(Inp_Signal_MACD_PeriodFast);
   signal.PeriodSlow(Inp_Signal_MACD_PeriodSlow);
   signal.PeriodSignal(Inp_Signal_MACD_PeriodSignal);
   signal.TakeLevel(Inp_Signal_MACD_TakeProfit);
   signal.StopLevel(Inp_Signal_MACD_StopLoss);
//--- Check signal parameters
   if(!signal.ValidationSettings())
     {
      //--- failed
      printf(__FUNCTION__+": error signal parameters");
      ExtExpert.Deinit();
      return(-4);
     }
//--- Creation of trailing object
   CTrailingNone *trailing=new CTrailingNone;
   if(trailing==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating trailing");
      ExtExpert.Deinit();
      return(-5);
     }
//--- Add trailing to expert (will be deleted automatically))
   if(!ExtExpert.InitTrailing(trailing))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing trailing");
      ExtExpert.Deinit();
      return(-6);
     }
//--- Set trailing parameters
//--- Check trailing parameters
   if(!trailing.ValidationSettings())
     {
      //--- failed
      printf(__FUNCTION__+": error trailing parameters");
      ExtExpert.Deinit();
      return(-7);
     }
//--- Creation of money object
   CMoneyNone *money=new CMoneyNone;
   if(money==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating money");
      ExtExpert.Deinit();
      return(-8);
     }
//--- Add money to expert (will be deleted automatically))
   if(!ExtExpert.InitMoney(money))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing money");
      ExtExpert.Deinit();
      return(-9);
     }
//--- Set money parameters
//--- Check money parameters
   if(!money.ValidationSettings())
     {
      //--- failed
      printf(__FUNCTION__+": error money parameters");
      ExtExpert.Deinit();
      return(-10);
     }
//--- Tuning of all necessary indicators
   if(!ExtExpert.InitIndicators())
     {
      //--- failed
      printf(__FUNCTION__+": error initializing indicators");
      ExtExpert.Deinit();
      return(-11);
     }
//--- succeed
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function of the expert                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ExtExpert.Deinit();
  }
//+------------------------------------------------------------------+
//| Function-event handler "tick"                                    |
//+------------------------------------------------------------------+
void OnTick(void)
  {
   ExtExpert.OnTick();
  }
//+------------------------------------------------------------------+
//| Function-event handler "trade"                                   |
//+------------------------------------------------------------------+
void OnTrade(void)
  {
   ExtExpert.OnTrade();
  }
//+------------------------------------------------------------------+
//| Function-event handler "timer"                                   |
//+------------------------------------------------------------------+
void OnTimer(void)
  {
   ExtExpert.OnTimer();
  }
//+------------------------------------------------------------------+

Update

After a while, Alpari's customer service answered:

The Spread is indicated correctly in the specification at our website. Spread is specified by 3 digits after the dot. It is about 19 US dollars difference between Bid and Ask prices.

So, a spread of 19094 is in reality 19.094.

Upvotes: 4

Views: 1594

Answers (2)

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

Try to debug the EA.

Your log will help to understand the problem, maybe it is that stop_loss_in_pips = 0 and suggested_stoploss = OrderOpenPrice() - stop_loss_in_pips * pip_Size = OrderOpenPrice() so too close to the market

PrintFormat( "%i %s %s - stoploss price before sending an order at %.5f is %.5f",
              __LINE__,
              __FUNCSIG__,
              __FILE__,
              orderSendPrice,
              orderStopLossPrice
              );

Upvotes: 3

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

inside the CSampleExpert::LongOpened() function you can add a block to ignore an entry in case of a large spread:

double spread = m_symbol.Ask()
              - m_symbol.Bid();            // Spread(); is int, not double

if (  spread >  InpMaxSpread * m_adjusted_point ){
      printf( "%i - spread %.5f is too high!", __LINE__, spread );
      return;
}

and the same with ShortOpened(). Also do not forget to add InpMaxSpread into inputs.

As for the question about stop loss - it depends on what you need, an example of use of a take profit is in the code.

double stoploss = m_symbol.Bid() - InpStopLoss * m_adjusted_point;

if (  m_trade.PositionOpen( m_symbol,
                            ORDER_TYPE_BUY,
                            InpLots,
                            price,
                            stoploss,
                            tp
                            )
      ){ ... }

Upvotes: 3

Related Questions