4

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.

kenorb
  • 118,428
  • 63
  • 588
  • 624
alec_djinn
  • 7,288
  • 8
  • 29
  • 58

2 Answers2

3

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
              );
user3666197
  • 1
  • 6
  • 43
  • 77
Daniel Kniaz
  • 4,338
  • 2
  • 11
  • 18
  • thanks for your answer. I checked the trades and suddenly the spread for BTCUSD went from 2 to 13000 (yes, without 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 somwhere in July 2017. – alec_djinn Nov 01 '17 at 23:52
  • in mql4 spread is given in settings, if you do not use current spread then you know if for sure. in mql5 spread is given with bars (time, OHLC, volume,spread) so it is possible that spread is too large. maybe because of SeqWit but i am not an expert in that. i would suggest to introduce a max spread filter in the code of ea: `if spread > input parameter - skip trading` – Daniel Kniaz Nov 02 '17 at 08:14
  • It looks like it is related to the fork indeed, even though it is not clear to me what do they want to accomplish by increasing the spread that much. Also, they said it would be until August, but it is still in place. Here the communication from Alpari https://alpari.com/en/company/news/4729_27072017/ – alec_djinn Nov 04 '17 at 10:45
3

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
                            )
      ){ ... }
user3666197
  • 1
  • 6
  • 43
  • 77
Daniel Kniaz
  • 4,338
  • 2
  • 11
  • 18
  • In which file is `CSampleExpert::LongOpened()` defined? I cannot find it. – alec_djinn Nov 05 '17 at 11:34
  • path = Terminal ID\MQL5\Experts\Examples\MACD\MACD Sample.mq5; lines 300 and 335 – Daniel Kniaz Nov 05 '17 at 14:11
  • 1
    regarding `ExpertMACD.mq5` (I looked at another demo ea not this one) - it has `CExpert::Processing()` that comes from `CExpert class (Expert.mqh)`, that calles `CExpert::CheckOpen() - CheckOpenLong() && CheckOpenShort(), OpenLong()` has good place to check that function I think (e.g. in line 753 in `Expert.mqh`) – Daniel Kniaz Nov 07 '17 at 09:19
  • I'll check it out. Thanks! – alec_djinn Nov 07 '17 at 09:33
  • Still not working. I can't evolve the EA with any stop loss. If you manage to make it work please post the full code. Thanks anyway. – alec_djinn Nov 11 '17 at 11:15
  • The file that we are talking about is just an example, and quite a complicated example because it takes long to find the right place where some decision is made. Ok, each tick `ExtExpert.OnTick()` is called that goes ('->') to `CExpert.Processing()` -> selecting direction then `CheckOpen()` -> `CheckLong()` -> here `m_signal` decides on tp and sl levels in `bool m_signal.CheckOpenLong(***)' function. in order to assign the SL/TP - need to modify that function -> `CExpertSignal.OpenLongParams(***)`. – Daniel Kniaz Nov 11 '17 at 14:30
  • How EA knows which value to take? `OnInit()` -> `singal = new CExpertSignal(), CExpert.InitSignal(signal)`. so you can check and debug which value is set in InitSignal() function, and also debug `SL` value in `CExpertSignal.CheckOpenLong(***)` function after `OpenLongParams(***)` is called with success. – Daniel Kniaz Nov 11 '17 at 14:30