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

 
K-2SO:

Hats off to you, you almost got it right! At all three brokers previously reviewed with different margin percentages, the calculation for gold (for orders in one direction) is correct.

But the script still fails with exotics. I have stopped at fxcm broker. The percentage margin for gold is 70000, for conventional currency pairs is 130, the currency of margin seems to be USD. And nothing is counting correctly anywhere! (. I myself have been looking for the key to it for two days, and in fact as a result of this I am now looking for an answer to the question, how come that as a result of calculations of base currencies and their rates with quotes currencies, we get margin currency... Maybe it's this, or maybe it's the fact that this broker takes into account the percentage of margin even for normal currency pairs.

You can download the terminal here ru.files.fm/u/xfezz883#_ , unzip it, run the exe file, start the demo...

I opened a demo, even two, one has no gold, the other XAUUSD with the percentage of margin 70000 and the standard lot 1. And the reason is not correct calculation in the

percentage = NormalizeDouble(
                             margin          // Маржа получена в валюте депозита с учётом плеча
                           /(contractSize    // Размер контракта в базовой валюте
                            *price           // Умножаем на текущую цену и получаем в валюте депозита
                            /100)            // Это для того чтобы коэффициент перевести в проценты
                           *(calcMode == 0 ? leverage : 1) // Это получено методом научно-технического тыка.
                                    // Если способ расчёта 0 - Forex; то надо учесть плечо
                                    //                     1 - CFD; то плечо не учитывается
                                    //                     2 - Futures; 3 - CFD на индексы НЕ проверялись, их у меня нету...
                           , 0);

experiment with these lines yourself.

      percentage = NormalizeDouble(margin/(contractSize*price/100)*(calcMode == 0 ? leverage : 1), 0);
      orderMargin = (orderLots*contractSize*orderOpenPrice*percentage/100)/(calcMode == 0 ? leverage : 1);

If I get bored, maybe I'll experiment too.

 
Alexey Viktorov:

Crosses are not a problem to calculate. You just need to take a quote that translates the currency of the margin into the currency of the deposit.

For example EURJPY price

If the deposit is in USD, you should use EURUSD. CADJPY should be calculated using USDCAD. Here we should see how to add the deposit currency to the margin currency, we shouldn't just enter it in the list.

The counter ones are not so difficult having MarketInfo(symbol, MODE_MARGINHEDGED). The only problem is to find the counter first, and then to decompose part of the counter and the rest fully...

In general, I see the only benefit of this article is that the trader knows in advance what margin will be taken when the pending order is activated and removes the pending order in time if there is not enough money. I once struggled with this when placing an EA on the market.

2017.06.06 18:00:01.890 Script vik2 XAUUSD,H1: removed
2017.06.06 18:00:01.875 vik2 XAUUSD,H1: uninit reason 0
2017.06.06 18:00:01.875 vik2 XAUUSD,H1: ******** AccountMargin = 12.93 USD
2017.06.06 18:00:01.875 vik2 XAUUSD,H1: ******** Процент маржи 1 Маржа ордера XAUUSD 1.0 = 12.933
2017.06.06 18:00:01.875 vik2 XAUUSD,H1: initialized
2017.06.06 18:00:01.859 Script vik2 XAUUSD,H1: loaded successfully
2017.06.06 17:59:51.593 Compiling 'vik2'

leverage 100

 
Alexey Viktorov:

I opened a demo, even two, one has no gold, the other has XAUUSD with a margin percentage of 70000 and a standard lot of 1. And the reason for the incorrect calculation is

Well, that's what this whole topic is about... and I think there is no universal calculation after all )
 

And then in normal currency pairs in the Forex calculation method, do you have to take percentage into account?

2017.06.06 18:09:54.640 Script vik2 EURUSD,H1: removed
2017.06.06 18:09:54.640 vik2 EURUSD,H1: uninit reason 0
2017.06.06 18:09:54.640 vik2 EURUSD,H1: ******** AccountMargin = 1295.77 USD
2017.06.06 18:09:54.640 vik2 EURUSD,H1: ******** Процент маржи 115 Маржа ордера EURUSD 1.0 = 1295.774
2017.06.06 18:09:54.640 vik2 EURUSD,H1: initialized
2017.06.06 18:09:54.625 Script vik2 EURUSD,H1: loaded successfully


 
K-2SO:
Well, that's what this whole topic is about... and I don't think there's a one-size-fits-all calculation.)
Why not? There's a link on the first page with formulas. It can be broken down into several algorithms depending on how the calculation is done. What I suggested to experiment is fundamentally wrong, do not waste your time. You have to go the other way.
 
Alexey Viktorov:
Why not? There is a link on the first page where the formulas are available. It can be broken down into several algorithms depending on how the calculation is done. What I suggested to experiment is fundamentally wrong, don't waste your time. You have to go the other way.
I tried these formulas on the FOREX calculation method, nothing works correctly at all on the last broker, even for EURUSD.
 
K-2SO:
I tried these formulas on the FOREX calculation method, on the last broker nothing works correctly at all, not even for EURUSD.
How come it doesn't work? My script uses formulas from there and seems to work for forex and cfd. Futures and indices are calculated using different formulas and I didn't use them.
 
Alexey Viktorov:
How come it doesn't work? In my script, the formulas are from there and seem to work for forex and cfd. But futures and indices are calculated using different formulas and I haven't used them.
I gave you screenshots above of how they work...
 
K-2SO:
I gave you screenshots above of how they work...
Right. My script counts margin for CFDs and forex, and your screenshots are for futures, the formulas for which are on the same page.
 
Alexey Viktorov:
Right. My script calculates margin for CFD and forex, and your screenshots are for futures, the formulas for which are on the same page.

Where did you get that conclusion from?^ ^


The way to calculate margin for XAUUSD, also Forex... there profit is futures, not the one we are trying to calculate.
Reason: