Formula to Calculate Lot size for Full Leverage

Hello,

Currently i am using : Position Size Calculator | HFM and this show accurate Lot size but i am looking for formula so i can code into my EA.

I am trying

double MaxLot_UsingTickSize = NormalizeDouble(((AccountFreeMargin() * AccountLeverage()) / Bid) / (1 / MarketInfo(Symbol(), MODE_TICKSIZE)), 2);
double MaxLot_UsingLotSize = NormalizeDouble(((AccountFreeMargin() * AccountLeverage()) / Bid) / (MarketInfo(Symbol(), MODE_LOTSIZE)), 2);
double MaxLot_UsingMarginRequired = NormalizeDouble(AccountFreeMargin() / MarketInfo(Symbol(), MODE_MARGINREQUIRED), 2);

But these are not working perfectly. How can i do this?

You should use Account Balance instead of Free Margin.

The best way to trade using bot is by having fix parameter. For example you want to have 1% profit / risk, this calculation must be based on Account balance.

double balance = AccountInfoDouble (ACCOUNT_BALANCE);

You will have a steady strategy compare to using Equity or Free Margin.
My formula is more and less bellow.

            Account Balance x Leverage
Max Lot  = ----------------------------  
               Symbol Contract Size

Of course you need to consider required margin to open a position.