You are on page 1of 2

//@version=4

strategy("Bollinger Bands and Fibonacci Retracement", overlay=true)

// Inputs

BBlength = input(20, title="Bollinger Bands Length")

BBmult = input(2.0, title="Bollinger Bands Standard Deviation Multiplier")

FibLevel1 = input(0.236, title="Fibonacci Level 1")

FibLevel2 = input(0.382, title="Fibonacci Level 2")

FibLevel3 = input(0.618, title="Fibonacci Level 3")

// Bollinger Bands

BBupper = sma(close, BBlength) + BBmult * stdev(close, BBlength)

BBlower = sma(close, BBlength) - BBmult * stdev(close, BBlength)

// Fibonacci Retracement Levels

Hi = highest(close, 50)

Lo = lowest(close, 50)

Ret1 = Hi - (Hi - Lo) * FibLevel1

Ret2 = Hi - (Hi - Lo) * FibLevel2

Ret3 = Hi - (Hi - Lo) * FibLevel3

// Plots

plot(BBupper, color=color.blue)

plot(BBlower, color=color.blue)

plot(Ret1, color=color.purple)

plot(Ret2, color=color.purple)

plot(Ret3, color=color.purple)

// Buy/Sell Signals

longSignal = crossover(close, BBupper) and close > Ret1


shortSignal = crossunder(close, BBlower) and close < Ret1

// Strategy Entry and Exit

if (longSignal)

strategy.entry("Buy", strategy.long)

if (shortSignal)

strategy.entry("Sell", strategy.short)

// Strategy Stop Loss and Take Profit

stopLoss = Ret3

takeProfit = Ret2

strategy.exit("Exit Buy", "Buy", stop=stopLoss, limit=takeProfit)

strategy.exit("Exit Sell", "Sell", stop=stopLoss, limit=takeProfit)

// Plot Buy and Sell Signals

plotshape(longSignal, style=shape.triangleup, color=color.green, location=location.belowbar,


size=size.small, transp=0)

plotshape(shortSignal, style=shape.triangledown, color=color.red, location=location.abovebar,


size=size.small, transp=0)

You might also like