Are stop orders making money? [yohtza]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © yohtza

//@version=5
indicator("Are stop orders making money? [yohtza]", overlay = true)

import yohtza/Bpa/12 

scan_for_dojis = input.bool(true, "Scan for doji signal bars")
alerts = input.bool(false, "Recieve an alert once stop order makes money")
color bl_border_color = input.color(color.new(color.gray, 100), "Border color - bull")
color br_border_color = input.color(color.new(color.gray, 100), "Border color - bear")
color bl_bg_color = input.color(color.new(color.gray, 90), "Background color - bull")
color br_bg_color = input.color(color.new(color.gray, 90), "Background color - bear")
border_style = input.string(line.style_solid, "Border style", [line.style_solid, line.style_dotted, line.style_dashed])
ema_period = input.int(20, "Ema period", minval = 5, maxval=999)
tick = syminfo.mintick
ema = ta.ema(close, 20)

type BlTrade
    float stop
    float tp
    int index
type BrTrade
    float stop
    float tp
    int index

var bltrades = array.new<BlTrade>()
var brtrades = array.new<BrTrade>()

var bool blcondition = na
var bool brcondition = na
if scan_for_dojis
    blcondition := barstate.isconfirmed and high < ema and (Bpa.isBullTrendBar() or Bpa.isDoji())
    brcondition := barstate.isconfirmed and low > ema and (Bpa.isBearTrendBar() or Bpa.isDoji()) 
else
    blcondition := barstate.isconfirmed and high < ema and (Bpa.isBullTrendBar() or Bpa.isDoji())
    brcondition := barstate.isconfirmed and low > ema and (Bpa.isBearTrendBar() or Bpa.isDoji()) 

if blcondition
    size = high - low
    tp = high + size + tick
    stop = low - tick
    trade = BlTrade.new(stop, tp, bar_index)
    bltrades.push(trade)

if brcondition
    size = high - low
    tp = low - size - tick
    stop = high + tick
    trade = BrTrade.new(stop, tp, bar_index)
    brtrades.push(trade)

if bltrades.size() > 0
    for [index, value] in bltrades
        if high > value.tp 
            // bull made money
            box.new(value.index -1, value.tp, value.index + 1, value.stop, border_color =  bl_border_color, bgcolor = bl_bg_color, border_style = border_style)
            bltrades.remove(index)
            if alerts
                alert("stop order bulls made money")
            break
        if low < value.stop
            // stop hit
            bltrades.remove(index)

if brtrades.size() > 0
    for [index, value] in brtrades
        if low < value.tp 
            // bears made money
            box.new(value.index -1, value.tp, value.index + 1, value.stop, border_color =  br_border_color, bgcolor = br_bg_color, border_style = border_style)
            brtrades.remove(index)
            if alerts
                alert("stop order bears made money")
            break
        if high > value.stop
            // stop hit
            brtrades.remove(index)



