Calculate the risk of all open orders

Back to topics list To post a new topic, please log in or register
avatar
372
mar 2015.02.02 11: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;
         }
      }
   }
avatar
1211
deysmacro 2015.02.02 11:52 #
 
Quite ok if no hedging.
avatar
372
mar 2015.02.02 12: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.
avatar
Moderator
3418
GumRai 2015.02.02 12: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

avatar
372
mar 2015.02.02 12: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;
         }
      }
   }

 

avatar
372
mar 2015.02.02 17: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));
}

 

avatar
1211
deysmacro 2015.02.02 18:47 #
 
Thanks for sharing. Would help a lot for newbies.
avatar
2017
gooly 2015.02.02 23:34 #
 
riskPips = direction*(OrderOpenPrice() - OrderStopLoss());

You don't have pips but points!

Which is correct for the following money calculation!



avatar
13001
WHRoeder 2015.02.03 03:47 #
 
Don't use TickValue by itself. https://forum.mql4.com/41259/page3#512466
avatar
372
mar 2015.02.03 10: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.

Back to topics list  

To add comments, please log in or register