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?