MT5 EA works well most of the time but occasionally miscalculated a 100 lot trade

 

Dear coders,

I ordered a freelancer to code my custom EA which worked well most of the time, but occasionally, it opened 100 lots of trade, and he could not yet figure out the cause.

The lot calculation, TP, SL, are calculated based on risk percentage towards symbol pips value, daily ATR value and account balance, and EA should open multiple pairs based on my input parameters.

EA has opened all of 28 pairs repeatedly and correctly, but occasionally, some trades went wrong with 100 lots size, while they were just expected to open trades around 0.1 to 0.25 lots.

Any advice or recommendation on what to look at or what can possibly cause it would be greatly appreciated.

Thank you.

 
Milldoku:

Dear coders,

I ordered a freelancer to code my custom EA which worked well most of the time, but occasionally, it opened 100 lots of trade, and he could not yet figure out the cause.

The lot calculation, TP, SL, are calculated based on risk percentage towards symbol pips value, daily ATR value and account balance, and EA should open multiple pairs based on my input parameters.

EA has opened all of 28 pairs repeatedly and correctly, but occasionally, some trades went wrong with 100 lots size, while they were just expected to open trades around 0.1 to 0.25 lots.

Any advice or recommendation on what to look at or what can possibly cause it would be greatly appreciated.

Thank you.

the code.

And unless you post the code here, then, we can not help you.

But i recommend that you chat to the developer that wrote the code.

but a suggestion: it has been discussed on the website, how _Point and buffers such as ATR sometimes give weird numbers. Therefore this can cause a strange end result when they are used in a math calculation. The coder has to put in error detection methods to both detect when this happens, and have either a alternate way to calculate the lot, OR cause the ea to sleep or wait until the math returns a value that is acceptable and valid.

 
Milldoku:

Dear coders,

I ordered a freelancer to code my custom EA which worked well most of the time, but occasionally, it opened 100 lots of trade, and he could not yet figure out the cause.

The lot calculation, TP, SL, are calculated based on risk percentage towards symbol pips value, daily ATR value and account balance, and EA should open multiple pairs based on my input parameters.

EA has opened all of 28 pairs repeatedly and correctly, but occasionally, some trades went wrong with 100 lots size, while they were just expected to open trades around 0.1 to 0.25 lots.

Any advice or recommendation on what to look at or what can possibly cause it would be greatly appreciated.

Thank you.

I guess you should review you stop loss placement logic. Sometimes the logic generates too close stop losses that cause huge lot sizes to achieve the balance percentage at risk. So you should make sure to have a minimum distance to set your SL.

 
Revo Trades #:

the code.

And unless you post the code here, then, we can not help you.

But i recommend that you chat to the developer that wrote the code.

but a suggestion: it has been discussed on the website, how _Point and buffers such as ATR sometimes give weird numbers. Therefore this can cause a strange end result when they are used in a math calculation. The coder has to put in error detection methods to both detect when this happens, and have either a alternate way to calculate the lot, OR cause the ea to sleep or wait until the math returns a value that is acceptable and valid.

Hi Revo Trades,

Thank you for responding my question. Yes the coder did mention about possible error with ATR results, and the first updated version after trying to fix this problem not yet solved the issue. I will pass your suggestion to my coder. Hope it helps him ..... to help me :)

I am not coder so I don't know where to look, and the codes consist of more than 800 lines, but here are some parts related to lot size. I believe the math calculations are correct, and most of the trades are correct:

   string symbol = positiveList[i];
   double point = SymbolInfoDouble(symbol,SYMBOL_POINT);
   double op = SymbolInfoDouble(symbol,SYMBOL_BID);
   double sl = op+ATRP(i)*SLATRMultiplier;
   double tp = op-ATRP(i)*TPATRMultiplier;
   double lot = AccountInfoDouble(ACCOUNT_BALANCE)*RiskPct/100/(MathAbs(op-sl)/point)/SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_VALUE);

   MyPositionOpen(symbol,ORDER_TYPE_SELL,lot,op,sl,tp,TradeComment,Magic);
}

for(int i=0;i<negativeAmount;i++)
{
   string symbol = negativeList[i];
   double point = SymbolInfoDouble(symbol,SYMBOL_POINT);
   double op = SymbolInfoDouble(symbol,SYMBOL_ASK);
   double sl = op-ATRN(i)*SLATRMultiplier;
   double tp = op+ATRN(i)*TPATRMultiplier;
   double lot = AccountInfoDouble(ACCOUNT_BALANCE)*RiskPct/100/(MathAbs(op-sl)/point)/SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_VALUE);

   MyPositionOpen(symbol,ORDER_TYPE_BUY,lot,op,sl,tp,TradeComment,Magic);
Improperly formatted code edited by moderator.
 
Yashar Seyyedin #:

I guess you should review you stop loss placement logic. Sometimes the logic generates too close stop losses that cause huge lot sizes to achieve the balance percentage at risk. So you should make sure to have a minimum distance to set your SL.

Hello Yashar,

Thank you for responding my post. I normally use daily ATR distance for my TP and SL, with multiplier factor more than 1x. Using same setting, same pairs, most of the trades were correct, only occasionally few trades came with 100 lots size. I blew my funded challenge account once due to this error and almost blew another one, saved by max dd filter but now put the account in critical.

 

Please, always use the CODE button (Alt-S) when inserting code.

Code button in editor

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Fernando Carreiro #:

Please, always use the CODE button (Alt-S) when inserting code.

thank you Fernando for the reminder and did the correction for me. I appreciate it.

 

hello again,

just to update, my coder sent me several updated version but still once for a while the same 100 lots trade appeared, occassionally. He added several filters but it seems did not work. If anyone had similar problem and had found the solution please share the idea of the solution.


or perhaps the mql5 moderators can help.

thank you.

 
Milldoku #: just to update, my coder sent me several updated version but still once for a while the same 100 lots trade appeared, occassionally. He added several filters but it seems did not work. If anyone had similar problem and had found the solution please share the idea of the solution. or perhaps the mql5 moderators can help.

We can't help you much, because the problem is in the code. The following is incorrect!

double lot = AccountInfoDouble(ACCOUNT_BALANCE)*RiskPct/100/(MathAbs(op-sl)/point)/SymbolInfoDouble(symbol,SYMBOL_TRADE_TICK_VALUE);

Also, don't do it "manually"—use the OrderCalcProfit function!

Forum on trading, automated trading systems and testing trading strategies

SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE) sometimes zero

Fernando Carreiro, 2022.08.23 17:41

You can! These are the steps I take. I supply the function with a lot size equal to the “Max Lot Size” allowed for the symbol in question, then calculate the ratio needed to achieve the fractional risk that I wish to apply, to get the correct volume for the order. I then align that with the “Lot Step” and finally check it against both the maximum and minimum allowed lots for the symbol.

The reason I use the “maximum” lots instead of just “1.0” lots as a reference value is because there is no guarantee that the value of 1.0 is within the minimum and maximum values allowed. Given that using 1.0, or the maximum, gives equivalent results anyway (by using the ratio method), I choose to use the “max lots” as the reference point which also offers the most precision for the calculation.

Something like this ...

// This code will not compile. It is only a example reference

if( OrderCalcProfit( eOrderType, _Symbol, dbLotsMax, dbPriceOpen, dbPriceStopLoss, dbProfit ) )
{
   dbOrderLots = fmin( fmax( round( dbRiskMax * dbLotsMax / ( -dbProfit * dbLotsStep ) )
               * dbLotsStep, dbLotsMin ), dbLotsMax ); 
      
   // the rest of the code ...
};

Also abide by the various contract specifications and broker conditions ...

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.
Documentation on MQL5: Trade Functions / OrderCalcProfit
Documentation on MQL5: Trade Functions / OrderCalcProfit
  • www.mql5.com
OrderCalcProfit - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

We can't help you much, because the problem is in the code. The following is incorrect!

Also, don't do it "manually"—use the OrderCalcProfit function!

Also abide by the various contract specifications and broker conditions ...

thank you so much Fernando I will forward your comment to the coder

Reason: