MT5 wizard EA, adding manual stop values kills profitability

 

I have an MQL5 EA I built using the wizard, but I'm kind of stumped on something. It uses a PSAR trailing stop. If I set no manual stop loss, I get 2794 trades within the test period with a profit of Recovery Factor of 4.04, profit of 9510.91. If I set any initial stop loss (I've tried everything from 1 point up to a million), it increases the number of trades (range effect up to a max of 2877), drops the RF (range effect from 1.94 at best on down to losses), and cuts the profit (5633.36 at best on down to losing). Adding a manual stop loss larger than any trade ever hits shouldn't affect the profitability, right?

In fact, this is weird - if I set the stop loss to something ridiculous, like, say, 10 million points, it kills all the long trades (turns them into losses).

Any ideas?
Basic Principles - Trading Operations - MetaTrader 5 Help
  • www.metatrader5.com
Before you proceed to study the trade functions of the platform, you must have a clear understanding of the basic terms: order, deal and position...
 
Scott Allen: I have an MQL5 EA I built using the wizard, but I'm kind of stumped on something. It uses a PSAR trailing stop. If I set no manual stop loss, I get 2794 trades within the test period with a profit of Recovery Factor of 4.04, profit of 9510.91. If I set any initial stop loss (I've tried everything from 1 point up to a million), it increases it to 2877 trades, drops the RF to 1.94, and the profit down to 5633.36. Adding a manual stop loss, particularly one higher than any trade ever hits, shouldn't affect the profitability, right? In fact, this is weird - if I set the stop loss to something ridiculous, like, say, 10 million points, it kills all the long trades (turns them into losses). Any ideas?

Please remember that we cannot read your mind, nor see your computer. Such a vague explanation does not make it clear what your issue may be.

Only by showing your code, log files results, some screenshots, etc. can we begin to understand some of your explanation.

However, given that you did not code it yourself and may lack some coding knowledge, any answers we give, may not be totally understandable to you. Please be aware of that possibility.

 
Fernando Carreiro #:

Please remember that we cannot read your mind, nor see your computer. Such a vague explanation does not make it clear what your issue may be.

Only by showing your code, log files results, some screenshots, etc. can we begin to understand some of your explanation.

However, given that you did not code it yourself and may lack some coding knowledge, any answers we give, may not be totally understandable to you. Please be aware of that possibility.

The code was generated by the MT5 wizard, using the PSAR signal and a PSAR trailing stop (yeah, I know it's weird, but it's intentional - the idea is for it to be the foundation for a 2-timeframe PSAR strategy). And I appreciate your point — I'm enough of a developer (plenty of experience in other languages) to read/understand/make sense of code, but MQL is new to me, so I'm using wizard-generated code as both a starting point and a way to learn MQL. I can't write it from scratch, but I'm sure I'll be fine with any explanation of solutions.

