bennycode / trading-signals

Technical indicators to run technical analysis with JavaScript & TypeScript. 📈
https://bennycode.com/trading-signals
MIT License
598 stars 90 forks source link

How can I update the last column's indicator of a realtime kline chart? eg: RSI #653

Closed jias closed 5 months ago

jias commented 6 months ago

I am making a real-time K-line chart (like Binance), so the indicator of the last bar needs to be updated when new trades happened. But in the following rsi6 example, after calling the update() method, how to cancel the last value and recalc the current value?

const rsi6 = new RSI(6)

// some rsi6.update calls are hidden

// the first 20:10 bar's rsi with the 100 close value
rsi6.update(100)

// the later 20:10 bar's rsi should be update after some new trades? Need you help. Thx
rsi6.?
bennycode commented 6 months ago

Hi @jias, your feature request sounds like this one https://github.com/bennycode/trading-signals/discussions/626 - Am I right with this?

If so, then please allow me some questions:

1) Would you prefer a dedicated method (like replace) instead of using update or do you prefer giving a state to the update method (like update('replace'))? 2) Do you know when you want to replace the value, so that you could either call an update method or replace method?

bennycode commented 6 months ago

I made an API proposal here: https://github.com/bennycode/trading-signals/pull/654

bennycode commented 5 months ago

I am happy to announce that this feature is now possible with v5. 🥳

Just pass true as a second parameter to the update function:

const rsi6 = new RSI(6);
rsi6.update(100);
rsi6.update(50, true); // replaces 100 with 50