Day Week Month High & Low
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © sxiong1111
// Script Created On:   7/28/2022
// Script Updated On:   8/9/2022
// Script Version:      1.2
// Description:         

//@version=5
indicator(title = "Day Week Month High & Low", shorttitle = "DWM High & Low", overlay = true)


// Variables and Inputs
userTimeFrame2D = input.bool(false, "Show 2-Day High & Low Instead of current Day, Week or Month?", tooltip = "Enabling this, overrides/ignores the timeframe setting directly below.\n\nThis setting shows the 2-Day high and low.  It compares the latest trading day with the previous trading day and shows the highest of the two & shows the lowest of the two.", group = "Settings")
userTimeFrame = input.timeframe(title = "Timeframe", defval = "D", options = ['60', 'D', 'W', 'M'], tooltip = "You can choose from HOUR (60), DAY (D), WEEK (W) or MONTH (M).\n\nThis indicator was specifically designed for showing the high & low for the DAY, WEEK or MONTH timeframe, but for those who are day trading and like to keep tight entries & stops, can use the 1 hour timeframe setting.  Please note that each time an hour has passed (when the market is currently open), the 1 hour timeframe setting resets to use the current 1 hour bar.  This can be useful for those who scalp trades.", group = "Settings")
lineColorH = input.color(title = "Day, Week or Month High Line Color", defval = color.new(#F19CBB, 60), tooltip = "Color for the high line.", group = "Settings")
lineColorL = input.color(title = "Day, Week or Month Low Line Color", defval = color.new(#F19CBB, 60), tooltip = "Color for the low line.", group = "Settings")
lineStyle = input.string(defval = "Dashed", title = "Line Style", options = ["Dashed", "Dotted", "Solid"], tooltip = "You can choose the line style as DASHED, DOTTED or SOLID.", group = "Settings")
lineAlt = input.bool(false, "Change the Line Color If the Day High/Low Matches Week High/Low?", tooltip = "This setting only works if the chosen timeframe is set to the DAY timeframe (and also works if you enable the 2-Day high & low).  This setting could potentially benefit swing traders who opens & closes trades on a weekly basis.\n\nAllows you to use an alternate color (that you can define directly below) when the Day's High or Low matches the Week's High or Low.\n\nThis could potentially signify a resistance or support level for the week.  Normally, this wouldn't be valid or make sense on the first trading day of the week, since the day's high & low is always the same as the week's high & low (on the first trading day of the week).", group = "Settings")
lineColorAltH = input.color(title = "Day High Line Color (if the Day's High Matches the Week's High)", defval = color.new(#FFBF00, 40), tooltip = "Color for the high line (if the timeframe setting is set to DAY and the day's high matches the week's high).", group = "Settings")
lineColorAltL = input.color(title = "Day Low Line Color (if the Day's Low Matches the Week's Low)", defval = color.new(#FFBF00, 40), tooltip = "Color for the low line (if the timeframe setting is set to DAY and the day's low matches the week's low).", group = "Settings")


var price2D_High = 0.0
var price2D_Low = 0.0
priceH_High = request.security(syminfo.tickerid, "60", high)
priceH_Low = request.security(syminfo.tickerid, "60", low)
pricePD_High = request.security(syminfo.tickerid, "D", high[1])
pricePD_Low = request.security(syminfo.tickerid, "D", low[1])
priceD_High = request.security(syminfo.tickerid, "D", high)
priceD_Low = request.security(syminfo.tickerid, "D", low)
priceW_High = request.security(syminfo.tickerid, "W", high)
priceW_Low = request.security(syminfo.tickerid, "W", low)
priceM_High = request.security(syminfo.tickerid, "M", high)
priceM_Low = request.security(syminfo.tickerid, "M", low)
tmp_priceHigh = 0.0
tmp_priceLow = 0.0
var userLineStyle = "line.style_dashed"

var ln_max = line.new(na, na, na, na, extend = extend.left, style = line.style_dashed, width = 1, color = lineColorH)       // Upper Line
var ln_min = line.new(na, na, na, na, extend = extend.left, style = line.style_dashed, width = 1, color = lineColorL)       // Lower Line


// Configure User-Preferred Line Style
if (lineStyle == "Dashed")
    userLineStyle := line.style_dashed
else if (lineStyle == "Dotted")
    userLineStyle := line.style_dotted
else
    userLineStyle := line.style_solid


// Calculations
price2D_High := pricePD_High > priceD_High ? pricePD_High : priceD_High
price2D_Low := pricePD_Low < priceD_Low ? pricePD_Low : priceD_Low


// Draws the horizontal lines
if bar_index > 1
    if (userTimeFrame2D)
        line.set_xy1(ln_max, bar_index - 1, price2D_High)
        line.set_xy2(ln_max, bar_index, price2D_High)
        line.set_xy1(ln_min, bar_index - 1, price2D_Low)
        line.set_xy2(ln_min, bar_index, price2D_Low)
    else
        if (userTimeFrame == "60")
            tmp_priceHigh := priceH_High
            tmp_priceLow := priceH_Low
        else if (userTimeFrame == "D")
            tmp_priceHigh := priceD_High
            tmp_priceLow := priceD_Low
        else if (userTimeFrame == "W")
            tmp_priceHigh := priceW_High
            tmp_priceLow := priceW_Low
        else
            tmp_priceHigh := priceM_High
            tmp_priceLow := priceM_Low
        
        line.set_xy1(ln_max, bar_index - 1, tmp_priceHigh)
        line.set_xy2(ln_max, bar_index, tmp_priceHigh)
        line.set_xy1(ln_min, bar_index - 1, tmp_priceLow)
        line.set_xy2(ln_min, bar_index, tmp_priceLow)


line.set_style(ln_max, userLineStyle)
line.set_style(ln_min, userLineStyle)
if ((lineAlt == true) and (userTimeFrame == "D" or userTimeFrame2D == true))
    if (priceD_High >= priceW_High)
        line.set_color(ln_max, lineColorAltH)
    else
        line.set_color(ln_max, lineColorH)
    if (priceD_Low <= priceW_Low)
        line.set_color(ln_min, lineColorAltL)
    else
        line.set_color(ln_min, lineColorL)
else
    line.set_color(ln_max, lineColorH)
    line.set_color(ln_min, lineColorL)

