TradingView
MarxBabu
Jul 10, 2016 8:10 AM

SUPERTREND THREE LINE STRATEGY 

EURO / U.S. DOLLARICE

Description

This is slightly modified SuperTrend Script.

1.Buy when all three lines are Green and cover the position even one green become Red line
2.Sell when all the three line are red and cove the position even one become Green.

//@version=2
//Test
//
study("CandleST", shorttitle="Candle", overlay=false)
// --------------- Calculating HA Candle's values

plotcandle(open, high, low, close, title='candle', color=(open < close) ? green : red, wickcolor=gray)
// END

//The below is Super Trend Strategy
Factor1=input(1,type =float, minval=1,maxval = 100)
Factor2=input(2,type =float, minval=1,maxval = 100)
Factor3=input(3,type =float, minval=1,maxval = 100)
Pd1=input(10,minval=1,maxval = 100)
Pd2=input(10,minval=1,maxval = 100)
Pd3=input(10,minval=1,maxval = 100)
//ST 1
Up1=hl2-(Factor1*atr(Pd1))
Up2=hl2-(Factor2*atr(Pd2))
Up3=hl2-(Factor3*atr(Pd3))
Dn1=hl2+(Factor1*atr(Pd1))
Dn2=hl2+(Factor2*atr(Pd2))
Dn3=hl2+(Factor3*atr(Pd3))
//ST1
TrendUp1=close[1]>TrendUp1[1]? max(Up1,TrendUp1[1]) : Up1
TrendUp2=close[1]>TrendUp2[1]? max(Up2,TrendUp2[1]) : Up2
TrendUp3=close[1]>TrendUp3[1]? max(Up3,TrendUp3[1]) : Up3

TrendDown1=close[1]<TrendDown1[1]? min(Dn1,TrendDown1[1]) : Dn1
TrendDown2=close[1]<TrendDown2[1]? min(Dn2,TrendDown2[1]) : Dn2
TrendDown3=close[1]<TrendDown3[1]? min(Dn3,TrendDown3[1]) : Dn3

Trend1 = close > TrendDown1[1] ? 1: close< TrendUp1[1]? -1: nz(Trend1[1],1)
Tsl1 = Trend1==1? TrendUp1: TrendDown1
Trend2 = close > TrendDown2[1] ? 1: close< TrendUp2[1]? -1: nz(Trend2[1],1)
Tsl2 = Trend2==1? TrendUp2: TrendDown2
Trend3 = close > TrendDown3[1] ? 1: close< TrendUp3[1]? -1: nz(Trend3[1],1)
Tsl3 = Trend3==1? TrendUp3: TrendDown3

linecolor1 = Trend1 == 1 ? yellow : red
linecolor2 = Trend2 == 1 ? green : red
linecolor3 = Trend3 == 1 ? #7FFF00 : red

plot(Tsl1, color = linecolor1 , style = line , linewidth = 2,title = "SuperTrend1")
plot(Tsl2, color = linecolor2 , style = line , linewidth = 2,title = "SuperTrend2")
plot(Tsl3, color = linecolor3 , style = line , linewidth = 2,title = "SuperTrend3")

