tickwire 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/README.md +209 -2
- package/dist/backtest/backtest.d.ts +101 -0
- package/dist/backtest/backtest.js +200 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +179 -0
- package/dist/core/aggregate.d.ts +19 -0
- package/dist/core/aggregate.js +64 -0
- package/dist/core/clock.d.ts +6 -0
- package/dist/core/clock.js +18 -0
- package/dist/core/errors.d.ts +51 -0
- package/dist/core/errors.js +53 -0
- package/dist/core/http.d.ts +38 -0
- package/dist/core/http.js +119 -0
- package/dist/core/instrument.d.ts +14 -0
- package/dist/core/instrument.js +22 -0
- package/dist/core/provider.d.ts +47 -0
- package/dist/core/provider.js +1 -0
- package/dist/core/rate-limiter.d.ts +30 -0
- package/dist/core/rate-limiter.js +68 -0
- package/dist/core/stream.d.ts +90 -0
- package/dist/core/stream.js +224 -0
- package/dist/core/timeframe.d.ts +16 -0
- package/dist/core/timeframe.js +21 -0
- package/dist/core/types.d.ts +52 -0
- package/dist/core/types.js +1 -0
- package/dist/core/util.d.ts +8 -0
- package/dist/core/util.js +24 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +22 -0
- package/dist/indicators/atr.d.ts +21 -0
- package/dist/indicators/atr.js +41 -0
- package/dist/indicators/bollinger.d.ts +22 -0
- package/dist/indicators/bollinger.js +42 -0
- package/dist/indicators/ema.d.ts +16 -0
- package/dist/indicators/ema.js +40 -0
- package/dist/indicators/index.d.ts +8 -0
- package/dist/indicators/index.js +8 -0
- package/dist/indicators/indicator.d.ts +15 -0
- package/dist/indicators/indicator.js +17 -0
- package/dist/indicators/macd.d.ts +31 -0
- package/dist/indicators/macd.js +48 -0
- package/dist/indicators/rsi.d.ts +17 -0
- package/dist/indicators/rsi.js +53 -0
- package/dist/indicators/sma.d.ts +14 -0
- package/dist/indicators/sma.js +43 -0
- package/dist/indicators/vwap.d.ts +25 -0
- package/dist/indicators/vwap.js +37 -0
- package/dist/providers/alpaca.d.ts +44 -0
- package/dist/providers/alpaca.js +227 -0
- package/dist/providers/binance.d.ts +31 -0
- package/dist/providers/binance.js +187 -0
- package/dist/providers/coinbase.d.ts +28 -0
- package/dist/providers/coinbase.js +179 -0
- package/dist/providers/create.d.ts +6 -0
- package/dist/providers/create.js +21 -0
- package/dist/providers/kraken.d.ts +30 -0
- package/dist/providers/kraken.js +203 -0
- package/dist/providers/options.d.ts +22 -0
- package/dist/providers/options.js +1 -0
- package/dist/providers/paging.d.ts +30 -0
- package/dist/providers/paging.js +59 -0
- package/dist/series/check.d.ts +32 -0
- package/dist/series/check.js +63 -0
- package/dist/series/live.d.ts +31 -0
- package/dist/series/live.js +121 -0
- package/dist/series/load.d.ts +35 -0
- package/dist/series/load.js +100 -0
- package/dist/series/store.d.ts +50 -0
- package/dist/series/store.js +148 -0
- package/dist/series/time.d.ts +15 -0
- package/dist/series/time.js +62 -0
- package/dist/watch/sinks.d.ts +7 -0
- package/dist/watch/sinks.js +22 -0
- package/dist/watch/watch.d.ts +78 -0
- package/dist/watch/watch.js +203 -0
- package/docs/agents.md +84 -0
- package/docs/positioning.md +94 -0
- package/docs/providers.md +92 -0
- package/package.json +68 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { InvalidArgumentError } from '../core/errors.js';
|
|
2
|
+
import { checkNumber, checkPeriod, runBatch } from './indicator.js';
|
|
3
|
+
/** Bollinger Bands: SMA ± k × population standard deviation over `period` values (defaults 20, 2). */
|
|
4
|
+
export class BollingerBands {
|
|
5
|
+
period;
|
|
6
|
+
stdDev;
|
|
7
|
+
window = [];
|
|
8
|
+
value;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.period = options.period ?? 20;
|
|
11
|
+
this.stdDev = options.stdDev ?? 2;
|
|
12
|
+
checkPeriod('BollingerBands', this.period);
|
|
13
|
+
if (!(this.stdDev >= 0))
|
|
14
|
+
throw new InvalidArgumentError('BollingerBands: stdDev must be >= 0');
|
|
15
|
+
}
|
|
16
|
+
next(x) {
|
|
17
|
+
checkNumber('BollingerBands', x);
|
|
18
|
+
this.window.push(x);
|
|
19
|
+
if (this.window.length > this.period)
|
|
20
|
+
this.window.shift();
|
|
21
|
+
if (this.window.length < this.period)
|
|
22
|
+
return undefined;
|
|
23
|
+
// Two passes over the window: exact and stable, O(period) per update.
|
|
24
|
+
let sum = 0;
|
|
25
|
+
for (const v of this.window)
|
|
26
|
+
sum += v;
|
|
27
|
+
const mean = sum / this.period;
|
|
28
|
+
let sq = 0;
|
|
29
|
+
for (const v of this.window)
|
|
30
|
+
sq += (v - mean) ** 2;
|
|
31
|
+
const sd = Math.sqrt(sq / this.period);
|
|
32
|
+
this.value = { middle: mean, upper: mean + this.stdDev * sd, lower: mean - this.stdDev * sd };
|
|
33
|
+
return this.value;
|
|
34
|
+
}
|
|
35
|
+
reset() {
|
|
36
|
+
this.window.length = 0;
|
|
37
|
+
this.value = undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
export function bollinger(values, options = {}) {
|
|
41
|
+
return runBatch(new BollingerBands(options), values);
|
|
42
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { type Indicator } from './indicator.js';
|
|
2
|
+
/**
|
|
3
|
+
* Exponential moving average, smoothing 2 / (period + 1), seeded with the simple average
|
|
4
|
+
* of the first `period` values (first value at index period - 1).
|
|
5
|
+
*/
|
|
6
|
+
export declare class EMA implements Indicator<number, number> {
|
|
7
|
+
readonly period: number;
|
|
8
|
+
readonly alpha: number;
|
|
9
|
+
private count;
|
|
10
|
+
private seedSum;
|
|
11
|
+
value: number | undefined;
|
|
12
|
+
constructor(period: number);
|
|
13
|
+
next(x: number): number | undefined;
|
|
14
|
+
reset(): void;
|
|
15
|
+
}
|
|
16
|
+
export declare function ema(values: Iterable<number>, period: number): (number | undefined)[];
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { checkNumber, checkPeriod, runBatch } from './indicator.js';
|
|
2
|
+
/**
|
|
3
|
+
* Exponential moving average, smoothing 2 / (period + 1), seeded with the simple average
|
|
4
|
+
* of the first `period` values (first value at index period - 1).
|
|
5
|
+
*/
|
|
6
|
+
export class EMA {
|
|
7
|
+
period;
|
|
8
|
+
alpha;
|
|
9
|
+
count = 0;
|
|
10
|
+
seedSum = 0;
|
|
11
|
+
value;
|
|
12
|
+
constructor(period) {
|
|
13
|
+
this.period = period;
|
|
14
|
+
checkPeriod('EMA', period);
|
|
15
|
+
this.alpha = 2 / (period + 1);
|
|
16
|
+
}
|
|
17
|
+
next(x) {
|
|
18
|
+
checkNumber('EMA', x);
|
|
19
|
+
this.count++;
|
|
20
|
+
if (this.count < this.period) {
|
|
21
|
+
this.seedSum += x;
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
if (this.count === this.period) {
|
|
25
|
+
this.value = (this.seedSum + x) / this.period;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
this.value = this.value + this.alpha * (x - this.value);
|
|
29
|
+
}
|
|
30
|
+
return this.value;
|
|
31
|
+
}
|
|
32
|
+
reset() {
|
|
33
|
+
this.count = 0;
|
|
34
|
+
this.seedSum = 0;
|
|
35
|
+
this.value = undefined;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export function ema(values, period) {
|
|
39
|
+
return runBatch(new EMA(period), values);
|
|
40
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { type Indicator, runBatch } from './indicator.js';
|
|
2
|
+
export { SMA, sma } from './sma.js';
|
|
3
|
+
export { EMA, ema } from './ema.js';
|
|
4
|
+
export { RSI, rsi } from './rsi.js';
|
|
5
|
+
export { MACD, macd, type MACDOptions, type MACDValue } from './macd.js';
|
|
6
|
+
export { BollingerBands, bollinger, type BollingerOptions, type BollingerValue } from './bollinger.js';
|
|
7
|
+
export { ATR, atr, type HLC } from './atr.js';
|
|
8
|
+
export { VWAP, vwap, type VWAPInput, type VWAPOptions } from './vwap.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { runBatch } from './indicator.js';
|
|
2
|
+
export { SMA, sma } from './sma.js';
|
|
3
|
+
export { EMA, ema } from './ema.js';
|
|
4
|
+
export { RSI, rsi } from './rsi.js';
|
|
5
|
+
export { MACD, macd } from './macd.js';
|
|
6
|
+
export { BollingerBands, bollinger } from './bollinger.js';
|
|
7
|
+
export { ATR, atr } from './atr.js';
|
|
8
|
+
export { VWAP, vwap } from './vwap.js';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Incremental indicator: feed one input at a time with `next`, get the new value
|
|
3
|
+
* (`undefined` while the indicator is warming up).
|
|
4
|
+
*/
|
|
5
|
+
export interface Indicator<I, O> {
|
|
6
|
+
next(input: I): O | undefined;
|
|
7
|
+
/** Last value returned by `next`. */
|
|
8
|
+
readonly value: O | undefined;
|
|
9
|
+
/** Forget every input. */
|
|
10
|
+
reset(): void;
|
|
11
|
+
}
|
|
12
|
+
export declare function checkPeriod(name: string, period: number, min?: number): void;
|
|
13
|
+
export declare function checkNumber(name: string, value: number): void;
|
|
14
|
+
/** Run an incremental indicator over a series: one output per input, `undefined` during warm-up. */
|
|
15
|
+
export declare function runBatch<I, O>(indicator: Indicator<I, O>, inputs: Iterable<I>): (O | undefined)[];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { InvalidArgumentError } from '../core/errors.js';
|
|
2
|
+
export function checkPeriod(name, period, min = 1) {
|
|
3
|
+
if (!Number.isInteger(period) || period < min) {
|
|
4
|
+
throw new InvalidArgumentError(`${name}: period must be an integer >= ${min} (got ${period})`);
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export function checkNumber(name, value) {
|
|
8
|
+
if (!Number.isFinite(value))
|
|
9
|
+
throw new InvalidArgumentError(`${name}: input must be a finite number (got ${value})`);
|
|
10
|
+
}
|
|
11
|
+
/** Run an incremental indicator over a series: one output per input, `undefined` during warm-up. */
|
|
12
|
+
export function runBatch(indicator, inputs) {
|
|
13
|
+
const out = [];
|
|
14
|
+
for (const input of inputs)
|
|
15
|
+
out.push(indicator.next(input));
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Indicator } from './indicator.js';
|
|
2
|
+
export interface MACDValue {
|
|
3
|
+
/** Fast EMA minus slow EMA. */
|
|
4
|
+
macd: number;
|
|
5
|
+
/** EMA of the MACD line. */
|
|
6
|
+
signal: number;
|
|
7
|
+
/** MACD minus signal. */
|
|
8
|
+
histogram: number;
|
|
9
|
+
}
|
|
10
|
+
export interface MACDOptions {
|
|
11
|
+
fast?: number;
|
|
12
|
+
slow?: number;
|
|
13
|
+
signal?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Moving Average Convergence Divergence (defaults 12, 26, 9). Values start once the signal line
|
|
17
|
+
* is ready, at index slow + signal - 2 (33 with the defaults).
|
|
18
|
+
*/
|
|
19
|
+
export declare class MACD implements Indicator<number, MACDValue> {
|
|
20
|
+
readonly fast: number;
|
|
21
|
+
readonly slow: number;
|
|
22
|
+
readonly signal: number;
|
|
23
|
+
private readonly fastEma;
|
|
24
|
+
private readonly slowEma;
|
|
25
|
+
private readonly signalEma;
|
|
26
|
+
value: MACDValue | undefined;
|
|
27
|
+
constructor(options?: MACDOptions);
|
|
28
|
+
next(x: number): MACDValue | undefined;
|
|
29
|
+
reset(): void;
|
|
30
|
+
}
|
|
31
|
+
export declare function macd(values: Iterable<number>, options?: MACDOptions): (MACDValue | undefined)[];
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { InvalidArgumentError } from '../core/errors.js';
|
|
2
|
+
import { EMA } from './ema.js';
|
|
3
|
+
import { checkNumber, runBatch } from './indicator.js';
|
|
4
|
+
/**
|
|
5
|
+
* Moving Average Convergence Divergence (defaults 12, 26, 9). Values start once the signal line
|
|
6
|
+
* is ready, at index slow + signal - 2 (33 with the defaults).
|
|
7
|
+
*/
|
|
8
|
+
export class MACD {
|
|
9
|
+
fast;
|
|
10
|
+
slow;
|
|
11
|
+
signal;
|
|
12
|
+
fastEma;
|
|
13
|
+
slowEma;
|
|
14
|
+
signalEma;
|
|
15
|
+
value;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.fast = options.fast ?? 12;
|
|
18
|
+
this.slow = options.slow ?? 26;
|
|
19
|
+
this.signal = options.signal ?? 9;
|
|
20
|
+
if (this.fast >= this.slow)
|
|
21
|
+
throw new InvalidArgumentError('MACD: fast period must be shorter than slow period');
|
|
22
|
+
this.fastEma = new EMA(this.fast);
|
|
23
|
+
this.slowEma = new EMA(this.slow);
|
|
24
|
+
this.signalEma = new EMA(this.signal);
|
|
25
|
+
}
|
|
26
|
+
next(x) {
|
|
27
|
+
checkNumber('MACD', x);
|
|
28
|
+
const fast = this.fastEma.next(x);
|
|
29
|
+
const slow = this.slowEma.next(x);
|
|
30
|
+
if (fast === undefined || slow === undefined)
|
|
31
|
+
return undefined;
|
|
32
|
+
const macd = fast - slow;
|
|
33
|
+
const signal = this.signalEma.next(macd);
|
|
34
|
+
if (signal === undefined)
|
|
35
|
+
return undefined;
|
|
36
|
+
this.value = { macd, signal, histogram: macd - signal };
|
|
37
|
+
return this.value;
|
|
38
|
+
}
|
|
39
|
+
reset() {
|
|
40
|
+
this.fastEma.reset();
|
|
41
|
+
this.slowEma.reset();
|
|
42
|
+
this.signalEma.reset();
|
|
43
|
+
this.value = undefined;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
export function macd(values, options = {}) {
|
|
47
|
+
return runBatch(new MACD(options), values);
|
|
48
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Indicator } from './indicator.js';
|
|
2
|
+
/**
|
|
3
|
+
* Relative Strength Index with Wilder's smoothing. The first value needs `period + 1` inputs.
|
|
4
|
+
* A flat series gives 50; a series with gains and no losses gives 100.
|
|
5
|
+
*/
|
|
6
|
+
export declare class RSI implements Indicator<number, number> {
|
|
7
|
+
readonly period: number;
|
|
8
|
+
private prev;
|
|
9
|
+
private count;
|
|
10
|
+
private avgGain;
|
|
11
|
+
private avgLoss;
|
|
12
|
+
value: number | undefined;
|
|
13
|
+
constructor(period?: number);
|
|
14
|
+
next(x: number): number | undefined;
|
|
15
|
+
reset(): void;
|
|
16
|
+
}
|
|
17
|
+
export declare function rsi(values: Iterable<number>, period?: number): (number | undefined)[];
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { checkNumber, checkPeriod, runBatch } from './indicator.js';
|
|
2
|
+
/**
|
|
3
|
+
* Relative Strength Index with Wilder's smoothing. The first value needs `period + 1` inputs.
|
|
4
|
+
* A flat series gives 50; a series with gains and no losses gives 100.
|
|
5
|
+
*/
|
|
6
|
+
export class RSI {
|
|
7
|
+
period;
|
|
8
|
+
prev;
|
|
9
|
+
count = 0;
|
|
10
|
+
avgGain = 0;
|
|
11
|
+
avgLoss = 0;
|
|
12
|
+
value;
|
|
13
|
+
constructor(period = 14) {
|
|
14
|
+
this.period = period;
|
|
15
|
+
checkPeriod('RSI', period);
|
|
16
|
+
}
|
|
17
|
+
next(x) {
|
|
18
|
+
checkNumber('RSI', x);
|
|
19
|
+
const prev = this.prev;
|
|
20
|
+
this.prev = x;
|
|
21
|
+
if (prev === undefined)
|
|
22
|
+
return undefined;
|
|
23
|
+
const change = x - prev;
|
|
24
|
+
const gain = change > 0 ? change : 0;
|
|
25
|
+
const loss = change < 0 ? -change : 0;
|
|
26
|
+
this.count++;
|
|
27
|
+
if (this.count <= this.period) {
|
|
28
|
+
this.avgGain += gain / this.period;
|
|
29
|
+
this.avgLoss += loss / this.period;
|
|
30
|
+
if (this.count < this.period)
|
|
31
|
+
return undefined;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.avgGain = (this.avgGain * (this.period - 1) + gain) / this.period;
|
|
35
|
+
this.avgLoss = (this.avgLoss * (this.period - 1) + loss) / this.period;
|
|
36
|
+
}
|
|
37
|
+
if (this.avgLoss === 0)
|
|
38
|
+
this.value = this.avgGain === 0 ? 50 : 100;
|
|
39
|
+
else
|
|
40
|
+
this.value = 100 - 100 / (1 + this.avgGain / this.avgLoss);
|
|
41
|
+
return this.value;
|
|
42
|
+
}
|
|
43
|
+
reset() {
|
|
44
|
+
this.prev = undefined;
|
|
45
|
+
this.count = 0;
|
|
46
|
+
this.avgGain = 0;
|
|
47
|
+
this.avgLoss = 0;
|
|
48
|
+
this.value = undefined;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function rsi(values, period = 14) {
|
|
52
|
+
return runBatch(new RSI(period), values);
|
|
53
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type Indicator } from './indicator.js';
|
|
2
|
+
/** Simple moving average of the last `period` values. */
|
|
3
|
+
export declare class SMA implements Indicator<number, number> {
|
|
4
|
+
readonly period: number;
|
|
5
|
+
private readonly window;
|
|
6
|
+
private head;
|
|
7
|
+
private sum;
|
|
8
|
+
private sinceResync;
|
|
9
|
+
value: number | undefined;
|
|
10
|
+
constructor(period: number);
|
|
11
|
+
next(x: number): number | undefined;
|
|
12
|
+
reset(): void;
|
|
13
|
+
}
|
|
14
|
+
export declare function sma(values: Iterable<number>, period: number): (number | undefined)[];
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { checkNumber, checkPeriod, runBatch } from './indicator.js';
|
|
2
|
+
/** Simple moving average of the last `period` values. */
|
|
3
|
+
export class SMA {
|
|
4
|
+
period;
|
|
5
|
+
window = [];
|
|
6
|
+
head = 0;
|
|
7
|
+
sum = 0;
|
|
8
|
+
sinceResync = 0;
|
|
9
|
+
value;
|
|
10
|
+
constructor(period) {
|
|
11
|
+
this.period = period;
|
|
12
|
+
checkPeriod('SMA', period);
|
|
13
|
+
}
|
|
14
|
+
next(x) {
|
|
15
|
+
checkNumber('SMA', x);
|
|
16
|
+
if (this.window.length < this.period) {
|
|
17
|
+
this.window.push(x);
|
|
18
|
+
this.sum += x;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
this.sum += x - this.window[this.head];
|
|
22
|
+
this.window[this.head] = x;
|
|
23
|
+
this.head = (this.head + 1) % this.period;
|
|
24
|
+
// Re-add the window from scratch now and then so rounding errors never accumulate.
|
|
25
|
+
if (++this.sinceResync >= this.period * 64) {
|
|
26
|
+
this.sinceResync = 0;
|
|
27
|
+
this.sum = this.window.reduce((a, b) => a + b, 0);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
this.value = this.window.length === this.period ? this.sum / this.period : undefined;
|
|
31
|
+
return this.value;
|
|
32
|
+
}
|
|
33
|
+
reset() {
|
|
34
|
+
this.window.length = 0;
|
|
35
|
+
this.head = 0;
|
|
36
|
+
this.sum = 0;
|
|
37
|
+
this.sinceResync = 0;
|
|
38
|
+
this.value = undefined;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
export function sma(values, period) {
|
|
42
|
+
return runBatch(new SMA(period), values);
|
|
43
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Indicator } from './indicator.js';
|
|
2
|
+
export interface VWAPInput {
|
|
3
|
+
/** Candle open time in ms (used for the daily anchor). */
|
|
4
|
+
time: number;
|
|
5
|
+
high: number;
|
|
6
|
+
low: number;
|
|
7
|
+
close: number;
|
|
8
|
+
volume: number;
|
|
9
|
+
}
|
|
10
|
+
export interface VWAPOptions {
|
|
11
|
+
/** `day` restarts the average at each UTC day (default), `none` never restarts. */
|
|
12
|
+
anchor?: 'day' | 'none';
|
|
13
|
+
}
|
|
14
|
+
/** Volume-weighted average price of the typical price (high + low + close) / 3. */
|
|
15
|
+
export declare class VWAP implements Indicator<VWAPInput, number> {
|
|
16
|
+
readonly anchor: 'day' | 'none';
|
|
17
|
+
private pv;
|
|
18
|
+
private vol;
|
|
19
|
+
private day;
|
|
20
|
+
value: number | undefined;
|
|
21
|
+
constructor(options?: VWAPOptions);
|
|
22
|
+
next(bar: VWAPInput): number | undefined;
|
|
23
|
+
reset(): void;
|
|
24
|
+
}
|
|
25
|
+
export declare function vwap(bars: Iterable<VWAPInput>, options?: VWAPOptions): (number | undefined)[];
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { checkNumber, runBatch } from './indicator.js';
|
|
2
|
+
/** Volume-weighted average price of the typical price (high + low + close) / 3. */
|
|
3
|
+
export class VWAP {
|
|
4
|
+
anchor;
|
|
5
|
+
pv = 0;
|
|
6
|
+
vol = 0;
|
|
7
|
+
day;
|
|
8
|
+
value;
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
this.anchor = options.anchor ?? 'day';
|
|
11
|
+
}
|
|
12
|
+
next(bar) {
|
|
13
|
+
for (const v of [bar.high, bar.low, bar.close, bar.volume])
|
|
14
|
+
checkNumber('VWAP', v);
|
|
15
|
+
if (this.anchor === 'day') {
|
|
16
|
+
const day = Math.floor(bar.time / 86_400_000);
|
|
17
|
+
if (day !== this.day) {
|
|
18
|
+
this.day = day;
|
|
19
|
+
this.pv = 0;
|
|
20
|
+
this.vol = 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
this.pv += ((bar.high + bar.low + bar.close) / 3) * bar.volume;
|
|
24
|
+
this.vol += bar.volume;
|
|
25
|
+
this.value = this.vol > 0 ? this.pv / this.vol : undefined;
|
|
26
|
+
return this.value;
|
|
27
|
+
}
|
|
28
|
+
reset() {
|
|
29
|
+
this.pv = 0;
|
|
30
|
+
this.vol = 0;
|
|
31
|
+
this.day = undefined;
|
|
32
|
+
this.value = undefined;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export function vwap(bars, options = {}) {
|
|
36
|
+
return runBatch(new VWAP(options), bars);
|
|
37
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type InstrumentLike } from '../core/instrument.js';
|
|
2
|
+
import type { CandleQuery, Provider, ProviderCapabilities, RequestOptions } from '../core/provider.js';
|
|
3
|
+
import { type StreamOptions, type TickStream } from '../core/stream.js';
|
|
4
|
+
import { type Timeframe } from '../core/timeframe.js';
|
|
5
|
+
import type { Candle, Quote, Trade } from '../core/types.js';
|
|
6
|
+
import type { ProviderOptions } from './options.js';
|
|
7
|
+
export interface AlpacaOptions extends ProviderOptions {
|
|
8
|
+
/** API key id (default: `APCA_API_KEY_ID` environment variable). */
|
|
9
|
+
keyId?: string;
|
|
10
|
+
/** API secret key (default: `APCA_API_SECRET_KEY` environment variable). */
|
|
11
|
+
secretKey?: string;
|
|
12
|
+
/** Data feed: `iex` (free plan) or `sip` (paid plans). Default `iex`. */
|
|
13
|
+
feed?: 'iex' | 'sip' | (string & {});
|
|
14
|
+
/** Corporate action adjustment for bars (default `raw`). */
|
|
15
|
+
adjustment?: 'raw' | 'split' | 'dividend' | 'all';
|
|
16
|
+
}
|
|
17
|
+
/** Alpaca timeframe string for a tickwire timeframe (`5m` → `5Min`), or undefined if Alpaca has no equivalent. */
|
|
18
|
+
export declare function alpacaTimeframe(timeframe: string): string | undefined;
|
|
19
|
+
/**
|
|
20
|
+
* Alpaca Market Data API v2 for US stocks. Needs a free Alpaca account and API key
|
|
21
|
+
* (`APCA_API_KEY_ID` / `APCA_API_SECRET_KEY`). The free plan serves the IEX feed and allows
|
|
22
|
+
* 200 requests per minute; the default client-side budget is 180 per minute. The free plan also allows
|
|
23
|
+
* a single WebSocket connection at a time: run one stream per account.
|
|
24
|
+
*/
|
|
25
|
+
export declare class AlpacaProvider implements Provider {
|
|
26
|
+
readonly id = "alpaca";
|
|
27
|
+
readonly capabilities: ProviderCapabilities;
|
|
28
|
+
private readonly http;
|
|
29
|
+
private readonly clock;
|
|
30
|
+
private readonly wsUrl;
|
|
31
|
+
private readonly WebSocket;
|
|
32
|
+
private readonly keyId;
|
|
33
|
+
private readonly secretKey;
|
|
34
|
+
private readonly feed;
|
|
35
|
+
private readonly adjustment;
|
|
36
|
+
constructor(options?: AlpacaOptions);
|
|
37
|
+
private credentials;
|
|
38
|
+
nativeSymbol(input: InstrumentLike): string;
|
|
39
|
+
getCandles(input: InstrumentLike, timeframe: Timeframe, query?: CandleQuery): Promise<Candle[]>;
|
|
40
|
+
getQuote(input: InstrumentLike, options?: RequestOptions): Promise<Quote>;
|
|
41
|
+
private stream;
|
|
42
|
+
streamTrades(input: InstrumentLike, options?: StreamOptions): TickStream<Trade>;
|
|
43
|
+
streamCandles(input: InstrumentLike, timeframe: Timeframe, options?: StreamOptions): TickStream<Candle>;
|
|
44
|
+
}
|