Discussion of article "MQL5 Cookbook: How to Avoid Errors When Setting/Modifying Trade Levels"

 

New article MQL5 Cookbook: How to Avoid Errors When Setting/Modifying Trade Levels is published:

In continuation of our work on the Expert Advisor from the previous article of the series called "MQL5 Cookbook: Analyzing Position Properties in the MetaTrader 5 Strategy Tester", we will enhance it with a whole lot of useful functions, as well as improve and optimize the existing ones. The Expert Advisor will this time have external parameters that can be optimized in the MetaTrader 5 Strategy Tester and will in some ways resemble a simple trading system.

MQL5 Cookbook: How to Avoid Errors When Setting/Modifying Trade Levels

Author: Anatoli Kazharski

 
Great Article!
 

Yes,

this serie of articles are great.

Nevertheless,  I don't understand why when I try the EA in the strategy tester, the TP (takeprofit) and SL (StopLoss) never appears (the SL appears, but it seems that it is when I use the trailing stop only ==> the modification of the SL works, but not the first set, when the order is done)

In the sme time, when I try to debug the EA in live, the order is passed, and SL and TP are correctly set as soon as the order is send (immediatly in TradingBlock(); and it is not  necessary to wait to ModifyTrailingStop();

Anyone can give an explanation ?

Is it a broker problem  (Alpari UK) ?

Thanks  for help.

 
Macgyver75:

Yes,

this serie of articles are great.

Nevertheless,  I don't understand why when I try the EA in the strategy tester, the TP (takeprofit) and SL (StopLoss) never appears (the SL appears, but it seems that it is when I use the trailing stop only ==> the modification of the SL works, but not the first set, when the order is done)

In the sme time, when I try to debug the EA in live, the order is passed, and SL and TP are correctly set as soon as the order is send (immediatly in TradingBlock(); and it is not  necessary to wait to ModifyTrailingStop();

Anyone can give an explanation ?

Is it a broker problem  (Alpari UK) ?

Thanks  for help.

Can you explain why you are thinking there is no TP and SL. I have the 2.
 
Hello, when testing your expert advisor in strategy tester on GOLD I always obtained prompts Failed to open position - Invalid stops - no positions were opened. What was wrong? It was written to avoid this type of error?
 
tatankaska:
Hello, when testing your expert advisor in strategy tester on GOLD I always obtained prompts Failed to open position - Invalid stops - no positions were opened. What was wrong? It was written to avoid this type of error?
I'm afraid we need some code in order to help you. Most probable thing is that the error does exist in your code. Check that your stops are larger than minimal allowed distance and that you did not, by any chance, misplace buy and sell stops. Also make sure your broker does allow specifing stops at position opening - some of them require to add stops only to existing positions (so called "market execution").
 
marketeer:
I'm afraid we need some code in order to help you. Most probable thing is that the error does exist in your code. Check that your stops are larger than minimal allowed distance and that you did not, by any chance, misplace buy and sell stops. Also make sure your broker does allow specifing stops at position opening - some of them require to add stops only to existing positions (so called "market execution").

Hi I tested only expert advisor attached to the article How to avoid errors when setting/modifying trade level, without any change (Positionpropertiesplus). Instatrader platform by Instaforex. The functions in this advisor are written so, that if i set wrong stops parameters they are set to lowest allowed. So normaly should i obtain no error prompt - that is true by forex pairs. But by GOLD in this platform I obtain by each attempt of positionpropertiesplus EA to open position with stops, the error prompt invalid stops and no position in strategy tester was open. Is that the case you mentioned - problem with instatrader platform and broker? In GOLD symbol properties is written instant execution.

Here is example of function for stoploss calculation from mentioned EA - I used the EA attached to the mentioned article without any change, i only used  GOLD instead of forex pairs:

//+------------------------------------------------------------------+

//| Calculating the Stop Loss value                                  |

//+------------------------------------------------------------------+

double CalculateStopLoss(ENUM_ORDER_TYPE order_type)

  {

//--- If Stop Loss is required

   if(StopLoss>0)

     {

      //--- For the calculated Stop Loss value

      double sl=0.0;

      //--- If you need to calculate the value for a BUY position

      if(order_type==ORDER_TYPE_BUY)

        {

         // Calculate the level

         sl=NormalizeDouble(sym_ask-CorrectValueBySymbolDigits(StopLoss*sym_point),sym_digits);

         //--- Return the calculated value if it is lower than the lower limit of the Stops level

         //    If the value is higher or equal, return the adjusted value

         return(sl<sym_down_level ? sl : sym_down_level-sym_offset);

        }

      //--- If you need to calculate the value for a SELL position

      if(order_type==ORDER_TYPE_SELL)

        {

         //--- Calculate the level

         sl=NormalizeDouble(sym_bid+CorrectValueBySymbolDigits(StopLoss*sym_point),sym_digits);

         //--- Return the calculated value if it is higher that the upper limit of the Stops level

         //    If the value is lower or equal, return the adjusted value

         return(sl>sym_up_level ? sl : sym_up_level+sym_offset);

        }

     }

//---

   return(0.0);

  } 
 
tatankaska:
...

Forum on trading, automated trading systems and testing trading strategies


Hello,

Please use the SRC button when you post code. Thank you.


This time, I edited it for you.


 
tatankaska:

Hi I tested only expert advisor attached to the article How to avoid errors when setting/modifying trade level, without any change (Positionpropertiesplus). Instatrader platform by Instaforex. The functions in this advisor are written so, that if i set wrong stops parameters they are set to lowest allowed. So normaly should i obtain no error prompt - that is true by forex pairs. But by GOLD in this platform I obtain by each attempt of positionpropertiesplus EA to open position with stops, the error prompt invalid stops and no position in strategy tester was open. Is that the case you mentioned - problem with instatrader platform and broker? In GOLD symbol properties is written instant execution.

Here is example of function for stoploss calculation from mentioned EA - I used the EA attached to the mentioned article without any change, i only used  GOLD instead of forex pairs:

Yes, that was the case. By GOLD in Instatrader the stops could be placed only after the opening of position. When position opened without stops and after opening modified, everything was OK. Thanks guys.
 

Once more to the calculation of stops (SL/TP) in attached EA - they are calculated correctly only by instruments where symbol point = symbol tick size. That is not the case by GOLD.

Stops must be in this case (by GOLD) corrected to valid symbol ticksize, otherwise you obtain error prompt - invalid stops .

So the error prompts by GOLD are not result of market execution. EA needs a bit correction in calculation of SL/TP, if you want to use it with GOLD.  

 
I modify this to the code

//+------------------------------------------------------------------+
//| Setting the info panel                                           |
//|------------------------------------------------------------------+
void SetInfoPanel()
  {
//--- Visualization or real time modes
   if(ShowInfoPanel && (IsVisualMode() || IsRealtime()))
     {
     // The remaining code of the SetInfoPanel() function
     // ...
     }
  }


Reason: