MQL4 string syntax

MQL4 string backslash escape

Inside an MQL4 string constant, write \\ when the final text needs one literal \. A single backslash starts an escape sequence, so an unknown sequence can trigger warning 25 and produce an undefined result.

Open the Coding Agent, then choose MQL4 from the language menu.

One literal backslashMQL4
string folder = "Reports\\2026";
Print(folder); // Reports\2026

Incorrect source

string path = "Reports\daily.csv";

Correct source

string path = "Reports\\daily.csv";

MQL4 escape sequence reference

The source column shows what belongs between the string's double quotes. The compiler converts each supported sequence into the output character.

Needed outputWrite in MQL4String example
One backslash\\"Reports\\daily.csv"
Double quote\""Status: \"ready\""
New line\n"first\nsecond"
Horizontal tab\t"symbol\tprice"
Carriage return\r"first\r\nsecond"

Complete .mq4 example

This script covers paths, embedded quotes, tabs, line endings, and a sandboxed file write. The filename passed to FileOpen is relative to the MT4 file sandbox.

A correctly escaped absolute path is still not permission to access that path. MQL4 file functions remain inside the terminal sandbox.

EscapeExamples.mq4MQL4
#property strict

void OnStart()
  {
   string report_path = "Reports\\daily.csv";
   string quoted_text = "Status: \"ready\"";
   string table_header = "symbol\tstatus\r\n";

   Print(report_path);
   Print(quoted_text);
   Print(table_header);

   int handle = FileOpen(report_path, FILE_WRITE|FILE_CSV);
   if(handle != INVALID_HANDLE)
     {
      FileWrite(handle, "EURUSD", "ready");
      FileClose(handle);
     }
   else
     {
      Print("FileOpen failed. Error ", GetLastError());
     }
  }

Three string patterns worth checking

Most escape bugs come from paths, quoted text, or formatted output. Check the exact runtime text you want before editing the source literal.

Folder and file paths

Double every separator that appears inside the source literal. For FileOpen, keep the path relative to the permitted sandbox.

string path = "Reports\\daily.csv";

Quotes inside text

Escape an embedded double quote so the compiler does not treat it as the end of the string.

string status = "Order state: \"ready\"";

Rows and columns

Use tab and line-ending sequences when the final text needs structured rows rather than visible slash characters.

string rows = "symbol\tstatus\r\nEURUSD\tready";
Focused MQL4 repair

Repair the string without rewriting the program

Paste one self-contained .mq4 file into Pineify and ask for a narrow escape-sequence repair. The MQL4 Coding Agent keeps the source editable and can use single-file MetaEditor diagnostics to locate remaining compiler issues.

Prompt to use

Fix only invalid MQL4 string escapes in this file. Preserve behavior, return one self-contained .mq4 file, and explain each changed literal.

Open MQL4 Coding Agent

A clean static check does not prove runtime behavior, file access, broker compatibility, or live-trading safety.

Pineify MQL4 Coding Agent with MQL4 selected and an editable MetaTrader 4 code artifact

Check the result in four steps

  1. 1

    Find each literal backslash

    Inspect the text between double quotes and identify every backslash that should appear in the final value.

  2. 2

    Double the backslash in source code

    Replace each literal backslash with two backslashes in the MQL4 string constant.

  3. 3

    Check the surrounding API rule

    For FileOpen, use a relative path inside the MetaTrader 4 file sandbox rather than an arbitrary absolute path.

  4. 4

    Compile and test the result

    Run MetaEditor diagnostics, then execute the code in the intended MT4 environment to verify the runtime value and file location.

Frequently asked questions

How do I escape a backslash in an MQL4 string?

Write two backslashes in the source string. MQL4 reads \\ as one literal backslash in the resulting value.

How do I write a folder path in MQL4?

Double each backslash inside the string literal. For example, Reports\\daily.csv produces Reports\daily.csv at runtime.

How do I include double quotes inside an MQL4 string?

Use a backslash before each embedded double quote. For example, "Status: \"ready\"" produces Status: "ready".

Why does FileOpen reject an absolute Windows path?

MQL4 file operations are restricted to the terminal file sandbox. Use a relative filename or subfolder under MQL4\Files, Tester\Files, or the shared folder selected by FILE_COMMON.

What happens when MQL4 sees an unknown escape sequence?

The MQL4 reference says the result is undefined when a backslash is followed by an unsupported character. MetaEditor lists this as compiler warning 25, so the source should be corrected even if compilation continues.

Can Pineify check an MQL4 string repair?

Yes. Select MQL4 in the Coding Agent, paste the single-file .mq4 source, and request a focused repair. Pineify can run single-file MetaEditor diagnostics, but you still need to compile and test the result in your target MT4 installation.

Last verified against the official MQL4 reference on . Always compile and run the final source in the MetaTrader 4 build and environment you plan to use.

Fix the exact MQL4 string that is failing

Keep the rest of the program intact, review the changed literal, and run MetaEditor diagnostics before testing it in MT4.

Open Coding Agent