Real-time streaming data sample-r and aggre-gator with closed-circuit recording, efficient memory management and FIFO/LIFO readout sweeps, providing endless (and seamless) data capture, and processing.
You are building a real-time high frequency data processing app that does something along the lines of:
This library will easily do the following for you:
The lib offers several important advantages in Performance and Memory utilization, as it records data into Closed-circuit buffers caleld Tracks, similar to the way CCTV works.
You may also know this Design Pattern as Flyweight. Each track is essentially an array of fixed length,
containing empty placeholders in the shape of your data.
All you need to do is:
yarn add @reactiff/sample-gator
const sampler = new SamplingBuffer({
interval: 1000, // 1000 ms == 1 second
bufferLength: 3600,
fields: [ 'time', 'price', 'qty' ],
});
sampler.onTrackStart = (track) => {
track.onUpdate = () => {
const samples = [];
track.fifo((pos, track) => {
items[pos.ordinal] = track[pos.index];
});
// analyze samples...
}
};
sampler.startSampling();
These methods facilitate reading data from the Closed-circuit buffer, taking care of complicated cursor and offset positions.
They both accept a callback of form:
(pos, track) => void
Param | Description |
---|---|
track | internal array of samples |
pos | indexer: { index, ordinal, relative } |
pos props
Prop | Purpose |
---|---|
pos.index | 1 Sample index in the Track |
pos.ordinal | True iteration number (always zero based) |
pos.relative | Relative offset from cursor |
1 Sample index should only be used for accessing the Sample in the Track. It doesn’t always start with zero, rather it starts with internal cursor position within the Closed-circuit loop.
In previous example we used an array of field names corresponding to data fields. You can define your own fields and how they should be calculated.
import { value, when } from '@reactiff/sample-gator';
const field = {
_buy: (d) => when(d.qty > 0, () => d.buy = 1), // [^4]
_sell: (d) => when(d.qty < 0, () => d.sell = 1),
open: {
fn: (d, curr) => value(curr, d.price),
fill: p => p.close
}, // [^5]
high: {
fn: (d, curr) => Math.max(
d.price,
value(curr, d.price)),
fill: p => p.close
},
buyVol: {
fn: (d, curr) => when(d.buy,
value(curr, 0) + d.qty),
fill: () => 0
},
cumNetVol: {
fn: (d, curr) => value(curr, 0) + d.qty,
cumulative: true // [^6]
},
};
[^4] - Underscore fields are special
- they are run first
- they do not get added to sample (hidden)
- they perform some operation e.g. here they set a value on the data object itself
[^5] - The fill() callback defines how the field’s value should be calculated for empty Samples
[^6] - Cumulative fields do not get reset with each new sample, rather their values are rolled forward
You can define expressions by performing Rolling Window calculations, also known as Moving Averages on a single Serie, over N number of samples in its history. Here is how you would define a Simple Moving Average (SMA) of 10:
// Simple Moving Average over 10:
buffer.addExpression('sma10', (series) => {
// get closing price Serie
return series.close.mean(10);
})
once such expression is added, it can also be used in other expressions. For example, Exponential Moving Average (EMA) uses a slightly different formula, where it uses its previous value as the basis of calculation. However, there is no previous sample for the very First value in the serie of course, that’s why previous SMA of same length is used. Therefore, an EMA expression requires N + 1 elements in the serie to work.
// Exponential Moving Average of 10
buffer.addExpression('ema10', (series) => {
const sma10 = series.sma10;
const ema10 = series.ema10; // reference this serie
if (ema10.availableLength < 11) {
return undefined;
}
// get previous value
let prev = ema10.value(-1);
if (!prev) {
prev = sma10.value(-1);
// or calculate on the fly:
prev = series.price.mean(n, -1);
}
const k = 2 / (n + 1);
const ema = series.price.value() * k + prev * (1 - k);
return ema;
})
Here is another useful example where we check if EMA and SMA cross each other. Note that the output is only generated when one of the two conditions is met:
buffer.addExpression('ema10xsma10', (_: any) => {
// check cross to the up side
if (_.ema10.value( 0) > _.sma10.value( 0) &&
_.ema10.value(-1) < _.sma10.value(-1)) {
return 1;
}
// check cross to the down side
if (_.ema10.value( 0) < _.sma10.value( 0) &&
_.ema10.value(-1) > _.sma10.value(-1)) {
return -1;
}
return undefined;
});
MIT © Rick Ellis