FIREFLY INTERVAL RELEASE
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © fireflies_

// @version=5
indicator("FIREFLY INTERVAL RELEASE", overlay=true)
import TradingView/ta/5

// ---------------------------------------------------
// MA
// ---------------------------------------------------
src = close
ma1_inp = input.int(25, "ma1")
ma2_inp = input.int(99, "ma2")
ma1 = ta.sma(src, ma1_inp)
ma2 = ta.sma(src, ma2_inp)
plot(ma1, "ma1", color = color.rgb(242, 12, 177), linewidth = 1)
plot(ma2, "ma2", color = color.rgb(24, 165, 246))

// ---------------------------------------------------
// MACD
macd_fast_inp = input.int(12, "MACD FAST")
macd_slow_inp = input.int(26, "MACD SLOW")
macd_signal_inp = input.int(title="MACD SIGNAL",  minval = 1, maxval = 50, defval = 9)
macd_source = input.string(title = "MACD SOURCE", defval = "EMA", options = ["SMA", "EMA"])
macd_signal_source = input.string(title = "MACD SIGNAL SOURCE", defval = "EMA", options = ["SMA", "EMA"])
// HISTOGRAM
macd_fast = macd_source == "SMA" ? ta.sma(src, macd_fast_inp) : ta.ema(src, macd_fast_inp)
macd_slow = macd_source == "SMA" ? ta.sma(src, macd_slow_inp) : ta.ema(src, macd_slow_inp)
macd_signal = macd_signal_source == "SMA" ? ta.sma((macd_fast - macd_slow), macd_signal_inp) : ta.ema((macd_fast - macd_slow), macd_signal_inp)
hist = macd_fast - macd_slow - macd_signal

// ATR 
atr_inp = input.int(14, title="ATR LENGTH")
atr_value = ta.atr(atr_inp)

// BULL
var line bull_key_high = na
var line bull_key_low = na
var label bull_point = na
var bull_arr_line = array.new_line()
// BEAR
var line bear_key_high = na
var line bear_key_low = na
var label bear_point = na
var bear_arr_line = array.new_line()

// BULL STOP
var float bull_stop = na
var label bull_stop_key = na
var bool stop_isTrue = false

// BUY CONDITION
var float bull_point_high = na
var bool buy_isTrue = false
var label buy_key = na


if hist < 0
    if hist[1] < hist 
        if hist[2] > hist[1]
            label.delete(bull_point)
            line.delete(bull_key_high)
            line.delete(bull_key_low)
            bull_point := label.new(bar_index, low, text = "HIGH: " + str.tostring(high) + "\nLOW: " + str.tostring(low), style = label.style_label_up, color = color.rgb(39, 176, 85), yloc = yloc.belowbar, textcolor = color.white, size = size.normal)
            bull_key_high := line.new(bar_index[1], high, bar_index, high, width=1, color=color.rgb(76, 175, 79, 100), extend = extend.right, style = line.style_dashed)
            bull_key_low := line.new(bar_index[1], low, bar_index, low, width=1, color=color.rgb(76, 175, 79, 100), extend = extend.right, style = line.style_dashed)
            linefill.new(bull_key_high, bull_key_low, color = color.rgb(76, 175, 79, 80))
            array.push(bull_arr_line, line.new(bar_index[1], high, bar_index, high, width=1, color=color.green, extend = extend.both))
            bull_point_high := high
            buy_isTrue := true
else
    if hist[1] > hist
        if hist[2] < hist[1]
            line.delete(bear_key_low)
            line.delete(bear_key_high)
            label.delete(bear_point)
            bear_point := label.new(bar_index, high, text = "HIGH: " + str.tostring(high) + "\nLOW: " + str.tostring(low), style = label.style_label_down, color = color.rgb(176, 39, 39), yloc = yloc.abovebar, textcolor = color.white, size = size.normal)
            bear_key_low := line.new(bar_index[1], low, bar_index, low, width=1, color=color.rgb(255, 82, 82, 100), extend = extend.right, style = line.style_dashed)
            bear_key_high := line.new(bar_index[1], high, bar_index, high, width=1, color=color.rgb(255, 82, 82, 100), extend = extend.right, style = line.style_dashed)
            linefill.new(bear_key_low, bear_key_high, color = color.rgb(255, 82, 82, 80))
            array.push(bear_arr_line, line.new(bar_index[1], low, bar_index, low, width=1, color=color.red, extend = extend.both))

// RESISTANCE LINE
bull_line_inp = input.int(1, "BOTTOM LINE")
if array.size(bull_arr_line) > bull_line_inp
    bullal = array.shift(bull_arr_line)
    line.delete(bullal)
bear_line_inp = input.int(1, "UPPER LINE")
if array.size(bear_arr_line) > bear_line_inp
    bearal = array.shift(bear_arr_line)
    line.delete(bearal)


// BUY SIGNAL
// ---------------------------------------------------
if  buy_isTrue and close > bull_point_high
    bull_stop := math.round(low - atr_value, 4)
    label.delete(buy_key)
    buy_key := label.new(bar_index, high, text = "BUY\n STOP VALUE:" + str.tostring(bull_stop), style = label.style_label_lower_right, color = color.rgb(0, 179, 249, 10), textcolor = color.white)
    label.delete(bull_stop_key)
    buy_isTrue := false
    stop_isTrue := true

if stop_isTrue and low < bull_stop
    bull_stop_key := label.new(bar_index, low, text = "STOP", style = label.style_label_lower_left, color = color.rgb(251, 164, 3, 20), textcolor = color.rgb(248, 246, 243))
    stop_isTrue := false

