WON Weeklies
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Amphibiantrading

//@version=5
indicator("WON Weeklies", overlay = true)

//inputs
var g_5weeks    = "5 Weeks or More Up"
showUpWeks      = input(true, 'Highlight 5 or more weeks up in a row', group = g_5weeks)
boxBgCol        = input(color.new(color.blue,90), 'Box Background Color',group = g_5weeks)
boxBorderCol    = input(color.blue, 'Box Border Color',group = g_5weeks)

var g_3wt       = '3 Weeks Tight'
twtBg           = input(false, "Color background if 3WT", group = g_3wt, inline = 'a')
circleClose     = input(true, "Circles around tight closes",group = g_3wt, inline ='b')
circleClo       = input(color.new(color.aqua,70), "",group = g_3wt, inline = 'b')
bgCol           = input(color.new(color.aqua,70), "",group = g_3wt, inline = 'a')
tightMax        = input.float(1.5, "Tight Close Max", minval = .25, maxval = 3, step = .25, group = g_3wt)

// 5 or more weeks in a row up
// declare variables for box inputs
var int startindex = na
var float lowestlow = na
var float highesthigh = na
var box five_weeks = na
var fiveWeekBool = false

// define positive close and make sure it is confirned
closeup     = close > close[1] and  timeframe.isweekly and barstate.isconfirmed
closedown   = close < close[1] and  timeframe.isweekly and barstate.isconfirmed

// declare counter and start variable
var start_counter   = false
var counter         = 0

//start the counter 
start_counter   := closeup ? true : false
counter         := start_counter ? counter + 1 : counter

//declare arrays to get the low and high values
var lows    = array.new<float>(5)
var highs   = array.new<float>(5)

if counter == 5
    lows        := array.from(low[4], low[3], low[2], low[1], low)
    highs       := array.from(high[4], high[3], high[2], high[1], high)

// reassign the box variables to get get the highs, lows and start bar_index
lowestlow   := array.min(lows)
highesthigh := array.max(highs)
startindex  := counter >= 5 ? bar_index - 4 : na

//plot the box
if counter == 5 and na(five_weeks) and showUpWeks
    five_weeks := box.new(startindex, highesthigh, bar_index, lowestlow, border_color = boxBorderCol, border_style = line.style_dotted, bgcolor = boxBgCol)
    fiveWeekBool := true

// change box variables if more than 5 weeks up in a row
if closeup and not na(five_weeks) and showUpWeks
    box.set_right(five_weeks, bar_index)
    box.set_top(five_weeks, math.max(high, highesthigh))

// reset the counters
start_counter   := closedown ? false : start_counter
counter         := not start_counter ? 0 : counter

// reset the arrays and box
if counter == 0
    array.clear(lows)
    array.clear(highs)
    five_weeks := na
    fiveWeekBool := false

// 3 Weeks Tight 
//calculate 3wt
percentchangefirst  = ((close - close[1]) / close[1]) * 100
percentchangesecond = ((close[1] - close[2]) / close[2]) * 100
twt                 = math.abs(percentchangefirst) <= tightMax and math.abs(percentchangesecond) <= tightMax and timeframe.isweekly

//create label circles
if twt and circleClose
    label.new(bar_index[1], close[1], style = label.style_circle, color=circleClo)

// color the background
bgcolor(twt and twtBg and not twt[1]? bgCol : na)
bgcolor(twt and twtBg and not twt[1]? bgCol : na, offset = -1)
bgcolor(twt and twtBg and not twt[1]? bgCol : na, offset = -2)
bgcolor(twt and twt[1] and twtBg? bgCol : na )

//alerts
alertcondition(twt, '3 Weeks Tight', '{{ticker}} 3 Weeks Tight')
alertcondition(fiveWeekBool and not fiveWeekBool[1], '5 Weeks Up', '{{ticker}} up 5 weeks in a row')
