0% found this document useful (0 votes)
147 views6 pages

Accurate Trend Band Trading System

Uploaded by

amitbh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views6 pages

Accurate Trend Band Trading System

Uploaded by

amitbh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

// This source code is subject to the terms of the Mozilla Public License 2.

0 at
https://mozilla.org/MPL/2.0/
// ? noop42

//@version=5
indicator(title='Accurate trend band trading system', shorttitle='Accurate trend
band trading system', overlay=true)
//no1=input.string("Multi Oscillators Price Levels ",title =
"=================indicator1=============")
// Input parameters
src =close// input(close, title='Source')
src_ind = 'RSI'//input.string(title='Source indicator', defval='RSI',
options=['Stochastic RSI', 'Stochastic CCI', 'RSI', 'CCI'], group='Options')
custom_timeframe = ''//input.timeframe(title='Timeframe', defval='',
group='Options')

// Drawing options
show_sup = true//input.bool(title='Show supports', defval=true, group='Drawings')
show_res = true//input.bool(title='Show resistances', defval=true,
group='Drawings')
lines_width = 3//input.int(title='Lines width', defval=3 , group='Drawings')
sup_src = low //input.source(title='Draw supports on', defval=low,
group='Drawings')
res_src = high//input.source(title='Draw resistances on', defval=high,
group='Drawings')
extend_level = 'None'//input.string('None', title='Extend levels',
options=['Right', 'Left', 'Both', 'None'], group='Drawings')
sup_col = #34eb3a//input.color(title='Supports color', defval=#34eb3a,
group='Drawings')
res_col =#eb4731// input.color(title='Resistances color', defval=#eb4731,
group='Drawings')

// Indicators
smoothKD =3// input.int(3, 'K & D smoothing', minval=1, group='Stoch RSI/CCI')
lengthStoch =14// input.int(14, 'Stochastic Length', minval=1, group='Stoch
RSI/CCI')
stoch_low = 20//input.int(20, title='Stoch Oversold Level', group='Stoch RSI/CCI')
stoch_high = 80//input.int(80, title='Stoch Overbought Level', group='Stoch
RSI/CCI')

lengthRSI = 14//input.int(14, 'RSI Length', minval=1, group='RSI / Stoch RSI')


rsi_low = 30//input.int(30, title='RSI Oversold Level', group='RSI')
rsi_high = 70//input.int(70, title='RSI Overbought Level', group='RSI')

lengthCCI = 20//input.int(20, 'CCI Length', minval=1, group='CCI')


cci_low = -100//input.int(-100, title='CCI Oversold Level', group='CCI')
cci_high = 100//input.int(100, title='CCI Overbought Level', group='CCI')

// Indicators Data
rsi_tf = request.security(syminfo.tickerid, custom_timeframe, ta.rsi(src,
lengthRSI))
cci_tf = request.security(syminfo.tickerid, custom_timeframe, ta.cci(src,
lengthCCI))

stoch_k_tf = request.security(syminfo.tickerid, custom_timeframe,


ta.sma(ta.stoch(rsi_tf, rsi_tf, rsi_tf, lengthStoch), smoothKD))
stoch_d_tf = request.security(syminfo.tickerid, custom_timeframe,
ta.sma(stoch_k_tf, smoothKD))
stock_k_cci_tf = request.security(syminfo.tickerid, custom_timeframe,
ta.sma(ta.stoch(cci_tf, cci_tf, cci_tf, lengthStoch), smoothKD))
stoch_d_cci_tf = request.security(syminfo.tickerid, custom_timeframe,
ta.sma(stock_k_cci_tf, smoothKD))

avgscci = (stock_k_cci_tf + stoch_d_cci_tf ) / 2


avgsrsi = (stoch_k_tf + stoch_d_tf) / 2

// Extreme zones
srsi_ob = avgsrsi > stoch_high
srsi_os = avgsrsi < stoch_low

scci_ob = avgscci > stoch_high


scci_os = avgscci < stoch_low

rsiob = rsi_tf > rsi_high


rsios = rsi_tf < rsi_low

cciob = cci_tf > cci_high


ccios = cci_tf < cci_low

// Indicator signal selection


ob = src_ind == 'RSI' ? rsiob : src_ind == 'CCI' ? cciob : src_ind == 'Stochastic
RSI' ? srsi_ob : scci_ob
os = src_ind == 'RSI' ? rsios : src_ind == 'CCI' ? ccios : src_ind == 'Stochastic
RSI' ? srsi_os : scci_os

