stop out again

 

hi,


im still working on a function to get the maximum available lot size without getting the risk of a stop out.

there seems to be a bug somewhere, maybe someone of you can help me?


the idea is, to caluclate the maximum los of all open orders. then substract that from the account balance, because equity can change during open trades.

after getting this value, i have to calculate the difference between this value and the stop out value.... etc. etc...


double calculateLotSize(int c)
{
   RefreshRates();
   
   int    numOrders;
   double lotSize;
   double oOpenPrice;
   double oStopLoss;
   double oLots;
   int    oType;
   string oSymbol;
   double oSwap;
   double oPipDiff;
   double oSum;
   double oSumTotal;
   double minLot;
   double maxLot;
   int    accStopOutMode;
   int    accStopOutLevel;
   double accBalance;
   double maxPrice;
   
   numOrders       = OrdersTotal();
   oSum            = 0.0;
   lotSize         = 0.0;
   oSumTotal       = 0.0;
   minLot          = MarketInfo(currencies[c],MODE_MINLOT);
   maxLot          = MarketInfo(currencies[c],MODE_MAXLOT);
   accStopOutMode  = AccountStopoutMode();
   accStopOutLevel = AccountStopoutLevel();
   accBalance      = AccountBalance();
   
   if(numOrders > 0) {
      for(int i=0;i<numOrders;i++) {
         if(OrderSelect(i, SELECT_BY_POS)==true) {
            oOpenPrice = OrderOpenPrice();
            oStopLoss  = OrderStopLoss();
            oLots      = OrderLots();
            oType      = OrderType();
            oSymbol    = OrderSymbol();
            oSwap      = OrderSwap();
         
            if(oType == OP_BUY) {
               oPipDiff = oOpenPrice-oStopLoss;
            }
            if(oType == OP_SELL) {
               oPipDiff = oStopLoss-oOpenPrice;
            }
            oSum = ((oPipDiff * MarketInfo(oSymbol,MODE_TICKVALUE) * MarketInfo(oSymbol,MODE_LOTSIZE) + MarketInfo(oSymbol, MODE_SPREAD)) * oLots) + oSwap;
            oSumTotal = oSumTotal + oSum;
         }
      }
   }
   
   if(accStopOutMode == 0) {
      maxPrice = MathFloor((accBalance - oSumTotal) / (100 / accStopOutLevel));
   } else {
      maxPrice = 0;
   }
   
   double optSize = (maxPrice / 100) * riskVal;
   
   lotSize = MathFloor(optSize / (slPoints * MarketInfo(currencies[c], MODE_TICKVALUE)));
   
   if(lotSize < minLot) {
      return(0);
   }
   if(lotSize > maxLot) {
      lotSize = maxLot;
   }
   
   return(lotSize);
}
 
mk77ch:

hi,


im still working on a function to get the maximum available lot size without getting the risk of a stop out.

there seems to be a bug somewhere, maybe someone of you can help me?


the idea is, to caluclate the maximum los of all open orders. then substract that from the account balance, because equity can change during open trades.

after getting this value, i have to calculate the difference between this value and the stop out value.... etc. etc...


What is the relevance of the lot-size to the propensity to get stopped out?

 

lets say you have a balance of USD 10'000 and you open a new trade with a stop loss. dont you need what the maximum stop loss could be to not be stopped out by the broker? could you please fix the function?


best regards,

mike

 
Reason: