How to get the "Percentage of Margin" programmatically - page 7

 
K-2SO:

Tests show that when the margin percentage is equal to 1, leverage is not taken into account! But when it is 100 and above, it is already taken into account. I wish I could find a broker with a margin percentage between 2 and 99 inclusive.

Something is wrong. Leverage is used when you calculate this percentage from the margin on 1 standard lot, and leverage is taken into account there.

Compile this script and if there are open orders run it on any pair.

void OnStart()
{
 double size = 0, percentage = 0, orderMargin = 0, accountMargin = 0;
 long leverage = 0;
 for(int i = 0; i < OrdersTotal(); i++)
  {
   int tupe = -1;
   if(OrderSelect(i, SELECT_BY_POS) && (tupe=OrderType()) < OP_BUYLIMIT)
    {
     string symbol = OrderSymbol();
     string symbolCurencyMargin = SymbolInfoString(symbol, SYMBOL_CURRENCY_MARGIN);
     double orderOpenPrice = OrderOpenPrice();
     double margin = MarketInfo(symbol, MODE_MARGINREQUIRED);
     double ask = MarketInfo(symbol, MODE_ASK);
     double bid = MarketInfo(symbol, MODE_BID);
     double price = symbolCurencyMargin == "USD" ? 1 : tupe == OP_BUY ? bid : ask;
      size = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
      leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
      percentage = NormalizeDouble(margin/(size*price/100)*leverage, 0);
      orderMargin = (size*orderOpenPrice*percentage/100)/leverage;
      accountMargin += orderMargin;
      Print(symbolCurencyMargin, " ******** Маржа ", symbol, " = ", orderMargin);
    }
  }
 Print(AccountInfoString(ACCOUNT_CURRENCY), " ******** AccountMargin = ", DoubleToString(accountMargin, 2));
}/********************************************************************/
I only have 2 orders open, it shows correctly for them.
 
Alexey Viktorov:

Something is wrong. Leverage is used when you count that percentage from the margin on 1 standard lot, and leverage is taken into account there.

Compile this script and if there are open orders run it on any pair.

I have only 2 orders open, it shows correctly.

I have done it. At present time the market is closed in demo mode and only orders on gold are open. My words are confirmed by insta (margin percentage:1) your script shows some kind of space...

I don't know what to do with it, but I have to correctly calculate the percentage of margin in metaquotes and robot, where the margin percentage is 100 and 200.

 
Renat Akhtyamov:
Open a demo account with 5 quid and a leverage of 100 and see what the margin percentage will be, for example, the same for gold or even funnier - the ruble...
I have not mastered the calculation of percentage margin, the logic of calculations seems to have been understood, but I have not written the code yet. If you have your own, let's check what you are saying )
 
K-2SO:

Done. Now on my demos while the market is closed only on gold orders are open. My words are confirmed by insta (margin percentage:1) your script shows some kind of space... so doesSYMBOL_CURRENCY_MARGIN

MetaQuotes and RoboQuotes where the margin percentage is 100 and 200, everything is correct.

I do not understand what it shows wrong. I opened 2 more orders for oil where the margin percentage = 1 and the contract size is only 1000 and not 100,000 as for currencies.

This is what is printed

2017.06.05 08:40:04.978 test EURUSD.e,H1: USD ******** AccountMargin = 2207.23
2017.06.05 08:40:03.360 test EURUSD.e,H1: US_ ******** Маржа BRENT = 508.0
2017.06.05 08:39:34.326 test EURUSD.e,H1: US_ ******** Маржа WTI = 484.9
2017.06.05 08:39:25.185 test EURUSD.e,H1: XAU ******** Маржа XAUUSD.e = 840.4333333333334
2017.06.05 08:39:19.651 test EURUSD.e,H1: EUR ******** Маржа EURUSD.e = 373.8933333333333

Here is a screenshot


For some reason, it shows the exact penny.

So, apart from words, put pictures...

 
Alexey Viktorov:

So in addition to words, post pictures...


Somewhere like this (lot 0.05, leverage 300):

2017.06.05 12:06:11.968 Script gold_test_vik XAUUSD,H1: removed
2017.06.05 12:06:11.953 gold_test_vik XAUUSD,H1: uninit reason 0
2017.06.05 12:06:11.953 gold_test_vik XAUUSD,H1: USD ******** AccountMargin = 8193395.74
2017.06.05 12:06:11.953 gold_test_vik XAUUSD,H1: USD ******** Маржа XAUUSD = 8193395.736
2017.06.05 12:06:11.953 gold_test_vik XAUUSD,H1: initialized
2017.06.05 12:06:11.937 Script gold_test_vik XAUUSD,H1: loaded successfully

 
K-2SO:


