Dynamic Value at Risk Calculation

 

Came across this earlier and think it's pretty useful for Multi Symbol Portfolio Analysis.

I've converted the code provided in this video: https://www.youtube.com/watch?v=3FV8PTikKc0

So that it dynamically measures the Value at Risk of the open trades on the Account.

Note: the issue that I'm having is that Buy & Sell trades for the same symbol both add to the value at risk.

This should not happen, go to 8:30 in the video to understand why

It would be good to discuss potential solutions for this.

The key part of the code that needs to be changed is:

   string CurrPortAssets[];
   double CurrPortLotSizes[];      //+ve - LONG,  -ve - SHORT
   
   uint positions = PositionsTotal();
   ArrayResize(CurrPortAssets,positions);
   ArrayResize(CurrPortLotSizes,positions);
   for (uint i=0; i<positions; i++)
      {
      string activo = PositionGetSymbol(i);
      PositionSelect(activo);
      double cantidad = PositionGetDouble(POSITION_VOLUME);
      long direction = PositionGetInteger(POSITION_TYPE);
      CurrPortAssets[i] = activo;
      CurrPortLotSizes[i] = direction==POSITION_TYPE_BUY? cantidad : -cantidad;
      }
      
Coding Incremental VaR in MQL4 and MQL5 for Better Trading Risk Management | Free Code Download
Coding Incremental VaR in MQL4 and MQL5 for Better Trading Risk Management | Free Code Download
  • 2022.03.29
  • www.youtube.com
Incremental Value at Risk (VaR) can assist with better trading and portfolio risk management. This MQL4 and MQL5 coding tutorial with free code download can ...