How to calculate Maximum lot size based on Equity

 

I have been trying to write a function that calculates the maximum lot size allowed on my account based on my account balance. I am entering a market order so there aren't any stop losses, I put those on after the trade has been entered. I am only trading one position at a time so I can use the full margin available.

Here is the line of code I am using:

double maxLotSizecalculated=((AccountEquity()/MarketInfo(Symbol(),MODE_MARGINREQUIRED))/10);

For example, my equity is £280, the margin required is £45.94.

This gives: (280/45.94)/10 = 0.6 Lots.

However, when I try to enter a trade with 0.6 lots I get an error #134, not enough money. The largest lot size I can enter is 0.36 lots which is nearly half of what I have calculated. 

What am I doing wrong and how do I calculate the maximum lot size I can trade?

Many thanks. 

 

Hello!

In your example the maximum order size is 6.1 lots
Maybe this will help

void OnStart()

{

Print ("=====================================================================");

Print ("ACCOUNT_BALANCE        = ",AccountInfoDouble(ACCOUNT_BALANCE         ));

Print ("ACCOUNT_EQUITY         = ",AccountInfoDouble(ACCOUNT_EQUITY          ));

Print ("ACCOUNT_MARGIN         = ",AccountInfoDouble(ACCOUNT_MARGIN          ));

Print ("ACCOUNT_MARGIN_FREE    = ",AccountInfoDouble(ACCOUNT_FREEMARGIN      ));

Print ("---------------------------------------------------------------------");

Print ("SYMBOL                 = ",Symbol()                                   );

Print ("MARGIN FOR 1 LOT       = ",MarketInfo(Symbol(),MODE_MARGINREQUIRED   ));

Print ("MODE_MARGINCALCMODE    = ",MarketInfo(Symbol(),MODE_MARGINCALCMODE   ));

Print ("MODE_MARGININIT        = ",MarketInfo(Symbol(),MODE_MARGININIT       ));

Print ("MODE_MARGINMAINTENANCE = ",MarketInfo(Symbol(),MODE_MARGINMAINTENANCE));

Print ("=====================================================================");

}

 
And another moment
This is a reasonable way to use all the available margin
But
Flex multi-order positions instead of a single order seems even more reasonable
For example instead of order of 0.36 lots I would prefer 36 orders of 0.01 lots
It is safer and gives a little advantage when the price goes wrong
 
Darren M: I have been trying to write a function that calculates the maximum lot size allowed on my account based on my account balance. I am entering a market order so there aren't any stop losses,

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk. Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

  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:

... No SL means you have infinite risk. 

Absolutely wrong statement

As well as the next statements in that post

Moreover

Posting hundreds of identical duplicate posts by copy/paste is not a good practice for this forum

 
Darren M:

I have been trying to write a function that calculates the maximum lot size allowed on my account based on my account balance. I am entering a market order so there aren't any stop losses, I put those on after the trade has been entered. I am only trading one position at a time so I can use the full margin available.

Here is the line of code I am using:

For example, my equity is £280, the margin required is £45.94.

This gives: (280/45.94)/10 = 0.6 Lots.

However, when I try to enter a trade with 0.6 lots I get an error #134, not enough money. The largest lot size I can enter is 0.36 lots which is nearly half of what I have calculated. 

What am I doing wrong and how do I calculate the maximum lot size I can trade?

Many thanks. 

You are not taking into account the amount used for opening the trade, i.e. the margin requirement.

Instead, a simpler rule might work for you. Use the minimum allowed lot for every 500 (money) in your balance.

 

It also depends on what direction you put your order.

If u hedge in the same direction, you will be limited. this method is very risky to blow your account in no time. Also, if your only option is to make small additional orders this means that your initial order risk is too high, or you didn't perform money mgt and got out of your trade, however you only live once.

If you counterorder in the other direction, your available equity will be a lot higher because you theoretically temporary undo your initial order.

Whatever you do, both methods are tricky situations to lose even more money than you already do, so be sure what you do and make the good bet (because that's what it basically is)

good luck

 

Many thanks for your replies. 

My problem is that I am using a market order without a stop loss and then my EA controls the risk and creates the stop loss after the trade has been entered. I basically wanted to enter the biggest trade I can and then close it if it goes against me. As I mentioned, with the account size I am using in my example above the maximum lot size that my broker will accept is 0.36 lots. After that trade has been placed then my EA will look after the risk. My question is still, how do I work out the maximum trade allowed on my account ignoring the stop losses? The maximum loss my EA will allow is 1% btw. 

Perhaps it isn't possible to calculate this via an EA, which seems surprising to me, and I will have to manually work it out in a way that @Fernando Morales suggested, by calculating the max lot size per £50 account balance so that the trade size can increase when my balance increases (or vice versa!)

If anyone knows of a function I can use I would appreciate it. 


Many thanks. 

Fernando Morales
Fernando Morales
  • www.mql5.com
Added topic OrderModify gives error 1 even checking the TP values before I have created this function to update the TP/SL of market orders. In tester it sometimes shows error 1 whereas the TP and new price are the same values. if TakeProfit() and _precio are the same, I expect the function to skip OrderModify(). Most News about PDC Pip Daily...
 

Hello everybody,


There exist exemple code in Expert\Examples\Moving Average;

I hope this will helped you

 
LUC JACOBUS A VERHEECKE:

Hello everybody,


There exist exemple code in Expert\Examples\Moving Average;

I hope this will helped you

Thanks, but that isn't quite what I am after. 

Thanks anyway!

 
Fernando Morales:

You are not taking into account the amount used for opening the trade, i.e. the margin requirement.

Instead, a simpler rule might work for you. Use the minimum allowed lot for every 500 (money) in your balance.

Why balance? How you can use a margin that you can not have? What do you do if you have a balance of 1000 and an equity of 500... Why you are supposing that the balance is always the same than the equity? 

Reason: