Error 134 when there is more than enough funds

 

I have a basic Expert Advisor that uses:

  • ATR for setting SL and TP ( ATR*1.5) 
  • 2-5% risk
  • atrPip converts the atr to pips and rounds up.
  • I use the atrPip to calculate the perPip value for my risk and calculate the lot size I need to trade
I am testing with a $1000 account in a micro account. However I get a error 134 even though there should be enough margin. It only needed 0.24 lots.

   double atr = iCustom(Symbol(), 0, "ATR", 0,0);
   double atrPip = MathFloor(((iCustom(Symbol(), 0, "ATR", 0,0))*10000)+1);   
   
   accountSize = AccountBalance();
   usedMargin =  AccountInfoDouble( ACCOUNT_MARGIN  );
   marginCall = AccountInfoDouble(ACCOUNT_MARGIN_SO_CALL);
   double perPipValue = (accountSize*(risk/100))/(atrPip*1.5);
   double lot = (MathFloor(perPipValue*10))/100; 

   int orderBuy1 = OrderSend (Symbol(), OP_BUY, lot, Open[0], 10, slBull ,tpBull, "Buy Order", 5, 0, clrGreen);

Tester: not enough money for sell 0.24 GBPUSD at 1.23449 sl: 1.23655 tp: 0.00000


Any help or suggestion will be appreciated.


 
double perPipValue = (accountSize*(risk/100))/(atrPip*1.5);
double lot = (MathFloor(perPipValue*10))/100; 
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total.

In code (MT4): Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder:
Never risk more than a small percentage of your account, certainly less than 2% per trade, 6% total.

In code (MT4): Risk depends on your initial stop loss, lot size, and the value of the pair. It does not depend on margin and leverage.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce the stop goes below the support.
  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/pip but it takes account of the exchange rates of the pair vs. your account currency.)
  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum 2017.10.10
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum 2018.02.11
              Lot value calculation off by a factor of 100 - MQL5 programming forum 2019.07.19
  4. You must normalize lots properly and check against min and max.
  5. You must also check FreeMargin to avoid stop out

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

Thank you for your quick reply, I really appreciat it. My calculation for risk is exactly how you mentioned, the "risk" variable is available for extern input. So if I wish to risk 2% the the calculation is

e.g. accountSize = 1000, atrPip = 7. sl = 7.5 x 1.5 =10.5

1000*(2/100)/7*1.5 = 1.9... So 1.9 pip is roughly the value per pip for 10.5 or 11 pip im looking for. 1.9 x 10.5 = 19.95 which is roughly 2% of 1000.

Then the next lot calculation then figures out my lot size which come to around 0.19 in this example but in my actual test it was 2.4.

But my actual question is that the test is throwing a 134 error for this trade when its the first trade. With a $1000 account I should easily be able to afford it, so I don't understand the reason for the 

134 not enough money error. Any idea or quirk in the system I'm not aware of? The system runs when I change 100 => 1000 in the lot calculation but the risk is like 0.2% then

 
forexpadawan:

Thank you for your quick reply, I really appreciat it. My calculation for risk is exactly how you mentioned, the "risk" variable is available for extern input. So if I wish to risk 2% the the calculation is

e.g. accountSize = 1000, atrPip = 7. sl = 7.5 x 1.5 =10.5

1000*(2/100)/7*1.5 = 1.9... So 1.9 pip $ is roughly the value per pip for 10.5 or 11 pip im looking for. 1.9 x 10.5 = 19.95 which is roughly 2% of 1000.

Then the next lot calculation then figures out my lot size which come to around 0.19 in this example but in my actual test it was 2.4 0.24.

But my actual question is that the test is throwing a 134 error for this trade when its the first trade. With a $1000 account I should easily be able to afford it, so I don't understand the reason for the 

134 not enough money error. Any idea or quirk in the system I'm not aware of? The system runs when I change 100 => 1000 in the lot calculation but the risk is like 0.2% then

Your lot calculation is not universal (will not work in all cases, what is the *10 and the /100 ?), but ok let's say it works for your actual needs.

Not enough money means you don't have enough margin to open this trade, you need to check it before the opening if you want to avoid this error. Use AccountFreeMarginCheck() for that.

The margin depends of the leverage for the traded symbol. How much is it ? And how much free margin do you have available at the time of opening this trade ?

PS: Please pay attention to typos (highlighted) !

 

Thank you for your reply Alain, I appreciate any help or advice. I just tried using AccountFreeMarginCheck() you suggested which cleared a lot of things for me.

My account in the test was £1000. and even though my risk was 2% which is not too large, since my sl was not huge my ea calculate a large lot size(for my account which was 0.24).

0.24 is essentially 24000, and with my 30:1 leverage the margin is roughly 800 which leaves me only 200 free margin (20% of my account). This is lower than my minimum allowed hence error 134.


Sorry about the types the typos. The *10 in my calc was to floor the number e.g. 0.19 *10 give 1.9. Them MathFloor(1.9) = 1.0. then /100 gives 0.01 which is the lot size.

Without *10 the lot size would be 0.019 which gives a error 131. Hope this clears my logic. 


Sorry for the long explanation, you probably already understood it but though might be helpful for someone new like me reading it. This is my first ea an am learning a lot.

Again really appreciate the help and advise.

Reason: