PopGun Trading, PG Pattern Detector
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Roul_Charts

//@version=5

// PopGun Trading, PG Pattern Detector
// PG Detection by Wes Bennett (sorry - we work without ATR Calculation, and a lot of the ATR targets where unreachable)
// PG Box and CandleSize Calculation by Hawky_4s
// For the greatest Community
// June 2022

indicator(title='PopGun Trading, PG Pattern Detector', shorttitle="PG Tool", overlay=true)

// Inputs

GP0 = '===== Input ====='

PG_Box = input.bool(title='Show PG Box?', defval=false, group=GP0)
Bar_Color = input.bool(title='Change Bar Color on PG Candle?', defval=true, group=GP0)
Background_Color = input.bool(title='Change Background Color on PG Candle?', defval=true, group=GP0)


GP1 = '===== Targets ====='
Target1 = input.bool(title='Show Target 1?', defval=true, group=GP1)
Target2 = input.bool(title='Show Target 2?', defval=true, group=GP1)
Target3 = input.bool(title='Show Target 3?', defval=true, group=GP1)
Target4 = input.bool(title='Show Target 4?', defval=false, group=GP1)
Target5 = input.bool(title='Show Target 5?', defval=false, group=GP1)
Target6 = input.bool(title='Show Target 6?', defval=false, group=GP1)


// PG Detection
PG_Detector = high[2] >= high[1] and high[1] <= high and low[2] <= low[1] and low[1] >= low

//Not in Use as we think: a Pop Gun is neutral and the next candles will decide the bullish/bearish breakout. But i want to establish a switch to activate this extra mode...
//  PG_Bull = high[2] >= high[1] and high[1] <= high and low[2] <= low[1] and low[1] >= low and close >= high[1]
//  PG_Bear = high[2] >= high[1] and high[1] <= high and low[2] <= low[1] and low[1] >= low and close <= low[1]

//
// Calculations
//

candleSize = math.abs(high - low)

PG_high = high
PG_low = low

bulltarget1 = high + candleSize
bulltarget2 = high + candleSize * 2
bulltarget3 = high + candleSize * 3
bulltarget4 = high + candleSize * 4
bulltarget5 = high + candleSize * 5
bulltarget6 = high + candleSize * 6


beartarget1 = low - candleSize
beartarget2 = low - candleSize * 2
beartarget3 = low - candleSize * 3
beartarget4 = low - candleSize * 4
beartarget5 = low - candleSize * 5
beartarget6 = low - candleSize * 6


// Future Evaluations
// PG Breakout Calculation for next Version
//isUp = close > open
//isDown = close <= open
//isOutsideUp = high > high[1] and low < low[1] and isUp
//isOutsideDown = high > high[1] and low < low[1] and isDown
//isInside = high < high[1] and low > low[1]


//
// PG BOX
//


if PG_Box and PG_Detector
    string labelText = "High:" + str.tostring(high, format.mintick) + "\n" +
                       "Low:" + str.tostring(low, format.mintick) + "\n" +
                       "Size:" + str.tostring(candleSize, format.mintick) + "\n\n" +
                       "Targets above:\n" +
                       "1. " + str.tostring(bulltarget1, format.mintick) + "\n" + 
                       "2. " + str.tostring(bulltarget2, format.mintick) + "\n" + 
                       "3. " + str.tostring(bulltarget3, format.mintick) + "\n\n" + 
                       "Targets below:\n" +
                       "1. " + str.tostring(beartarget1, format.mintick) + "\n" + 
                       "2. " + str.tostring(beartarget2, format.mintick) + "\n" + 
                       "3. " + str.tostring(beartarget3, format.mintick)
                       
    label.new( x = bar_index, y = math.max(high, high[1]), text = labelText, textalign = text.align_left, color = color.new(color.orange, 50), textcolor = color.new(color.white, 0), style = label.style_label_lower_right, size = size.large)

//
// Activate Alert Menu
//

alertcondition(PG_Detector, title='PopGun Trigger', message='PG detected!')


//
// Plots and Colors
//

bgcolor((PG_Detector and Background_Color) ? color.new(color.rgb(179, 179, 179),80) : na, title='PG Candle Background')

barcolor((PG_Detector and Bar_Color) ? color.new(color.rgb(185, 46, 209),0) : na, title='PG Candle Color')


plot((PG_Detector and Target1) ? bulltarget1 : na, style=plot.style_circles, linewidth=6, title='Bullish Trade Target 1', color=color.new(color.rgb(102, 255, 102), 0))
plot((PG_Detector and Target2) ? bulltarget2 : na, style=plot.style_circles, linewidth=5, title='Bullish Trade Target 2', color=color.new(color.rgb(51, 204, 51), 0))
plot((PG_Detector and Target3) ? bulltarget3 : na, style=plot.style_circles, linewidth=4, title='Bullish Trade Target 3', color=color.new(color.rgb(0, 153, 51), 0))
plot((PG_Detector and Target4) ? bulltarget4 : na, style=plot.style_circles, linewidth=3, title='Bullish Trade Target 4', color=color.new(color.rgb(0, 153, 51), 0))
plot((PG_Detector and Target5) ? bulltarget5 : na, style=plot.style_circles, linewidth=2, title='Bullish Trade Target 5', color=color.new(color.rgb(0, 153, 51), 0))
plot((PG_Detector and Target6) ? bulltarget6 : na, style=plot.style_circles, linewidth=1, title='Bullish Trade Target 6', color=color.new(color.rgb(0, 153, 51), 0))



plot((PG_Detector and Target1) ? beartarget1 : na, style=plot.style_circles, linewidth=6, title='Bearish Trade Target 1', color=color.new(color.rgb(255, 0, 0), 0))
plot((PG_Detector and Target2) ? beartarget2 : na, style=plot.style_circles, linewidth=5, title='Bearish Trade Target 2', color=color.new(color.rgb(224, 31, 31), 0))
plot((PG_Detector and Target3) ? beartarget3 : na, style=plot.style_circles, linewidth=4, title='Bearish Trade Target 3', color=color.new(color.rgb(179, 0, 0), 0))
plot((PG_Detector and Target4) ? beartarget4 : na, style=plot.style_circles, linewidth=3, title='Bearish Trade Target 4', color=color.new(color.rgb(179, 0, 0), 0))
plot((PG_Detector and Target5) ? beartarget5 : na, style=plot.style_circles, linewidth=2, title='Bearish Trade Target 5', color=color.new(color.rgb(179, 0, 0), 0))
plot((PG_Detector and Target6) ? beartarget6 : na, style=plot.style_circles, linewidth=1, title='Bearish Trade Target 6', color=color.new(color.rgb(179, 0, 0), 0))



