Features of the mql5 language, subtleties and tricks - page 8

 

In the Help it says

ENUM_SYMBOL_CALC_MODE

Identifier

Description

The formula

SYMBOL_CALC_MODE_FOREX

Forex mode - profit and margin calculation for Forex

Margin: Lots*Contract_Size/Leverage

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_FUTURES

Futures mode - calculation of margin and profit for futures

Margin: Lots *InitialMargin*Percentage/100

Profit: (close_price-open_price)*TickPrice/TickSize*Lots

SYMBOL_CALC_MODE_CFD

CFD mode - calculation of margin and profit for CFD

Margin: Lots *ContractSize*MarketPrice*Percentage/100

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_CFDINDEX

CFD index mode - calculation of margin and profit for CFD on indices

Margin: (Lots*ContractSize*MarketPrice)*TickPrice/TickSize

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_CFDLEVERAGE

CFD Leverage mode - calculation of margin and profit for CFD when trading with leverage

Margin: (Lots*ContractSize*MarketPrice*Percentage)/Leverage

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_EXCH_STOCKS

Exchange mode - calculation of margin and profit for trading securities on the exchange

Margin: Lots*ContractSize*OpenPrice

Profit: (close_price-open_price)*Contract_Size*Lots

SYMBOL_CALC_MODE_EXCH_FUTURES

Futures mode - calculation of margin and profit for trading futures contracts on the exchange

Margin: Lots*InitialMargin or Lots*MaintenanceMargin

Profit: (close_price-open_price)*Lots*TickPrice/TickSize

SYMBOL_CALC_MODE_EXCH_FUTURES_FORTS

FORTS Futures mode - calculation of margin and profit for trading futures contracts on FORTS. The margin can be decreased by the MarginDiscount value according to the following rules:

1. If the price of a long position (Buy order) is lower than the calculated price, then MarginDiscount = Lots*((PriceSettle-PriceOrder)*TickPrice/TickSize).

2. If the price of a short position (a sell order) is higher than the settlement price, then MarginDiscount = Lots*((PriceOrder-PriceSettle)*TickPrice/TickSize)

Where:

    • PriceSettle - settlement (clearing) price of the previous session;
    • PriceOrder - weighted average price of a position or opening price specified in the order (request);
    • TickPrice - tick price (cost of price change by one point);
    • TickSize - tick size (minimum step of price change)

Margin: Lots*InitialMargin or Lots*MaintenanceMargin

Profit: (close_price-open_price)*Lots*TickPrice/TickSize

SYMBOL_CALC_MODE_SERV_COLLATERAL

Collateral mode - the instrument is used as a non-trading asset in the trading account. The market value of an open position is calculated on the basis of volume, current market price, contract size and liquidity ratio. The value is recorded in Assets, which are summed up with Equity. Thus, open positions in such an instrument increase the size of Free Margin and serve as additional collateral for open positions in traded instruments

Margin: no

Profit: no

Market value: Lots*ContractSize*MarketPrice*LiquityRate

 
fxsaber:

It says in the Synopsis

With all due respect, I intend to take you back to your reading

P.S.

You can get it, but if we calculate by AUDCAD, then the margin will be in AUD currency, and the account is dollar. If you use AUDUSD price, you will get the required amount of margin, but it is not universal and bad, because there are accounts with prefixes and suffixes, and here starts a whole story. Also, there may simply be no open pair in the market review, and the calculation will be "buried in a cinder.

