Your topic has been moved to the section: MQL4 and MetaTrader 4
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
Problem solved, OnTester also works correctly in MT4, but it doesn't contain all the statistics that MT5 has. for example STAT_MIN_MARGINLEVEL, STAT_SHARPE_RATIO, STAT_RECOVERY_FACTOR ,... are not available and results are always zero.
double OnTester() { double sharp_ratio =sharp_ratio_calculator(); return sharp_ratio; // Return Sharpe Ratio for optimization } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double sharp_ratio_calculator() { int totalTrades = OrdersHistoryTotal(); if(totalTrades <= 1) // Ensure there are trades to analyze return 0; double returns[], totalProfit = 0.0, meanReturn = 0.0, variance = 0.0; // Loop through all historical orders for(int i = 0; i < totalTrades; i++) { if(OrderSelect(i, SELECT_BY_POS, MODE_HISTORY)) // Select trade from history { double profit = OrderProfit(); double balanceBefore = AccountBalance() - totalProfit; // Simulate balance before this trade double tradeReturn = balanceBefore > 0 ? profit / balanceBefore : 0.0; totalProfit += profit; // Update cumulative profit returns[i] = tradeReturn; // Store return for this trade } } // Calculate mean of returns for(int i = 0; i < totalTrades; i++) meanReturn += returns[i]; meanReturn /= totalTrades; // Calculate variance (for standard deviation) for(int i = 0; i < totalTrades; i++) variance += MathPow(returns[i] - meanReturn, 2); double stdDev = MathSqrt(variance / totalTrades); // Risk-free return (assumed 0 for simplicity) double riskFreeReturn = 0; // Calculate Sharpe Ratio double sharpeRatio = (meanReturn - riskFreeReturn) / stdDev; return sharpeRatio; }
Here is a piece of code that calculates Sharpe Ratio
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
is always zero. does OnTester() works correctly in MT4?
if this problem persists, I think the only solution is converting the code to MT5, do you have any other suggestion?