SSL Channel: TradingView (Pine Script) to MT5 (MQL5)
Short answer
SSL is two simple moving averages, of the highs and of the lows, swapped by a state Hlv: +1 after a close above the high average, -1 after a close below the low average. Both averages are finite windows, so Pine v6 and MQL5 return identical values from the first drawn bar.
The SSL Channel draws two simple moving averages, one of the highs and one of the lows, and swaps which is labelled "up" and "down" according to a state called Hlv: +1 after a close above the high average, -1 after a close below the low average, otherwise unchanged. A cross of the two drawn lines is the signal. Both averages are finite windows, so a Pine Script v6 and an MQL5 version return identical values from the first drawn bar.
How the SSL Channel is calculated
One input, len (default 10). Per bar:
smaHigh = SMA(high, len)andsmaLow = SMA(low, len).hlv = close > smaHigh ? 1 : close < smaLow ? -1 : hlv[1]. The state only changes when a close leaves the channel; while price closes between the two averages it remembers the last breakout direction. It starts at 0 before the first breakout.sslUp = hlv < 0 ? smaLow : smaHighandsslDown = hlv < 0 ? smaHigh : smaLow. In a bullish state the "up" line is the high average and sits above the "down" line; in a bearish state the lines swap, so the visible cross is the momenthlvchanges sign.
Because hlv can only be 0, 1 or -1 and depends on the previous value only through "unchanged", the indicator is path dependent in a limited way: it stays in a state until a close leaves the channel. Two versions fed the same bars make the same decisions, so the lines match exactly once len bars are available.
Pine Script v6 version
hlv is a var int so that the "unchanged" branch reads the previous bar's value. Markers appear on the bar where hlv first becomes 1 or -1.
//@version=6
indicator("SSL Channel, written out", "SSL", overlay = true)
len = input.int(10, "Length", minval = 1)
float smaHigh = ta.sma(high, len)
float smaLow = ta.sma(low, len)
// Hlv: +1 after a close above the high average, -1 after a close below the
// low average, otherwise unchanged. It decides which average is on top.
var int hlv = 0
hlv := close > smaHigh ? 1 : close < smaLow ? -1 : hlv
float sslUp = hlv < 0 ? smaLow : smaHigh
float sslDown = hlv < 0 ? smaHigh : smaLow
bool crossUp = hlv == 1 and hlv[1] != 1
bool crossDown = hlv == -1 and hlv[1] != -1
plot(sslUp, "SSL Up", color.green, 2)
plot(sslDown, "SSL Down", color.red, 2)
plotshape(crossUp, "Bullish cross", shape.circle, location.belowbar, color.green, size = size.tiny)
plotshape(crossDown, "Bearish cross", shape.circle, location.abovebar, color.red, size = size.tiny)
alertcondition(crossUp, "SSL bullish", "SSL Channel turned bullish")
alertcondition(crossDown, "SSL bearish", "SSL Channel turned bearish")During the first len - 1 bars the averages are na, both comparisons are false and hlv stays 0; the lines are na because they are built from the averages.
MQL5 version
The MQL5 indicator computes the two averages with a small Sma() over the oldest-first high[] and low[] arrays and keeps Hlv in a calculation buffer, one value per bar, so recalculating the current bar on each tick starts from the previous bar's state. Two line buffers and two arrow buffers (dot, Wingdings 159) stand in for the two plots and two plotshape calls.
//+------------------------------------------------------------------+
//| SSLChannel_TV.mq5 - SMA(high)/SMA(low) channel with Hlv state |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 4
#property indicator_label1 "SSL Up"
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrGreen
#property indicator_width1 2
#property indicator_label2 "SSL Down"
#property indicator_type2 DRAW_LINE
#property indicator_color2 clrRed
#property indicator_width2 2
#property indicator_label3 "Bullish cross"
#property indicator_type3 DRAW_ARROW
#property indicator_color3 clrGreen
#property indicator_label4 "Bearish cross"
#property indicator_type4 DRAW_ARROW
#property indicator_color4 clrRed
input int InpLen = 10; // Length
double SslUp[], SslDown[], UpDot[], DownDot[]; // drawn
double Hlv[]; // per-bar state: +1 / -1 / 0
int OnInit()
{
if(InpLen < 1)
return INIT_PARAMETERS_INCORRECT;
SetIndexBuffer(0, SslUp, INDICATOR_DATA);
SetIndexBuffer(1, SslDown, INDICATOR_DATA);
SetIndexBuffer(2, UpDot, INDICATOR_DATA);
SetIndexBuffer(3, DownDot, INDICATOR_DATA);
SetIndexBuffer(4, Hlv, INDICATOR_CALCULATIONS);
for(int p = 0; p < 4; p++)
PlotIndexSetDouble(p, PLOT_EMPTY_VALUE, EMPTY_VALUE);
PlotIndexSetInteger(2, PLOT_ARROW, 159);
PlotIndexSetInteger(3, PLOT_ARROW, 159);
IndicatorSetString(INDICATOR_SHORTNAME, "SSL(" + (string)InpLen + ")");
return INIT_SUCCEEDED;
}
double Sma(const double &a[], const int i, const int len)
{
if(i < len - 1) return EMPTY_VALUE;
double s = 0.0;
for(int k = 0; k < len; k++) s += a[i - k];
return s / len;
}
int OnCalculate(const int rates_total, const int prev_calculated,
const datetime &time[], const double &open[], const double &high[],
const double &low[], const double &close[], const long &tick_volume[],
const long &volume[], const int &spread[])
{
int start = prev_calculated > 0 ? prev_calculated - 1 : 0;
for(int i = start; i < rates_total; i++)
{
double smaHigh = Sma(high, i, InpLen);
double smaLow = Sma(low, i, InpLen);
UpDot[i] = EMPTY_VALUE; DownDot[i] = EMPTY_VALUE;
if(smaHigh == EMPTY_VALUE)
{
SslUp[i] = EMPTY_VALUE; SslDown[i] = EMPTY_VALUE; Hlv[i] = 0;
continue;
}
// Hlv keeps its previous value until a close leaves the channel
double prevHlv = (i > 0) ? Hlv[i - 1] : 0;
Hlv[i] = (close[i] > smaHigh) ? 1 : (close[i] < smaLow ? -1 : prevHlv);
SslUp[i] = (Hlv[i] < 0) ? smaLow : smaHigh;
SslDown[i] = (Hlv[i] < 0) ? smaHigh : smaLow;
if(Hlv[i] == 1 && prevHlv != 1) UpDot[i] = low[i];
if(Hlv[i] == -1 && prevHlv != -1) DownDot[i] = high[i];
}
return rates_total;
}iMA handles with MODE_SMA on PRICE_HIGH and PRICE_LOW would give the same averages; the loop is used here because it keeps the indicator in one file and makes the i - k window visible next to the Pine code.
Differences you will see on the chart
| Topic | TradingView | MetaTrader 5 |
|---|---|---|
| Warm-up | Lines start on bar len - 1. | Same bar; EMPTY_VALUE before. |
| Values | Identical: both lines are plain SMAs and hlv makes the same decision on the same close. No seeding or history-depth effect. | |
| Initial state | hlv = 0 until the first close outside the channel; with 0 the "up" line is smaHigh. | Same; Hlv is 0 on warm-up bars and Hlv < 0 is false. |
| Markers | plotshape circles below/above the bar. | Arrow buffers with a dot symbol at the low/high. |
| Realtime bar | The current bar's hlv can change with every tick while the close is near an average; the lines may swap and swap back. | Identical behaviour. |
Closed bars never repaint.
What the Stratlate converter does with this script
Converted with the Stratlate engine, the script above is graded CLEAN. ta.sma(high, len) and ta.sma(low, len) become pine_sma(high, ...) and pine_sma(low, ...) over the price arrays; the var int hlv becomes a per-bar array initialised to 0 and carried forward, with hlv[1] read through pine_hist; the two plots become DRAW_LINE buffers, and the two plotshape(..., shape.circle) calls become DRAW_ARROW buffers with Wingdings code 159. alertcondition becomes pine_alertcond, which raises an MT5 alert once per bar on the live bar. The report's only note is the informational PM221 about minval having no MQL input field; the emitted OnInit enforces the minimum instead. Compile the result and backtest it before use.
Using it in MetaTrader 5
- Save as
SSLChannel_TV.mq5inMQL5\Indicators, compile (F7, no warnings) and attach it with Length 10. - From an EA,
iCustom(_Symbol, _Period, "SSLChannel_TV", 10); buffer 4 (Hlv) is the cleanest signal: 1 means bullish, -1 bearish. A change between bar 2 and bar 1 (the last closed bar) is a cross. - Use bar 1 rather than bar 0: on the open bar a close that hovers around an average swaps the lines repeatedly.
- For a terminal alert add
Alert()whenUpDotorDownDoton the current bar is non-empty, guarded by the bar's open time so it fires once.
Pitfalls
- Resetting
Hlvto 0 on every bar. The "unchanged" branch is the whole point; without it the lines swap every time price closes inside the channel and the chart shows constant crosses. - Keeping
Hlvin astatic int. It works on history and breaks on the live bar, where each tick re-enters the loop and the state can be carried from the current bar into itself. Use a buffer indexed by bar. - Mixing up which average is "up". In the bullish state the up line is
smaHigh. Swapping the ternary branches inverts every signal while the lines still look plausible. - Using close-based
iMAhandles. The channel is built from highs and lows;PRICE_CLOSEaverages produce a narrower channel and different breakout bars. ArraySetAsSerieson half the arrays. Ifhigh[]is flipped butHlv[]is not,i - 1points at the wrong bar in one of them. Keep all arrays oldest-first.
FAQ
Why do the SSL lines match TradingView exactly when EMA-based indicators do not?
Both lines are simple moving averages over a fixed window of highs and lows, and the state only switches on a close outside the channel. Nothing carries a seed from the first bar of history, so the same bars produce the same output.
What does Hlv stand for and why is it 0 at the start?
It is the high/low state variable of the indicator: 1 after a close above the high average, -1 after a close below the low average. Before the first breakout it has no information, so it is 0, which the ternary treats as "not bearish" and draws the high average as the up line.
Is the signal the cross of the lines or the change of Hlv?
They are the same event. The lines are the two averages assigned by hlv, so they can only cross when hlv changes sign. Reading Hlv from buffer 4 avoids comparing floating-point line values.
Convert your own version
Have a variant of this indicator, or any other TradingView script? Paste it and get MQL5 that is checked against the original on the same price history, with every construct that could not be carried over reported.
Open the converter