Somewhere like this (lot 0.05, leverage 300):

I see. Pay attention to the currency of the margin. Mine is XAU and yours is USD and then you have to do something with this line

double price = symbolCurencyMargin == "USD" ? 1 : tupe == OP_BUY ? bid : ask;

so that the quote is included in the calculation. Maybe add a path to the symbol.

And for the double check change this line like this

double price = tupe == OP_BUY ? bid : ask;

But in this case it won't work correctly for currencies.

In general, we should consider all variants of path to the symbol and all variants of margin currencies for the universality.

 
Alexey Viktorov:

I see. Pay attention to the currency of the margin. Mine is XAU and yours is USD and then you have to do something with this line

so that the quote is included in the calculation. Maybe you should add a path to the symbol.

To check it again, change this line like this


All the same, it is not correct, the order is the same, the result is the same:

2017.06.05 12:47:50.984 gold_test_vik XAUUSD,H1: USD ******** AccountMargin = 6392.70
2017.06.05 12:47:50.984 gold_test_vik XAUUSD,H1: USD ******** Маржа XAUUSD = 6392.7
Alexey Viktorov:

Generally, all variants of path to the symbol and margin currency should be taken into account for the sake of universality.

I see the margin currency, but what path variants to the symbol are we talking about?


p.s. The formula without leverage counts everything accurately:

margin=(OrderLots()*contract*OrderOpenPrice())/100*Percentage; // инста - процент маржи 1% 
 
K-2SO:

Still wrong, same order, same result:

Margin currency is clear, but which paths to the instrument are we talking about?


p.s. According to the formula without leverage everything is calculated accurately:

Yeah, I messed up a bit... The lot size is not considered at all. It's shown correctly because my orders are of 1 standard lot... I should add this line

orderMargin = (OrderLots()* size*orderOpenPrice*percentage/100)/leverage;

The path to the symbol is defined as follows

SymbolInfoString(symbol, SYMBOL_PATH);

but alas, there is no unambiguous naming, so it's not so easy to set the condition.

Robo, the ecn demo has such a path,


and on a cent.

Others may be similar, but not unambiguous. Of course we can compare with substring, but to be sure of universality we should check many brokerage companies.

Although... you could try to check for the way the collateral is calculated.

I will check it now.

 
Alexey Viktorov:

Yeah, I messed up a bit... The lot size is not taken into account at all. I'm getting it right because the orders are 1 standard lot each... I should add this line.

I did, but the result is still wrong)

Alexey Viktorov:

The path to the symbol is defined as follows

I mean, how, where and why do I need to use this path?
 

What a pain in the ass... Check how it counts for you.

void OnStart()
{
 double contractSize = 0, orderMargin = 0, accountMargin = 0; double percentage = 0;
 long leverage = 0;
 for(int i = 0; i < OrdersTotal(); i++)
  {
   int type = -1;
   if(OrderSelect(i, SELECT_BY_POS))
    {
      type = OrderType();
     string symbol = OrderSymbol();
     string symbolCurencyMargin = SymbolInfoString(symbol, SYMBOL_CURRENCY_MARGIN);
     double orderLots = OrderLots();
      leverage = AccountInfoInteger(ACCOUNT_LEVERAGE);
     double margin = MarketInfo(symbol, MODE_MARGINREQUIRED);
     double ask = MarketInfo(symbol, MODE_ASK);
     double bid = MarketInfo(symbol, MODE_BID);
     int calcMode = int(SymbolInfoInteger(symbol, SYMBOL_TRADE_CALC_MODE));
     int stringFind = StringFind(symbol, "USD");
     double price = stringFind == 0 ? 1 : type%2 == OP_BUY ? bid : ask;
     double orderOpenPrice = stringFind == 0 ? 1 : OrderOpenPrice();
      contractSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_CONTRACT_SIZE);
      percentage = NormalizeDouble(margin/(contractSize*price/100)*(calcMode == 0 ? leverage : 1), 0);
      orderMargin = (orderLots*contractSize*orderOpenPrice*percentage/100)/(calcMode == 0 ? leverage : 1);
       Print("******** Процент маржи ", int(percentage), " Маржа ордера ", symbol, " ", orderLots, " = ", orderMargin);
      if(type < OP_BUYLIMIT)
       accountMargin += orderMargin;
    }
  }
 Print("******** AccountMargin = ", DoubleToString(accountMargin, 2)," ", AccountInfoString(ACCOUNT_CURRENCY));
}/********************************************************************/
I made this one to count pending orders as well, but it doesn't include the total margin.