Reviewable MQL4 patterns

MQL4 code examples with safe boundaries

A useful MQL4 example shows its program type, data timing, history guards, return-value checks, and remaining runtime assumptions. Copy the pattern, not an unexplained promise about the strategy.

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

ClosedBarSignal.mq4.mq4
#property strict

input int FastPeriod = 10;
input int SlowPeriod = 30;

datetime last_bar_time = 0;

bool IsNewBar()
  {
   datetime bar_time = iTime(NULL, 0, 0);
   if(bar_time == 0 || bar_time == last_bar_time)
      return(false);

   last_bar_time = bar_time;
   return(true);
  }

int OnInit()
  {
   if(FastPeriod < 1 || SlowPeriod <= FastPeriod)
      return(INIT_PARAMETERS_INCORRECT);

   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   if(!IsNewBar() || Bars <= SlowPeriod + 2)
      return;

   double fast_closed = iMA(NULL, 0, FastPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
   double slow_closed = iMA(NULL, 0, SlowPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

   PrintFormat("Closed-bar EMA values: fast=%.5f slow=%.5f",
               fast_closed, slow_closed);
  }
  • Complete program structure
  • Closed-bar timing guard
  • Indicator return values
  • No hidden dependencies

Separate calculation examples from execution examples

An indicator calculation can be correct while order ownership, price selection, volume, stop distance, or retry behavior remains undefined. Keep the first example read-only until the signal values and timing are understood.

The closed-bar sample uses shift 1 and runs once after a new bar begins. That is one explicit timing choice, not a universal rule. Intrabar strategies need a different state model.

Example typeEvidence it should includeRemaining test
ScriptOnStart and bounded taskCorrect chart and file context
IndicatorBuffers, OnCalculate, history guardVisual values and refresh behavior
Expert AdvisorOnTick or timer state and order filtersTester, broker, and demo execution

Read every symbol, timeframe, and shift argument

MQL4 indicator and series functions often accept NULL or zero for the current chart. That convenience can hide assumptions when code is moved to another symbol or timeframe.

Make the data contract visible in comments or inputs. Check whether shift 0 or shift 1 is intended, whether enough history is loaded, and how a zero or negative return value is handled.

  • Validate input ranges in OnInit
  • Guard the minimum number of bars
  • Name variables by timing, such as fast_closed
  • Check API failure returns before using data

A high-probability label is not code evidence

A mean-reversion or crossover example demonstrates implementation mechanics. It does not establish a statistical edge. Probability claims require a defined dataset, costs, execution model, validation method, and unseen test period.

Use examples to inspect source structure. Evaluate strategy claims separately through reproducible tests and keep the result conditional on those assumptions.

Review the complete example

This EA skeleton validates inputs, detects a new chart bar, reads moving averages from the last closed bar, and logs the values. It deliberately omits order placement so the calculation can be reviewed independently.

This sample calculates and logs values only. Any added trading action needs explicit order, volume, stop, ownership, and error-handling rules.

ClosedBarSignal.mq4.mq4
#property strict

input int FastPeriod = 10;
input int SlowPeriod = 30;

datetime last_bar_time = 0;

bool IsNewBar()
  {
   datetime bar_time = iTime(NULL, 0, 0);
   if(bar_time == 0 || bar_time == last_bar_time)
      return(false);

   last_bar_time = bar_time;
   return(true);
  }

int OnInit()
  {
   if(FastPeriod < 1 || SlowPeriod <= FastPeriod)
      return(INIT_PARAMETERS_INCORRECT);

   return(INIT_SUCCEEDED);
  }

void OnTick()
  {
   if(!IsNewBar() || Bars <= SlowPeriod + 2)
      return;

   double fast_closed = iMA(NULL, 0, FastPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);
   double slow_closed = iMA(NULL, 0, SlowPeriod, 0, MODE_EMA, PRICE_CLOSE, 1);

   PrintFormat("Closed-bar EMA values: fast=%.5f slow=%.5f",
               fast_closed, slow_closed);
  }
Where Pineify fits

Move from the rule to editable MQL4 source

Pineify can generate or revise one self-contained .mq4 file with MQL4-specific reference context. Its independent static checker returns MetaEditor diagnostics so you can repair source issues before testing in your own MT4 environment.

Open MQL4 Coding Agent

Static diagnostics do not prove runtime behavior, backtest results, broker compatibility, or live-trading safety.

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

A practical workflow

  1. 1

    Identify the program type

    Confirm whether the example is a script, indicator, or Expert Advisor before copying a handler.

  2. 2

    Mark every assumption

    Record symbol, timeframe, shift, history, spread, and order ownership choices.

  3. 3

    Compile the smallest version

    Check the calculation and logging before adding trade execution.

  4. 4

    Test the completed behavior

    Use Strategy Tester and a demo account with recorded broker and execution settings.

Frequently asked questions

Where should I put MQL4 code examples?

Put EAs under MQL4/Experts, indicators under MQL4/Indicators, and scripts under MQL4/Scripts, then compile the .mq4 source in MetaEditor.

Can I copy an MQL4 example into any program type?

No. Event handlers and available behavior depend on whether the source is an EA, indicator, or script.

Why does the example use shift 1?

Shift 1 is the latest closed bar. It avoids using a still-changing bar when the stated rule is closed-bar evaluation.

Does an MQL4 sample EA prove a strategy works?

No. It demonstrates source structure. Strategy performance requires reproducible testing with data, costs, and execution assumptions.

Can Pineify adapt an MQL4 sample?

Yes. Select MQL4, provide the source and exact requested change, then review the resulting single-file artifact and diagnostics.

Last verified against the linked official references on . Compile and test the final source in the MetaTrader 4 build, broker, symbol, and account environment you plan to use.

Continue with one reviewable MQL4 file

Define the requirement, inspect the generated source, repair diagnostics, and test the behavior in your own MT4 environment.

Open Coding Agent