TradingView is a great place to test ideas. MetaTrader is where many traders actually run them. Moving a Pine Script strategy into an MQL5 Expert Advisor is less about rewriting indicators and more about matching assumptions that Pine hides by default.
Match the bar rule first
Ask one question before any indicator code: when does the strategy decide?
If Pine uses strategy.entry on bar close, the EA should act when a new bar opens with the previous bar fully known. If Pine evaluates on every tick inside the bar, the EA needs OnTick logic and you must accept path dependent fills. Mixing a closed bar research process with an every tick live bot is a common reason live results look nothing like the chart.
Handle multi timeframe data carefully
request.security is convenient in Pine. In MQL5 you typically CopyRates or CopyBuffer on another period. Two rules keep you honest:
- If research assumed a closed higher timeframe candle, wait until that candle is closed before reading it.
- Do not rebuild the higher timeframe series from incomplete lower bars in a way that peeks at future ticks.
Lookahead in research shows up as perfect entries that never appear in a live log.
Model costs the way the broker behaves
Pine strategy testers often fill at the signal price. Live MT5 fills include spread, possible slippage, stop level, and freeze level. Put those into your acceptance tests:
- Minimum free margin and max spread gates before entry.
- Deviation or max slip on market orders.
- Logging of requested price versus fill price on every deal.
If the edge is smaller than realistic costs, do not spend weeks polishing the port.
Translate alerts into a protocol
Many people keep the Pine script on TradingView and only send alerts into MT5. That can work if the contract is strict:
- JSON with strategy id, side, symbol, bar time, and a nonce or alert id.
- Shared secret verification on the receiver.
- Deduplication so retries do not open a second position.
- A max alerts per hour cap inside the EA.
Verify with a short forward window
Run the Pine strategy and the EA on the same symbol for one to two weeks with identical size rules. Compare entry times, not only net profit. If entries drift by a bar, fix timing before you tune parameters.
Custom Trading Bots builds MT4 and MT5 EAs and TradingView bridges with these guards in place. https://customtradingbots.com/?utm_source=devto&utm_campaign=pinetomql5
Top comments (1)
I would separate the signal timestamp from the fill timestamp in that comparison. With default Pine strategy settings, a market order created at bar close normally fills on the next available tick, usually the next bar open. process_orders_on_close can change that. Before porting, freeze that setting and calc_on_every_tick, then compare the closed bar used for the decision, order creation time, and actual fill time separately. A one-bar difference between a signal marker and a fill marker can be expected rather than a timing bug.