Download MetaTrader 5

Calculate the risk of all open orders

To add comments, please log in or register
Find and download Code Base apps via MetaTrader. It is fast!
mar
1146
mar 2015.02.02 10:24 

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;
         }
      }
   }
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2015.02.02 10:52  
Quite ok if no hedging.
mar
1146
mar 2015.02.02 11:03  
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.
Keith Watford
Moderator
9647
Keith Watford 2015.02.02 11:27  
 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

mar
1146
mar 2015.02.02 11:39  

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;
         }
      }
   }
mar
1146
mar 2015.02.02 16:12  

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));
}
SYED NAUFAL GADDAFI
1401
SYED NAUFAL GADDAFI 2015.02.02 17:47  
Thanks for sharing. Would help a lot for newbies.
Carl Schreiber
6609
Carl Schreiber 2015.02.02 22:34  
riskPips = direction*(OrderOpenPrice() - OrderStopLoss());

You don't have pips but points!

Which is correct for the following money calculation!

whroeder1
14532
whroeder1 2015.02.03 02:47  
mar
1146
mar 2015.02.03 09:03  

@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.

To add comments, please log in or register