Strategy · Pine Script v4 · On the chart

Prototype

by hasnocool· code credited to KAIST291

This strategy overlays Supertrend-style ATR trailing stop levels on the price chart alongside multi-period moving averages, volume metrics, and Bollinger Bands.

View on GitHub

GitHub stars106on the repository
Pine versionv4older, still runs
Lines of code131
Settings23inputs
LicenceGPL-3.0
Last update2 years ago18 Sep 2024

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

131 lines of Pine Script v4 from Bollinger band & Volume based strategy V2.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

Bollinger band & Volume based strategy V2.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © KAIST291
//@version=4
initial_capital=1000
strategy("prototype", overlay=true,default_qty_type=strategy.cash,default_qty_value=500,commission_type=strategy.commission.percent,commission_value=0.04)
Periods = input(title="ATR Period", type=input.integer, defval=10)//sefval원래값이10이였음(이평선 일)
src1 = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src1-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src1+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
length1=input(1)
length3=input(3)
length7=input(7)
length9=input(9)
length14=input(14)
length20=input(20)
length60=input(60)
length120=input(120)
ma1= sma(close,length1)
ma3= sma(close,length3)
ma7= sma(close,length7)
ma9= sma(close,length9)
ma14=sma(close,length14)
ma20=sma(close,length20)
ma60=sma(close,length60)
ma120=sma(close,length120)
rsi=rsi(close,14)
// BUYING VOLUME AND SELLING VOLUME //
BV = iff( (high==low), 0, volume*(close-low)/(high-low))
SV = iff( (high==low), 0, volume*(high-close)/(high-low))
vol = iff(volume > 0, volume, 1)
dailyLength = input(title = "Daily MA length", type = input.integer, defval = 50, minval = 1, maxval = 100)
weeklyLength = input(title = "Weekly MA length", type = input.integer, defval = 10, minval = 1, maxval = 100)
//-----------------------------------------------------------
Davgvol = sma(volume, dailyLength)
Wavgvol = sma(volume, weeklyLength)
//-----------------------------------------------------------
length = input(20, minval=1)
src = input(close, title="Source")
mult = input(2.0, minval=0.001, maxval=50, title="StdDev")
mult2= input(1.5, minval=0.001, maxval=50, title="exp")
mult3= input(1.0, minval=0.001, maxval=50, title="exp1")
mult4= input(2.5, minval=0.001, maxval=50, title="exp2")
basis = sma(src, length)
dev = mult * stdev(src, length)
upper = basis + dev
lower = basis - dev
dev2= mult2 * stdev(src, length)
Supper= basis + dev2
Slower= basis - dev2
dev3= mult3 * stdev(src, length)
upper1= basis + dev3
lower1= basis - dev3
dev4= mult4 * stdev(src, length)
upper2=basis + dev4
lower2=basis - dev4
offset = input(0, "Offset", type = input.integer, minval = -500, maxval = 500)
plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
//----------------------------------------------------
exit=(close-strategy.position_avg_price / strategy.position_avg_price*100)
bull=( BV>SV and BV>Davgvol)
bull2=(BV>SV and BV>Davgvol)
bux =(close>Supper and close>Slower and volume<Davgvol)
bear=(SV>BV and SV>Davgvol)
con=(BV>Wavgvol and rsi>80)
imInATrade = strategy.position_size != 0
highestPriceAfterEntry = valuewhen(imInATrade, high, 0)
//////////////////////////////////////////////////////////////////////////////////
sec=((open[1]-close[1])/(high[1]-low[1]))
wgt=1-sec
longcondition = (low+wgt)<(open)
shortcondition= (low+wgt)>(open)
// STRATEGY LONG //
if (bull and open>upper1 and open>Supper and open>upper and (trend == 1 ? up : na) and longcondition)
    strategy.entry("Long",strategy.long)
    
//////////////////////////////////////////////////////////////////////////////////
if (strategy.openprofit>5)
    strategy.close("Long")
else if (rsi>80 and strategy.position_avg_price*1.004<close)
    strategy.close("Long")
else if (close < strategy.position_avg_price*0.91)
    strategy.close("Long")
else if (open>upper and strategy.position_avg_price*1.004<close)
    strategy.close("Long")
//////////////////////////////////////////////////////////////////////////////////
if (strategy.position_size>0 and (trend == 1 ? na : dn))
    strategy.close("Long")
