Need some help, this Function is returning the wrong lotsizes

 
//+------------------------------------------------------------------+
//| Calculate lot size based on risk percentage                      |
//+------------------------------------------------------------------+
double CalculateLotSize(string symbol, double accountBalance, double stopLossPoints, double riskPercentage)
{
    // Get symbol properties
    double tickSize = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_SIZE);
    double tickValue = SymbolInfoDouble(symbol, SYMBOL_TRADE_TICK_VALUE);
    double pointValue = SymbolInfoDouble(symbol, SYMBOL_POINT);

    // Calculate risk amount in account currency
    double riskAmount = accountBalance * (riskPercentage / 100.0);

    // Calculate lot size
    double lotSize = riskAmount / (stopLossPoints * tickValue / pointValue);

    Print("Symbol: ", symbol, ", Account Balance: ", accountBalance, ", Risk Percentage: ", riskPercentage, ", Stop Loss Points: ", stopLossPoints, ", Risk Amount: ", riskAmount);
    Print("Tick Size: ", tickSize, ", Tick Value: ", tickValue, ", Point Size: ", pointValue, ", Point Value: ", pointValue);
    Print("Calculated Lot Size: ", lotSize);

    return lotSize;
}


//Debug Prints

2024.07.19 14:20:23.925 gg EA v2.0 (USDCAD,M15) Setting stop loss for SELL: 1.37186
2024.07.19 14:20:23.925 gg EA v2.0 (USDCAD,M15) Symbol: USDCAD, Account Balance: 6856.4, Risk Percentage: 1.0, Stop Loss Points: 1.37186, Risk Amount: 68.564
2024.07.19 14:20:23.925 gg EA v2.0 (USDCAD,M15) Tick Size: 0.00001, Tick Value: 0.7291605903284141, Point Size: 0.00001, Point Value: 0.00001
2024.07.19 14:20:23.925 gg EA v2.0 (USDCAD,M15) Calculated Lot Size: 0.0006854300887845695
2024.07.19 14:20:23.925 gg EA v2.0 (USDCAD,M15) Adjusted lot size (below min limit): 0.01

 
 double lotSize = riskAmount / (stopLossPoints * tickValue / pointValue);

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin or leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% account total.

  1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support. Then you compute your lot size.

  2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

  3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
              MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
              Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
              Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

  4. You must normalize lots properly and check against min and max.

  5. You must also check Free Margin to avoid stop out

  6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

Most pairs are worth about $10 per PIP. A $5 risk with a (very small) 5 PIP SL is $5/$10/5 or 0.1 Lots maximum.

 
William Roeder #:

Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin or leverage. 

A bold statement. The account leverage determines the lot size you are permitted to use. higher leverage will permit higher volume, and what do you think a larger lot size means? More risk. It's all correlated. A high leverage will enable a trader to take higher risk, which is what a lot of people want.

 
#define MAX_RISK_POS 5.00 // Maximum Risk Allowed Per Position
//---
enum ENUM_RISK
  {
   RiskPerc   = 1, // Percentage
   RiskFixLot = 2  // Fixed Lot
  };
//---
input ENUM_RISK InpPosRiskMode = RiskPerc; // Positions Risk Mode
input double    InpPosRiskSize = 0.50;     // Positions Risk Size
//+--------------------------------------------------------------------------------------------------------------------+
//| This function calculates volume                                                                                    |
//+--------------------------------------------------------------------------------------------------------------------+
bool CalculateVolume(string symbol, ENUM_ORDER_TYPE type, double price, double sl, double &lots)
  {
//--- Local variables
   double step_vol, acc_bal, loss, profit;

//--- Check if stop loss is zero
   if(sl == 0.0 && InpPosRiskMode == RiskPerc)
     {
      Print(__FUNCTION__, " - Invalid Stop Loss for calculating trade volume: ", symbol);
      return(false);
     }
//--- Get minimal step of volume changing
   if(!SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP, step_vol))
     {
      Print(__FUNCTION__, " - Error getting the SYMBOL_VOLUME_STEP: ", GetLastError(), " - ", symbol);
      return(false);
     }
//--- Get account capital in deposit currency
   acc_bal = AccountInfoDouble(ACCOUNT_BALANCE) + AccountInfoDouble(ACCOUNT_CREDIT);

//--- Risk Mode: Fixed Lot
   if(InpPosRiskMode == RiskFixLot)
     {
      //--- Get volume size
      lots = InpPosRiskSize;
     }
//--- Risk Mode: Percentage
   else
     {
      //--- Get volume size
      if(!OrderCalcProfit(type, symbol, 1.0, price, sl, profit))
        {
         Print(__FUNCTION__, " - Error calculating estimated trading profit: ", GetLastError(), " - ", symbol);
         return(false);
        }
      loss = -profit; // "profit" will always be negative since we use "sl" as the price_close in OrderCalcProfit
      lots = MathFloor(acc_bal * InpPosRiskSize / loss / 100.0 / step_vol) * step_vol;
     }

//--- Normalizes the volume
   if(!NormalizeVolume(symbol, lots))
     {
      Print(__FUNCTION__, " - Error when normalizing the volume: ", symbol);
      return(false);
     }

