You are on page 1of 4

//@version=4

study(shorttitle="The_Trend_Trader - 2", title="The_Trend_Trader - 2", overlay=true)


// 
res          = input("D",title = "Main Trend",type= input.resolution)
BBperiod      = input(6)
BBdeviations  = input (0.10)
UseATRfilter  = input (true)
ATRperiod     =input  (1)
hl            = input (false)
//
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=3,transp=0,title="Trend Line") 
plotshape(buy == 1 and hl == false? TrendLine-atr(1) :na, text='B', style= shape.triangleup,
location=location.absolute, color=color.blue, textcolor=color.white, offset=0,
transp=0,size=size.small)
plotshape(sell == 1 and hl == false ?TrendLine+atr(1):na, text='B', style=shape.triangledown,
location=location.absolute, color=color.red, textcolor=color.white, offset=0,
transp=0,size=size.small)
//
alertcondition(sell == 1 ,title="Sell",message="Sell")
alertcondition(buy == 1 ,title="Buy",message="Buy")
alertcondition(buy == 1 or sell == 1 ,title="Buy/Sell",message="Buy/Sell")

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

CCI        =input (3)


ATR        =input (10)
Multiplier =input (1)
original   =input (false)
thisCCI = cci(close, CCI)
lastCCI = nz(thisCCI[1])
calcx()=> 
    Dn= high + Multiplier * sma(tr,ATR)
    Up= low - Multiplier * sma(tr,ATR)
    if (thisCCI >= 0 and lastCCI < 0) 
        Up := Dn[1]
    if (thisCCI <= 0 and lastCCI > 0) 
        Dn := Up[1]

    if (thisCCI >= 0)
        if (Up < Up[1])
            Up := Up[1]
    else
        if (thisCCI <= 0)
            if (Dn > Dn[1])
                Dn := Dn[1]

  
    x = 0.0
    x := thisCCI >= 0 ?Up:thisCCI <= 0 ?Dn:x[1]
  x

tempx = calcx()

calcswap() =>
    swap = 0.0
    swap := tempx>tempx[1]?1:tempx<tempx[1]?-1:swap[1]
    swap

tempswap = calcswap()

swap2=tempswap==1?color.green:color.purple
swap3=thisCCI >=0 ?color.green:color.purple
swap4=original?swap3:swap2
//display current timeframe's Trend

//plot(tempx,color=swap4,transp=0,linewidth=4)

htfx = security(syminfo.tickerid,res,tempx[1],lookahead = barmerge.lookahead_on)


htfswap4 = security(syminfo.tickerid,res,swap4,lookahead = barmerge.lookahead_on)

plot(htfx,color=htfswap4,transp=0,linewidth=5,title="Main Trend")

You might also like