EA Backtests and Optimization all Stop Losses were perfect > Live abnormally wide SL

 

Hello I hope the MQL5 forum can help me out,

My Back tests in Strategy Tester for my MT5 EA, have been flawless, fast optimization (168 tests), ‘fast Open Prices only’ Single Tests , ‘Every Tick based on real ticks’ decent results, stop losses, Take profit and lot sizes, have all been correct over 5 year tests – I was very satisfied. (No Errors in the Logs, in 100s of simulated trades, no abnormal Lot sizes etc..).

I have put this EA on a Live Account and 2/3 of my trades, give abnormally wide Stop Losses (up to 7,000 points) and because of that I receive a ‘popup’ an Alert (which I coded) and get the minimum lot size 0.01 . Just 1 of the Trades is Correct (22 pip stop).

I was wondering if the code compiles too quickly, when calculating the Stop Loss;  

1) Do I have to put Sleep() functions in my code?  - I don’t have any yet.

2) Is the code compiled too quickly for a Real Live Trading setting, vs Strategy Tester Simulations ?

3) Should I put a Sleep() function B4 “Buy Market Now” for example?


I have gone back to Strategy Tester but again, no Errors ever. I have run out of ideas.


I have attached some images – as I don’t have the credentials to embed images.

-2 images show wide (4,000 to 7,000) Stop Losses and also the trade where it is correct.

-1 image is of my Expert Log with Alerts printed to console.


If you can help me out, pls...

 

If your EA is properly coded, you should never need to use "sleep".

As for the rest, please read and apply the following. It is valid for ALL EAs, even non Market ones.

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
Fernando Carreiro #:

If your EA is properly coded, you should never need to use "sleep".

As for the rest, please read and apply the following. It is valid for ALL EAs, even non Market ones.

okay, I am reading Now thanks.

 

I have found the culprit:


custom indicator loads and removes itself

11:24:12.727   Indicators        custom indicator ATR_Trailing_Stop_1_Buffer (USDJPY,M30) loaded successfully

11:25:36.787   Indicators        custom indicator ATR_Trailing_Stop_1_Buffer (USDJPY,M30) removed


custom indicator ATR_Trailing_Stop_1_Buffer , continuously loads and removes itself, I have never had this problem using this indicator in manual trading - it has never removed itself from the chart while I was trading.

I use this indicator to Set Stop Losses (So underlined in Red - see image), so that is why Stop Loss was submitted as sl= 0.000, which meant I automatically received a lot Size Alert and triggered minimum lot size=0.01 and a huge SL .

 

When I find a Solution I will post in here and, any forum topic where custom indicator has been loading and removing.


The Code for the indicator is here, if anyone can quickly spot anything, anyhow I'll be up all night looking up MQL5 documentation.

ATR Trailing Stop with 1 Buffer only
ATR Trailing Stop with 1 Buffer only
  • www.mql5.com
This is an edit of the Mod_ATR_Trailing_Stop by MQL5 user @Scriptor found here https://www.mql5.com/en/code/20423 . MT5 indicator .mql5 and .ex5 files, report any bugs, I'll fix.
 
Phil_GMT #:

I have found the culprit:



11:24:12.727   Indicators        custom indicator ATR_Trailing_Stop_1_Buffer (USDJPY,M30) loaded successfully

11:25:36.787   Indicators        custom indicator ATR_Trailing_Stop_1_Buffer (USDJPY,M30) removed


custom indicator ATR_Trailing_Stop_1_Buffer , continuously loads and removes itself, I have never had this problem using this indicator in manual trading - it has never removed itself from the chart while I was trading.

I use this indicator to Set Stop Losses (So underlined in Red - see image), so that is why Stop Loss was submitted as sl= 0.000, which meant I automatically received a lot Size Alert and triggered minimum lot size=0.01 and a huge SL .

 

When I find a Solution I will post in here and, any forum topic where custom indicator has been loading and removing.


The Code for the indicator is here, if anyone can quickly spot anything, anyhow I'll be up all night looking up MQL5 documentation.

Why don’t you code this indicator into your EA… instead of using a custom 
 
Daniel Cioca #:
Why don’t you code this indicator into your EA… instead of using a custom 

Thank you, @Daniel Cioca for the suggestion. I will try that. I thought iCustom was the proper protocol. I did include the indicator as a #resource.



I just got this Warning message about opencl.dll, I am using a Windows Server 2022 VPS, that does not have Nvidia Graphics type card, what it has is a Microsoft Basic/Remote Display Adapter.

(Open CL driver comes with your graphics card apparently)

Could opencl.dll be causing my problems? I have OpenCL enabled in my MT5 settings, should I disable it since I don't have opencl.dll ?


OpenCL Driver graphics