//////////////////////////////////////////////////////////////////////////////////
if (low<ma20 and low<lower1 and close<Slower and crossunder(ma60,ma120)and bear)
    strategy.entry("Short",strategy.short)
else if (ma3<ma9 and ma9<ma20 and ma20<ma60 and bear and (trend == 1 ? na : dn) and high<ma20 and close<lower1 and high<Slower and rsi>30)
    strategy.entry("Short",strategy.short)
    
    
if (strategy.openprofit>10)
    strategy.close("Short")
else if (rsi<20 and close<strategy.position_avg_price*0.996)
    strategy.close("Short")
else if (high<lower and close<strategy.position_avg_price*0.996)
    strategy.close("Short")
else if (strategy.openprofit < strategy.position_avg_price*0.92-close)
    strategy.close("Short")
else if (strategy.position_size<0 and (trend == 1 ? up : na))
    strategy.close("Short")
else if (open<lower and close<strategy.position_avg_price*0.996)
    strategy.close("Short")
Expand (135 lines)

Overview

Prototype flags trend direction changes using an ATR offset channel, plotting buy and sell signals directly on the price chart when the trend flips. Configurable settings include ATR periods, multiplier factors, calculation methods, standard deviation bands, and multiple moving average periods. The script lacks explicit stop loss or take profit risk controls.

Details

Published by

hasnocool on GitHub

Code credit

KAIST291, as written in the script's header

Type

Strategy, Pine Script v4

Draws

On the price chart

Category

Trend, Volatility & bands, Volume, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Topics

analysisbtcusdtcryptocurrencydatapandaspythonsimulationtradingtradingview

Settings

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

SettingTypeDefaultVariable
ATR Periodint10Periods
Sourcesourcehl2src1
ATR Multiplierfloat3.0Multiplier
Change ATR Calculation Method ?booltruechangeATR
Show Buy/Sell Signals ?booltrueshowsignals
Highlighter On/Off ?booltruehighlighting
length1int1length1
length3int3length3
length7int7length7
length9int9length9
length14int14length14
length20int20length20
Show all 23 settings
SettingTypeDefaultVariable
length60int60length60
length120int120length120
Daily MA lengthint50dailyLength
Weekly MA lengthint10weeklyLength
lengthint20length
Sourcesourceclosesrc
StdDevfloat2.0mult
expfloat1.5mult2
exp1float1.0mult3
exp2float2.5mult4
Offsetint0offset
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

This strategy enters long or short when eight conditions align over the last two bars. It requires EMA 10 and EMA 55 alignment on the one-hour, four-hour and daily charts, a positive ADX slope, matching monitor direction, and price relative to a 400-bar point

Strategy v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ruckard ABCBitcoin TradingLatino B002
8 stars

This overlay strategy combines three distinct SuperTrend levels with multiple moving averages and volume profile analytics on the main price chart. It calculates trend directions using three configurable ATR periods and multipliers alongside SMA, EMA, and HMA

Strategy v5 TrendVolatility & bands
Updated 4 years ago · None stated GitHub repository of ATR 3Lvls VP Strategy

This mean-reversion strategy plots Bollinger Bands and a long-term moving average directly on the price chart. It enters trades when price candles penetrate the outer Bollinger Bands beyond a user-defined body percentage, anticipating a price reversal. Entries

Strategy v5 Mean reversionVolatility & bands
Updated 2 months ago · None stated GitHub repository of Bollinger Bands Winner PRO [Alorse]
257 stars

This overlay strategy targets one-minute ES futures and plots volume-weighted moving-average bands from the open, scaled by a standard-deviation multiple, plus a slower band from ten-minute opens. A higher-timeframe exponential moving average can filter trades

Strategy v6 Moving averagesVolatility & bands
Updated 6 weeks ago · None stated GitHub repository of ES - Futures 1M No Repaint

STRG-BBForce

by harryguiacorn
219 stars

STRG-BBForce is a Bollinger Bands trend-following strategy. It calculates standard Bollinger Bands and tracks directional alignment across the upper, lower, and basis lines. When all three lines slope upward, it opens a long position and closes short positions

Strategy v5 TrendVolatility & bands
Updated 1 year ago · None stated GitHub repository of STRG-BBForce
218 stars

This strategy enters long when a faster moving average crosses above a slower one and short when it crosses below, unless long-only mode is on. An optional volume filter requires volume above a 20-bar average times a multiplier. Exits use a percentage stop los

Strategy v6 TrendMoving averages
Updated 3 months ago · None stated GitHub repository of MA Cross Strategy Template

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