Calculate the risk of all open orders

 

Hi forum,

I want to make an indicator which calculates the total risk of all open orders. My idea is to check all OP_BUY and OP_SELL orders and calculate the risk of each individial order by using OrderOpenPrice() and OrderStopLoss(). Then I convert this into my account currency, add them and display the sum as a percentage of AccountBalance(). Is that the only way to calculate the risk of all open orders or is there an easier way? I checked all information about the account properties but I haven' found anything like this. 

This is the way I do it:

 

   for (int i = 1; i <= OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderType() == OP_BUY) {
            openOrders++;
            riskPips = OrderOpenPrice() - OrderStopLoss();
            riskMoney   = riskPips*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
            totalRisk = totalRisk + riskMoney;
         }
         else if (OrderType() == OP_SELL) {
            openOrders++;
            riskPips = OrderStopLoss() - OrderOpenPrice();
            riskMoney   = riskPips*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
            totalRisk = totalRisk + riskMoney;
         }
      }
   }
 
Quite ok if no hedging.
 
I also thought that but totalRisk is definitely wrong. I am just checking it with 1-2 orders on a demo account to locate the error.
 
 if (OrderType() == OP_BUY) {
            openOrders++;
            riskPips = OrderStopLoss() - OrderOpenPrice();
            // Should that be OrderOpenPrice() - OrderStopLoss() ???

            riskMoney   = riskPips*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
            // riskPips/Point*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE)
            // Then that is assuming that Tick Size is the same as Point
            totalRisk = totalRisk + riskMoney;
         }

 Some changes to consider

 

I changed the riskPips-calculation. It was just a copy-paste-error.

And your advice with riskPips/Point works! Thank you!!!

I also modified the loop. There was a bug though I don't know why i = 0 and i < OrdersTotal() works and i = 1 and i <= OrdersTotal() doesn't.

   for (int i = 0; i < OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderType() == OP_BUY) {
            openOrders++;
            riskPips = OrderOpenPrice() - OrderStopLoss();
            riskMoney   = riskPips/Point*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
            totalRisk = totalRisk + riskMoney;
         }
         else if (OrderType() == OP_SELL) {
            openOrders++;
            riskPips = OrderStopLoss() - OrderOpenPrice();
            riskMoney   = riskPips/Point*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
            totalRisk = totalRisk + riskMoney;
         }
      }
   }
 

I couldn't use Point because Point only refers to the current chart symbol. So I had different results within different chart symbols. Therefore I modified it. Here is the final version as a script.


double riskPips, riskMoney, totalRisk;
int direction, openOrders = 0;
void OnStart()
{
   for (int i = 0; i < OrdersTotal(); i++) {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderType() == OP_BUY) direction = 1;
         else if (OrderType() == OP_SELL) direction = -1;
         openOrders++;
         riskPips = direction*(OrderOpenPrice() - OrderStopLoss());
         riskMoney   = riskPips/MarketInfo(OrderSymbol(), MODE_POINT)*OrderLots()*MarketInfo(OrderSymbol(), MODE_TICKVALUE);
         totalRisk = totalRisk + riskMoney;
      }
   }
   Comment(openOrders, " OPEN ORDERS // TOTAL RISK IN ", AccountCurrency(), " = ", DoubleToStr(totalRisk, 2));
}
 
Thanks for sharing. Would help a lot for newbies.
 
riskPips = direction*(OrderOpenPrice() - OrderStopLoss());

You don't have pips but points!

Which is correct for the following money calculation!

 
Don't use TickValue by itself. https://www.mql5.com/en/forum/133792/page3#512466
 

@gooly: I am not sure understanding you. Do you see a mistake in the calculation? I tried it with several trades on demo and live accounts and it seems to work very good.

@WHRoeder: I often read that you suggest using DeltaValuePerLot() instead of tickvalue alone and I also tried to use it here but I don't know how to do that. I thought that it is necessary for a risk calculation but here I only use it to convert the quote currency into my account currency.