(Edited to Elaborate and Repost) Wrong Lotsize Calculation

 

Hello, I am learning to code and came across these 2 functions that calculate the optimal lot size for the order. I tried both of them however, they are both giving me a bigger than supposed lot size sometimes (e.g. returns a 1.5 lot size for a $1000 account, or a 2.94 lot size for a $10000 account) so it's causing an error 134 not enough money all the time.

A fellow trader/programmer reminds me on my old post that it might be a stop loss that's too tight, which is very possible and true. So I'm sending the code here to see if there's anything wrong with the code itself, if not, is having a bigger stop loss the easiest or best way to adjust? Or is it possible to calculate a lot size based on risk percentage AND stop loss AND margin? But consider myself new to programming, it might just be too difficult.

My risk percentage is always fixed to 2% for now for this strategy, so I guess it can be hard coded, if it's going to make a difference.

Thanks in advance!

double ATR9 = iATR(NULL,0,9,0);
int Magic = 3333;

//---------BUY ORDER------------------------
int executeBuy()
{
   double entry = Ask;
   entry = NormalizeDouble(entry,_Digits);
   
   int shift = iLowest(NULL,0,MODE_LOW,5,1);
   double LowInLastFive = iLow(NULL,0,shift);
   
   double SL = (LowInLastFive - ATR9);
   SL = NormalizeDouble(SL,_Digits);
   double SLpips = (entry - SL) / GetPipValue();
   SLpips = NormalizeDouble(SLpips,_Digits);
   
   double TP = entry + SLpips * 1.5 * GetPipValue();
   TP = NormalizeDouble(TP,_Digits);
   
      return OrderSend(NULL,OP_BUY,CalculateLotSize(SLpips),entry,0,SL,TP,NULL,Magic,0,clrGreen);
}
   


//-----SELL ORDER-------------------------------
int executeSell()
{
   double entry = Bid;
   entry = NormalizeDouble(entry,_Digits);
   
   int shift = iHighest(NULL,0,MODE_HIGH,5,1);
   double HighInLastFive = iHigh(NULL,0,shift);
   
   double SL = (HighInLastFive + ATR9);
   SL = NormalizeDouble(SL,_Digits);
   double SLpips = (SL - entry)/ GetPipValue();
   SLpips = NormalizeDouble(SLpips,_Digits);
   
   double TP = entry - SLpips *1.5 * GetPipValue();
   TP = NormalizeDouble(TP,_Digits);
   
         
      return OrderSend(NULL,OP_SELL,CalculateLotSize(SLpips),entry,0,SL,TP,NULL,Magic,0,clrRed);

}



double GetPipValue()
{
   if(_Digits >=4)
   {
      return 0.0001;
   }
   else
   {
      return 0.01;
   }
}



//-----LOT SIZE FUNCTION 1------------------------------------------

double CalculateLotSize(double SLinPips)
{
   double MaxRiskPerTrade = 2;
   double LotSize = 0;
   
   double nTickValue = MarketInfo(Symbol(), MODE_TICKVALUE);
   
   if ((Digits == 3) || (Digits == 5)){
      nTickValue = nTickValue * 10;
   }
   
   LotSize = (AccountBalance() * MaxRiskPerTrade / 100) / (SLinPips * nTickValue);
   LotSize = MathRound(LotSize / MarketInfo(Symbol(), MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
   
   return LotSize;
}


//-----LOT SIZE FUNCTION 2------------------------------------------------------------------
double OptimalLotSize(double RiskPrc,double SLpips)
     {
     double accBal = AccountBalance();
     Print("Account Balance: " + accBal);
     
     double LotSize = MarketInfo(NULL,MODE_LOTSIZE);
     Print("Lot Size: " + LotSize);
     
     double TickValue = MarketInfo(NULL,MODE_TICKVALUE);
     
     if(Digits <= 3)
     {
      TickValue = TickValue /100;
     }
     Print("TickValue: " + TickValue);
     
     double LossInDollar = accBal * RiskPrc;
     Print("Loss in Dollar: " + LossInDollar);
     
     double LossInQuoteCurrency = LossInDollar / TickValue;
     Print("Loss in Quote Currency: " + LossInQuoteCurrency);
     
     double optimalLotSize = NormalizeDouble(LossInQuoteCurrency /(SLpips * GetPipValue())/LotSize,2);
     
     return optimalLotSize;
     }
      
    
     //VERSION TWO BASED ON Risk Percentage
     double OptimalLotSize(double RiskPrc, double entry, double SL)
     {
      double SLpips = (entry - SL) / GetPipValue();
      return OptimalLotSize(RiskPrc,SLpips);
     }
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Compilation Errors
Documentation on MQL5: Constants, Enumerations and Structures / Codes of Errors and Warnings / Compilation Errors
  • www.mql5.com
MetaEdtior 5 shows error messages about the program errors detected by the built-in compiler during compilation. The list of these errors is given...
 
  1.    double entry = Ask;
    

    Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. double ATR9 = iATR(NULL,0,9,0);
    int Magic = 3333;
    

    Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  3.    LotSize = (AccountBalance() * MaxRiskPerTrade / 100) / (SLinPips * nTickValue);
       LotSize = MathRound(LotSize / MarketInfo(Symbol(), MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
    

    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.

  4.    double entry = Ask;
       entry = NormalizeDouble(entry,_Digits);
    

    Prices (and lots) you get from the terminal are already correct (normalized).

  5. return OrderSend(NULL,OP_SELL,CalculateLotSize(SLpips),entry,0,SL,TP,NULL,Magic,0,clrRed);
    

    Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
      MT5: create them.
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. Those are not assignments; they are initialization of a common (globally declared), or static variable(s) with a constant. They work exactly the same way in MT4/MT5/C/C++.

    1. They are initialized once on program load.

    2. They don't update unless you assign to them.

    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5, or MT4 with strict (which you should always use).

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and Don't try to use any price (or indicator) or server related functions in OnInit (or on load or in OnTimer before you've received a tick), as there may be no connection/chart yet:

      1. Terminal starts.
      2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
      3. OnInit is called.
      4. For indicators OnCalculate is called with any existing history.
      5. Human may have to enter password, connection to server begins.
      6. New history is received, OnCalculate called again.
      7. A new tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.

    4. Unlike indicators, EAs are not reloaded on chart change, so you must reinitialize them, if necessary.
                external static variable - MQL4 programming forum #2 (2013)

  3. 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.

  4. Prices (and lots) you get from the terminal are already correct (normalized).

  5. Be careful with NULL.

    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not, OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
      MT5: create them.
    5. Cloud Protector Bug? - MQL4 programming forum (2020)

thank you for your detailed reply!