// Price lines
sup_final_src = request.security(syminfo.tickerid, custom_timeframe, sup_src)
res_final_src = request.security(syminfo.tickerid, custom_timeframe, res_src)

var support = sup_final_src


var resistance = res_final_src

//Set current levels


support := os and not os[1] ? sup_final_src : support
resistance := ob and not ob[1] ? res_final_src : resistance

// Update current levels


support := (os and os[1]) and (sup_final_src < support) ? sup_final_src : support
resistance := (ob and ob[1]) and (res_final_src > resistance) ? res_final_src :
resistance

ext = extend_level == "Right" ? extend.right : extend_level == "Left" ? extend.left


: extend_level == "Both" ? extend.both : extend.none

var line sup = na


var line res = na
var int sup_offset = 0
var int res_offset = 0

// Draw current
if show_sup and (not os) and os[1]
sup := line.new(bar_index[1], support, bar_index, support, extend=ext,
color=sup_col, width=lines_width)
else
line.set_x2(sup, line.get_x2(sup) +1)
if show_res and (not ob) and ob[1]
res := line.new(bar_index[1], resistance, bar_index, resistance, extend=ext,
color=res_col, width=lines_width)
else
line.set_x2(res, line.get_x2(res) +1)

//no2=input.string("EMAS",title = "=================indicator2=============")
ema1=5//input.int(5,title = "EMA1")
ema2=13//input.int(13,title = "EMA2")
ema3=34//input.int(34,title = "EMA3")
ema4=200//input.int(200,title = "过滤 EMA4")
EMAP1=ta.ema(close,ema1)
EMAP2=ta.ema(close,ema2)
EMAP3=ta.ema(close,ema3)
EMAP4=ta.ema(close,ema4)
plot(EMAP1)
plot(EMAP2)
plot(EMAP3,linewidth = 3,color = close>EMAP3?#34eb3a:close<EMAP3?color.red:na)
emabuy=close>EMAP1 and close >EMAP2 and close >EMAP3 and open< EMAP1 and open
<EMAP2 and open <EMAP3
emabuy2=close > EMAP4 and emabuy
// plotshape(emabuy2,title = "trend band trading",style =shape.arrowup,size
=size.large ,location=location.belowbar)
plotshape(emabuy2, title='trend band trading', text=' BUY ', style=shape.labelup,
location=location.belowbar, color=#34eb3a, textcolor=color.new(color.white,
0),size=size.small)//⇧
plotchar(emabuy2,char="⇧", title='trend band
trading',location=location.belowbar,size=size.large,precision=3,color=#34eb3a)
//no3=input.string("MACD-Divergence_bear_bull",title =
"=================indicator3=============")
//indicator(title='MACD-Divergence_bear_bull', timeframe='')
// Getting inputs

fast_length = 12//input(title='Fast Length', defval=12)


slow_length = 26//input(title='Slow Length', defval=26)
//src = input(title='Source', defval=close)
signal_length = 9//input.int(title='Signal Smoothing', minval=1, maxval=50,
defval=9)
sma_source = 'EMA'//input.string(title='Oscillator MA Type', defval='EMA',
options=['SMA', 'EMA'])
sma_signal = 'EMA'//input.string(title='Signal Line MA Type', defval='EMA',
options=['SMA', 'EMA'])
// Plot colors
col_macd = #2962FF//input.color(#2962FF, 'MACD Line ', group='Color Settings',
inline='MACD')
col_signal = #FF6D00//input.color(#FF6D00, 'Signal Line ', group='Color Settings',
inline='Signal')
col_grow_above =#26A69A// input.color(#26A69A, 'Above Grow', group='Histogram',
inline='Above')
col_fall_above = #B2DFDB//input.color(#B2DFDB, 'Fall', group='Histogram',
inline='Above')
col_grow_below = #FFCDD2//input.color(#FFCDD2, 'Below Grow', group='Histogram',
inline='Below')
col_fall_below = #FF5252//input.color(#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)
macd = fast_ma - slow_ma
signal = sma_signal == 'SMA' ? ta.sma(macd, signal_length) : ta.ema(macd,
signal_length)
hist = macd - signal
// 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)

donttouchzero = true//input(title='Don\'t touch the zero line?', defval=true)

lbR = 5//input(title='Pivot Lookback Right', defval=5)


