Is it possible to find the current risk exposure of all open positions and pending orders?

 

I would like to make an indicator that shows the total account exposure - i.e. the amount in account currency that the trader would loose if all open positions and pending orders were filled and all hit stop loss. Once I have this figure I would then show it as a percentage of account size. 

I know in MT5 if you hover mouse over SL level it shows the amount for that position you would loose if stop loss is hit. I need a way to find the total for all stop loss amounts for all open orders.

Is there a way to programmatically get this figure for each ticket at sum them together?

 
Mark Stewart:I would like to make an indicator that shows the total account exposure - i.e. the amount in account currency that the trader would loose if all open positions and pending orders were filled and all hit stop loss. Once I have this figure I would then show it as a percentage of account size. I know in MT5 if you hover mouse over SL level it shows the amount for that position you would loose if stop loss is hit. I need a way to find the total for all stop loss amounts for all open orders. Is there a way to programmatically get this figure for each ticket at sum them together?

You will probably find some example code in the CodeBase, but the main MQL5 function to consider is OrderCalcProfit.

Essentially, you would iterate over current positions (real exposure) and current pending orders (possible exposure), and apply the function (summing it all up), taking into account the Type (buy/sell), Symbol, Volume, Open price and Stop-Loss price of each position and/or pending order.

 
Fernando Carreiro #:

You will probably find some example code in the CodeBase, but the main MQL5 function to consider is OrderCalcProfit.

Essentially, you would iterate over current positions (real exposure) and current pending orders (possible exposure), and apply the function (summing it all up), taking into account the Type (buy/sell), Symbol, Volume, Open price and Stop-Loss price of each position and/or pending order.

Oh amazing, I never new the OrderCalcProfit function existed. Thank you very much for pointing me in the right direction!

 
Mark Stewart #: Oh amazing, I never new the OrderCalcProfit function existed. Thank you very much for pointing me in the right direction!
And to calculate the required margin, use the function OrderCalcMargin.
Documentation on MQL5: Trade Functions / OrderCalcMargin
Documentation on MQL5: Trade Functions / OrderCalcMargin
  • www.mql5.com
OrderCalcMargin - Trade Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:
And to calculate the required margin, use the function OrderCalcMargin.

Thank you. It appears the only issue is you can't call either from within an indicator, they are only available in EA's. https://www.mql5.com/en/docs/runtime/running;

Which is ok... I'll just have to re-think the way I do this slightly. 

Cheers! 

Documentation on MQL5: MQL5 programs / Program Running
Documentation on MQL5: MQL5 programs / Program Running
  • www.mql5.com
Program Running - MQL5 programs - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Mark Stewart #: Thank you. It appears the only issue is you can't call either from within an indicator, they are only available in EA's. https://www.mql5.com/en/docs/runtime/running. Which is ok... I'll just have to re-think the way I do this slightly. 

Then use the following ...

[Money Value] = [Volume] * [Stop Size] * [Tick Value] / [Tick Size]

Forum on trading, automated trading systems and testing trading strategies

How to calculate take profit from currency

Fernando Carreiro, 2022.09.05 17:00

These are all the same equation written in different ways ...

[Volume]      = [Money Value] * [Tick Size] / ( [Tick Value] * [Stop Size] )
[Stop Size]   = [Money Value] * [Tick Size] / ( [Tick Value] * [Volume]    )
[Money Value] = [Volume]      * [Stop Size] * [Tick Value]   / [Tick Size]

[Volume] in lots
[Stop Size] in quote price change
[Money Value] in account currency

Forum on trading, automated trading systems and testing trading strategies

Symbol Point Value

Fernando Carreiro, 2022.05.18 21:05

double
   dbTickSize   = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_SIZE  ), // Tick size
   dbTickValue  = SymbolInfoDouble( _Symbol, SYMBOL_TRADE_TICK_VALUE ), // Tick value
   dbPointSize  = SymbolInfoDouble( _Symbol, SYMBOL_POINT ),            // Point size
   dbPointValue = dbTickValue * dbPointSize / dbTickSize;               // Point value

Remember, it's best to use tick size and tick value in your calculations, instead of point size and its value.

Reason: