Position Size Calculating Function

root7x7

Trader
Apr 22, 2022
3
0
17
37
Hello,

I've been trying to create a function to calculate the risk percentage, but I keep running into errors, especially since I want a function that works for all currencies, indices, gold, and oil, similar to this link:
https://www.myfxbook.com/forex-calculators/position-size.

On the chart, I set the stop-loss line as follows:
MQL4:
 double stopLossPrice = NormalizeDouble(ObjectGetDouble(0, "StopLoss", OBJPROP_PRICE1),Digits);
 // here buy market
 double myStop = GetPips(Ask,stopLossPrice);
 double lot = GetLot(myStop);
I have a function to calculate the difference between the entry price and the stop loss, but I'm not sure if it's 100% accurate.
MQL4:
double GetPips(double entryPrice, double stopLoss)
  {
   double point = MarketInfo(Symbol(), MODE_POINT);
   int pointDifference = int((entryPrice - stopLoss) / point/10);
 
   if(pointDifference < 0)
     {
      pointDifference = -pointDifference;
     }
 
   return pointDifference;
  }
There are errors in my lot size calculation function.
MQL4:
extern double Risk = 0.25;
extern double Amount = 1000;
double GetLot(double SLPips)
  {
 
   double Lotsize = 0;
 
   double nTickValue = MarketInfo(Symbol(),MODE_TICKVALUE);
   if((Digits==3) || (Digits == 5))
     {
      nTickValue = nTickValue * 10;
     }
 
   Lotsize = (Amount * Risk / 100) / (SLPips * nTickValue);
   Lotsize = MathRound(Lotsize / MarketInfo(Symbol(),MODE_LOTSTEP)) * MarketInfo(Symbol(), MODE_LOTSTEP);
   double minLotSize = MarketInfo(Symbol(), MODE_MINLOT);
   double maxLotSize = MarketInfo(Symbol(), MODE_MAXLOT);
   if(Lotsize < minLotSize)
     {
      return minLotSize;
     }
 
   if(Lotsize > maxLotSize)
     {
      return maxLotSize;
     }
 
   return Lotsize;
  }
I want to solve the problem with this function so that it calculates the risk percentage from the Risk variable. If it's set to 1%, when the stop loss is hit, I only lose 1%, no more and no less. I often use a stop loss of 0.5 or sometimes 0.25.

I tried using ChatGPT, but it was giving me strange results.
 

root7x7

Trader
Apr 22, 2022
3
0
17
37
Hello Endivid,
First, thank you for moving the topic to the appropriate section. The function works with Forex but not perfectly 100%. For instance, if you have $100,000 and a 1% risk, this means that if the stop-loss is hit, you would lose $1,000. However, the actual loss can sometimes exceed $1,000, as shown in the attached image, where it can be $1,030 or even $1,100. I'm not sure what the reason for this discrepancy is.

Also, I would like this function to work with CFDs. Is it possible to help me achieve this?

Now, when I try to open a trade on NAS100, I receive error 134, which means there's not enough money. I'm basically entering with a 1% risk.

I've created a simple script in the attachments. Please add a line on the chart named "StopLoss" and another named "TakeProfit." Drag the script to open a buy trade.

In summary:
1. I want the function to provide me with the lot size to ensure that my loss is exactly 1% according to the "Risk" variable, and not more or less.
2. I want the function to work with various CFDs like NAS100, US30, Gold, Oil, etc.

Thank you.
 

Attachments

  • TestBuy.mq4
    3 KB · Views: 1
  • 1.png
    1.png
    51 KB · Views: 3
  • 2.png
    2.png
    53.3 KB · Views: 3
  • 3.png
    3.png
    44.2 KB · Views: 3
  • 4.png
    4.png
    4 KB · Views: 3

Enivid

Administrator
Staff member
Nov 30, 2008
18,620
1,367
144
Odesa
www.earnforex.com
Hello Endivid,
First, thank you for moving the topic to the appropriate section. The function works with Forex but not perfectly 100%. For instance, if you have $100,000 and a 1% risk, this means that if the stop-loss is hit, you would lose $1,000. However, the actual loss can sometimes exceed $1,000, as shown in the attached image, where it can be $1,030 or even $1,100. I'm not sure what the reason for this discrepancy is.

Also, I would like this function to work with CFDs. Is it possible to help me achieve this?

Now, when I try to open a trade on NAS100, I receive error 134, which means there's not enough money. I'm basically entering with a 1% risk.

I've created a simple script in the attachments. Please add a line on the chart named "StopLoss" and another named "TakeProfit." Drag the script to open a buy trade.

In summary:
1. I want the function to provide me with the lot size to ensure that my loss is exactly 1% according to the "Risk" variable, and not more or less.
2. I want the function to work with various CFDs like NAS100, US30, Gold, Oil, etc.

Thank you.
As for the small difference in the Forex position size, perhaps, there is some time delay between the calculation and the execution? Because, $30 on a 10-lot trade is just 3 points of difference, which is very easy to get if there is some delay between the calculation and entry.

As for the CFD calculation, there are some differences in the formula. You can consult our Position Sizer's source code to see what you need to account for.
 
  • 👍
Reactions: root7x7