IndicatorName = "VStop"; AddInput("I", Inputs.Candle); AddSeries("vstop", DrawAs.Line, Color.Red, false); AddSeries("Up", DrawAs.Custom, Color.Green); AddSeries("Dn", DrawAs.Custom, Color.Red); AddParameter("length", 20); AddParameter("mult", 2); AddGlobalVariable("is_uptrend", Types.Boolean, false); AddGlobalVariable("is_uptrend_prev", Types.Boolean, false); AddGlobalVariable("max_", Types.Double, 0.0); AddGlobalVariable("min_", Types.Double, 0.0); AddGlobalVariable("c", Types.Int, 0); var atr_ = ATR(I, length); double max1 = Math.Max(max_, I.Close[0]); double min1 = Math.Min(min_, I.Close[0]); is_uptrend_prev = is_uptrend; double stop = is_uptrend_prev ? max1 - mult * atr_[0] : min1 + mult * atr_[0]; double vstop_prev = CurrentIndex > length ? vstop[1] : 0.0; double vstop1 = is_uptrend_prev ? Math.Max(vstop_prev, stop) : Math.Min(vstop_prev, stop); is_uptrend = (I.Close[0] - vstop1) >= 0; bool is_trend_changed = is_uptrend != is_uptrend_prev; max_ = is_trend_changed ? I.Close[0] : max1; min_ = is_trend_changed ? I.Close[0] : min1; vstop = is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1; c += is_uptrend != is_uptrend_prev ? -c : 1; if (is_uptrend) { Up[0] = vstop[0]; if (c > 0) Up.DrawLine(); } else { Dn[0] = vstop[0]; if (c > 0) Dn.DrawLine(); }
study("SSL channel", overlay=true) period=input(title="Period", defval=10) len=input(title="Period", defval=10) smaHigh=sma(high, len) smaLow=sma(low, len) Hlv = na Hlv := close > smaHigh ? 1 : close < smaLow ? -1 : Hlv[1] sslDown = Hlv < 0 ? smaHigh: smaLow sslUp = Hlv < 0 ? smaLow : smaHigh plot(sslDown, linewidth=2, color=red) plot(sslUp, linewidth=2, color=lime)