You are on page 1of 3

//@version=5

// Bollinger Bands Breakout Strategy


// The bot enters long when price crosses over the upper band
// And shorts when price crosses below the lower band
// Disclaimer:
// 1. I am not licensed financial advisors or broker dealers. I do not tell you
// when or what to buy or sell. I developed this software which enables you
// execute manual or automated trades using TradingView. The
// software allows you to set the criteria you want for entering and exiting
// trades.
// 2. Do not trade with money you cannot afford to lose. I am not accountable
for any losses.
// 3. I do not guarantee consistent profits or that anyone can make money with
no
// effort. And I am not selling the holy grail.
// 4. Every system can have winning and losing streaks.
// 5. Money management plays a large role in the results of your trading. For
// example: lot size, account size, broker leverage, and broker margin call
// rules all have an effect on results. Also, your Take Profit and Stop Loss
// settings for individual pair trades and for overall account equity have a
// major impact on results. If you are new to trading and do not understand
// these items, then I recommend you seek education materials to further your
// knowledge.
// Edited and constructed by TradeTactics, TP/SL and alert signals provided by War-
Jackel
// If this helped you and if you are able consider sending a tip
// BTC (BTC Network): 18wQFct5yhWDMhVvEM1m8b41EgvecAxhhJ
// USDT (ERC20): 0xaaf28ebb293aadfebe519a2888639d40c39419ef

strategy(shorttitle='Trade Tactics Template', title='Trade Tactics Template',


overlay=true, currency=currency.NONE, initial_capital = 1000, default_qty_type =
strategy.percent_of_equity, default_qty_value = 100, commission_value = 0.05)

//Inputs
showBB = input.bool(false, "Show Bollinger Bands")
showTrade = input.bool(false, 'Show TP/SL')
src = input(close)
length = input.int(41, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)

//Formulas
basis = ta.sma(src, length)
dev = ta.stdev(src, length)
dev2 = mult * dev

upper1 = basis + dev


lower1 = basis - dev
upper2 = basis + dev2
lower2 = basis - dev2

//Style
colorBasis = src >= basis ? color.blue : color.orange

//Plots
pBasis = plot(showBB ? basis : na, linewidth=2, color=colorBasis)
pUpper1 = plot(showBB ? upper1 : na, color=color.new(color.blue, 0),
style=plot.style_circles)
pUpper2 = plot(showBB ? upper2 : na, color=color.new(color.blue, 0))
pLower1 = plot(showBB ? lower1 : na, color=color.new(color.orange, 0),
style=plot.style_circles)
pLower2 = plot(showBB ? lower2 : na, color=color.new(color.orange, 0))

//Fills
fill(pBasis, pUpper2, color =showBB ? color.new(color.blue, 80) : na)
fill(pUpper1, pUpper2, color=showBB ? color.new(color.blue, 80) : na)
fill(pBasis, pLower2, color =showBB ? color.new(color.orange, 80) : na)
fill(pLower1, pLower2, color=showBB ? color.new(color.orange, 80) : na)

// Strategy code begins here


import TradingView/Strategy/2 as css

//DATE RANGE FILTER {


i_tradeDirection = input.string(title='Trade Direction',
defval=strategy.direction.all, options=[strategy.direction.all,
strategy.direction.long, strategy.direction.short], group='Trade Filters')
strategy.risk.allow_entry_in(i_tradeDirection)
i_startTime = input.time(defval=timestamp('01 Jan 1999 00:00 +0000'), title='Start
Time', group='Trade Filters')
i_endTime = input.time(defval=timestamp('01 Jan 2099 00:00 +0000'), title='End
Time', group='Trade Filters')
inDateRange = time >= i_startTime and time <= i_endTime
//}
//SL TP PERCENT {
string sltp= "STOP/TAKE PROFIT"
usetpsl = input.bool(false, 'Use TP/SL', inline=sltp, group=sltp)
float percentStop = input.float(1.0, "Stop %", minval = 0.0, step = 0.25,
group=sltp, inline='percent')
float percentTP = input.float(2.0, "Limit %", minval = 0.0, step = 0.25,
group=sltp, inline='percent')
//}
//ALERTS {
i_alert_txt_entry_long = input.text_area(defval = "", title = "Long Entry Message",
group = "Alerts")
i_alert_txt_entry_short = input.text_area(defval = "", title = "Short Entry
Message", group = "Alerts")
i_alert_txt_exit_long = input.text_area(defval = "", title = "Long Exit Message",
group = "Alerts")
i_alert_txt_exit_short = input.text_area(defval = "", title = "Short Exit Message",
group = "Alerts")
//}

// Using the input stop/ limit percent, we can convert to ticks and use the ticks
to level functions.
// This can be used to calculate the take profit and stop levels.
float sl = css.ticksToStopLevel (css.percentToTicks (percentStop))
float tp = css.ticksToTpLevel (css.percentToTicks (percentTP))

exitPrice = strategy.closedtrades.exit_price(strategy.closedtrades-1)
bias = math.sign(strategy.position_size)
avg = strategy.position_avg_price

// Conditions used to reference position and determine trade bias


bool long = strategy.position_size > 0
bool short = strategy.position_size < 0
bool enterLong = long and not long [1]
bool enterShort = short and not short[1]
bool enter = enterLong or enterShort
bool exit = strategy.position_size == 0 and not (strategy.position_size ==
0)[1]
bool flat = strategy.position_size == 0

//STRATEGY PLOTS {
avgerage = plot(showTrade ? avg : na, "ENTRY", not enter ?
color.new(color.white, 0) : na, 2, plot.style_linebr)
slp = plot(showTrade ? sl : na, "STOP LOSS", not enter ?
color.new(color.red, 60) : na, 4, plot.style_linebr)
tpp = plot(showTrade ? tp : na, "TAKE PROFIT", not enter ?
color.new(color.green, 60) : na, 4, plot.style_linebr)

fill(tpp, avgerage, color = color.new(color.green, 80), title='TP Background')


fill(avgerage, slp, color = color.new(color.red, 80) , title= 'SL Background')
//}

if usetpsl == true and long


css.exitPercent("exit long", percentStop, percentTP,
alertMessage=i_alert_txt_exit_long)

if usetpsl == true and short


css.exitPercent("exit short", percentStop, percentTP,
alertMessage=i_alert_txt_exit_short)

long_entry = ta.crossover(src, upper1)


short_entry = ta.crossunder(src, lower1)

bool longCondition = long_entry


bool shortCondition = short_entry

// Create entries based on the cross conditions for both trades biases.
if longCondition and inDateRange
strategy.close("SHORT", "SX" , alert_message=i_alert_txt_exit_short)
strategy.entry("LONG", strategy.long, alert_message=i_alert_txt_entry_long)

if shortCondition and inDateRange


strategy.close("LONG", "LX", alert_message=i_alert_txt_exit_long)
strategy.entry("SHORT", strategy.short, alert_message=i_alert_txt_entry_short)

You might also like