0
votes

I "made" my first Tradingview script, based on copying other public scripts. I can figure out most of it, except the last part. I am wondering whether it is possible to add an if-statement once, until an other specific event has taken place.

In the printscreen below I have an example. In my script I have a colored trendline and candles. I want to enter a short when trendline is red and candle is black (first green circle). But the series plot again bar color changes from white to black (red circles). I want to add alert only the first time (green circle) until a new event has taken place; when the trendline changes color (purple circle).

Chart example

BBperiod          = input(defval = 19,     title = "BB Period",    type = input.integer, minval       = 1)
BBdeviations      = input(defval = 1.00,     title = "BB Deviations",    type = input.float, minval = 0.1, step=0.05)
UseATRfilter      = input(defval = true, title = "ATR Filter",  type = input.bool)
ATRperiod         = input(defval = 7,     title = "ATR Period",    type = input.integer, minval = 1)

BBUpper=sma (close,BBperiod)+stdev(close, BBperiod)*BBdeviations
BBLower=sma (close,BBperiod)-stdev(close, BBperiod)*BBdeviations
//
TrendLine = 0.0
iTrend = 0.0
buy = 0.0
sell = 0.0
//
BBSignal = close>BBUpper? 1 : close<BBLower? -1 : 0
// 
if BBSignal == 1 and UseATRfilter == 1
TrendLine:=low-atr(ATRperiod)
if TrendLine<TrendLine[1] 
    TrendLine:=TrendLine[1]
if BBSignal == -1 and UseATRfilter == 1
TrendLine:=high+atr(ATRperiod)
if TrendLine>TrendLine[1]
    TrendLine:=TrendLine[1]
if BBSignal == 0 and UseATRfilter == 1
TrendLine:=TrendLine[1]
//
if BBSignal == 1 and UseATRfilter == 0
TrendLine:=low
if TrendLine<TrendLine[1] 
    TrendLine:=TrendLine[1]
if BBSignal == -1 and UseATRfilter == 0
TrendLine:=high
if TrendLine>TrendLine[1]
    TrendLine:=TrendLine[1]
if BBSignal == 0 and UseATRfilter == 0
TrendLine:=TrendLine[1]
//
iTrend:=iTrend[1]
if TrendLine>TrendLine[1] 
iTrend:=1
if TrendLine<TrendLine[1] 
iTrend:=-1
//
buy:=iTrend[1]==-1 and iTrend==1 ? 1 : na
sell:=iTrend[1]==1 and iTrend==-1? 1 : na
//
plot(TrendLine, color=iTrend > 0?color.blue:color.red         ,style=plot.style_line,linewidth=2,transp=0,title="Trend Line") 


//BarcolorT1
length = input(title="Length", type=input.integer, defval=50)
src_=input(close, title="Source", type=input.source)
mult=input(6.0, title="Mult")
barc=input(true, title="Use barcolor?")
plots=input(false, title="Show plots?")


r(src, n)=>
s = 0.0
for i = 0 to n-1
    s := s + ((n-(i*2+1))/2)*src[i]
x=s/(n*(n+1))
x

l=sma(low, length)
h=sma(high, length)
lr= l+mult*r(low, length)
hr= h+mult*r(high, length)

trend=0
trend:=src_ > lr and src_ > hr ? 1 : src_ < lr and src_ < hr ? -1 : trend[1]

long=0
short=0
long:= trend==1 and src_>h ? 1 : trend==-1 ? -1 : long[1]
short:= trend==-1 and src_<l ? 1 : trend==1 ? -1 : short[1]

barcolor(barc? (long>0? color.green : short>0? color.black : trend>0? color.white: trend<0 ?     color.white : color.blue) : na)

//plot shape
data = short!=short[1] and short==1 and iTrend < 0
plotshape(data, style=shape.xcross)
1

1 Answers

0
votes

You can achieve your goal with an extra flag that will be activated and deactivated on required events. Here is the code:

var active = false

// Activation
active := active or data

// Deactivation
if nz(iTrend[1]) <= 0 and iTrend > 0
    active := false

activated = not nz(active[1], false) and active

plotshape(activated, style=shape.triangledown, color=color.red, size=size.small, location=location.belowbar)

Add it to the end of your script and it will show red triangles when the trend line is red and bar color changes from white to black for the first time.

AAPL, 60