Tucsky / aggr

Cryptocurrency trades aggregator
https://charts.aggr.trade/
GNU General Public License v3.0
830 stars 240 forks source link

Use setData() to update previous plot values? #259

Closed lahomagit closed 1 year ago

lahomagit commented 1 year ago

Is it possible to update previous data of a plot via the setData method of the ISeriesAPI?

This code attempts to draw a horizontal line at the high of loaded data (using a line plot as a priceline, essentially), updating previous values of the line when a new high is found, but it does not work (nor does it throw any error):

if(lastTime === 0){

    lastTime = null
    seriesData = []
    theHigh = null
    theBar = null

}

// if new bar and not first bar
if(bar.timestamp % bar.timeframe == 0 && lastTime && bar.timestamp > lastTime){

    (theBar) = {
        time: lastTime,
        value: theHigh
    }

    //push previous bar data
    seriesData.push(theBar)

}

if(!theHigh){
    theHigh = $price.high
}

if($price.high > theHigh){

    theHigh = $price.high

    if(seriesData){

        // update all values in seriesData to new high
        for(let i = 0; i < seriesData.length; i++){
            seriesData[i].value = theHigh
        }

        //update series
        try {
            series[0].setData(seriesData)     
        } catch (error) {
            console.log("error: " + error.message)
        }
    }

}

lastTime = bar.timestamp

line(theHigh)
Tucsky commented 1 year ago

Try one of these

image

high of loaded data indicator_high-pivot-pivotlength.txt

image

high found in the last n bars indicator_high-line.txt

note

lahomagit commented 1 year ago

The pivots example is excellent, thank you. Is there any way to make it automatically draw new pivots as data is added to the chart? Right now it seems to require toggling the indicator to redraw with new data, and I haven't been able to solve that.

Tucsky commented 1 year ago

You are right there was a bug in the script :) Fixed it here with option to use body of candle instead of wicks 👍🏼 2022-11-14 11 06 24 indicator_high-pivot-pivotlength copy 1.txt

Redline600 commented 1 year ago

Invalid input for function brokenarea, expected 2 arguments (lowerValue and higherValue). Dude, I'm getting such an error, I haven't been able to solve it for 2 days, can you help me? Can you give me this code working. please i need this

lahomagit commented 1 year ago

@Redline600 Can you share your code? Are you trying to use one of the indicators Tucsky provided in this thread? Their last upload (indicator_high-pivot-pivotlength copy 1.txt) has been working great for me.

Redline600 commented 1 year ago

yes, I solved the problems myself in my study last night. Can we communicate for different projects?