plotshape(cross(close,Tsl1) and close>Tsl1 , "UpShape1", shape.flag,location.bottom,yellow,0,0)
plotshape(cross(Tsl1,close) and close<Tsl1 , "DownShape1", shape.triangledown , location.bottom, red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

plotshape(cross(close,Tsl2) and close>Tsl2 , "UpShape2", shape.triangleup,location.bottom,green,0,0)
plotshape(cross(Tsl2,close) and close<Tsl2 , "DownShape2", shape.triangledown , location.bottom, red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

plotshape(cross(close,Tsl3) and close>Tsl3 , "UpShape1", shape.triangleup,location.bottom,green,0,0)
plotshape(cross(Tsl3,close) and close<Tsl3 , "DownShape2", shape.triangledown , location.bottom, red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

plotarrow(Trend1 == 1 and Trend1[1] == -1 ? Trend1 : na, title="UpSTArrow1", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend1 == -1 and Trend1[1] == 1 ? Trend1 : na, title="DnSTArrow1", colordown=red, maxheight=60, minheight=50, transp=0)
//ST2
plotarrow(Trend2 == 1 and Trend2[1] == -1 ? Trend2 : na, title="UpSTArrow2", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend2 == -1 and Trend2[1] == 1 ? Trend2 : na, title="DnSTArrow2", colordown=red, maxheight=60, minheight=50, transp=0)
//ST3
plotarrow(Trend3 == 1 and Trend3[1] == -1 ? Trend3 : na, title="UpSTArrow3", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend3 == -1 and Trend3[1] == 1 ? Trend3 : na, title="DnSTArrow3", colordown=red, maxheight=60, minheight=50, transp=0)


//Alert code :
alertcondition(Trend1 == 1 and Trend1 == -1 ? Trend1 : na, title='Up Entry Arrow', message='Up Entry Arrow!')
alertcondition(Trend1 == -1 and Trend1 == 1 ? Trend1 : na, title='Down Entry Arrow', message='Down Entry Arrow!')
Comments
karmaah
Throws an error line 72 : Undeclared identifier trend1
joyapco
I'd like to ask what the green and red arrows are for? Are they signals for when to buy or sell?

Apologies if they are general knowledge. I'm new to Trading View.
joyapco
Okay I get it:
Green arrows indicate if a line turned red to green at that point
Red arrows indicate if a line turned green to red at that point

Then follow the stated rule of the script:
1.Buy when all three lines are Green and cover the position even one green become Red line
2.Sell when all the three line are red and cove the position even one become Green.

So my next questions are:
1. Where do I set the stop loss?
2. What is the exit strategy for this?
UfukAliTurk
Sorry: Trend1 == 1 and Trend1 == -1 is that true?
MarxBabu

//@version=2
//Test
//
study("CandleST", shorttitle="Candle", overlay=false)
// --------------- Calculating HA Candle's values

plotcandle(open, high, low, close, title='candle', color=(open < close) ? green : red, wickcolor=gray)
// END

//The below is Super Trend Strategy
Factor1=input(1,type =float, minval=1,maxval = 100)
Factor2=input(2,type =float, minval=1,maxval = 100)
Factor3=input(3,type =float, minval=1,maxval = 100)
Pd1=input(10,minval=1,maxval = 100)
Pd2=input(10,minval=1,maxval = 100)
Pd3=input(10,minval=1,maxval = 100)
//ST 1
Up1=hl2-(Factor1*atr(Pd1))
Up2=hl2-(Factor2*atr(Pd2))
Up3=hl2-(Factor3*atr(Pd3))
Dn1=hl2+(Factor1*atr(Pd1))
Dn2=hl2+(Factor2*atr(Pd2))
Dn3=hl2+(Factor3*atr(Pd3))
//ST1
TrendUp1=close[1]>TrendUp1[1]? max(Up1,TrendUp1[1]) : Up1
TrendUp2=close[1]>TrendUp2[1]? max(Up2,TrendUp2[1]) : Up2
TrendUp3=close[1]>TrendUp3[1]? max(Up3,TrendUp3[1]) : Up3

TrendDown1=close[1]<TrendDown1[1]? min(Dn1,TrendDown1[1]) : Dn1
TrendDown2=close[1]<TrendDown2[1]? min(Dn2,TrendDown2[1]) : Dn2
TrendDown3=close[1]<TrendDown3[1]? min(Dn3,TrendDown3[1]) : Dn3

Trend1 = close > TrendDown1[1] ? 1: close< TrendUp1[1]? -1: nz(Trend1[1],1)
Tsl1 = Trend1==1? TrendUp1: TrendDown1
Trend2 = close > TrendDown2[1] ? 1: close< TrendUp2[1]? -1: nz(Trend2[1],1)
Tsl2 = Trend2==1? TrendUp2: TrendDown2
Trend3 = close > TrendDown3[1] ? 1: close< TrendUp3[1]? -1: nz(Trend3[1],1)
Tsl3 = Trend3==1? TrendUp3: TrendDown3

linecolor1 = Trend1 == 1 ? yellow : red
linecolor2 = Trend2 == 1 ? green : red
linecolor3 = Trend3 == 1 ? #7FFF00 : red

plot(Tsl1, color = linecolor1 , style = line , linewidth = 2,title = "SuperTrend1")
plot(Tsl2, color = linecolor2 , style = line , linewidth = 2,title = "SuperTrend2")
plot(Tsl3, color = linecolor3 , style = line , linewidth = 2,title = "SuperTrend3")

plotshape(cross(close,Tsl1) and close>Tsl1 , "UpShape1", shape.flag,location.bottom,yellow,0,0)
plotshape(cross(Tsl1,close) and close<Tsl1 , "DownShape1", shape.triangledown , location.bottom, red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

plotshape(cross(close,Tsl2) and close>Tsl2 , "UpShape2", shape.triangleup,location.bottom,green,0,0)
plotshape(cross(Tsl2,close) and close<Tsl2 , "DownShape2", shape.triangledown , location.bottom, red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

plotshape(cross(close,Tsl3) and close>Tsl3 , "UpShape1", shape.triangleup,location.bottom,green,0,0)
plotshape(cross(Tsl3,close) and close<Tsl3 , "DownShape2", shape.triangledown , location.bottom, red,0,0)
//plot(Trend==1 and Trend[1]==-1,color = linecolor, style = circles, linewidth = 3,title="Trend")

plotarrow(Trend1 == 1 and Trend1[1] == -1 ? Trend1 : na, title="UpSTArrow1", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend1 == -1 and Trend1[1] == 1 ? Trend1 : na, title="DnSTArrow1", colordown=red, maxheight=60, minheight=50, transp=0)
//ST2
plotarrow(Trend2 == 1 and Trend2[1] == -1 ? Trend2 : na, title="UpSTArrow2", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend2 == -1 and Trend2[1] == 1 ? Trend2 : na, title="DnSTArrow2", colordown=red, maxheight=60, minheight=50, transp=0)
//ST3
plotarrow(Trend3 == 1 and Trend3[1] == -1 ? Trend3 : na, title="UpSTArrow3", colorup=lime, maxheight=60, minheight=50, transp=0)
plotarrow(Trend3 == -1 and Trend3[1] == 1 ? Trend3 : na, title="DnSTArrow3", colordown=red, maxheight=60, minheight=50, transp=0)


//Alert code :
alertcondition(Trend1 == 1 and Trend1 == -1 ? Trend1 : na, title='Up Entry Arrow', message='Up Entry Arrow!')
alertcondition(Trend1 == -1 and Trend1 == 1 ? Trend1 : na, title='Down Entry Arrow', message='Down Entry Arrow!')
sjcwealth
Does it work on 1m,5m,15m,30m,60m,240m timeframes?
sjcwealth
What do you mean by 3 lines? "1.Buy when all three lines are Green and cover the position even one green become Red line
2.Sell when all the three line are red and cove the position even one become Green. "
MarxBabu
Please read it again and try interpreting if not i would explain you
sjcwealth
Hi MarxBabu thanks for your reply. I sent a private message with my understanding of the indicators on the chart. Hopefully it makes sense and I got the hang of it. An explanation is appreciated. Happy trading!
harikuttandb
@sjcwealth, also send me the describtions to harihadhz@gmail.com thank you
More