Calculate risk and size, nearly there but not quite...

 
      double DollarsToRisk = (RiskPercent / 100.0) * AccountBalance();
      double SpreadCost = Lots * MarketInfo(Symbol(),MODE_TICKVALUE) * MarketInfo(Symbol(),MODE_SPREAD);//In dollars,verified ok on all pairs!
      double StoppLossCost = Lots * SL * MarketInfo(Symbol(),MODE_TICKVALUE);
      
      SendLots = DollarsToRisk / (StoppLossCost + StoppLossCost);

The aim is to fairly accurately calculate tradesize (lots) so that if it goes to stopploss I know almost to the dollar how much will be lost.

The problem, as can be seen above, is that the unknown that we're trying to calculate is actually a variable in the calculation.

Anyone see a way to do this, or is it simply impossible ?

 
//+------------------------------------------------------------------+
//| LotSize |
//+------------------------------------------------------------------+
double LotSize(double Risk, double SL)
{
double StopLoss = SL / Point / 10;
double Size = Risk / 100 * AccountBalance() / 10 / StopLoss;
double MinLot = MarketInfo(Symbol(), MODE_MINLOT);

if(Size < MinLot) Size = MinLot;

return(NormalizeDouble(Size, 2));
}


for 5 digits broker

u need to input how many pips to lose

 
Huh?
 
DayTrader:

The aim is to fairly accurately calculate tradesize (lots) so that if it goes to stopploss I know almost to the dollar how much will be lost.

The problem, as can be seen above, is that the unknown that we're trying to calculate is actually a variable in the calculation.

Anyone see a way to do this, or is it simply impossible ?


double GetLots(int TradeType, double TradeStopLevel)
{
   double         MinLotSize=0;
   double         MaxLotSize=0;
   double         LotStep=0;
   double         Decimals=0;
   int            CurrencyLotSize=0;

   double OrderLotSize;
 
   MinLotSize = MarketInfo(Symbol(),MODE_MINLOT);
   MaxLotSize = MarketInfo(Symbol(),MODE_MAXLOT);
   LotStep = MarketInfo(Symbol(),MODE_LOTSTEP);
   CurrencyLotSize = MarketInfo(Symbol(),MODE_LOTSIZE);

   if(LotStep == 0.01) {Decimals = 2;}
   if(LotStep == 0.1) {Decimals = 1;}
   
   if(TradeType==OP_BUY)
   {
      if(RiskPercent*0.01*AccountFreeMargin()<(Ask-TradeStopLevel)*CurrencyLotSize*MinLotSize)
      {
         Print("Unable to open long trade. Stop loss is too large!");
         return(0);
      }
      else
      {
         OrderLotSize = (RiskPercent*0.01*AccountFreeMargin())/((Ask-TradeStopLevel)*CurrencyLotSize);
         OrderLotSize = NormalizeDouble(OrderLotSize, Decimals);
      }
   }
   if(TradeType==OP_SELL)
   {
      if(RiskPercent*0.01*AccountFreeMargin()<(TradeStopLevel-Bid)*CurrencyLotSize*MinLotSize)
      {
         Print("Unable to open short trade. Stop loss is too large!");
         return(0);
      }
      else
      {
         OrderLotSize = (RiskPercent*0.01*AccountFreeMargin())/((TradeStopLevel-Bid)*CurrencyLotSize);
         OrderLotSize = NormalizeDouble(OrderLotSize, Decimals);
      }
   }
   if (OrderLotSize > MaxLotSize) OrderLotSize = MaxLotSize;
   return(OrderLotSize);  
}

I usually use this function. The amount of money to lose is controlled by RiskPercent variable which you can make extern.

You can also change the function so that instead of passing stop level you can pass stop loss in pips.

Regards.

 

Thanks. Do this function include the cost of spread in determining OrderLotSize?

Maybe you could mod it for me so it takes stoploss pips instead of level ?

 
DayTrader:

The aim is to fairly accurately calculate tradesize (lots) so that if it goes to stopploss I know almost to the dollar how much will be lost.

The problem, as can be seen above, is that the unknown that we're trying to calculate is actually a variable in the calculation.

Anyone see a way to do this, or is it simply impossible ?

Delete where you have "Lots *" Those two calculations produce cost per lot. DollarsToRisk/CostPerLot is wanted lots. Here's what I've been thinking:

external    int MMMode_F0M1G2       = 1;
external double MMMode_Multiplier   = 100;
            int countOfAllTrades;

//+------------------------------------------------------------------+
//|                                              MoneyManagement.mq4 |
//|                                                         WHRoeder |
//|                                               WHRoeder@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "WHRoeder"
#property link      "mailto:WHRoeder@yahoo.com"

double LotSize(double stopLossPoints, bool modifying=false) {
    // This function computes the lot size for a trade.
    // Explicit inputs are SL and if returned lot size will be used in a new
    // order or be used to modify an pending order (trade count-1.)
    // SL must be relative to bid/ask. E.G. SL=bid-30*points
    // Implicit inputs are the MM mode, the MM multiplier and currently open or
    // pending trade count. 
    // Mode, multiplier, trade count determined the maximum dollar risk allowed
    // relative to the AccountBalance. 
    // SL determines the maximum dolar risk possible per lot.
    ////////////////////////////////////////////////////////////////////////////
    double riskable, ab=AccountBalance();
    switch( MMMode_F0M1G2 ) {
        case 0:                                                 // fixed
            riskable = MMMode_Multiplier;
            break;
        case 1:                                                 // moderate
            // See https://www.mql5.com/en/articles/1526 Fallacies, Part 1: Money
            // Management is Secondary and Not Very Important
            riskable = MathSqrt( MMMode_Multiplier * ab )/ab;   // ~const rate.
            riskable = MathSqrt( MMMode_Multiplier * ab *
                                 MathPow(1-riskable, countOfAllTrades-modifying) 
                                );
            break;
        case 2:                                                 // geometrical
            riskable = MMMode_Multiplier * ab *
                       MathPow( 1-MMMode_Multiplier, countOfAllTrades-modifying );
            break;
        default: riskable = 0;  break;
    }
    double maxLossPerLot    = stopLossPoints *
                              MarketInfo( Symbol(), MODE_TICKVALUE );
    double size     = riskable / maxLossPerLot;
    double LotStep  = MarketInfo(Symbol(),MODE_LOTSTEP);
       int steps    = (size+LotStep/2)/LotStep;
    size            = MathMin( steps*LotStep, MarketInfo(Symbol(),MODE_MAXLOT) );
    if(debug||size==0)  print(  "LotSize(SL=",DoubleToStr(stopLossPoints,Digits),
                                ",mod=",modifying,")=",size,
                                " [",riskable,"/",maxLossPerLot,
                                ",AB=",ab,AccountCurrency(),
                                ",mode=",Mode_F0M1G2,
                                ",mult=",MMMode_Multiplier,
                                ",coat=",countOfAllTrades,
                                "]" )
    return( size );
}
// MoneyManagement.mq4
Reason: