Indicator · Pine Script v6 · On the chart

TVT MA Crossover with Stop Loss

by namuan

This indicator plots fast and slow moving averages using either EMA or SMA directly on the main chart to identify trend direction and crossovers.

View on GitHub

GitHub stars318on the repository
Pine versionv6current
Lines of code55
Settings7inputs
LicenceMIT
Last update2 months ago18 Aug 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

55 lines of Pine Script v6 from tv-scripts/tvt-ma-crossover-stop.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

tvt-ma-crossover-stop.pine
//@version=6
indicator(title="[TVT] MA Crossover with Stop-Loss", shorttitle="TVT-MA-STP", overlay=true, timeframe="", timeframe_gaps=true)

// === INPUTS ===
// MA Parameters
src = input(close, title="Source")
use_ema = input.bool(true, title="Use EMA over SMA?")
fast_len = input.int(50, title="Fast MA Length", minval=1)
slow_len = input.int(200, title="Slow MA Length", minval=1)

// ATR Stop Parameters
atr_len = input.int(14, title="ATR Length", minval=1)
atr_mult = input.float(2.5, title="ATR Multiplier", minval=0.1)
hhv_len = input.int(10, title="HHV Period", minval=1)

// === CALCULATIONS ===
// Calculate MAs
fast_ma = use_ema ? ta.ema(src, fast_len) : ta.sma(src, fast_len)
slow_ma = use_ema ? ta.ema(src, slow_len) : ta.sma(src, slow_len)

// Calculate ATR Trailing Stop
atr_val = ta.atr(atr_len)
prev_stop = ta.highest(high - atr_mult * atr_val, hhv_len)
trailing_stop = ta.cum(1) < 16 ? close :
     close > ta.highest(high - atr_mult * atr_val, hhv_len) and close > close[1] ?
     ta.highest(high - atr_mult * atr_val, hhv_len) : prev_stop

// === STRATEGY LOGIC ===
bullish = fast_ma > slow_ma
bearish = fast_ma < slow_ma

// === PLOTTING ===
// Plot MAs
plot(fast_ma, "Fast MA", color.new(color.blue, 0), 2)
plot(slow_ma, "Slow MA", color.new(color.orange, 0), 2)

// Plot trailing stop
plot(trailing_stop, "Trailing Stop", color.new(color.red, 0), 2, plot.style_linebr)

// Background color for trend direction
bgcolor(bullish ? color.new(color.green, 90) : bearish ? color.new(color.red, 90) : na)

// Plot crossover signals
plotshape(ta.crossover(fast_ma, slow_ma), "Bullish Crossover", shape.labelup, location.belowbar, color.green, text="BUY", textcolor=color.white)
plotshape(ta.crossunder(fast_ma, slow_ma), "Bearish Crossover", shape.labeldown, location.abovebar, color.red, text="SELL", textcolor=color.white)

// Plot stop hits
plotshape(ta.crossunder(close, trailing_stop) and bullish, "Stop Hit", shape.triangledown, location.abovebar, color.red, size=size.small)
plotshape(ta.crossover(close, trailing_stop) and bearish, "Stop Hit", shape.triangleup, location.belowbar, color.green, size=size.small)

// === ALERTS ===
alertcondition(ta.crossover(fast_ma, slow_ma), "Bullish MA Crossover", "Fast MA crossed above Slow MA")
alertcondition(ta.crossunder(fast_ma, slow_ma), "Bearish MA Crossover", "Fast MA crossed below Slow MA")
alertcondition(ta.crossunder(close, trailing_stop), "Long Stop Hit", "Price crossed below trailing stop")
alertcondition(ta.crossover(close, trailing_stop), "Short Stop Hit", "Price crossed above trailing stop")

Overview

TVT MA Crossover with Stop Loss calculates an ATR-based trailing stop line derived from the highest high minus a multiplied ATR value over a configurable lookback period. Green and red background shading highlights trend bias, while shapes indicate crossover signals and trailing stop breaches. Customizable parameters include moving average lengths, MA calculation type, ATR length, and multiplier.

Details

Published by

namuan on GitHub

Type

Indicator, Pine Script v6

Draws

On the price chart

Category

Trend, Moving averages, Volatility & bands

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Topics

trading-strategiesmarket-analysisdtalestock-market-analysistelegram-botearnings-calendaroptions-trading

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Sourcesourceclosesrc
Use EMA over SMA?booltrueuse_ema
Fast MA Lengthint50fast_len
Slow MA Lengthint200slow_len
ATR Lengthint14atr_len
ATR Multiplierfloat2.5atr_mult
HHV Periodint10hhv_len
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of NVDA Trend Ribbon

Bollinger Bands Force

by harryguiacorn
219 stars

This indicator modifies standard Bollinger Bands to measure directional trend force based on line alignment. It calculates a simple moving average basis along with upper and lower volatility bands. When all three lines move upward or downward simultaneously, i

Indicator v5 Volatility & bandsTrend
Updated 1 year ago · None stated GitHub repository of Bollinger Bands Force

Adaptive MA Slope

by capissimo
114 stars

This indicator calculates Kaufman's Adaptive Moving Average (KAMA) on a chosen price source and determines trend direction based on the slope across a configurable lookback window. Signals can be filtered by short-term volatility expansion using ATR comparison

Indicator v4 Moving averagesTrend
Updated 1 year ago · None stated GitHub repository of Adaptive MA Slope

Adaptive Price Zone

by mihakralj
99 stars

The Adaptive Price Zone indicator calculates volatility bands around a double-smoothed exponential moving average baseline directly on the price chart. It calculates internal smoothing factors using the square root of the user-specified period, double-smoothin

Indicator v6 Volatility & bandsTrend
Updated 4 weeks ago · Apache-2.0 GitHub repository of Adaptive Price Zone

More indicators

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers