Pluto Star - Bollinger Band Trap
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
//@version=4

//DESCRIPTION
//Pluto star appears on a chart when price goes in the in the extreme price range territory, i.e. beyond 2 standard deviation from the mean (or mid Bollinger Band).
//What makes a Pluto Star appear on a chart:
//1.    Check if the candle[1]'s' high and low, both are completely outside of the Bollinger Bands (close, 20, 2) - Lets call it Pluto Star Candle
//2.    Pluto Star Candle must not be a result of sudden price movement. Hence the previous candle[2] must give a BB Blast. 
//      In other words, the candle[2] must have it's either open or close outside of Bollinger Bands, to confirm a BB Blast before the Pluto Star
//3.    Candle, following the Pluto Star must not break the high (in case of upper BB i.e. short call) or low (in case of lower BB, i.e. long call), to confirm the reversal to the mean
//      This implies that Pluto Star appears on chart, above/below the next candle of actual Pluto Star Candle       

//----- The above 3 conditions make a Pluto Star appear on a chart. But one must wait for a trade signal. Read the following conditions

//4.    There is a signal line, which is nothing but ema(close,5)
//5.    The red dotted line is the signal range (and also acts as Stop Loss). The price must close above/below the signal line within the signal range  
//6.    For a red Pluto Star (short call), the price must close below the signal line, within next 6 candles (signal range). Else there is no trigger for a trade
//7.    For a green Pluto Star (long call), the price must close above the signal line, within next 6 candles (signal range). Else there is no trigger for a trade
//8.    If any of the candle crosses the Stop Loss line within signal range, there is no trigger for a trade
//9.    In a normal scenario, the price must return to the mean, i.e. mid Bollinger Band. In best case scenario, it must go to the opposite side Bollinger Band.
//Recommendation: Test it with Nifty and Bank Nifty charts on 30 mins and 1 hour timeframes 


study(title="Pluto", overlay=true)

var bool isPlutoS = false
var bool isPlutoL = false

//Read BB data
[mBB, uBB, lBB] = bb(close, 20, 2)

//Check if there previous candle was a Pluto Star Candle for Long call
if (high < high[1]) and (high[1] > uBB[1]) and (low[1] > uBB[1]) and ((open[2] > uBB[2]) or (close[2] > uBB[2]))
    isPlutoS := true
else
    isPlutoS := false

//Check if there previous candle was a Pluto Star Candle for Long call
if (low > low[1]) and (high[1] < lBB[1]) and (low[1] < lBB[1]) and ((open[2] < lBB[2]) or (close[2] < lBB[2]))
    isPlutoL := true
else
    isPlutoL := false

//Plot signal line
ema5 = ema(close, 5)
plot(ema5, color=color.gray, style=plot.style_line, linewidth=1,title="Signal Line")

//Plot the star above/below the current candle
plotchar(isPlutoS, title="SELL", char='★', location=location.abovebar, color=color.red, size=size.small)
plotchar(isPlutoL, title="BUY", char='★', location=location.belowbar, color=color.green, size=size.small)


//Plot the signal range and stop loss dotted line
if isPlutoS and (high[2] > high[1])
    line.new(bar_index[1], high[2], bar_index+5, high[2], style=line.style_dotted, color=color.red, width=2)

if isPlutoS and (high[2] < high[1])
    line.new(bar_index[1], high[1], bar_index+5, high[1], style=line.style_dotted, color=color.red, width=2)


if isPlutoL and (low[2] < low[1])
    line.new(bar_index[1], low[2], bar_index+5, low[2], style=line.style_dotted, color=color.red, width=2)

if isPlutoL and (low[2] > low[1])
    line.new(bar_index[1], low[1], bar_index+5, low[1], style=line.style_dotted, color=color.red, width=2)


isPlutoS := false
isPlutoL := false
