3

My question is in two closely related parts:

  1. Given the entry price and stop loss, how can I calculate my lot size so that 2.5% of my current account balance would be risked on trades?

  2. Also in cases where the 2.5% lot size is not a tradable lot size, say 0.013 and 0.018, how can I round the lot size already calculated in 1. to the closest tradable lot size 0.01 (for 0.013) and 0.02 (for 0.018)?

I know how to calculate the number of pips between my entry price and stop loss:

double DiffInPips = MathAbs(NormalizeDouble(stopLoss-openPrice,Digits)/Point);

I believe it is needed to solve this, but not sure how to arrive at the desired lot sizes in both of the above.

user3666197
  • 1
  • 6
  • 50
  • 92
SuperHueman
  • 165
  • 15

2 Answers2

3

Calculating the risk-exposed part of your equity is a trivial but not on-topic here, so let me here focus on the programming, solving the item 2:

double NormalizeLotsToTRADE( const double aLotSizeREQUESTED )         // DEMO CODE
{  
   double  aMinSIZE = MarketInfo( _Symbol, MODE_MINLOT  ),
           aMaxSIZE = MarketInfo( _Symbol, MODE_MAXLOT  ),
           aFixSTEP = MarketInfo( _Symbol, MODE_LOTSTEP );
   
   return( MathMin(            aMaxSIZE,                              // never > MAXLOT
                    MathMax(   aMinSIZE,                              // never < MINLOT
                             ( aMinSIZE                               // ~ min + steps
                             + aFixSTEP * MathFloor( ( aLotSizeREQUESTED
                                                     - aMinSIZE
                                                       )
                                                     / aFixSTEP
                                                     )
                               )
                             )
                    )
           );
    }

For a case your Risk-Manager is not so strict on your risk-modeling caveats and permit you to go into "closest" rounding, may use MathRound() instead of risk-management strict using of MathFloor().

halfer
  • 19,824
  • 17
  • 99
  • 186
user3666197
  • 1
  • 6
  • 50
  • 92
  • Thanks for the answer but I have to admit that the first part isn't trivial for me. I haven't been able to calculate it and I am trying out some ideas with making an EA. – SuperHueman Aug 13 '19 at 14:17
  • 1
    Just split the task into several steps. Compute how much money you can lose. Compute how much money would you lose if trading 1.00 lot in case of SL. Compute the lot size as a ratio. Same as you do it when trading manually. After you received a value (which is probably not a SOF-question) - normalize as shown in this example. – Daniel Kniaz Aug 13 '19 at 14:28
  • Right on spot, Daniel +1-ed you for your wise, calm and methodic help you provide MQL4/5-askers – user3666197 Aug 13 '19 at 16:06
  • @user3666197 I've finally been able to calculate the lot size based on the entry Price, stop loss price and percentage risk. I used the code you provided in this solution to normalize the lot size but it always results in 0.01 lot size no matter the figure. [Here](https://pastebin.com/xKbc6mnv) is the most relevant part of my code. – SuperHueman Aug 29 '19 at 13:35
  • @user3666197 I recently gained enough upvotes to upvote answers. I also make sure I've implemented a solution before I accept an answer. I was just able to solve the first part of my question, before then I couldn't make use of your solution. I am still learning a lot. – SuperHueman Aug 29 '19 at 15:49
0
double GetLotSize()
{
    double balance = AccountBalance();
    double tradeAmount = balance * RiskPercentage / 100.0;
    
    double pipValue = MarketInfo(Symbol(), MODE_TICKVALUE) / 
    MarketInfo(Symbol(), MODE_TICKSIZE);
    double pipsToRisk = StopLossPips;
    
    double lotSize = tradeAmount / (pipsToRisk * pipValue);

    double minLot = MarketInfo(Symbol(), MODE_MINLOT);
    double maxLot = MarketInfo(Symbol(), MODE_MAXLOT);
    double lotStep = MarketInfo(Symbol(), MODE_LOTSTEP);

    lotSize = MathRound(lotSize / lotStep) * lotStep;
    lotSize = MathMax(minLot, MathMin(maxLot, lotSize));

    return lotSize;
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 12 '23 at 13:08