Here's the code:
//+------------------------------------------------------------------+
//|                                                    PSAR-PSAR.mq5 |
//|                                  Copyright 2023, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Include                                                          |
//+------------------------------------------------------------------+
#include <Expert\Expert.mqh>
//--- available signals
#include <Expert\Signal\SignalSAR.mqh>
//--- available trailing
#include <Expert\Trailing\TrailingParabolicSAR.mqh>
//--- available money management
#include <Expert\Money\MoneyFixedLot.mqh>
//+------------------------------------------------------------------+
//| Inputs                                                           |
//+------------------------------------------------------------------+
//--- inputs for expert
input string Expert_Title                 ="PSAR-PSAR"; // Document name
ulong        Expert_MagicNumber           =27294;       //
bool         Expert_EveryTick             =false;       //
//--- inputs for main signal
input int    Signal_ThresholdOpen         =10;          // Signal threshold value to open [0...100]
input int    Signal_ThresholdClose        =10;          // Signal threshold value to close [0...100]
input double Signal_PriceLevel            =0.0;         // Price level to execute a deal
input double Signal_StopLevel             =50.0;        // Stop Loss level (in points)
input double Signal_TakeLevel             =50.0;        // Take Profit level (in points)
input int    Signal_Expiration            =4;           // Expiration of pending orders (in bars)
input double Signal_SAR_Step              =0.02;        // Parabolic SAR(0.02,0.2) Speed increment
input double Signal_SAR_Maximum           =0.2;         // Parabolic SAR(0.02,0.2) Maximum rate
input double Signal_SAR_Weight            =1.0;         // Parabolic SAR(0.02,0.2) Weight [0...1.0]
//--- inputs for trailing
input double Trailing_ParabolicSAR_Step   =0.02;        // Speed increment
input double Trailing_ParabolicSAR_Maximum=0.2;         // Maximum rate
//--- inputs for money
//input double Money_FixLot_Percent         =10.0;        // Percent
input double Money_FixLot_Lots            =0.1;         // Fixed volume
//+------------------------------------------------------------------+
//| Global expert object                                             |
//+------------------------------------------------------------------+
CExpert ExtExpert;
//+------------------------------------------------------------------+
//| Initialization function of the expert                            |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- Initializing expert
   if(!ExtExpert.Init(Symbol(),Period(),Expert_EveryTick,Expert_MagicNumber))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing expert");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Creating signal
   CExpertSignal *signal=new CExpertSignal;
   if(signal==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating signal");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//---
   ExtExpert.InitSignal(signal);
   signal.ThresholdOpen(Signal_ThresholdOpen);
   signal.ThresholdClose(Signal_ThresholdClose);
   signal.PriceLevel(Signal_PriceLevel);
   signal.StopLevel(Signal_StopLevel);
   signal.TakeLevel(Signal_TakeLevel);
   signal.Expiration(Signal_Expiration);
//--- Creating filter CSignalSAR
   CSignalSAR *filter0=new CSignalSAR;
   if(filter0==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating filter0");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
   signal.AddFilter(filter0);
//--- Set filter parameters
   filter0.Step(Signal_SAR_Step);
   filter0.Maximum(Signal_SAR_Maximum);
   filter0.Weight(Signal_SAR_Weight);
//--- Creation of trailing object
   CTrailingPSAR *trailing=new CTrailingPSAR;
   if(trailing==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating trailing");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Add trailing to expert (will be deleted automatically))
   if(!ExtExpert.InitTrailing(trailing))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing trailing");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Set trailing parameters
   trailing.Step(Trailing_ParabolicSAR_Step);
   trailing.Maximum(Trailing_ParabolicSAR_Maximum);
//--- Creation of money object
   CMoneyFixedLot *money=new CMoneyFixedLot;
   if(money==NULL)
     {
      //--- failed
      printf(__FUNCTION__+": error creating money");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Add money to expert (will be deleted automatically))
   if(!ExtExpert.InitMoney(money))
     {
      //--- failed
      printf(__FUNCTION__+": error initializing money");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Set money parameters
//   money.Percent(Money_FixLot_Percent);
   money.Lots(Money_FixLot_Lots);
//--- Check all trading objects parameters
   if(!ExtExpert.ValidationSettings())
     {
      //--- failed
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- Tuning of all necessary indicators
   if(!ExtExpert.InitIndicators())
     {
      //--- failed
      printf(__FUNCTION__+": error initializing indicators");
      ExtExpert.Deinit();
      return(INIT_FAILED);
     }
//--- ok
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Deinitialization function of the expert                          |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
   ExtExpert.Deinit();
  }
//+------------------------------------------------------------------+
//| "Tick" event handler function                                    |
//+------------------------------------------------------------------+
void OnTick()
  {
   ExtExpert.OnTick();
  }
//+------------------------------------------------------------------+
//| "Trade" event handler function                                   |
//+------------------------------------------------------------------+
void OnTrade()
  {
   ExtExpert.OnTrade();
  }
//+------------------------------------------------------------------+
//| "Timer" event handler function                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
   ExtExpert.OnTimer();
  }
//+------------------------------------------------------------------+

Input variables:

Backtest results with SL set to 0:

There seems to be a range effect up to 55000 (see attached file).

As far as what happens above that, I think I figured that out. Above 800,000 points, it starts killing long trades. My best guess is that it's hitting the point that some of those are invalid stop loss values on long trades, i.e., they'd be below 0. The backtest period is going back 2+ years, so that makes sense, so never mind about that issue.

But what doesn't make sense is why adding a stop loss of, say, 800,000 points would cut the profitability in half vs. no stop loss.

Thanks for any help you can offer.
 

Well pick out one of the results of the optimizer and run it in visual mode. Then pickup one or more trades and let them run in the debugger: first set in the Editor Option time, symbol, .. and then start the debugger and control in detail how your EA behaves.

 
Carl Schreiber #:

Well pick out one of the results of the optimizer and run it in visual mode. Then pickup one or more trades and let them run in the debugger: first set in the Editor Option time, symbol, .. and then start the debugger and control in detail how your EA behaves.

Haven't used the debugger yet. Lots more learning to do, I guess, before I can fix this seemingly simple problem. *sigh*