BestInterval - MetaTrader 5 Library | MT5 EA Download - MetaTrader 5 Resources





Market patterns depend on intraday or intraweek intervals. This is why it makes sense to limit your trading system by time.
For example, there are scalping trading systems that specialize in trading during Asian and American sessions. It is also feasible to disable the trading system during periods of high volatility.
Accordingly, the question arises: How to find the best time for your TS trade? Typically, this interval can be searched intuitively or with a standard tester. Let's forget intuition as a non-technical case. As for using a standard tester to solve such problems, this automatically results in a significant increase in the number of runs and therefore optimization and money (cloud). However, there are still some areas for improvement.
It is also possible that there is really a good TS. However, its positive characteristics are hidden because it has no temporal filter. After all, if a suitable trading range could be found for this TS, it would not be discarded but would trigger additional research...
This cross-platform library allows the system to find optimal trading ranges very quickly and as efficiently as possible.
Example for use with annotations:
#include// https://www.mql5.com/en/code/16006 #include// Calculate the best trading range Blank solution initialization ( constant integer ) { Best Interval Best Interval; // So we created an object to calculate the best trading range Best interval.Set(); // Placed transaction history Constant integer amount interval = 3 ; // How many worst intervals should be deleted for ( integer i = 0 ; i < amount interval; i++) if (BestInterval.DeleteWorseInterval()) // If we deleted something print (BestInterval.ToString()); // Print the acquired transaction data else rest ; // Otherwise, we will exit }
Now, let's see the practical application of this library in scalping EA.
Let's launch the 24/7 scalper in the MT5 tester. We will see the following

View single pass log
Number of intervals to remove = 0 00:00:00 - 23:59:59 : Profit = 20121.00 , Total = 1138 , PF = 1.68 , Avg = 17.68 Summary: 00:00:00 - 23:59:59 : Profit = 20121.00 , Total = 1138 , PF = 1.68 , mean = 17.68
This is raw channel data - no gaps have been discarded.
Let us now provide further log entries in succession, where the effect of removing weak intervals will gradually become apparent.
Delete a range:
Number of intervals to remove = 1 00:00:00 - 10:08:04 : Profit = 6830.00 , Total = 203 , PF = 3.10 , Average = 33.65 11:05:24 - 23:59:59 : Profit = 17086.00 , Total = 819 , PF = 1.93 , Avg = 20.86 Summary: 00:00:00 - 23:59:59 : Profit = 23916.00 , Total = 1022 , PF = 2.10 , mean = 23.40
The highlighted string is the transaction interval. We can see that there are no transactions between 10am and 11am. The interval is removed.
Delete two intervals:
Number of intervals to remove = 2 00:00:00 - 10:08:04 : Profit = 6830.00 , Total = 203 , PF = 3.10 , Average = 33.65 11:05:24 - 12:21:06 : Profit = 2767.00 , Total = 112 , PF = 2.46 , Average = 24.71 13:02:59 - 23:59:59 : Profit = 15915.00 , Total = Chapter 664 , PF = 2.17 , Average = 23.97 Summary : 00:00:00 - 23:59:59 : Profit = 25512.00 , Total = Chapter 979 , PF = 2.36 , Average = 26.06
Finally delete three intervals
Number of intervals to remove = 3 00:00:00 - 10:08:04 : Profit = 6830.00 , Total = 203 , PF = 3.10 , Average = 33.65 11:05:24 - 12:21:06 : Profit = 2767.00 , Total = 112 , PF = 2.46 , Avg = 24.71 13:02:59 - 17:26:06 : Profit = 7476.00 , Total = Chapter 348 , PF = 1.98 , Avg = 21.48 17th : 47:36 - 23:59:59 : Profit = 9640.00 , Total = 288 , PF = 3.31 , Avg = 33.47 Summary: 00:00 : 00 - 23:59:59 : Profit = 26713.00 , Total = Chapter 951 , PF = 2.57 , Average = 28.09
Note how the TS parameter improves with each deleted interval.
Let's apply the calculated filter to TS

We can visually compare the two graphs before and after. Maybe, for the sake of clarity, someone will make a proper animated GIF...
As a result, the TS parameters are significantly improved almost for free: we spent as little effort and time as possible.
The above example is based on adding only two strings to the initial EA
#Define optimal interval_ONTESTER // The optimization criterion is the profit of the optimal interval. #include// Calculate the best trading range
The highlighted string allows you not to write anything else in the EA. It also specifies custom optimization criteria so that the optimizer no longer has to use the old-fashioned way to find good intervals. This criterion immediately returns calculated TS results within a good interval and shows better TS potential than when using standard optimization.
To apply the found intervals when calculating, set Best Interval Action = true in its input.

All TS should be examined extensively, including optimal trading range calculations. Therefore, this tool is a must-have.
It is reasonable to connect to all current, past and future TS (using the two strings above) to search for the best trading range. This implementation greatly facilitates the creation of profitable TS or improving the robustness of existing TS
Please share your experience before and after applying the library (as mentioned above) in the form of screenshots and appropriate MT4/5 single channel log snippets.
Attachment download
📎 files.mqh (0.85 KB)
📎 deal.mqh (2.28 KB)
📎interval.mqh (3.49 KB)
📎 bestinterval.mqh (22.97 KB)
📎 bestinterval_example.mq5 (0.38 KB)
Source: MQL5 #22710
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •