Code migration guide

MQL4 to thinkScript migration guide for studies and AddOrder

MQL4 indicator calculations can move into a thinkScript study. Expert Advisor OnTick execution, terminal files, and OrderSend ticket management have to be removed or reduced to chart signals and AddOrder simulated orders. Loops and mutable buffers become definitions, recursion, or fold expressions.

Generate a thinkScript draft, then save and test it in the thinkorswim editor as the intended study or strategy.

Conversion map

What changes between MetaTrader 4 and thinkorswim

The migration is a model translation. Use this map to decide where the source behavior belongs before writing target syntax.

Source and target platform conversion map
ConcernSource modelTarget modelMigration decision
LifecycleExpert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate.Declarative expressions are evaluated over chart data; studies, strategies, scans, and watchlist columns have different contexts.Choose when each MetaTrader 4 calculation should run in thinkorswim; matching syntax alone does not preserve timing.
Market data and historyPrice series use terminal arrays and shift-based access; indicators commonly expose calculated values through buffers.Definitions and plots form time series and access prior values with bracket indexing or recursive definitions.Recheck bar indexing, warm-up requirements, timeframe requests, and behavior on an unfinished bar.
Indicators and chart outputCustom indicators bind buffers, while built-in indicator functions can return values directly for a requested shift.Studies use def, plot, painting, and built-in study functions to render chart analysis.Map calculations before recreating plots, labels, colors, and platform-specific drawing objects.
Orders and positionsClassic trade functions such as OrderSend work with order tickets, lots, prices, stops, and a shared order pool.Strategies use AddOrder to add simulated orders to charts; platform conditional orders are a separate workflow.Define fill timing, sizing, pyramiding or hedging, stops, and position ownership for the target platform.
Native validationCompile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact.Paste into thinkScript Editor, save it as the correct study or strategy, add it to a chart, and inspect plots or the strategy report.Treat generated thinkScript as a draft until it passes the target editor, chart, and backtest workflow.

Code example

Moving-average cross: MQL4 to thinkScript

The MQL4 fragment calculates a current-bar crossover. The thinkScript study plots that condition on its chart series; it contains no AddOrder simulation and sends no live order. Align bar timing, session data, and moving-average inputs before comparing the displayed signals.

MQL4 source fragment
double fast0 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 0);
double fast1 = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
double slow0 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 0);
double slow1 = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE, 1);
bool longSignal = fast1 <= slow1 && fast0 > slow0;
thinkScript target fragment
def fast = Average(close, 10);
def slow = Average(close, 20);
plot longSignal = fast crosses above slow;

Workflow

A four-step migration path

Keep generation, target-platform validation, and behavior comparison as separate stages.

  1. 1

    Inventory the source behavior

    Separate chart calculations from OnTick execution, terminal files, and OrderSend tickets in the .mq4 Expert Advisor or custom indicator. A thinkScript study can keep the chart subset.

  2. 2

    Choose the target artifact

    Save the result as a thinkScript study or strategy. Studies, strategies, scans, and watchlist columns use different contexts. AddOrder belongs on a strategy and adds simulated chart orders.

  3. 3

    Rewrite through target APIs

    Rewrite loops and mutable buffers as definitions, recursion, or fold expressions. Prior values use bracket indexing. Leave OrderSend lots, prices, stops, and the shared order pool out of the study.

  4. 4

    Validate behavior natively

    Paste into thinkScript Editor, save it as the correct study or strategy, add it to a chart, and inspect plots or the strategy report.

Manual decisions

Decisions code replacement cannot make

  • Identify the chart-calculation subset that fits a thinkScript study or strategy.
  • Rewrite loops and mutable buffers as supported definitions, recursion, or fold expressions.
  • Discard MT4 trade execution features that thinkScript chart strategies do not provide.

Limitations

What the draft cannot prove

  • thinkScript evaluates declarative expressions over chart data. MQL4 OnTick execution, terminal files, and OrderSend ticket management are removed or reduced to chart signals and AddOrder simulations.
  • AddOrder adds simulated orders to a thinkorswim chart. Platform conditional orders are a separate workflow, so an MT4 ticket with lots, prices, and stops needs its own decision.
  • Pineify can generate and revise a thinkScript draft. Saving it in thinkScript Editor and checking plots or the strategy report on a chart remain the validation steps.

Use cases

When this conversion is useful

  • Move an MT4 custom indicator into a thinkorswim study.
  • Visualize an EA signal without attempting to port its live execution engine.
  • Adapt simple MT4 alerts and plots to thinkorswim chart tools.

FAQ

MQL4 to thinkScript questions

Build the thinkScript draft with its native model in view

Bring the complete source and your artifact, timing, data, and order requirements. Validate the result in thinkorswim.