0
votes

I'm using pine editor for coding in TradingView, I 'm using 3 supertrends, now for a downtrend, 1st supertrend gives Sell[Red in color] signal and starts with red line downwards, after that 2nd supertrend gives Sell [Orande in color] signal and starts with red line downwards, similar process for 3rd supertrend[purple color], over here if all the supertrends are giving Sell signal, then I need to show one big parent SELL sign indicating that all the SELL conditions are satisfied, now if one of the signal turns green indicating Buy signal and later on it turns red i.e. SEll signal, again a parent SELL sign should be visible on the chart. this gives a hint that I need to short from here. I have attached screenshot could you please help me with the code.enter image description here

Here is the code for your reference

//@version=4
study("ST", overlay = true, format=format.price, precision=2, resolution="")

Periods         = input(title="ATR Period", type=input.integer, defval=10)
src             = input(hl2, title="Source")
Multiplier      = input(title="ATR Multiplier", type=input.float, step=0.1, defval=1.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=false)

buyBool         = false
sellBool        = false

atr2            = sma(tr, Periods)
atr             = changeATR ? atr(Periods) : atr2
up              = src-(Multiplier*atr)
up1             = nz(up[1],up)

up              := close[1] > up1 ? max(up,up1) : up
dn              = src+(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=1, 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=1, 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)

mPlot           = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

longFillColor   = highlighting ? (trend ==  1 ? color.green : color.white) : color.white
shortFillColor  = highlighting ? (trend == -1 ? color.red   : color.white) : color.white

fill(mPlot, upPlot, title="UpTrend Highligter",   color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)

alertcondition(buySignal,  title="SuperTrend Buy",  message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")

changeCond      = trend != trend[1]

alertcondition(changeCond, title="SuperTrend Direction Change", message="SuperTrend has changed direction!")

Periods1        = input(   11, title="atr1 Period",                      type=input.integer)
src1            = input(  hl2, title="Source")
Multiplier1     = input(  2.0, title="atr1 Multiplier1",                 type=input.float, step=0.1)
changeatr11     = input( true, title="Change atr1 Calculation Method ?", type=input.bool)
showsignals1    = input( true, title="Show Buy/Sell Signals ?",          type=input.bool)
highlighting1   = input(false, title="Highlighter On/Off ?",             type=input.bool)

buyBool1        = false
sellBool1       = false

atr121          = sma(tr, Periods1)
atr1            = changeatr11 ? atr(Periods1) : atr121
up123           = src1-(Multiplier1*atr1)
up1231          = nz(up123[1],up123)
up123           := close[1] > up1231 ? max(up123,up1231) : up123
dn123           = src1+(Multiplier1*atr1)
dn1231          = nz(dn123[1], dn123)
dn123           := close[1] < dn1231 ? min(dn123, dn1231) : dn123
trend1          = 1
trend1          := nz(trend1[1], trend1)
trend1          := trend1 == -1 and close > dn1231 ? 1 : trend1 == 1 and close < up1231 ? -1 : trend1

up123Plot1      = plot(trend1 == 1 ? up123 : na, title="up123 trend1", style=plot.style_linebr, linewidth=1, color=color.green)

buySignal1      = trend1 == 1 and trend1[1] == -1

plotshape(buySignal1                  ? up123 : na, title="up123trend1 Begins",             location=location.absolute, style=shape.circle,  size=size.tiny, color=color.orange,                        transp=0)
plotshape(buySignal1 and showsignals1 ? up123 : na, title="Buy",                text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.orange, textcolor=color.white, transp=0)

dn123Plot       = plot(trend1 == 1 ? na : dn123, title="Down trend1", style=plot.style_linebr, linewidth=1, color=color.red)
sellSignal1     = trend1 == -1 and trend1[1] == 1

plotshape(sellSignal1                  ? dn123 : na, title="Downtrend1 Begins",              location=location.absolute, style=shape.circle,    size=size.tiny, color=color.orange,                        transp=0)
plotshape(sellSignal1 and showsignals1 ? dn123 : na, title="Sell",              text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.orange, textcolor=color.white, transp=0)

mPlot1          = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)

