defparam calculateonlastbars=3000
cp = 10
once lastpoint = 0
if high[cp] >= highest[2*cp+1](high) then
PEAK = 1
else
PEAK = 0
endif
if low[cp] <= lowest[2*cp+1](low) then
TROUGH = 1
else
TROUGH = 0
endif
if PEAK then
prevtop = topy
TOPy = high[cp]
TOPx = barindex[cp]
endif
if TROUGH then
prevbot = boty
BOTy = low[cp]
BOTx = barindex[cp]
endif
if PEAK and (lastpoint=-1 or lastpoint=0) then
DRAWSEGMENT(lastX,lastY,TOPx,TOPy) COLOURED(200,0,0,255)
DRAWTEXT("■",TOPx,TOPy,Dialog,Bold,10) coloured(200,0,0,255)
lastpoint = 1
lastX = TOPx
lastY = TOPy
endif
if TROUGH and (lastpoint=1 or lastpoint=0) then
DRAWSEGMENT(lastX,lastY,BOTx,BOTy) COLOURED(0,200,0,255)
DRAWTEXT("■",BOTx,BOTy,Dialog,Bold,10) coloured(0,200,0,255)
lastpoint = -1
lastX = BOTx
lastY = BOTy
endif
// 1 bullish trend is starting
if boty>prevbot and close>topy and lasty<>lasty[1] and not trendup then
trendup = 1
endif
// 1.1 bullish trend is ending
//if trough and boty<prevbot and lasty<>lasty[1] and trendup then //Nicolas
if Close < boty then
trendup=0
endif
// 2 bearish trend is starting
if topy<prevtop and close<boty and lasty<>lasty[1] and not trenddown then
trenddown = 1
endif
// 2.1 bearish trend is ending
if Close > topy then
trenddown=0
endif
//trend background color
if trendup then
backgroundcolor(0,200,0,30)
endif
if trenddown then
backgroundcolor(250,0,0,30)
endif
RETURN //TOPy as "TOPy", BOTY as "BOTy", trendup as "trendup"