Serious Error in Lot Size calculation in MT4

The Lot Size is not adjusted according to the LOT_STEP configuration of the broker. 

So, if the Lot Step (e.g 0.2, 0.25, ...) is not a standard one, all trades fail with ErrorSend error 131.


This could be easily fixed adding the yellow code lines in the sqMMFixedAmount function (taken from the embedded fixLotSize function of the OrderReliable Library, which actually is not used)


double sqMMFixedAmount(string symbol, int orderType, double price, double sl, double RiskedMoney, int decimals, double LotsIfNoMM, double MaximumLots) {
   Verbose("Computing Money Management for order - Fixed amount");
   
   if(UseMoneyManagement == false) {
      Verbose("Use Money Management = false, MM not used");
      return (mmLotsIfNoMM);
   }
   
   symbol = correctSymbol(symbol);
   sl = NormalizeDouble(sl, (int) MarketInfo(symbol, MODE_DIGITS));
   
   double openPrice = price > 0 ? price : (orderType == OP_BUY ? sqGetAsk(symbol) : sqGetBid(symbol));
   double LotSize=0;


   if(RiskedMoney <= 0 ) {
      Verbose("Computing Money Management - Incorrect RiskedMoney value, it must be above 0");
      return(0);
   }
   
   double PointValue = MarketInfo(symbol, MODE_TICKVALUE) / MarketInfo(symbol, MODE_TICKSIZE);   
   double Smallest_Lot = MarketInfo(symbol, MODE_MINLOT);
   double Largest_Lot = MarketInfo(symbol, MODE_MAXLOT);    
   double LotStep = MarketInfo(symbol, MODE_LOTSTEP);

   //Maximum drawdown of this order if we buy 1 lot 
   double oneLotSLDrawdown = PointValue * MathAbs(openPrice - sl);

   if(oneLotSLDrawdown > 0) {
  LotSize = roundDown(RiskedMoney / oneLotSLDrawdown, decimals);
  
  double mod = MathMod(LotSize, LotStep);
          if(MathAbs(mod - LotStep) > 0.000001){
               LotSize -= mod;
     }
   }
   else {
  LotSize = 0;
   }
........

Attachments
Activtradesmt4.properties
(34.76 KiB)
  • Votes +1
  • Project StrategyQuant X
  • Type Bug
  • Status Fixed
  • Priority Normal

History

MS
#1

mscapin95

07.12.2022 15:08

Task created

Attachment H1 - NASDAQ WF Matrix - Strategy 178159 - original.mq4 added

Attachment H1 - NASDAQ WF Matrix - Strategy 178159 - fixed.mq4 added

H1 - NASDAQ WF Matrix - Strategy 178159 - original.mq4
(228.74 KiB)
H1 - NASDAQ WF Matrix - Strategy 178159 - fixed.mq4
(228.89 KiB)
Original and fixed MT4 EAs
TM
#2

tmatejka.

08.12.2022 09:15

Attachment 2022-12-08_9-08-24.mp4 added

2022-12-08_9-08-24.mp4
(1.54 MiB)

Hello,

this is fixed with the next update 136 we have already available (https://strategyquant.com/download/)

See the clip attached how to use. You just need to set "Order Size Multiplier"

MS
#3

mscapin95

08.12.2022 10:36
I do not believe we are speaking of the same issue.


If you use the same Broker parameters that your Broker uses (as in my case), the MT4 Lot Size is correct and should not be multiplied by any value.

Only the format is wrong, because it is not adapted to the LOTSTEP used by the broker.


E.G:


SQX strategy produces an order whose Lot Size is 3.51. The size is correct and so the Order Size Multiplier must be set to 1.

But, LOTSTEP of my broker is 0.2, then 3.51 cannot be accepted and the Lot Size must be rounded to 3.4 or 3.6 before being passed to OrderSend.  


In current EAs produced by SQX, LOTSTEP in being loaded from the Market but actually not used by the sqMMFixedAmount function. With the correction it is istead used to correct from 3.51 to 3.4, which can be accepted by the Broker.

E
#4

Emmanuel

11.12.2022 12:41
Voted for this task.
MS
#5

mscapin95

23.03.2023 12:53

Attachment FixedAmount_class.tpl added

FixedAmount_class.tpl
(2.61 KiB)
Bug still present in rel 136. 

Fix attached



Votes: +1

Drop files to upload

or

choose files

Max size: 5MB

Not allowed: exe, msi, application, reg, php, js, htaccess, htpasswd, gitignore

...
Wait please