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:
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.
There are errors in my lot size calculation function.
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.
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.