How does the MetaTrader platform calculate the required margin?

 
Hello,
In MetaTrader, we calculate MARGIN REQUIRED easily as follows:
MarketInfo(_Symbol, MODE_MARGINREQUIRED)
I tried to implement the same idea in cTrader, but I'm facing issues.
How can I create the same function as MarketInfo(_Symbol, MODE_MARGINREQUIRED), but with cAlgo code? 

I created the following code and it worked for the EUR/USD pair, but it didn't work for the US30 or US500 index.

double contractSize = 100000; // for UER/USD 
                    double marginPercentage = 100; // for UER/USD 
                    double currentPrice = symbol.Bid;
                    double leverage = Account.PreciseLeverage;  
                    double marginRequired = ((contractSize * currentPrice) * (marginPercentage / 100)) / leverage;
                    return marginRequired;

But when I change the values of the variables as follows:

contractSize = 1 // for US30

marginPercentage = 0.5   // for US30

These are the settings for US30.

Strange results start to appear.!

I really hope for a solution.

Thank you.