Compilation Error: ')' - open parenthesis expected

 

Hello everyone,

I am creating a custom indicator for MetaTrader 5, but I am encountering the following compilation error:

'Backtest Balance Display.mq5' Backtest Balance Display.mq5 1 1 ')' - open parenthesis expected Backtest Balance Display.mq5 39 49 1 errors, 0 warnings 2 1

Purpose of the Code:

This indicator aims to display backtest balance data on the chart. Specifically, it reads daily balance data from a CSV file resulting from a backtest and displays that balance corresponding to the chart's bars. This allows traders to visually confirm the performance of the backtest.

Code in Question:

mql5

       //+------------------------------------------------------------------+
//|                                     Backtest Balance Display.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

// Indicator to be displayed in a separate window
#property indicator_separate_window

// Set the number of indicator buffers to 1
#property indicator_buffers 1

// Set the indicator color and width
#property indicator_color1 Red
#property indicator_width1 2

// Array (buffer) to store balance data
double BalanceBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
    // Link the buffer to the indicator
    SetIndexBuffer(0, BalanceBuffer, INDICATOR_DATA);

    // Set the short name of the indicator
    IndicatorSetString(INDICATOR_SHORTNAME, "Backtest Balance Display");

    // Set the path of the CSV file where balance data is stored
    string file_path = "balance_history.csv";

    // Open the CSV file in read mode
    int file_handle = FileOpen(file_path, FILE_READ | FILE_CSV | FILE_ANSI);
    if (file_handle == INVALID_HANDLE)
    {
        // Error message if the file could not be opened
        Print("Failed to open file: ", file_path);
        return (INIT_FAILED);
    }

    // Skip the header row of the CSV file (header is "Date" and "Balance")
    FileReadString(file_handle); // Header Date
    FileReadString(file_handle); // Header Balance

    // Loop until the end of the file or until the number of bars is exceeded
    int i = 0;
    while (!FileIsEnding(file_handle) && i < Bars)
    {
        // Read the date and balance from the file
        string date_str = FileReadString(file_handle);
        double balance = FileReadNumber(file_handle);

        // Convert the date string to datetime type (time is fixed at 00:00:00)
        datetime dt = StringToTime(date_str + " 00:00:00");

        // Get the position of the bar corresponding to the date
        int pos = iBarShift(NULL, 0, dt, true);

        // If the position is valid, store the balance data in the buffer
        if (pos >= 0)
        {
            BalanceBuffer[pos] = balance;
            i++;
        }
    }

    // Close the file
    FileClose(file_handle);

    // Return success of initialization
    return (INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
    // Return value to continue from the previous calculation
    return (rates_total);
  }
//+------------------------------------------------------------------+

About the Error:

I have checked the location indicated by the error message but could not find any mismatched parentheses. Could anyone provide guidance on what might be causing this issue?

Additional Steps Taken:

  1. Code Structure Review:

    • Removed all comments to check if they were causing issues, but the error persists.
  2. Alternative Debugging Methods:

    • Created a new MQ5 file and copied the code in stages to identify the problematic part, but the error remains.
  3. IDE Settings and Environment Check:

    • Reset MetaEditor settings to default and cleared the editor cache, but no change.
  4. Environmental Dependence Issues:

    • Tested the code on a different PC and reinstalled MetaTrader 5, but the error persists.

If anyone has encountered a similar issue or can offer insight into possible causes and solutions, it would be greatly appreciated. Additionally, if there are similar indicators or resources that could serve as a reference, please let me know.

Thank you for your assistance.

Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • 2024.06.07
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
 
Good morning
Look at

Bars - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

The function expects parameters between ()... hence the compilation message
Documentation on MQL5: Timeseries and Indicators Access / Bars
Documentation on MQL5: Timeseries and Indicators Access / Bars
  • www.mql5.com
Returns the number of bars count in the history for a specified symbol and period. There are 2 variants of functions calls. Request all of the...
 

Hi

Change the line 53 to this:     

while (!FileIsEnding(file_handle) && i < iBars(Symbol(), PERIOD_CURRENT))

In mql5 there is no function called “Bars” – you need to use full function: iBars(Symbol(), PERIOD_CURRENT)

Best regards