longFillColor1  = highlighting1 ? (trend1 ==  1 ? color.green : color.white) : color.white
shortFillColor1 = highlighting1 ? (trend1 == -1 ? color.red   : color.white) : color.white

fill(mPlot1, up123Plot1, title="up123trend1 Highligter", color=longFillColor1)
fill(mPlot1, dn123Plot,  title="Downtrend1 Highligter",  color=shortFillColor1)

alertcondition(buySignal1,  title="Sup123ertrend1 Buy",  message="Sup123ertrend1 Buy!")
alertcondition(sellSignal1, title="Sup123ertrend1 Sell", message="Sup123ertrend1 Sell!")

changeCond1     = trend1 != trend1[1]

alertcondition(changeCond1, title="Sup123ertrend1 Direction Change", message="Sup123ertrend1 has changed direction!")
1
Please share the code you already have. - Bjorn Mistiaen
@BjornMistiaen I have edited my question, you can find code over there - Mysterious288
Please post code that compiles. This gives error Script could not be translated from: |B|up := close[1] > - Bjorn Mistiaen
@BjornMistiaen Sorry, could you please check now. - Mysterious288
I formatted and restructed your code for readability. Could you please edit your question to explain exactly what you want? Especially what you mean by "previous". Previous bar? Previous occurrence? Previous in relation to what? When do the "previous" detection needs to be reset? etc... If possible, include screenshots of what you mean. - Bjorn Mistiaen

1 Answers

0
votes
//@version=4
study("SuperTrend", "ST", true)

var float   m1          = input(1,  "ATR Multiplier", input.float,   step=0.1, group="Supertrend 1")
var int     p1          = input(10, "ATR Period",     input.integer,           group="Supertrend 1")
var float   m2          = input(1,  "ATR Multiplier", input.float,   step=0.1, group="Supertrend 2")
var int     p2          = input(15, "ATR Period",     input.integer,           group="Supertrend 2")
var float   m3          = input(1,  "ATR Multiplier", input.float,   step=0.1, group="Supertrend 3")
var int     p3          = input(20, "ATR Period",     input.integer,           group="Supertrend 3")

f_getColor_Resistance(_dir, _color) => _dir ==  1 and _dir == _dir[1] ? _color : na
f_getColor_Support(_dir, _color) =>    _dir == -1 and _dir == _dir[1] ? _color : na

[superTrend1, dir1] = supertrend(m1, p1)
[superTrend2, dir2] = supertrend(m2, p2)
[superTrend3, dir3] = supertrend(m3, p3)

sum_dir   = dir1 + dir2 + dir3
dir_long  = sum_dir==-3
dir_short = sum_dir==3

if (dir_long and dir_long != dir_long[1])
    label.new(bar_index, low, "Long",   style=label.style_label_up)

if (dir_short and dir_short != dir_short[1])
    label.new(bar_index, high, "Short", style=label.style_label_down)

colR1 = f_getColor_Resistance(dir1, color.red)
colS1 = f_getColor_Support(   dir1, color.green)

colR2 = f_getColor_Resistance(dir2, color.orange)
colS2 = f_getColor_Support(   dir2, color.yellow)

colR3 = f_getColor_Resistance(dir3, color.blue)
colS3 = f_getColor_Support(   dir3, color.maroon)

plot(superTrend1, "R1", colR1, linewidth=2)
plot(superTrend1, "S1", colS1, linewidth=2)

plot(superTrend2, "R1", colR2, linewidth=2)
plot(superTrend2, "S1", colS2, linewidth=2)

plot(superTrend3, "R1", colR3, linewidth=2)
plot(superTrend3, "S1", colS3, linewidth=2)