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);

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; }

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 tried using ChatGPT, but it was giving me strange results.