//--- Calculates the estimated risk of the operation
   if(!OrderCalcProfit(type, symbol, lots, price, sl, profit))
     {
      Print(__FUNCTION__, " - Error calculating estimated trading profit: ", GetLastError(), " - ", symbol);
      return(false);
     }

//--- Check maximum risk per position
   if(MathAbs(profit) > acc_bal * (MAX_RISK_POS / 100.0))
     {
      Print(__FUNCTION__, " - Very high risk of the operation, inappropriate for this strategy: ", symbol);
      return(false);
     }

//--- Successful calculation and checks
   return(true);
  }
//+--------------------------------------------------------------------------------------------------------------------+
//| This function normalizes the volume according to the minimum volume change step                                    |
//+--------------------------------------------------------------------------------------------------------------------+
bool NormalizeVolume(string symbol, double &lots)
  {
//--- Local variables
   double LotMin, LotMax, LotStep;

//--- Minimal allowed volume for trade operations
   if(!SymbolInfoDouble(symbol, SYMBOL_VOLUME_MIN, LotMin))
     {
      Print(__FUNCTION__, " - Error getting the SYMBOL_VOLUME_MIN: ", GetLastError(), " - ", symbol);
      return(false);
     }
//--- Maximal allowed volume for trade operations
   if(!SymbolInfoDouble(symbol, SYMBOL_VOLUME_MAX, LotMax))
     {
      Print(__FUNCTION__, " - Error getting the SYMBOL_VOLUME_MAX: ", GetLastError(), " - ", symbol);
      return(false);
     }
//--- Get minimal step of volume changing
   if(!SymbolInfoDouble(symbol, SYMBOL_VOLUME_STEP, LotStep))
     {
      Print(__FUNCTION__, " - Error getting the SYMBOL_VOLUME_STEP: ", GetLastError(), " - ", symbol);
      return(false);
     }

//--- Normalizes the volume
   lots = LotMin + MathFloor((lots - LotMin) / LotStep) * LotStep;
   lots = NormalizeDouble(MathMin(LotMax, MathMax(LotMin, lots)), 2);

//--- Normalized volume
   return(true);
  }

 
William Roeder #:
You must normalize lots properly and check against min and max.

thx mate

 
//+------------------------------------------------------------------+
//| Function to calculate lot size based on percentage risk        |
//+------------------------------------------------------------------+
double CalculateLotSizeByRisk(string symbol, double entryPrice, double stopLossPrice, double accountBalance, double RiskPercentage)
{
    // Declare necessary variables
    double lots = 0.0;
    CSymbolInfo symbolInfo;
    
    // Initialize the symbol info object
    symbolInfo.Name(symbol);
    symbolInfo.Refresh();
    
    // Retrieve symbol information
    double minLot = symbolInfo.LotsMin();       // Minimum volume allowed by the symbol
    double maxLot = symbolInfo.LotsMax();       // Maximum volume allowed by the symbol
    double lotStep = symbolInfo.LotsStep();     // Volume step size
    double tickValue = symbolInfo.TickValue();  // Tick value of the symbol
    double pointSize = symbolInfo.Point();      // Point size of the symbol

    // Calculate stop loss distance in points
    double stopLossDistance = MathAbs(entryPrice - stopLossPrice) / pointSize;

    // Debugging: Print symbol info
    Print("Symbol: ", symbol);
    Print("Min Lot: ", minLot, ", Max Lot: ", maxLot, ", Lot Step: ", lotStep, ", Tick Value: ", tickValue, ", Point Size: ", pointSize);
    
    // Calculate pip value of the stop loss
    double pipValue = stopLossDistance * tickValue;
    
    // Debugging: Print pip value
    Print("Stop Loss Distance (Points): ", stopLossDistance, ", Pip Value: ", pipValue);
    
    // Ensure pipValue is not zero to avoid division by zero error
    if (pipValue == 0.0)
    {
        Print("Error: pipValue is zero. Unable to calculate lot size.");
        return minLot; // Return minimum lot size as a fallback
    }
    
    // Calculate risk amount in currency
    double riskAmount = accountBalance * (RiskPercentage / 100.0);
    
    // Debugging: Print risk amount
    Print("Account Balance: ", accountBalance, ", Risk Percentage: ", RiskPercentage, ", Risk Amount: ", riskAmount);
    
    // Calculate lot size based on risk amount and pip value
    lots = riskAmount / pipValue;
    
    // Normalize lot size to the nearest valid step size
    lots = MathFloor(lots / lotStep) * lotStep;
    
    // Ensure the calculated lot size is within the allowed range
    if (lots < minLot)
        lots = minLot;
    if (lots > maxLot)
        lots = maxLot;
    
    // Return the calculated lot size
    return lots;
}

i think this works
 
Baker Kasule #:
Which library should you import to make 
CSymbolInfo

useable?


EDIT: Whoever needs it, it's 

#include <Trade\SymbolInfo.mqh>
 
Conor Mcnamara #: A bold statement. The account leverage determines the lot size you are permitted to use. higher leverage will permit higher volume, and what do you think a larger lot size means? More risk. It's all correlated. A high leverage will enable a trader to take higher risk, which is what a lot of people want.

You missed the point. Just because your car can do 150 doesn't mean you should go over the speed limit of 70.

Risk depends on your initial stop loss, lot size, and the value of the symbol. If you control your risk, increasing the allowed leverage changes nothing. Make your lotsize a function of risk, not leverage.