inChart - Lib
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © tv94067

//@version=5

// determine if price value is between chart high + x% and low - x% on the visible chart.
library("inChart", true)

// @function        Condition to determine if a given bar is visible on the chart.
// @returns         (bool) True if the current bar is visible.

barIsVisible() =>
    bool result = time >= chart.left_visible_bar_time and time <= chart.right_visible_bar_time

// @function        Determines the value of the highest `high` in visible bars.
// @returns         (float) The maximum high value of visible chart bars. 

chartHigh() => 
    var float chartHigh = na
    if barIsVisible() and high >= nz(chartHigh, high)
        chartHigh := high
    float result = chartHigh

// @function        Determines the value of the lowest `low` in visible bars.
// @returns         (float) The minimum low value of visible chart bars. 

chartLow() => 
    var float chartLow = na
    if barIsVisible() and low <= nz(chartLow, low)
        chartLow := low
    float result = chartLow

// @function        Condition to determine if price value is between chart high + x % and low - x % on the visible chart.
// @returns         (bool) True if price value is between chart high + x% and low - x% on the visible chart.

export inChart(float _price, float x_percent) =>
    bool result = _price <= chartHigh() * (1 + x_percent / 100) and _price >= chartLow() * (1 - x_percent / 100)


// Example code of inCHart

_price1 = chartHigh() + chartHigh() * 0.3 / 100
_price2 = chartLow() - chartLow() * 0.5 / 100

plot(inChart(_price1, 0.50) ? _price1 : na, 'Chart High + x %', color.green)
plot(inChart(_price2, 1.25) ? _price2 : na, 'Chart Low - x %', color.red)



