I wrote a function to calculate position loss, but the code needs improvement
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
It's better to use SYMBOL_TRADE_TICK_VALUE in the formula because different brokers can have different tick values due to variations in liquidity providers
double StopLossValueCalc(double lot, double sl_points) { // Get tick value for the current symbol (value per pip in the account currency per 1 lot) double tickValue = SymbolInfoDouble(_Symbol, SYMBOL_TRADE_TICK_VALUE); double stopLossValue = sl_points * tickValue; maxLoss = -(lot * stopLossValue); return maxLoss; }
example use:
StopLossValueCalc(1.0, 300); // the stop loss in base currency for 300 points of risk
I've already written the first part of the code. I think that it's the first ever open-source EA that calculates the StopLoss in Cash!!!!! Later we can add NetLossPerOnePrecentLotPerPoint into the code also, but at first, we should improve its calculation functions therefore it never gives us a wrong answer. It calculates everything perfectly right now when one side of the forex currency pairs is USD like EURUSD. But when USD is not on any sides then it calculates a wrong answer. Does anybody have any ideas to improve it? Here is the code: