Example code to get the highest high in the last 14 bars

Hi Devs,

can you please help with an example on how i can save in a const/var the current highest high in the last 14 bars of the chart?

i tried to code using the MovingHigh function and was not successful.

thanks in advance,
LD.

Hi @diazlaz,

Probably the easiest way to do this would be to create a little tool like this:

function Highs(period) {
    function highs(input) {
        return highs.push(input)
    }
    highs.push = function(input) {
        highs.state.values.push(input)
        //if we have more that period values...
        if(highs.state.values.length > period) {
           highs.state.values.shift() //...drop the last value in the set
        }
        //find highest value from set via reduce and Math.max
        return highs.state.values.reduce((a, b) => Math.max(a, b), 0)
    }

    highs.reset = () => highs.state = { values: [] }

    highs.reset()

    return highs
}

Then in your indicator code:

class HighsIndicator {
    init() { this.highs = Highs(this.props.period) }
   
    map(d) {
        return highs(d.value())
    }
}

//remember to add the period as user defined input
module.exports = {
    //...rest of indicator module omitted
    params: {
        period: predef.paramSpecs.period(14) //default to 14 bar period
    }
}

For more info on the specifics of Custom Indicators, see the official docs.

thank you, Alexander, I appreciate the response.