help required for a small function

 

I have a function here that i use to calculate the total risk per currency pair.

I have it displayed on my chart so i know my total exposure if all trades on that pair hit there stop loss.

void Pair_risk()
 {   
 
 if (show_risk==false)return;
 
 int i;
 double trade_cash_risk,percent_risk;
 
       
    for(i = OrdersTotal()-1; i >= 0 ; i--) 
            {
               
    if (OrderSelect(i,SELECT_BY_POS)==false) break;  
    
    if(OrderSymbol()==Symbol())
                     { 
    
   double lot_size=OrderLots();     
   double ask=MarketInfo(Symbol(),MODE_ASK); 
   double bid=MarketInfo(Symbol(),MODE_BID);  
  
   if (OrderStopLoss()==0)break;
   
   double SL_PT; double RISK; double risk_total;
 
  if ((OrderType() == OP_BUY) && (OrderStopLoss() < OrderOpenPrice()))  SL_PT = ((OrderOpenPrice() - OrderStopLoss())/Point/multiplier);  
  if ((OrderType() == OP_SELL) && (OrderStopLoss() > OrderOpenPrice())) SL_PT = ((OrderStopLoss() - OrderOpenPrice())/Point/multiplier);  
   
    
       trade_cash_risk = NormalizeDouble(lot_size*SL_PT*(MarketInfo(OrderSymbol(), MODE_TICKVALUE)),2); 
      
       percent_risk = NormalizeDouble((trade_cash_risk/AccountBalance()*100),1);
  
  
   if (OrderType() == OP_BUY) RISK = percent_risk;
   if (OrderType() == OP_SELL) RISK = percent_risk;
      
   if (OrderType() == OP_BUY || OrderType() == OP_SELL) risk_total+= RISK;      
  
         
      }
   }
   
   Print(risk_total); // I normally have it display on my chart and not printed.
   
    return;
  }  

Assume i have a BUY trade open...

This code seems to calculate the correct risk if i have one trade open and stoploss is <= or >= orderopenprice.

This code seems to calculate the correct risk if i have two or more trades open and ALL stoploss's are < orderopenprice.

The code calculates everything wrong if i have more than one trade open and i have at least one trade >= orderopenprice.


Can anyone can fix this code or rewrite how it should be.


I would be very grateful if you can.

Thanks.
 
//Money management. It calculates the lot size from the Stop Loss size and Risk input
double GetLots(double RiskInPips)
{
   double lot, dif;
   double   MinLot;
   double   MaxLot;
   double   LotStep;
   double   mpl;
   if(UseMoneyManagement)
   {
      double totalrisk = AccountBalance() * Risk / 100;
      double traderisk = totalrisk;
      double pip = traderisk/RiskInPips;
      lot = pip/MarketInfo(Symbol(), MODE_TICKVALUE);
      LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
      dif = MathMod(lot, LotStep);
      if(dif != 0)
      {
         lot -= dif;
      }
   }
   else
   {
      lot = Lots;
   }
   MinLot = MarketInfo(Symbol(), MODE_MINLOT);
   MaxLot = MarketInfo(Symbol(), MODE_MAXLOT);
   if(lot < MinLot) lot = MinLot;
   if(lot > MaxLot) lot = MaxLot;
   
   return(lot);
}
This Money management routine works.....It calculates lot size based on risk %, size in pips of SL, and account balance.  It takes risk in pips as input which is either fixed or calculated by your trade algorythm, uses risk% which is an external variable and reads account balance.  I use it with only 1 trade open at a time, but I see no reason it wouldn't work with multiple trades as it is not based on equity but account balance.
 
bluesman:
This Money management routine works.....It calculates lot size based on risk %, size in pips of SL, and account balance.  It takes risk in pips as input which is either fixed or calculated by your trade algorythm, uses risk% which is an external variable and reads account balance.  I use it with only 1 trade open at a time, but I see no reason it wouldn't work with multiple trades as it is not based on equity but account balance.

Thanks but i dont think this would work for more than one trades. We would need a loop to cycle through each trade and then add the total risk having gone through the loop.

 
My code does that, as well as preventing margin call at the most adverse excursion (i.e. SL)
 
bluesman:
This Money management routine works.....
Perhaps . . perhaps not.  MathMod() can give incorrect results . . .  https://www.mql5.com/en/forum/129832