Skip to content

Instantly share code, notes, and snippets.

@piboistudios
Created December 17, 2023 18:55
Show Gist options
  • Save piboistudios/e2c26be8094b883503a93a5550d7a253 to your computer and use it in GitHub Desktop.
Save piboistudios/e2c26be8094b883503a93a5550d7a253 to your computer and use it in GitHub Desktop.
Live Indicator Wrapper for `trading-signals` (requires `deepclone`)
import clone from 'deepclone';
import * as signals from 'trading-signals';
export class Indicator {
constructor(indicator) {
this.values = [];
this.indicator = indicator;
}
update(v) {
this.last = clone(this.indicator);
const value = this.indicator.update(v);
this.values.push(value);
return value;
}
replace(v) {
const head = clone(this.last);
const value = head.update(v);
this.values.splice(this.values.length - 1, 1, value);
return value;
}
get value() {
return this.values[this.values.length - 1];
}
static mk(indicator, args) {
const i = new signals[indicator](...args);
return new Indicator(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment