Strategy · Pine Script v5 · On the chart

MACD VWAP Supertrend

by kritarth2121· code credited to kt7756294

This strategy combines a MACD crossover, a positive histogram, VWAP versus the open and a Supertrend direction change within the last few bars to open long trades.

View on GitHub

Repaints

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code66
Settings14inputs
LicenceNone statedfile: MPL-2.0
Last update2 years ago10 Dec 2023

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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

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

MACD.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © kt7756294

//@version=5
strategy("MACD,VWAP,SUPERTREND", overlay=true,max_labels_count=500,max_lines_count=500,max_boxes_count = 500,calc_on_every_tick = true)
fast_length = input(title="Fast Length", defval=12)
slow_length = input(title="Slow Length", defval=26) 
src = input(title="Source", defval=close)
signal_length = input.int(title="Signal Smoothing",  minval = 1, maxval = 50, defval = 9)
sma_source = input.string(title="Oscillator MA Type",  defval="EMA", options=["SMA", "EMA"])
sma_signal = input.string(title="Signal Line MA Type", defval="EMA", options=["SMA", "EMA"])
// Plot colors
col_macd = input(#2962FF, "MACD Line  ", group="Color Settings", inline="MACD")
col_signal = input(#FF6D00, "Signal Line  ", group="Color Settings", inline="Signal")
col_grow_above = input(#26A69A, "Above   Grow", group="Histogram", inline="Above")
col_fall_above = input(#B2DFDB, "Fall", group="Histogram", inline="Above")
col_grow_below = input(#FFCDD2, "Below Grow", group="Histogram", inline="Below")
col_fall_below = input(#FF5252, "Fall", group="Histogram", inline="Below")
// Calculating
fast_ma = sma_source == "SMA" ? ta.sma(src, fast_length) : ta.ema(src, fast_length)
slow_ma = sma_source == "SMA" ? ta.sma(src, slow_length) : ta.ema(src, slow_length)
//Supertrend
atrPeriod = input(20, "ATR Length")
factor = input.float(2, "Factor", step = 0.01)

macd = fast_ma - slow_ma
signal = sma_signal == "SMA" ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)
hist = macd - signal
// slope1=str.tostring((macd-macd[5])/5, "#.##")
// slope2=str.tostring((signal-signal[5])/5,"#.##")
// label.new(bar_index,high,str.tostring(macd,"#.##") + " " + str.tostring(ta.vwap) + " "+slope2 ,textcolor = color.white)
// math.atan(ta.change(macd)/signal)
// vwapDifference=ta.vwap - close
[_, direction] = ta.supertrend(factor, atrPeriod)

buy  = ta.crossover(macd, signal) and hist > 0 and ta.vwap > open 
supertrendBuyCondition= (buy and  ( ta.change(direction) < 0 or ta.change(direction[1]) < 0 or ta.change(direction[2]) < 0 ) )   or ( ta.change(direction)<0 and ( buy[1] or buy [2] ))
timeBuyCondition= supertrendBuyCondition and time>=1400
sell = ta.crossunder(macd, signal) or ta.change(direction) >0
// label.new(bar_index,high,str.tostring(buy))

plotchar(timeBuyCondition,char="Buy",color=color.green ,location= location.belowbar)
plotchar(sell,char="Sell",color=color.red,location= location.belowbar)

// hline(0, "Zero Line", color=color.new(#787B86, 50))

// plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below)))
// plot(macd, title="MACD", color=col_macd)
// plot(signal, title="Signal", color=col_signal)
if timeBuyCondition
    strategy.entry("buy",direction = strategy.long)
if sell
    strategy.close("buy")



if ta.change(direction) < 0 and time(timeframe.period, "0930-1400:23456")
    label.new(bar_index,high,'Buy',textcolor=color.white)

if ta.change(direction) > 0
    label.new(bar_index,high,'Sell',textcolor=color.white)
plot(ta.vwap,color=color.blue)
time(timeframe.period,"1600-2000")
t1 = time(timeframe.period, "0930-1400:23456")
bgcolor(t1 ? color.new(color.blue, 90) : na)
// label.new(bar_index,low-10,str.tostring(timeframe.period))

Overview

MACD VWAP Supertrend closes the position when MACD crosses back under its signal line or Supertrend turns bearish. Settings cover MACD lengths, MA types, ATR length and factor. VWAP is plotted, buy and sell labels are added, and a weekday morning window is shaded. It has no stop loss, only long entries, and recalculates every tick.

Details

Published by

kritarth2121 on GitHub

Code credit

kt7756294, as written in the script's header

Type

Strategy, Pine Script v5

Draws

On the price chart

Category

Trend, Oscillators & momentum

Markets

Any symbol

Timeframes

M15-H1

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
Fast Lengthint12fast_length
Slow Lengthint26slow_length
Sourcesourceclosesrc
Signal Smoothingint9signal_length
Oscillator MA TypestringEMAsma_source
Signal Line MA TypestringEMAsma_signal
MACD Line  color#2962FFcol_macd
Signal Line  color#FF6D00col_signal
Above   Growcolor#26A69Acol_grow_above
Fallcolor#B2DFDBcol_fall_above
Below Growcolor#FFCDD2col_grow_below
Fallcolor#FF5252col_fall_below
Show all 14 settings
SettingTypeDefaultVariable
ATR Lengthint20atrPeriod
Factorfloat2factor
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

330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
285 stars

Mean Reversion Strategy is an overlay strategy that identifies pullbacks using a long-term EMA, RSI, and candlestick patterns. It generates long signals when a bullish engulfing candle forms below the EMA while RSI is oversold, and short signals when a bearish

Strategy v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Strategy
218 stars

This strategy generates trading signals based on a selectable momentum engine, supporting MACD, RSI, Stochastic, or Rate of Change. Entries trigger on signal crosses, zero-line crosses, or threshold crosses, with an optional moving average trend filter. Risk m

Strategy v6 Oscillators & momentumTrend
Updated 3 months ago · None stated GitHub repository of Momentum Strategy Template

255 EMA Strategy

by hasnocool
106 stars

255 EMA Strategy combines an exponential moving average with Average True Range bands and standard MACD crossovers to generate entry signals. Drawn directly on the price chart, it plots the central 255 EMA along with upper and lower volatility envelopes. Trade

Strategy v4 Moving averagesOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 255 EMA Strategy

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers