Skip to content
All library documents

MQL4 Expert Advisors: Data Access, New-Bar Detection, and Debugging

Article MQL5 articles

Summary

This primer covers practical MQL4 Expert Advisor tasks: checking free margin before opening a trade, reading historical price arrays, copying data from another symbol or timeframe, and reporting errors. It describes Alert, Comment, and Print for diagnostics, and explains that GetLastError returns and clears the latest error code, so it should be captured when an operation fails.

The article compares three ways to detect a new bar: monitoring the total bar count, checking volume, and comparing the current bar’s opening time with a saved value. It calls the time comparison the most reliable, noting that history loading can disrupt a bar-count check and rapidly arriving ticks can undermine a volume check. It also shows how CSV writing can replace a file’s contents or append records after seeking to the end. These are platform-specific programming notes, not trading signals; the margin threshold shown is explicitly tied to tests and should not be treated as a general live-account rule.

Key ideas

  • MQL4 historical price arrays place the latest observation at index zero.
  • The article recommends checking available margin before submitting an order and reading the error code if the operation fails.
  • GetLastError clears the stored error, so capture its result once when diagnosing a failure.
  • Comparing the current bar’s opening time with a saved timestamp is presented as the most reliable new-bar check.
  • CSV output can overwrite a file or append data when the file is opened for reading and writing and positioned at the end.

Tags

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.