Identify Current Index?

Hi, trying to modify master Tiki’s Range Forecast code to plot on the current index only (full code at bottom), so for the upper and lower plots want to replace “if (i > 0) {” with something like If(currentindex){ …

Been through the github and many indicators and assuming there is a way to identify the current bar, but not seeing/understanding how to do this. Starting to understand how to use “prior” and “back” and how to get “i”, but my brain can’t cipher how to get there with these.

FYI, using “If(prior)” it doesn’t plot on prior bars when the chart is initially loaded, but unfortunately it doesn’t repaint them to null when they are complete, so end up with the same issue.

Thank you for any help

Original code:

class tikiRangeForecast {
init() {
this.tickSize = this.contractInfo.tickSize
}

map(d, i, history) {
    if (i > 0) {
        const prior = history.prior()
        const range = prior.high() - prior.low()
        const high = d.high() 
        const low = d.low()
        const currentRange = high - low
        const rangeDiff = range - currentRange
        const upper = high + rangeDiff
        const lower = low - rangeDiff
        
        let style
        if (currentRange >= range) {
            style = {
                upper: {
                    color: 'transparent',
                },
                lower: {
                    color: 'transparent'
                }
            }
        }
        
        return {
            upper,
            lower,
            style
        };
    }
}

}

Hey @holdtheonions,

You can use d.isLast() to determine if you are at the current index or not. It updates each on each tick so you it should work in real time for you. Try replacing your conditions with if(d.isLast()) { ... } and you should have better luck with it.

Hi, thanks a bunch. Took me a bit, but finally got it to work!

I think I was missing the “is” previously and not getting anything, but seeing it referenced here now though graphics/GraphicsResponse | Tradovate Custom Indicators

map(d, i, history) {
    if (i>0){
        const prior = history.prior();
        const range = prior.high() - prior.low()
        const high = d.high() 
        const low = d.low()
        const currentRange = high - low
        const rangeDiff = range - currentRange
        
        if (d.isLast()) {
        const upper = high + rangeDiff
        const lower = low - rangeDiff
        
        return {
            upper,
            lower,
        };
        
        }
        
        if (!d.isLast()) {
        const upper = null
        const lower = null
        
        return {
            upper,
            lower,
        };
        
        }
        
    }        
    
}