lbL = 5//input(title='Pivot Lookback Left', defval=5)
rangeUpper = 60//input(title='Max of Lookback Range', defval=60)
rangeLower = 5//input(title='Min of Lookback Range', defval=5)
plotBull = true//input(title='Plot Bullish', defval=true)
plotHiddenBull = false
plotBear = true//input(title='Plot Bearish', defval=true)
plotHiddenBear = false
bearColor = color.red
bullColor = color.green
hiddenBullColor = color.new(color.green, 80)
hiddenBearColor = color.new(color.red, 80)
textColor = color.white
noneColor = color.new(color.white, 100)
osc = macd

plFound = na(ta.pivotlow(osc, lbL, lbR)) ? false : true


phFound = na(ta.pivothigh(osc, lbL, lbR)) ? false : true
_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper

//------------------------------------------------------------------------------
// Regular Bullish
// Osc: Higher Low

oscHL = osc[lbR] > ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1]) and


osc[lbR] < 0

// Price: Lower Low

priceLL = low[lbR] < ta.valuewhen(plFound, low[lbR], 1)


priceHHZero = ta.highest(osc, lbL + lbR + 5)
//plot(priceHHZero,title="priceHHZero",color=color.green)
blowzero = donttouchzero ? priceHHZero < 0 : true
bullCond = plotBull and priceLL and oscHL and plFound and blowzero

//plot(plFound ? osc[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2,


color=bullCond ? bullColor : noneColor, transp=0)

plotshape(bullCond ? low[lbR] : na, offset=-lbR, title='Regular Bullish Label',


text=' ↑ ', style=shape.labelup, location=location.absolute,
color=color.new(bullColor, 0), textcolor=color.new(textColor, 0),size=size.small)

//------------------------------------------------------------------------------
// Hidden Bullish
// Osc: Lower Low
oscLL = osc[lbR] < ta.valuewhen(plFound, osc[lbR], 1) and _inRange(plFound[1])

// Price: Higher Low

priceHL = low[lbR] > ta.valuewhen(plFound, low[lbR], 1)


hiddenBullCond = plotHiddenBull and priceHL and oscLL and plFound

//plot(plFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2,


color=hiddenBullCond ? hiddenBullColor : noneColor, transp=0)

plotshape(hiddenBullCond ? low[lbR] : na, offset=-lbR, title='Hidden Bullish


Label', text=' ↑ ', style=shape.labelup, location=location.absolute,
color=color.new(bullColor, 0), textcolor=color.new(textColor, 0),size=size.small)

//------------------------------------------------------------------------------
// Regular Bearish
// Osc: Lower High

oscLH = osc[lbR] < ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1]) and


osc[lbR] > 0

priceLLZero = ta.lowest(osc, lbL + lbR + 5)


//plot(priceLLZero,title="priceLLZero", color=color.red)
bearzero = donttouchzero ? priceLLZero > 0 : true

// Price: Higher High

priceHH = high[lbR] > ta.valuewhen(phFound, high[lbR], 1)

bearCond = plotBear and priceHH and oscLH and phFound and bearzero

//plot(phFound ? osc[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=2,


color=bearCond ? bearColor : noneColor, transp=0)

plotshape(bearCond ? high[lbR] : na, offset=-lbR, title='Regular Bearish Label',


text=' ↓ ', style=shape.labeldown, location=location.absolute,
color=color.new(bearColor, 0), textcolor=color.new(textColor, 0),size=size.small)

//------------------------------------------------------------------------------
// Hidden Bearish
// Osc: Higher High

oscHH = osc[lbR] > ta.valuewhen(phFound, osc[lbR], 1) and _inRange(phFound[1])

// Price: Lower High

priceLH = high[lbR] < ta.valuewhen(phFound, high[lbR], 1)

hiddenBearCond = plotHiddenBear and priceLH and oscHH and phFound

//plot(phFound ? osc[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=2,


color=hiddenBearCond ? hiddenBearColor : noneColor, transp=0)

plotshape(hiddenBearCond ? high[lbR] : na, offset=-lbR, title='Hidden Bearish


Label', text=' ↓ ', style=shape.labeldown, location=location.absolute,
color=color.new(bearColor, 0), textcolor=color.new(textColor, 0),size=size.small)
alertcondition(bullCond, title='Bull', message='Regular Bull Div {{ticker}} XXmin')
alertcondition(bearCond, title='Bear', message='Regular Bear Div {{ticker}} XXmin')
alertcondition(hiddenBullCond, title='H Bull', message='Hidden Bull Div {{ticker}}
XXmin')
alertcondition(hiddenBearCond, title='H Bear', message='Hidden Bear Div {{ticker}}
XXmin')

You might also like