PS it is great that I have had my credentials upgraded , thank you, I can now embed images. I'll try not to be a nuisance.

 

Is anything wrong with the following code? My EA works perfectly in Strategy Tester.

The rest were all declared as int as Global Variables

int OnInit()
  {
//---
  
   string indicatorName1 = "\\Indicators\\testing123\\ATR_Trailing_Stop_1_Buffer.ex5";

   ATR7_DeF_Anchor = iCustom(_Symbol,Anchor_TF, indicatorName1,7,2.0);
   ATR14_DeF_Anchor = iCustom(_Symbol,Anchor_TF, indicatorName1,14,2.0);
   
   ATR7_DeF_First = iCustom(_Symbol,First_TF, indicatorName1,7,2.0);
   ATR14_DeF_First = iCustom(_Symbol,First_TF, indicatorName1,14,2.0);
   
   ATR7_DeF_Second = iCustom(_Symbol,Second_TF, indicatorName1,7,2.0);
   ATR14_DeF_Second = iCustom(_Symbol,Second_TF, indicatorName1,14,2.0);
//---
   return(INIT_SUCCEEDED);
  }
 
Phil_GMT #: Is anything wrong with the following code? My EA works perfectly in Strategy Tester. The rest were all declared as int as Global Variables

Yes! You are not checking if obtaining the handle succeeded or not. You are assuming that it will ALWAYS obtain the handle.

You should be checking the return handle. In case of failure it returns INVALID_HANDLE, and when that happens you should check the error code (and log it), and report a failed initialisation — INIT_FAILED.

 
Fernando Carreiro #:

Yes! You are not checking if obtaining the handle succeeded or not. You are assuming that it will ALWAYS obtain the handle.

You should be checking the return handle. In case of failure it returns INVALID_HANDLE, and when that happens you should check the error code (and log it), and report a failed initialisation — INIT_FAILED.


AHA! I see I just googled what you said:

I got this from Orchard FX.

Thanks once Again @Fernando Carreiro , what can I say you have helped me out yet again!

if ( Handle == INVALID_HANDLE ) {
      PrintFormat( "Error %i", GetLastError() );
      return ( INIT_FAILED );
   }


 

I have found the solution to all my problems.

First of All I would like to thank all who took part in this Topic.

I made Several Mistakes.

#1 )

custom indicator ATR_Trailing_Stop_1_Buffer (USDJPY,M30) loaded successfully, Yes, I reported that this indicator removed itself, that was not true. What I did I ran a test in Strategy Tester and of course it will load and then unload once the test is completed and report that to the Journal, had nothing to do with the EA on my Chart.

#2)

I found out MT5 does not like to look far away from the MQL5 folder, in directories for files, for example I had my file buried in many sub-directories, and that affected adding iCustom() indicators as a #resource so when i located my EA in strategy tester:

...MQL5\Experts\tesing123\hello_there\I_am_in_here\No_but_yes\Live\USDJPY\supercalifradistic_expealidotious_EA.ex5
//+------------------------------------------------------------------+
//|                         supercalifradistic_expealidotious_EA.mq5 |
//|                        Copyright 2023, MetaQuotes Software Corp. |
//|                                                 https://mql5.com |
//+------------------------------------------------------------------+


#resource "Indicators\\tesing123\\ATR_Trailing_Stop_1_Buffer.ex5"


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+

int OnInit(){

string indicator1 = "::Indicators\\tesing123\\ATR_Trailing_Stop_1_Buffer.ex5" ;
int handle = iCustom(_Symbol, _Period ,indicator1, ...) ;
   

if ( handle  == INVALID_HANDLE ) {
      PrintFormat( "Error %i", GetLastError() );
      return ( INIT_FAILED );
   }
   
//---
   return(INIT_SUCCEEDED);

}

It would not find my iCustom indicator on tester initialization, test would fail to start. But when I paste the EA .ex5 file, in MQL5\Experts it works fine.

#3)

Finally about those wide Stop Losses, I realized I was at fault, nobody else; On initialization (or everytime I restarted my MT5 Terminal) 2 variables always needed to be retrieved Oninit() , before, the EA runs, otherwise they start on zero. My solution uses 2 while Loops (dangerous) to go back in time , counting candles back - shift, to find the first values if my variable ==0, as soon as a value is found I break; out of the iteration loop.


#4) Sorry for wasting everyone's time, I can feel myself becoming a better coder, definitely faster with MQL5. EA is working as it should, with a few more checks and balances suggested by @Fernando Carreiro


Topic can be Closed or Deleted.

 
Phil_GMT #: Topic can be Closed or Deleted.

On this forum, topics are never closed nor deleted (unless for breaking rules).

They remain open should other users have related questions and answers.

So, I ask that you please do not remove or amend content in your older posts.

Reason: