Indicator · Pine Script v5 · On the chart

Snag - Swing Trade Long Term V2

by dearvn· code credited to thinhdangho

This indicator identifies long-term trend directions and momentum shifts using weighted moving averages.

View on GitHub

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code188
Settings2inputs
LicenceNone statedfile: MPL-2.0
Last update4 years ago25 Jul 2022

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

188 lines of Pine Script v5 from swingtrade-v2.txt, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

swingtrade-v2.txt
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © thinhdangho

//@version=5
indicator("Snag - SWING TRADE LONG TERM V2", overlay=true)

pctUp = input.float(title='Top Up %', defval=5.0)
pctDown = input.float(title='Bottom Down %', defval=-5.0)

f_RoundUp(number, decimals) =>
    factor = math.pow(10, decimals)
    math.ceil(number * factor) / factor
    
f_nDecimals(_in) =>
    n  = int(na), s = str.tostring(_in), p = str.pos(s, ".")
    n := na(str.tonumber(s)) ? int(na) : na(p) ? 0 :
     str.length(str.substring(s, p + 1))

_n = f_nDecimals(close)

// Trend Up Strong
// Close Cross Up EMA 13, or Close Cross Up EMA 50 or Close Cross Up EMA 200
// Mark as Begin Up Trend
// Wait for EMA 13 Cross Up EMA 48 🡪 Confirm Strong Uptrend 🡪 BUY
// EMA 48 CROSS UP EMA 200 🡪 STRONGER UPTREND
// Trend Down Strong
// Close Cross Down EMA 13, or Close Cross Down EMA 50 or Close Cross Down EMA 200
// Mark as Begin Down Trend
// Wait for EMA 13 Cross Down EMA 48 🡪 Confirm Strong Downtrend 🡪 SELL
// EMA 48 CROSS DOWN EMA 200 🡪 STRONGER DOWNTREND

wma13 = ta.wma(close, 13)
plot(wma13, title="WMA13", color=color.blue)

wma48 = ta.wma(close, 48)
plot(wma48, title="WMA48", color=color.yellow)

wma200 = ta.wma(close, 200)
plot(wma200, title="WMA200", color=color.white)

crossUpWMA13 = ta.crossover(close, wma13)
crossDownWMA13 = ta.crossunder(close, wma13)
crossUpWMA48 = ta.crossover(close, wma48)
crossDownWMA48 = ta.crossunder(close, wma48)
crossUpWMA200 = ta.crossover(close, wma200)
crossDownWMA200 = ta.crossunder(close, wma200)

crossUpWMA13_48 = ta.crossover(wma13, wma48)
crossDownWMA13_48 = ta.crossunder(wma13, wma48)
crossUpWMA48_200 = ta.crossover(wma48, wma200)
crossDownWMA48_200 = ta.crossunder(wma48, wma200)

var price_wmacrossup_arr = array.new_float()
var price_wmacrossdown_arr = array.new_float()

if crossUpWMA48_200
    price_wmacrossdown_arr := array.new_float()
    array.push(price_wmacrossup_arr, close)
else if crossDownWMA48_200
    price_wmacrossup_arr := array.new_float()
    array.push(price_wmacrossdown_arr, close)
        
price_up = 0.0
price_down = 0.0
if array.size(price_wmacrossup_arr) > 0
    price_up := array.get(price_wmacrossup_arr, array.size(price_wmacrossup_arr)-1)
else if array.size(price_wmacrossdown_arr) > 0
    price_down := array.get(price_wmacrossdown_arr, array.size(price_wmacrossdown_arr)-1)

var bool isUpTrend = false
var bool isDownTrend = false

if crossUpWMA13 and isUpTrend == false
    isUpTrend := true
    isDownTrend := false
else if crossUpWMA48 and isUpTrend == false
    isUpTrend := true
    isDownTrend := false
else if crossUpWMA200 and isUpTrend == false
    isUpTrend := true
    isDownTrend := false

if crossDownWMA13 and isDownTrend == false
    isUpTrend := false
    isDownTrend := true
else if crossDownWMA48 and isDownTrend == false
    isUpTrend := false
    isDownTrend := true
else if crossDownWMA200 and isDownTrend == false
    isUpTrend := false
    isDownTrend := true
    
var int trendUp = 0
var int trendDown = 0

txtMarkTrend = ''
txtOrder = 'N/A'
if isUpTrend
    if crossUpWMA13_48
        trendUp := 1
        trendDown := 0
    if crossUpWMA48_200
        trendUp := 2
        trendDown := 0
        
if isDownTrend
    if crossDownWMA13_48
        trendUp := 0
        trendDown := 1
    if crossDownWMA48_200
        trendUp := 0
        trendDown := 2
        
if isUpTrend
    txtMarkTrend := 'Begin Up Trend'
    if trendUp == 1
        txtMarkTrend := 'Confirm Strong Uptrend'
        txtOrder := 'BUY'
    else if trendUp == 2
        txtMarkTrend := 'STRONGER UPTREND'
        txtOrder := 'BUY'

else if isDownTrend
    txtMarkTrend := 'Begin Down Trend'
    if trendDown == 1
        txtMarkTrend := 'Confirm Strong Downtrend'
        txtOrder := 'SELL'
    else if trendDown == 2
        txtMarkTrend := 'STRONGER DOWNTREND'
    txtOrder := 'SELL'
    
if barstate.islast
    var table panel = table.new("bottom_right", 6, 30)

    table.cell(panel, 0, 1, "TREND", bgcolor = color.yellow, text_color=color.black, width=18, text_halign=text.align_left)
    table.cell(panel, 1, 1, txtMarkTrend, bgcolor = color.yellow, text_color=color.black, width=30, text_halign=text.align_left)    
    table.cell(panel, 0, 2, "ORDER", bgcolor = color.yellow, text_color=color.black, width=18, text_halign=text.align_left)
    table.cell(panel, 1, 2, txtOrder, bgcolor = color.yellow, text_color=color.black, width=30, text_halign=text.align_left)    
    
    if price_up > 0
        price = high - price_up
        pct = f_RoundUp(price/price_up*100, 2)
        table.cell(panel, 0, 3, "Price Up", bgcolor = color.yellow, text_color=color.black, width=18, text_halign=text.align_left)
        table.cell(panel, 1, 3, str.format("${0}", price_up), bgcolor = color.yellow, text_color=color.black, width=30, text_halign=text.align_left)    
    
        txtTop = ''
        if pct >= pctUp
            txtTop := 'TOP'
        table.cell(panel, 0, 4, "Up Change", bgcolor = color.yellow, text_color=color.black, width=18, text_halign=text.align_left)
        table.cell(panel, 1, 4, str.format("${0}    {1}%    {2}", price, pct, txtTop), bgcolor = color.yellow, text_color=color.black, width=30, text_halign=text.align_left) 
        
    else if price_down > 0
        price = low - price_down
        pct = f_RoundUp(price/price_down*100, 2)
        table.cell(panel, 0, 3, "Price Down", bgcolor = color.yellow, text_color=color.black, width=18, text_halign=text.align_left)
        table.cell(panel, 1, 3, str.format("${0}", price_down), bgcolor = color.yellow, text_color=color.black, width=30, text_halign=text.align_left)    
    
        txtBottom = ''
        if pct <= pctDown
            txtBottom := 'BOTTOM'
        table.cell(panel, 0, 4, "Down Change", bgcolor = color.yellow, text_color=color.black, width=18, text_halign=text.align_left)
        table.cell(panel, 1, 4, str.format("${0}    {1}%   {2}", price, pct, txtBottom), bgcolor = color.yellow, text_color=color.black, width=30, text_halign=text.align_left)    
    
// Alert
var time_wmacrossup_arr = array.new_int()
var time_wmacrossdown_arr = array.new_int()

if trendUp == 1 or trendUp == 2
    time_wmacrossdown_arr := array.new_int()
    if array.size(time_wmacrossup_arr) == 0
        array.push(time_wmacrossup_arr, time)
else if trendDown == 1 or trendDown == 2
    time_wmacrossup_arr := array.new_int()
    if array.size(time_wmacrossdown_arr) == 0
        array.push(time_wmacrossdown_arr, time)
        
is_alert_wmacrossup = false
is_alert_wmacrossdown = false

if array.size(time_wmacrossup_arr) > 0
    if time == array.get(time_wmacrossup_arr, array.size(time_wmacrossup_arr)-1)
        is_alert_wmacrossup := true
else if array.size(time_wmacrossdown_arr) > 0
    if time == array.get(time_wmacrossdown_arr, array.size(time_wmacrossdown_arr)-1)
        is_alert_wmacrossdown := true
        
alertcondition(is_alert_wmacrossdown, title='ENTER PUT', message='Get out Call and Enter PUT')
alertcondition(is_alert_wmacrossup, title='ENTER CALL', message='Get out Put and Enter CALL')

Overview

Snag - Swing Trade Long Term V2 plots 13, 48, and 200-period WMAs directly on the price chart. The script evaluates price crossovers relative to each average as well as moving average crossovers (13/48 and 48/200) to classify emerging trend states and strength levels. Users can configure top and bottom percentage offset parameters via the inputs menu.

Details

Published by

dearvn on GitHub

Code credit

thinhdangho, as written in the script's header

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages

Markets

Any symbol

Timeframes

H4 and above

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

futurestrading-strategiestradingview

Settings

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

SettingTypeDefaultVariable
Top Up %float5.0pctUp
Bottom Down %float-5.0pctDown
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

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

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
257 stars

Donald SPX - Options Trading is an overlay indicator designed for intraday SPX analysis. It plots a 200-period Weighted Moving Average (WMA) on the price chart while evaluating multi-timeframe market structure across Daily, 15-minute, 5-minute, and 2-minute ba

Indicator v5 Moving averagesTrend Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald SPX - Options Trading

60K均線

by TaichiS
219 stars

This multi-timeframe indicator calculates and plots 20-period and 60-period simple moving averages from the 60-minute timeframe directly onto the price chart. The 20-period average displays as a green line, while the 60-period average appears in blue. It serve

Indicator v4 Moving averagesTrend Repaints
Updated 5 years ago · None stated GitHub repository of 60K均線

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