Case study · Pine Script to MT4 and MT5 conversion

HyperTrend PRO: a five-engine TradingView system, same signals on the same bars

A mature TradingView indicator ported to native MetaTrader 4 and 5 so that it signals on the same bars as the original, not merely looks similar.

Pine v5 reference implementation · .mq4 and .mq5 with compiled binaries · one 40-input block on both platforms · two PDF manuals

Pine in708 lines
MQL out3,099 lines
State arrays replacing var27
Inputs, identical MT4 and MT540
Alert conditions9

Counted from the delivered sources

The brief

Take a mature TradingView indicator, five interacting calculation engines, a nine-condition alert set, a live dashboard and an ATR risk overlay, and make it run natively on both MetaTrader 4 and MetaTrader 5 with the same signals on the same bars.

That last clause is the whole job. Most Pine ports "work" in the sense that they compile and draw something plausible. The failure mode clients care about is subtler: the MetaTrader version fires half a bar early, or on a different bar entirely, and nobody notices until real money is on it.

What was delivered

PlatformDeliverableSize
TradingViewPine v5 source, the reference implementation708 lines
MetaTrader 4.mq4 source plus compiled .ex41,472 lines
MetaTrader 5.mq5 source plus compiled .ex51,627 lines

The two MQL builds expose an identical 40-input block, so a client can move settings between platforms without relearning anything. Both use their platform's native idioms rather than a lowest-common-denominator wrapper: MT5 uses cached indicator handles with CopyBuffer, MT4 uses direct indicator calls.

The five engines

  • Adaptive SuperTrend. Pine's ta.supertrend() has no MetaTrader equivalent, so the ratcheting ATR band logic and its direction state machine were rebuilt from scratch, support ratcheting upward only and resistance downward only, so trend flips land on the same bars as TradingView.
  • Five-factor confluence scoring. MACD histogram rising above zero, EMA(150) above EMA(250), ADX(14) above 20, tick volume above its 20-period average (degrading to a pass where no volume feed exists), and a Hull MA(55) rising over two bars. The 0 to 5 score renders as stars and a Weak to Ultra label.
  • Contrarian RSI. An EMA-smoothed RSI feeding a Wilder true-range chain and a 4.236 Fibonacci trailing band with a five-branch state machine.
  • WaveTrend pullback. A crossover gated at plus or minus 60 for oversold and overbought re-entry dots.
  • ATR risk. The stop 1.5 to 2.2 ATR beyond the signal bar, three targets at 1, 2 and 3 times the stop distance, a ratcheting trailing stop and live hit detection.

Non-repainting by construction: every raw flip is gated on a confirmed bar and alerts fire only on the closed bar, so a signal that appears never later disappears.

What Pine hides and MQL exposes

Index-space collision is the bug class that sinks most ports. Pine and recursive calculations want oldest-first indexing; MQL plot buffers want newest-first. Mixing them produces an indicator that looks right on a static chart and is wrong in motion. The port carries an explicit, named conversion layer and iterates chronologically, converting per bar.

int ShiftToCI(int shift, int bars) { return bars - 1 - shift; }
int CIToShift(int ci, int bars)    { return bars - 1 - ci; }

Pine's var persistence became 27 explicit state arrays: SuperTrend value, direction and bands, trailing stops, triggers, counters, scores, entry and target levels, the RSI chain, the WaveTrend chain, the Hull MA, all resized as history grows.

UI primitives that do not exist. A Pine table became layered rectangle-plus-label object pairs positioned with absolute pixel maths from live chart dimensions, because MetaTrader labels have no background colour. Boxes became trend lines plus text objects; fills became two buffer lines plus rectangles; nine alertcondition() calls became one alert multiplexer with per-bar de-duplication so a live chart cannot spam.

Platform traps handled: MT4's eight-buffer ceiling (the design uses exactly 8 of 8), the MT5 first-attach race where handles report "not calculated yet" (return and retry next tick rather than render a half-empty chart), object addressing through the chart ID, and incremental recalculation so only the previously forming bar is reprocessed once confirmed.

Disclosed, not hidden

Four Pine-only features were deliberately not ported: MACD-gradient candle colouring, swing market-structure labels, a dashboard size selector, and webhook JSON alert formatting, because MetaTrader has no webhook layer. The client knew before work started, and the developer README carries an explicit "differences from the Pine version" section.

Delivery

  • Pine v5, .mq4 and .mq5 source plus compiled binaries.
  • Two complete PDF manuals, one for the MetaTrader builds and one for TradingView: installation, the five engines, every parameter, the alert conditions and an FAQ.
  • A long-form technical article documenting the architecture, and captioned chart screenshots across forex, metals, indices, crypto and equities.

A trader who has spent months trusting a TradingView indicator does not want a re-interpretation. The value is not typing the formulas into a new language; it is knowing which Pine behaviours cannot be reproduced honestly, saying so up front, and proving parity on the ones that can.

Chart captures are from the delivered MetaTrader build. This is analytical software, not investment advice; no indicator predicts future prices.

Have a Pine script to port?

Send the source and I tell you exactly what will match, what cannot, and why, before you order. Pine to MT4/MT5 conversion starts at $130 on Upwork.