IPDA operating range by makuchaku
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © makuchaku

//@version=4
study("IPDA operating range by makuchaku", shorttitle="IPDA operating range", overlay=true)


// all timeframes <= 2H
d_htf_h = security(syminfo.tickerid, "D", high, barmerge.gaps_off, barmerge.lookahead_on)
d_htf_l = security(syminfo.tickerid, "D", low, barmerge.gaps_off, barmerge.lookahead_on)
d_htf_h1 = security(syminfo.tickerid, "D", high[1], barmerge.gaps_off, barmerge.lookahead_on)
d_htf_l1 = security(syminfo.tickerid, "D", low[1], barmerge.gaps_off, barmerge.lookahead_on)    

// all timeframes > 2H & < D
w_htf_h = security(syminfo.tickerid, "W", high, barmerge.gaps_off, barmerge.lookahead_on)
w_htf_l = security(syminfo.tickerid, "W", low, barmerge.gaps_off, barmerge.lookahead_on)
w_htf_h1 = security(syminfo.tickerid, "W", high[1], barmerge.gaps_off, barmerge.lookahead_on)
w_htf_l1 = security(syminfo.tickerid, "W", low[1], barmerge.gaps_off, barmerge.lookahead_on)    

// For Daily
m_htf_h = security(syminfo.tickerid, "M", high, barmerge.gaps_off, barmerge.lookahead_on)
m_htf_l = security(syminfo.tickerid, "M", low, barmerge.gaps_off, barmerge.lookahead_on)
m_htf_h1 = security(syminfo.tickerid, "M", high[1], barmerge.gaps_off, barmerge.lookahead_on)
m_htf_l1 = security(syminfo.tickerid, "M", low[1], barmerge.gaps_off, barmerge.lookahead_on)    

// For Weekly
m6_htf_h = security(syminfo.tickerid, "6M", high, barmerge.gaps_off, barmerge.lookahead_on)
m6_htf_l = security(syminfo.tickerid, "6M", low, barmerge.gaps_off, barmerge.lookahead_on)
m6_htf_h1 = security(syminfo.tickerid, "6M", high[1], barmerge.gaps_off, barmerge.lookahead_on)
m6_htf_l1 = security(syminfo.tickerid, "6M", low[1], barmerge.gaps_off, barmerge.lookahead_on)    



//////////////////// IPDA RANGE //////////////////
htf_h = 0.0
htf_l = 0.0
htf_h1 = 0.0
htf_l1 = 0.0

if timeframe.isintraday and timeframe.multiplier <= 120
    htf_h := d_htf_h
    htf_l := d_htf_l
    htf_h1 := d_htf_h1
    htf_l1 := d_htf_l1

if timeframe.isintraday and timeframe.multiplier > 120
    htf_h := w_htf_h
    htf_l := w_htf_l
    htf_h1 := w_htf_h1
    htf_l1 := w_htf_l1

if timeframe.period == "D"
    htf_h := m_htf_h
    htf_l := m_htf_l
    htf_h1 := m_htf_h1
    htf_l1 := m_htf_l1

if timeframe.period == "W"
    htf_h := m6_htf_h
    htf_l := m6_htf_l
    htf_h1 := m6_htf_h1
    htf_l1 := m6_htf_l1


range_high = htf_h1
if htf_h > htf_h1
    range_high := htf_h
range_low = htf_l1
if htf_l < htf_l1
    range_low := htf_l

range_50pc = range_low + ((range_high - range_low)/2)
range_25pc = range_low + ((range_high - range_low)/4)
range_75pc = range_low + (3*(range_high - range_low)/4)
    
x_value_high = valuewhen((high >= range_high), bar_index, 0)
x_value_low = valuewhen((low <= range_low), bar_index, 0)


var line lh = na
var line ll = na
var line l50 = na
var line l25 = na
var line l75 = na
if barstate.islast
    line.delete(lh)
    line.delete(ll)
    line.delete(l50)
    line.delete(l75)
    line.delete(l25)
        
    lh := line.new(x1=x_value_high, y1=range_high, x2=bar_index, y2=range_high, extend=extend.right, color=color.new(color.red, 0), style=line.style_dashed, width=2)
    ll := line.new(x1=x_value_low, y1=range_low, x2=bar_index, y2=range_low, extend=extend.right, color=color.new(color.green, 0), style=line.style_dashed, width=2)
    l50 := line.new(x1=bar_index-1, y1=range_50pc, x2=bar_index, y2=range_50pc, extend=extend.right, color=color.new(color.black, 50), style=line.style_dashed, width=2)
    l25 := line.new(x1=bar_index-1, y1=range_25pc, x2=bar_index, y2=range_25pc, extend=extend.right, color=color.new(color.gray, 75), style=line.style_dashed, width=2)
    l75 := line.new(x1=bar_index-1, y1=range_75pc, x2=bar_index, y2=range_75pc, extend=extend.right, color=color.new(color.gray, 75), style=line.style_dashed, width=2)
//////////////////// IPDA RANGE //////////////////