The program that works with money (the developers' claim), can not calculate the money. The old, bad and slow mt4 could work with money, but the new and fancy mt5 cannot. How so?

 
fxsaber

This is the formula from the link:

Calculate, for example, the collateral for CADJPY, we get the collateral in CAD

SymbolInfoDouble("CADJPY",SYMBOL_TRADE_CONTRACT_SIZE) / AccountInfoInteger(ACCOUNT_LEVERAGE)*0.7625 // СADUSD

Where the0.7625 is CADUSD price, but the terminal doesn't even have such a pair to apply it in calculations.

Is it so difficult to transfer the"MODE_MARGINREQUIRED" calculation function to mt5 using the "copypaste" method, thus making the developers' life easier?

 
Artyom Trishkin:
I would like to have a calculation independent of the program type.
The developers think that margin calculation (and some other info functions) is a trading function and should be prohibited in indicators. I started a serious thread on this topic - its moderators have immediately cut it, as usual, anonymously. Now I do not care about it, I put in KB neutered indicator without margin.
 
Vitaly Muzichenko:

With all due respect, I intend to take you back to your reading.

I admit, I wasn't paying attention. For me, the calculation of the margin was somehow always unnecessary. Especially in indicators. Where it may be necessary in indicators?

I think the solution is very simple. When I will be free, I will think it out.

 
Vitaly Muzichenko:

You can get it, but if we calculate by AUDCAD, then the margin will be in AUD currency, and the account is a dollar account. If you substitute AUDUSD price, then we get the right amount of margin, but it is not universal and disgusting, because there are accounts with prefixes and suffixes, and there starts a whole story. Also, there may simply not be an open pair in the market review, and the calculation will be "buried in a cinder.

And what will the four return, if AUDUSD is not in the market overview?

For the specific task of margin reflection in the indicator it is possible to construct a crutch that takes into account the prefixes and suffixes (or using an auxiliary advisor). There is everything for trading.

 
Andrey Khatimlianskii:

And what will return the fourth, if AUDUSD is not in the market overview?

For the specific task of margin reflection in the indicator it is possible to construct a crutch that takes into account the prefixes and suffixes (or using an auxiliary advisor). Everything seems to be there for trading.

Four will return"MODE_MARGINREQUIRED" )

Alexey Volchanskiy:
I think that calculating margin (and something else from informational functions) is a trading function and should be prohibited in indicators. I started a serious thread on this topic - its moderators have immediately cut it, as usual, anonymously. Now I don't care about it, posted a neutered indicator without margin in KB.

Is it really trading? ))) Although yes, it's not forbidden to anneal. I wonder what it trades, does it open or close, or just modify orders?

fxsaber:

I admit, I wasn't paying attention. I always had no need to calculate my margin. Moreover in indicators. Where it may be necessary in indicators?

I think the solution is very simple. When I will be free, I will think of it.

In addition to the MA and stochastics, there are informative indicators. Not all trade with bots, for example, the information indicator to know the deposit before making a deal (observe MM), calculate the lot and see whether it fits the conditions of your TS, or do not enter into transaction and wait for another signal. And it's just a single example of use.

 
fxsaber:

I think the solution is very simple. I'll figure it out when I'm free.

The solution came immediately.
// Размер свободных средств, необходимых для открытия 1 лота на покупку
double GetMarginRequired( const string Symb )
{
  MqlTick Tick;

  return(SymbolInfoTick(Symb, Tick) ? Tick.ask * SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE) /
        (SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_SIZE) * AccountInfoInteger(ACCOUNT_LEVERAGE)) : 0);
}
This is a calculation for FOREX. The rest is by analogy.
 
fxsaber:
The solution came immediately.
// Размер свободных средств, необходимых для открытия 1 лота на покупку
double GetMarginRequired( const string Symb )
{
  MqlTick Tick;

  return(SymbolInfoTick(Symb, Tick) ? Tick.ask * SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_VALUE) * SymbolInfoDouble(Symb, SYMBOL_TRADE_CONTRACT_SIZE) *
         SymbolInfoDouble(Symb, SYMBOL_POINT) / (SymbolInfoDouble(Symb, SYMBOL_TRADE_TICK_SIZE) * AccountInfoInteger(ACCOUNT_LEVERAGE)) : 0);
}
This is a calculation for FOREX. The rest - by analogy.
Thank you very much, there is still a bug with yen pairs
 
Vitaly Muzichenko:
Thank you very much, there is still a bug with yen pairs
Yes, I messed up a bit. Now it is correct.