Balance_Reset – library MetaTrader 5

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 #include directive:


#include <BalanceReset.mqh>   // Include the balance reset library

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()

    InitBalance();  // Initialize the starting balance
    return INIT_SUCCEEDED;


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()

    CheckBalanceAndReset();  // Check and reset balance based on thresholds


//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)

    PrintBalanceResetResults();  // Output balance reset results to the log


Key Functions

  1. 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:

input double profit_threshold = 8.0;  // Profit threshold, default is 8%
input double loss_threshold = -6.0;   // Loss threshold, default is -6%


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:

void InitBalance()

    initial_balance = AccountInfoDouble(ACCOUNT_BALANCE);  // Storing the initial balance


3. Balance Reset Logic

Alternative:   VolatilityPivot_Signal - indicator MetaTrader 5
  • 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 TesterWithdrawal 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 TesterDeposit function restores the balance to the initial amount when the loss threshold is triggered.

Example:

void CheckBalanceAndReset()

    double current_balance = AccountInfoDouble(ACCOUNT_BALANCE);
    if (initial_balance == 0) 
        initial_balance = current_balance; // First tick, store the initial balance
    
    double profit_percentage = (current_balance - initial_balance) / initial_balance * 100.0;

    if (profit_percentage >= profit_threshold) 
        double withdrawal_amount = current_balance - initial_balance;
        if (TesterWithdrawal(withdrawal_amount)) 
            successful_resets++;
            ArrayResize(reset_times, ArraySize(reset_times) + 1);
            reset_times[ArraySize(reset_times) - 1] = TimeCurrent();
            Print("Profit reached. Balance has been reset to the initial value.");
        
    

    if (profit_percentage <= loss_threshold) 
        double deposit_amount = initial_balance - current_balance;
        if (TesterDeposit(deposit_amount)) 
            unsuccessful_resets++;
            ArrayResize(reset_times, ArraySize(reset_times) + 1);
            reset_times[ArraySize(reset_times) - 1] = TimeCurrent();
            Print("Loss reached. Balance has been reset to the initial value.");
        
    

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:

void PrintBalanceResetResults()

    PrintFormat("Number of successful profit resets: %d", successful_resets);
    PrintFormat("Number of successful loss resets: %d", unsuccessful_resets);
    
    for (int i = 1; i < ArraySize(reset_times); i++)
    
        int days_between_resets = (reset_times[i] - reset_times[i-1]) / 86400; // 86400 seconds in one day
        PrintFormat("Days between reset %d and reset %d: %d days", i, i + 1, days_between_resets);
    


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.

Alternative:   GannSwing - indicator MetaTrader 5



📈 ROBOTFX MetaTrader Expert Advisors and Indicators to maximize profits and minimize the risks