Balance Reset
This software component for MetaTrader 5 is built to enhance the capabilities of your trading environment. This library provides a collection of modular, reusable code. It is utilized by developers to organize common functions, allowing for the integration of complex logic across multiple Expert Advisors, indicators, or scripts without the need for code duplication.
How to Setup and Use Balance Reset
1. Storage: Place library files in the MQL/Libraries directory to ensure they are accessible to your projects.
2. Implementation: Include the library in your code using the #import directive, ensuring you match the exact function names and parameters.
3. Compilation: Ensure the library is present in the directory before you compile your main EA or script, as the compiler links them during this phase.
4. Management: Keep libraries organized in sub-folders if you manage many custom functions to maintain a clean project structure.
Frequently Asked Questions
Q: What is a library file used for? A: Libraries store reusable code modules, allowing you to centralize common logic used by multiple EAs or indicators.
Q: Is a library executable? A: No, libraries are non-executable files containing functions; they must be imported into an EA, indicator, or script to function.
Q: Can I update a library while the platform is running? A: You should compile your EA or script after updating a library to ensure the latest code changes are integrated.
Description & Settings
Update:
All the changes and additions to this library are outlined at the end of the code. This framework is flexible and can be adapted to your specific needs, whether it’s adding more complex logic for trading restrictions, scheduling trades, or fine-tuning performance based on optimization results.
Feel free to customize it further for your routine tasks!
This library is designed for testing Expert Advisors (EAs) in MetaTrader 5 with a specific focus on proprietary firm trading requirements. The goal is to simulate trading environments where the trader must meet certain profit and loss thresholds to pass a firm's challenge. The EA adjusts the account balance by resetting it to the initial value whenever specified profit or loss percentages are reached, mimicking the rules of many prop firms.
Here is the complete structure for the Expert Advisor (EA) using the BalanceReset.mqh library. This setup integrates the balance reset logic directly into the EA while maintaining clean separation of functions through the directive:
Key Functions
Adjustable Profit and Loss Thresholds
The library allows you to adjust the profit and loss thresholds that trigger balance resets during the backtesting process. These thresholds can be changed using input parameters, which makes it easier to customize the testing conditions.
Example:
2.
Initial Balance Initialization
This function stores the initial account balance at the beginning of the test. It only runs on the first tick to capture the initial starting balance.
Example:
3.
Balance Reset Logic
The core of the library checks the current balance against the initial balance to calculate the percentage of profit or loss. If the profit exceeds the specified threshold (e.g., 8%) or the loss exceeds its threshold (e.g., -6%), the balance is reset to the initial value.
Profit reset:
The function is used to withdraw the excess amount to bring the balance back to the initial value when the profit threshold is reached.
Loss reset:
The function restores the balance to the initial amount when the loss threshold is triggered.
Example:
4.
Result Logging
After the test is completed, this function outputs the number of successful resets (for both profit and loss) and the number of days between each reset. This provides insights into how frequently the balance resets occurred during the test.
Example:
Conclusion
This library helps in simulating a trading environment that adheres to common proprietary trading firm requirements. By resetting the balance when predefined profit and loss thresholds are met, it allows traders to test their strategies more effectively and analyze the performance of their EA based on prop firm rules.
Description of Code Changes and Additions:
The new code includes several enhancements and additions compared to the old code. Below is a detailed explanation of what has been added and modified:
New Input Parameters:
max_loss and min_won :
Purpose:
These input parameters allow you to set the maximum allowable loss ( max_loss ) and the minimum number of successful profit resets required ( min_won ). They provide more control over the optimization conditions.
Additional Variables:
reset_status[] :
Purpose:
An array added to store the status ("Profit reset" or "Loss reset") of each balance reset event.
stopOptimization :
Purpose:
A flag used to indicate when the optimization should be stopped based on certain conditions.
badResult :
Purpose:
A flag to mark the optimization result as unfavorable, which can be used to influence the outcome in OnTester() .
Modifications in CheckBalanceAndReset() :
Recording Reset Status:
Purpose:
When a profit reset occurs, the status is recorded in the reset_status[] array.
Handling Loss Resets with Optimization Stop Condition:
Purpose:
After a loss reset, the code now increments unsuccessful_resets , records the status, sets stopOptimization to true , and calls CheckStopCondition() to determine if the optimization should be halted.
New Function
CheckStopCondition() :
Purpose:
This function checks if stopOptimization is true and, if so, marks the result as bad and stops the current optimization pass using TesterStop() .
New Function
OnTester() :
Purpose:
This function provides a custom criterion for the optimization process. It checks if the number of successful resets meets the minimum requirement or if the result is bad. It calculates the difference between successful and unsuccessful resets and returns this value unless the difference is negative, in which case it returns a highly unfavorable result to influence the optimizer.
Enhancements
in PrintBalanceResetResults() :
Purpose:
The function now not only outputs the number of resets but also details each reset event, including the date, status, and the number of days between resets. This provides a more comprehensive log of the reset activities.
Variables Initialization and Usage:
Initial Balance Check:
Purpose:
Ensures that the initial_balance is set correctly at the first tick.
Profit Percentage Calculation:
Purpose:
Calculates the profit or loss percentage relative to the initial balance.
Summary of Additions:
Control Over Optimization Process:
The code now includes mechanisms to stop the optimization process based on specific conditions, such as exceeding the maximum number of unsuccessful resets or not achieving the minimum number of successful resets.
Enhanced Logging and Tracking:
The addition of reset_status[] and detailed logging in PrintBalanceResetResults() allows for better tracking of reset events and their outcomes.
Integration with Optimizer via OnTester() :
By implementing OnTester() , the script can communicate results back to the optimization engine, influencing the selection of parameter sets based on custom criteria. These changes enhance the functionality of the script by providing better control over trading operations during optimization, more detailed logging of events, and the ability to influence the optimization process based on custom-defined criteria.