@dsh-stack/trading-market-data 0.0.0-stage → 0.1.1
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/README.md +2 -2
- package/lib/index.d.ts +52 -0
- package/lib/index.js +65 -0
- package/package.json +38 -4
package/README.md
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
#
|
|
1
|
+
# trading-market-data
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Market data models and ingestion utilities for Stack trading tooling.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Candle } from "@dsh-stack/trading-research";
|
|
2
|
+
export interface MarketDataQuery {
|
|
3
|
+
readonly symbol: string;
|
|
4
|
+
readonly start?: number;
|
|
5
|
+
readonly end?: number;
|
|
6
|
+
readonly limit?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface MarketDataProvider {
|
|
9
|
+
readonly id: string;
|
|
10
|
+
listSymbols(): Promise<readonly string[]>;
|
|
11
|
+
getCandles(query: MarketDataQuery): Promise<readonly Candle[]>;
|
|
12
|
+
}
|
|
13
|
+
export declare class MarketDataRegistry {
|
|
14
|
+
private readonly providers;
|
|
15
|
+
/**
|
|
16
|
+
* Registers a new market data provider.
|
|
17
|
+
*
|
|
18
|
+
* @param provider - The MarketDataProvider to register.
|
|
19
|
+
* @throws Will throw an error if a provider with the same ID is already registered.
|
|
20
|
+
* @returns Nothing.
|
|
21
|
+
*/
|
|
22
|
+
register(provider: MarketDataProvider): void;
|
|
23
|
+
/**
|
|
24
|
+
* Retrieves the market data provider associated with the given ID.
|
|
25
|
+
* @param id - The ID of the market data provider to retrieve.
|
|
26
|
+
* @returns The MarketDataProvider associated with the ID, or undefined if not found.
|
|
27
|
+
* @throws Will throw an error if the ID is not found.
|
|
28
|
+
*/
|
|
29
|
+
get(id: string): MarketDataProvider | undefined;
|
|
30
|
+
/**
|
|
31
|
+
* Lists all registered market data providers.
|
|
32
|
+
* @returns An array of MarketDataProvider instances, representing all registered providers.
|
|
33
|
+
* @throws Will throw an error if there are no providers registered.
|
|
34
|
+
*/
|
|
35
|
+
list(): readonly MarketDataProvider[];
|
|
36
|
+
}
|
|
37
|
+
export declare class MemoryMarketDataProvider implements MarketDataProvider {
|
|
38
|
+
readonly id: string;
|
|
39
|
+
private readonly symbols;
|
|
40
|
+
/** Constructs an instance. */
|
|
41
|
+
constructor(id?: string);
|
|
42
|
+
/** seed implementation. */
|
|
43
|
+
seed(symbol: string, candles: readonly Candle[]): void;
|
|
44
|
+
/** listSymbols implementation. */
|
|
45
|
+
listSymbols(): Promise<readonly string[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Returns a sorted list of symbols for which candle data is available.
|
|
48
|
+
* @returns A sorted array of symbol names.
|
|
49
|
+
* @throws Will throw an error if no symbols have been seeded with candle data.
|
|
50
|
+
*/
|
|
51
|
+
getCandles(query: MarketDataQuery): Promise<readonly Candle[]>;
|
|
52
|
+
}
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
export class MarketDataRegistry {
|
|
2
|
+
providers = new Map();
|
|
3
|
+
/**
|
|
4
|
+
* Registers a new market data provider.
|
|
5
|
+
*
|
|
6
|
+
* @param provider - The MarketDataProvider to register.
|
|
7
|
+
* @throws Will throw an error if a provider with the same ID is already registered.
|
|
8
|
+
* @returns Nothing.
|
|
9
|
+
*/
|
|
10
|
+
register(provider) {
|
|
11
|
+
if (this.providers.has(provider.id))
|
|
12
|
+
throw new Error(`Market-data provider already registered: ${provider.id}`);
|
|
13
|
+
this.providers.set(provider.id, provider);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Retrieves the market data provider associated with the given ID.
|
|
17
|
+
* @param id - The ID of the market data provider to retrieve.
|
|
18
|
+
* @returns The MarketDataProvider associated with the ID, or undefined if not found.
|
|
19
|
+
* @throws Will throw an error if the ID is not found.
|
|
20
|
+
*/
|
|
21
|
+
get(id) {
|
|
22
|
+
return this.providers.get(id);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Lists all registered market data providers.
|
|
26
|
+
* @returns An array of MarketDataProvider instances, representing all registered providers.
|
|
27
|
+
* @throws Will throw an error if there are no providers registered.
|
|
28
|
+
*/
|
|
29
|
+
list() {
|
|
30
|
+
return [...this.providers.values()];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export class MemoryMarketDataProvider {
|
|
34
|
+
id;
|
|
35
|
+
symbols = new Map();
|
|
36
|
+
/** Constructs an instance. */
|
|
37
|
+
constructor(id = "memory") {
|
|
38
|
+
this.id = id;
|
|
39
|
+
}
|
|
40
|
+
/** seed implementation. */
|
|
41
|
+
seed(symbol, candles) {
|
|
42
|
+
const normalized = [...candles].sort((a, b) => a.time - b.time);
|
|
43
|
+
this.symbols.set(symbol, normalized);
|
|
44
|
+
}
|
|
45
|
+
/** listSymbols implementation. */
|
|
46
|
+
async listSymbols() {
|
|
47
|
+
return [...this.symbols.keys()].sort();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Returns a sorted list of symbols for which candle data is available.
|
|
51
|
+
* @returns A sorted array of symbol names.
|
|
52
|
+
* @throws Will throw an error if no symbols have been seeded with candle data.
|
|
53
|
+
*/
|
|
54
|
+
async getCandles(query) {
|
|
55
|
+
const data = this.symbols.get(query.symbol) ?? [];
|
|
56
|
+
const filtered = data.filter((candle) => {
|
|
57
|
+
if (query.start !== undefined && candle.time < query.start)
|
|
58
|
+
return false;
|
|
59
|
+
if (query.end !== undefined && candle.time > query.end)
|
|
60
|
+
return false;
|
|
61
|
+
return true;
|
|
62
|
+
});
|
|
63
|
+
return query.limit === undefined ? filtered : filtered.slice(-Math.max(0, query.limit));
|
|
64
|
+
}
|
|
65
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dsh-stack/trading-market-data",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "lib/index.js",
|
|
6
|
+
"types": "lib/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./lib/index.d.ts",
|
|
10
|
+
"default": "./lib/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc -p tsconfig.json",
|
|
15
|
+
"typecheck": "tsc -p tsconfig.typecheck.json --noEmit",
|
|
16
|
+
"verify": "node verify.mjs"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@dsh-stack/trading-research": "0.1.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"typescript": "^6.0.3"
|
|
23
|
+
},
|
|
24
|
+
"stack": {
|
|
25
|
+
"kind": "plugin",
|
|
26
|
+
"id": "stack.trading.market-data"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"lib",
|
|
33
|
+
"README.md"
|
|
34
|
+
],
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/marius-patrik/dsh-stack.git",
|
|
38
|
+
"directory": "plugins/trading-market-data"
|
|
39
|
+
}
|
|
40
|
+
}
|