Code migration guide
thinkScript to MQL4 migration guide for studies and OrderSend
A thinkScript study can become an MQL4 custom indicator once its declarative series run through callbacks and buffers. A strategy needs a separate Expert Advisor: AddOrder is the signal specification, and live trades use OrderSend tickets with lots, prices, stops, and broker-error handling.
Generate an MQL4 draft, then compile it in MetaEditor and test it in MetaTrader 4.
Conversion map
What changes between thinkorswim and MetaTrader 4
The migration is a model translation. Use this map to decide where the source behavior belongs before writing target syntax.
| Concern | Source model | Target model | Migration decision |
|---|---|---|---|
| Lifecycle | Declarative expressions are evaluated over chart data; studies, strategies, scans, and watchlist columns have different contexts. | Expert Advisors and indicators respond to predefined callbacks such as OnInit, OnTick, and OnCalculate. | Choose when each thinkorswim calculation should run in MetaTrader 4; matching syntax alone does not preserve timing. |
| Market data and history | Definitions and plots form time series and access prior values with bracket indexing or recursive definitions. | Price series use terminal arrays and shift-based access; indicators commonly expose calculated values through buffers. | Recheck bar indexing, warm-up requirements, timeframe requests, and behavior on an unfinished bar. |
| Indicators and chart output | Studies use def, plot, painting, and built-in study functions to render chart analysis. | Custom indicators bind buffers, while built-in indicator functions can return values directly for a requested shift. | Map calculations before recreating plots, labels, colors, and platform-specific drawing objects. |
| Orders and positions | Strategies use AddOrder to add simulated orders to charts; platform conditional orders are a separate workflow. | Classic trade functions such as OrderSend work with order tickets, lots, prices, stops, and a shared order pool. | Define fill timing, sizing, pyramiding or hedging, stops, and position ownership for the target platform. |
| Native validation | Paste into thinkScript Editor, save it as the correct study or strategy, add it to a chart, and inspect plots or the strategy report. | Compile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact. | Treat generated MQL4 as a draft until it passes the target editor, chart, and backtest workflow. |
Code example
Moving-average cross: thinkScript to MQL4
The thinkScript study plots the crossover, while the MQL4 fragment only defines a Boolean condition using iMA shifts 0 and 1. Neither places an order. Put the MQL4 calculation in the correct callback, add any required output buffers, and align current-bar confirmation and session data before comparing signals.
def fast = Average(close, 10);
def slow = Average(close, 20);
plot longSignal = fast crosses above slow;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;Workflow
A four-step migration path
Keep generation, target-platform validation, and behavior comparison as separate stages.
- 1
Inventory the source behavior
List def and plot formulas, bracket or recursive series, and AddOrder calls in the thinkScript study or strategy. Studies, strategies, scans, and watchlist columns are different contexts.
- 2
Choose the target artifact
Choose an MQL4 custom indicator for chart output or an Expert Advisor for trade execution. Indicators bind buffers. Expert Advisors respond to callbacks such as OnInit and OnTick.
- 3
Rewrite through target APIs
Add buffer management, warm-up, and per-bar calculation around the thinkScript formulas. Treat AddOrder as the signal specification for OrderSend entries, exits, lots, prices, and stops.
- 4
Validate behavior natively
Compile the .mq4 file in MetaEditor, attach it in MetaTrader 4, and test the correct EA or indicator artifact.
Manual decisions
Decisions code replacement cannot make
- Choose an MQL4 indicator for chart output or an Expert Advisor for trade execution.
- Add explicit buffer management, warm-up, and per-bar calculation around thinkScript formulas.
- Use AddOrder only as a signal specification when designing MT4 entries, exits, lots, and stops.
Limitations
What the draft cannot prove
- thinkScript evaluates declarative expressions and reads prior values with bracket indexing or recursive definitions. An MQL4 indicator exposes calculated values through buffers and reads built-in values at a requested shift.
- AddOrder adds a simulated order on the thinkorswim chart. An Expert Advisor places OrderSend tickets with lots, prices, stops, and a shared order pool, and has to account for broker errors.
- Pineify can generate and revise an MQL4 draft. Compilation in MetaEditor and attaching the Expert Advisor or indicator in MetaTrader 4 remain the checks for that artifact.
Use cases
When this conversion is useful
- Port a thinkorswim study to an MT4 custom indicator.
- Rebuild a thinkScript strategy as an MQL4 Expert Advisor.
- Add MT4 alerts or execution to a previously chart-only calculation.
FAQ
thinkScript to MQL4 questions
Official references
- thinkScript reference overview
Charles Schwab
- Creating thinkScript strategies
Charles Schwab
- MQL4 event handling functions
MetaQuotes
- MQL4 OrderSend reference
MetaQuotes
Related converters
Browse all convertersBuild the MQL4 draft with its native model in view
Bring the complete source and your artifact, timing, data, and order requirements. Validate the result in MetaTrader 4.