@quantform/core 0.7.19 → 0.7.20

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.
Files changed (24) hide show
  1. package/lib/backtest/use-backtest-scheduler.d.ts +2 -2
  2. package/lib/backtest/use-backtest-scheduler.d.ts.map +1 -1
  3. package/lib/backtest/use-backtest-scheduler.js +2 -2
  4. package/lib/backtest/{use-backtest-query-buffer.d.ts → use-backtest-storage-buffer.d.ts} +3 -3
  5. package/lib/backtest/use-backtest-storage-buffer.d.ts.map +1 -0
  6. package/lib/backtest/{use-backtest-query-buffer.js → use-backtest-storage-buffer.js} +3 -3
  7. package/lib/backtest/{use-backtest-query-cursor.d.ts → use-backtest-storage-cursor.d.ts} +18 -4
  8. package/lib/backtest/use-backtest-storage-cursor.d.ts.map +1 -0
  9. package/lib/backtest/{use-backtest-query-cursor.js → use-backtest-storage-cursor.js} +4 -4
  10. package/lib/backtest/use-backtest-storage.d.ts +6 -2
  11. package/lib/backtest/use-backtest-storage.d.ts.map +1 -1
  12. package/lib/backtest/use-backtest-storage.js +37 -32
  13. package/lib/backtest/use-backtest.d.ts +1 -23
  14. package/lib/backtest/use-backtest.d.ts.map +1 -1
  15. package/lib/backtest/use-backtest.js +2 -7
  16. package/package.json +1 -1
  17. package/src/backtest/use-backtest-scheduler.ts +8 -6
  18. package/src/backtest/{use-backtest-query-buffer.ts → use-backtest-storage-buffer.ts} +2 -2
  19. package/src/backtest/use-backtest-storage-cursor.ts +48 -0
  20. package/src/backtest/use-backtest-storage.ts +38 -33
  21. package/src/backtest/use-backtest.ts +2 -23
  22. package/lib/backtest/use-backtest-query-buffer.d.ts.map +0 -1
  23. package/lib/backtest/use-backtest-query-cursor.d.ts.map +0 -1
  24. package/src/backtest/use-backtest-query-cursor.ts +0 -40
@@ -1,5 +1,5 @@
1
1
  import { Observable } from 'rxjs';
2
- import { BacktestStorage } from './use-backtest';
2
+ import { BacktestQuery } from './use-backtest-storage-cursor';
3
3
  export declare const useBacktestScheduler: () => {
4
4
  stream: Observable<[{
5
5
  size(): number;
@@ -20,7 +20,7 @@ export declare const useBacktestScheduler: () => {
20
20
  timestamp(): number;
21
21
  stop(): void;
22
22
  tryContinue: () => void;
23
- watch<T>(query: BacktestStorage<T>): Observable<{
23
+ watch<T>(query: BacktestQuery<T>): Observable<{
24
24
  timestamp: number;
25
25
  payload: T;
26
26
  }>;
@@ -1 +1 @@
1
- {"version":3,"file":"use-backtest-scheduler.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,UAAU,EAAW,MAAM,MAAM,CAAC;AAI/D,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAKjD,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;mBASiC,MAAM;iBAAW,GAAG;;;;;;mBA+DrB,MAAM;;;CAgBrE,CAAC"}
1
+ {"version":3,"file":"use-backtest-scheduler.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-scheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,UAAU,EAAW,MAAM,MAAM,CAAC;AAM/D,OAAO,EAAE,aAAa,EAA4B,MAAM,+BAA+B,CAAC;AAExF,eAAO,MAAM,oBAAoB;;;;;;;;;;;;;;mBAWd,MAAM;iBAAW,GAAG;;;;;;mBAgEwB,MAAM;;;CAgBnE,CAAC"}
@@ -13,10 +13,10 @@ exports.useBacktestScheduler = void 0;
13
13
  const rxjs_1 = require("rxjs");
14
14
  const with_memo_1 = require("../with-memo");
15
15
  const use_backtest_options_1 = require("./use-backtest-options");
16
- const use_backtest_query_cursor_1 = require("./use-backtest-query-cursor");
16
+ const use_backtest_storage_cursor_1 = require("./use-backtest-storage-cursor");
17
17
  exports.useBacktestScheduler = (0, with_memo_1.withMemo)(() => {
18
18
  const { from } = (0, use_backtest_options_1.useBacktestOptions)();
19
- const { get, cursor } = (0, use_backtest_query_cursor_1.useBacktestQueryCursor)();
19
+ const { get, cursor } = (0, use_backtest_storage_cursor_1.useBacktestStorageCursor)();
20
20
  let timestamp = from;
21
21
  let stopAcquire = 1;
22
22
  let processing = false;
@@ -1,5 +1,5 @@
1
- import { BacktestStorage } from './use-backtest';
2
- export declare function useBacktestQueryBuffer<T>(storage: BacktestStorage<T>): {
1
+ import { BacktestQuery } from './use-backtest-storage-cursor';
2
+ export declare function useBacktestStorageBuffer<T>(storage: BacktestQuery<T>): {
3
3
  size(): number;
4
4
  peek(): {
5
5
  timestamp: number;
@@ -12,4 +12,4 @@ export declare function useBacktestQueryBuffer<T>(storage: BacktestStorage<T>):
12
12
  completed(): boolean;
13
13
  fetchNextPage(): Promise<void>;
14
14
  };
15
- //# sourceMappingURL=use-backtest-query-buffer.d.ts.map
15
+ //# sourceMappingURL=use-backtest-storage-buffer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-backtest-storage-buffer.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-storage-buffer.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAE9D,wBAAgB,wBAAwB,CAAC,CAAC,EAAE,OAAO,EAAE,aAAa,CAAC,CAAC,CAAC;;;mBAGtC,MAAM;iBAAW,CAAC;;;mBAAlB,MAAM;iBAAW,CAAC;;;;EAwChD"}
@@ -9,10 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.useBacktestQueryBuffer = void 0;
12
+ exports.useBacktestStorageBuffer = void 0;
13
13
  const storage_1 = require("../storage");
14
14
  const use_backtest_options_1 = require("./use-backtest-options");
15
- function useBacktestQueryBuffer(storage) {
15
+ function useBacktestStorageBuffer(storage) {
16
16
  const { from, to } = (0, use_backtest_options_1.useBacktestOptions)();
17
17
  let page = [];
18
18
  let index = 0;
@@ -49,4 +49,4 @@ function useBacktestQueryBuffer(storage) {
49
49
  }
50
50
  };
51
51
  }
52
- exports.useBacktestQueryBuffer = useBacktestQueryBuffer;
52
+ exports.useBacktestStorageBuffer = useBacktestStorageBuffer;
@@ -1,6 +1,20 @@
1
- import { BacktestStorage } from './use-backtest';
2
- export declare const useBacktestQueryCursor: () => {
3
- get<T>(query: BacktestStorage<T>): {
1
+ import { Query, QueryObject } from '../storage';
2
+ export interface BacktestQuery<V> {
3
+ query(query: Query<QueryObject> & {
4
+ where: {
5
+ timestamp: {
6
+ type: 'between';
7
+ min: number;
8
+ max: number;
9
+ };
10
+ };
11
+ }): Promise<{
12
+ timestamp: number;
13
+ payload: V;
14
+ }[]>;
15
+ }
16
+ export declare const useBacktestStorageCursor: () => {
17
+ get<T>(query: BacktestQuery<T>): {
4
18
  size(): number;
5
19
  peek(): {
6
20
  timestamp: number;
@@ -27,4 +41,4 @@ export declare const useBacktestQueryCursor: () => {
27
41
  fetchNextPage(): Promise<void>;
28
42
  } | undefined>;
29
43
  };
30
- //# sourceMappingURL=use-backtest-query-cursor.d.ts.map
44
+ //# sourceMappingURL=use-backtest-storage-cursor.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-backtest-storage-cursor.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-storage-cursor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAKlD,MAAM,WAAW,aAAa,CAAC,CAAC;IAC9B,KAAK,CACH,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,GAAG;QAC1B,KAAK,EAAE;YAAE,SAAS,EAAE;gBAAE,IAAI,EAAE,SAAS,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAC;KACrE,GACA,OAAO,CAAC;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,CAAC,CAAA;KAAE,EAAE,CAAC,CAAC;CACjD;AAED,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCnC,CAAC"}
@@ -9,14 +9,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.useBacktestQueryCursor = void 0;
12
+ exports.useBacktestStorageCursor = void 0;
13
13
  const with_memo_1 = require("../with-memo");
14
- const use_backtest_query_buffer_1 = require("./use-backtest-query-buffer");
15
- exports.useBacktestQueryCursor = (0, with_memo_1.withMemo)(() => {
14
+ const use_backtest_storage_buffer_1 = require("./use-backtest-storage-buffer");
15
+ exports.useBacktestStorageCursor = (0, with_memo_1.withMemo)(() => {
16
16
  const cursors = Array.of();
17
17
  return {
18
18
  get(query) {
19
- const buffer = (0, use_backtest_query_buffer_1.useBacktestQueryBuffer)(query);
19
+ const buffer = (0, use_backtest_storage_buffer_1.useBacktestStorageBuffer)(query);
20
20
  cursors.push(buffer);
21
21
  return buffer;
22
22
  },
@@ -1,6 +1,5 @@
1
1
  import { InferQueryObject, Query, QueryObject, QueryObjectType } from '../storage';
2
2
  import { Uri } from '../uri';
3
- import { BacktestStorage } from './use-backtest';
4
3
  export type BacktestStorageQuery<V> = {
5
4
  sync: <T extends QueryObjectType<K>, K extends QueryObject>(query: Query<InferQueryObject<T>> & {
6
5
  where: {
@@ -17,5 +16,10 @@ export type BacktestStorageQuery<V> = {
17
16
  }[]) => Promise<void>;
18
17
  }) => Promise<void>;
19
18
  };
20
- export declare function useBacktestStorage<V, P extends Record<string, string | number>>(uri: Uri<P>, { sync }: BacktestStorageQuery<V>): BacktestStorage<V>;
19
+ export declare function useBacktestStorage<V, P extends Record<string, string | number>>(uri: Uri<P>, { sync }: BacktestStorageQuery<V>): {
20
+ watch: () => import("rxjs").Observable<{
21
+ timestamp: number;
22
+ payload: V;
23
+ }>;
24
+ };
21
25
  //# sourceMappingURL=use-backtest-storage.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-backtest-storage.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,gBAAgB,EAChB,KAAK,EACL,WAAW,EACX,eAAe,EAGhB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEjD,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI;IACpC,IAAI,EAAE,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EACxD,KAAK,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAAG;QAClC,KAAK,EAAE;YAAE,SAAS,EAAE;gBAAE,IAAI,EAAE,SAAS,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAC;KACrE,EACD,OAAO,EAAE;QAAE,IAAI,EAAE,CAAC,OAAO,EAAE;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,OAAO,EAAE,CAAC,CAAA;SAAE,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,KAC/E,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB,CAAC;AASF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,EAC7E,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,EACX,EAAE,IAAI,EAAE,EAAE,oBAAoB,CAAC,CAAC,CAAC,GAChC,eAAe,CAAC,CAAC,CAAC,CAgDpB"}
1
+ {"version":3,"file":"use-backtest-storage.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-storage.ts"],"names":[],"mappings":"AACA,OAAO,EAEL,gBAAgB,EAChB,KAAK,EACL,WAAW,EACX,eAAe,EAGhB,MAAM,cAAc,CAAC;AACtB,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAI/B,MAAM,MAAM,oBAAoB,CAAC,CAAC,IAAI;IACpC,IAAI,EAAE,CAAC,CAAC,SAAS,eAAe,CAAC,CAAC,CAAC,EAAE,CAAC,SAAS,WAAW,EACxD,KAAK,EAAE,KAAK,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,GAAG;QAClC,KAAK,EAAE;YAAE,SAAS,EAAE;gBAAE,IAAI,EAAE,SAAS,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAC;KACrE,EACD,OAAO,EAAE;QAAE,IAAI,EAAE,CAAC,OAAO,EAAE;YAAE,SAAS,EAAE,MAAM,CAAC;YAAC,OAAO,EAAE,CAAC,CAAA;SAAE,EAAE,KAAK,OAAO,CAAC,IAAI,CAAC,CAAA;KAAE,KAC/E,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB,CAAC;AASF,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,EAC7E,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,EACX,EAAE,IAAI,EAAE,EAAE,oBAAoB,CAAC,CAAC,CAAC;;;;;EAsDlC"}
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.useBacktestStorage = void 0;
13
13
  const hash_code_1 = require("../hash-code");
14
14
  const storage_1 = require("../storage");
15
+ const use_backtest_scheduler_1 = require("./use-backtest-scheduler");
15
16
  const storageIndexObject = storage_1.Storage.createObject('index://range', {
16
17
  timestamp: 'number',
17
18
  uri: 'string',
@@ -19,6 +20,7 @@ const storageIndexObject = storage_1.Storage.createObject('index://range', {
19
20
  max: 'number'
20
21
  });
21
22
  function useBacktestStorage(uri, { sync }) {
23
+ const { watch } = (0, use_backtest_scheduler_1.useBacktestScheduler)();
22
24
  const storage = (0, storage_1.useStorage)(['backtest']);
23
25
  const storageObjectKey = uri.query;
24
26
  const storageObject = storage_1.Storage.createObject(storageObjectKey, {
@@ -27,39 +29,42 @@ function useBacktestStorage(uri, { sync }) {
27
29
  });
28
30
  const id = (0, hash_code_1.hashCode)(storageObjectKey);
29
31
  return {
30
- query(query) {
31
- return __awaiter(this, void 0, void 0, function* () {
32
- const [index] = yield storage.query(storageIndexObject, {
33
- limit: 1,
34
- where: { timestamp: (0, storage_1.eq)(id) }
35
- });
36
- const { min, max } = query.where.timestamp;
37
- if (!index || min < index.min || max > index.max) {
38
- yield sync(query, {
39
- save(objects) {
40
- return __awaiter(this, void 0, void 0, function* () {
41
- yield storage.save(storageObject, objects.map(it => ({
42
- timestamp: it.timestamp,
43
- payload: JSON.stringify(it.payload)
44
- })));
45
- });
46
- }
32
+ watch: () => watch({
33
+ query(query) {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const [index] = yield storage.query(storageIndexObject, {
36
+ limit: 1,
37
+ where: { timestamp: (0, storage_1.eq)(id) }
47
38
  });
48
- yield storage.save(storageIndexObject, [
49
- {
50
- timestamp: id,
51
- max,
52
- min,
53
- uri: storageObjectKey
54
- }
55
- ]);
56
- }
57
- return (yield storage.query(storageObject, query)).map(it => ({
58
- timestamp: it.timestamp,
59
- payload: JSON.parse(it.payload)
60
- }));
61
- });
62
- }
39
+ const { min, max } = query.where.timestamp;
40
+ if (!index || min < index.min || max > index.max) {
41
+ yield sync(query, {
42
+ save(objects) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ console.log('save');
45
+ yield storage.save(storageObject, objects.map(it => ({
46
+ timestamp: it.timestamp,
47
+ payload: JSON.stringify(it.payload)
48
+ })));
49
+ });
50
+ }
51
+ });
52
+ yield storage.save(storageIndexObject, [
53
+ {
54
+ timestamp: id,
55
+ max,
56
+ min,
57
+ uri: storageObjectKey
58
+ }
59
+ ]);
60
+ }
61
+ return (yield storage.query(storageObject, query)).map(it => ({
62
+ timestamp: it.timestamp,
63
+ payload: JSON.parse(it.payload)
64
+ }));
65
+ });
66
+ }
67
+ })
63
68
  };
64
69
  }
65
70
  exports.useBacktestStorage = useBacktestStorage;
@@ -1,24 +1,2 @@
1
- import { Observable } from 'rxjs';
2
- import { Query, QueryObject } from '../storage';
3
- export interface BacktestStorage<V> {
4
- query(query: Query<QueryObject> & {
5
- where: {
6
- timestamp: {
7
- type: 'between';
8
- min: number;
9
- max: number;
10
- };
11
- };
12
- }): Promise<{
13
- timestamp: number;
14
- payload: V;
15
- }[]>;
16
- }
17
- export declare function useBacktest<T>(input: Observable<{
18
- timestamp: number;
19
- payload: T;
20
- }>, query: BacktestStorage<T>): Observable<{
21
- timestamp: number;
22
- payload: T;
23
- }>;
1
+ export declare function useBacktest<T>(backtest: T, real: T): T;
24
2
  //# sourceMappingURL=use-backtest.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"use-backtest.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,MAAM,CAAC;AAElC,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAKlD,MAAM,WAAW,eAAe,CAAC,CAAC;IAChC,KAAK,CACH,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,GAAG;QAC1B,KAAK,EAAE;YAAE,SAAS,EAAE;gBAAE,IAAI,EAAE,SAAS,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAC;gBAAC,GAAG,EAAE,MAAM,CAAA;aAAE,CAAA;SAAE,CAAC;KACrE,GACA,OAAO,CAAC;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,CAAC,CAAA;KAAE,EAAE,CAAC,CAAC;CACjD;AAED,wBAAgB,WAAW,CAAC,CAAC,EAC3B,KAAK,EAAE,UAAU,CAAC;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,CAAC,CAAA;CAAE,CAAC,EACpD,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC;eADM,MAAM;aAAW,CAAC;GAWlD"}
1
+ {"version":3,"file":"use-backtest.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest.ts"],"names":[],"mappings":"AAEA,wBAAgB,WAAW,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,KAIlD"}
@@ -2,13 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useBacktest = void 0;
4
4
  const use_execution_mode_1 = require("../use-execution-mode");
5
- const use_backtest_scheduler_1 = require("./use-backtest-scheduler");
6
- function useBacktest(input, query) {
5
+ function useBacktest(backtest, real) {
7
6
  const { isReplay } = (0, use_execution_mode_1.useExecutionMode)();
8
- if (isReplay) {
9
- const { watch } = (0, use_backtest_scheduler_1.useBacktestScheduler)();
10
- return watch(query);
11
- }
12
- return input;
7
+ return isReplay ? backtest : real;
13
8
  }
14
9
  exports.useBacktest = useBacktest;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quantform/core",
3
- "version": "0.7.19",
3
+ "version": "0.7.20",
4
4
  "license": "MIT",
5
5
  "author": "Mateusz Majchrzak",
6
6
  "description": "Node.js library for building systematic trading strategies in reactive way.",
@@ -2,21 +2,23 @@ import { defer, filter, map, Observable, Subject } from 'rxjs';
2
2
 
3
3
  import { withMemo } from '@lib/with-memo';
4
4
 
5
- import { BacktestStorage } from './use-backtest';
6
5
  import { useBacktestOptions } from './use-backtest-options';
7
- import { useBacktestQueryBuffer } from './use-backtest-query-buffer';
8
- import { useBacktestQueryCursor } from './use-backtest-query-cursor';
6
+ import { useBacktestStorageBuffer } from './use-backtest-storage-buffer';
7
+ import { BacktestQuery, useBacktestStorageCursor } from './use-backtest-storage-cursor';
9
8
 
10
9
  export const useBacktestScheduler = withMemo(() => {
11
10
  const { from } = useBacktestOptions();
12
- const { get, cursor } = useBacktestQueryCursor();
11
+ const { get, cursor } = useBacktestStorageCursor();
13
12
 
14
13
  let timestamp = from;
15
14
  let stopAcquire = 1;
16
15
  let processing = false;
17
16
 
18
17
  const stream$ = new Subject<
19
- [ReturnType<typeof useBacktestQueryBuffer<any>>, { timestamp: number; payload: any }]
18
+ [
19
+ ReturnType<typeof useBacktestStorageBuffer<any>>,
20
+ { timestamp: number; payload: any }
21
+ ]
20
22
  >();
21
23
 
22
24
  const processNext = async () => {
@@ -79,7 +81,7 @@ export const useBacktestScheduler = withMemo(() => {
79
81
 
80
82
  tryContinue,
81
83
 
82
- watch<T>(query: BacktestStorage<T>): Observable<{ timestamp: number; payload: T }> {
84
+ watch<T>(query: BacktestQuery<T>): Observable<{ timestamp: number; payload: T }> {
83
85
  const storage = get<T>(query);
84
86
 
85
87
  return defer(() => {
@@ -1,9 +1,9 @@
1
1
  import { between } from '@lib/storage';
2
2
 
3
- import { BacktestStorage } from './use-backtest';
4
3
  import { useBacktestOptions } from './use-backtest-options';
4
+ import { BacktestQuery } from './use-backtest-storage-cursor';
5
5
 
6
- export function useBacktestQueryBuffer<T>(storage: BacktestStorage<T>) {
6
+ export function useBacktestStorageBuffer<T>(storage: BacktestQuery<T>) {
7
7
  const { from, to } = useBacktestOptions();
8
8
 
9
9
  let page: Array<{ timestamp: number; payload: T }> = [];
@@ -0,0 +1,48 @@
1
+ import { Query, QueryObject } from '@lib/storage';
2
+ import { withMemo } from '@lib/with-memo';
3
+
4
+ import { useBacktestStorageBuffer } from './use-backtest-storage-buffer';
5
+
6
+ export interface BacktestQuery<V> {
7
+ query(
8
+ query: Query<QueryObject> & {
9
+ where: { timestamp: { type: 'between'; min: number; max: number } };
10
+ }
11
+ ): Promise<{ timestamp: number; payload: V }[]>;
12
+ }
13
+
14
+ export const useBacktestStorageCursor = withMemo(() => {
15
+ const cursors = Array.of<ReturnType<typeof useBacktestStorageBuffer<any>>>();
16
+
17
+ return {
18
+ get<T>(query: BacktestQuery<T>) {
19
+ const buffer = useBacktestStorageBuffer<T>(query);
20
+
21
+ cursors.push(buffer);
22
+
23
+ return buffer;
24
+ },
25
+
26
+ async cursor() {
27
+ let current: ReturnType<typeof useBacktestStorageBuffer<any>> | undefined;
28
+
29
+ for (const cursor of cursors) {
30
+ if (cursor.completed()) {
31
+ continue;
32
+ }
33
+
34
+ if (cursor.size() == 0) {
35
+ await cursor.fetchNextPage();
36
+ }
37
+
38
+ if (cursor.peek()) {
39
+ if (!current || current.peek().timestamp > cursor.peek().timestamp) {
40
+ current = cursor;
41
+ }
42
+ }
43
+ }
44
+
45
+ return current;
46
+ }
47
+ };
48
+ });
@@ -10,7 +10,7 @@ import {
10
10
  } from '@lib/storage';
11
11
  import { Uri } from '@lib/uri';
12
12
 
13
- import { BacktestStorage } from './use-backtest';
13
+ import { useBacktestScheduler } from './use-backtest-scheduler';
14
14
 
15
15
  export type BacktestStorageQuery<V> = {
16
16
  sync: <T extends QueryObjectType<K>, K extends QueryObject>(
@@ -31,7 +31,8 @@ const storageIndexObject = Storage.createObject('index://range', {
31
31
  export function useBacktestStorage<V, P extends Record<string, string | number>>(
32
32
  uri: Uri<P>,
33
33
  { sync }: BacktestStorageQuery<V>
34
- ): BacktestStorage<V> {
34
+ ) {
35
+ const { watch } = useBacktestScheduler();
35
36
  const storage = useStorage(['backtest']);
36
37
  const storageObjectKey = uri.query;
37
38
  const storageObject = Storage.createObject(storageObjectKey, {
@@ -42,41 +43,45 @@ export function useBacktestStorage<V, P extends Record<string, string | number>>
42
43
  const id = hashCode(storageObjectKey);
43
44
 
44
45
  return {
45
- async query(query) {
46
- const [index] = await storage.query(storageIndexObject, {
47
- limit: 1,
48
- where: { timestamp: eq(id) }
49
- });
46
+ watch: () =>
47
+ watch({
48
+ async query(query) {
49
+ const [index] = await storage.query(storageIndexObject, {
50
+ limit: 1,
51
+ where: { timestamp: eq(id) }
52
+ });
50
53
 
51
- const { min, max } = query.where.timestamp;
54
+ const { min, max } = query.where.timestamp;
52
55
 
53
- if (!index || min < index.min || max > index.max) {
54
- await sync(query, {
55
- async save(objects) {
56
- await storage.save(
57
- storageObject,
58
- objects.map(it => ({
59
- timestamp: it.timestamp,
60
- payload: JSON.stringify(it.payload)
61
- }))
62
- );
63
- }
64
- });
56
+ if (!index || min < index.min || max > index.max) {
57
+ await sync(query, {
58
+ async save(objects) {
59
+ console.log('save');
60
+ await storage.save(
61
+ storageObject,
62
+ objects.map(it => ({
63
+ timestamp: it.timestamp,
64
+ payload: JSON.stringify(it.payload)
65
+ }))
66
+ );
67
+ }
68
+ });
65
69
 
66
- await storage.save(storageIndexObject, [
67
- {
68
- timestamp: id,
69
- max,
70
- min,
71
- uri: storageObjectKey
70
+ await storage.save(storageIndexObject, [
71
+ {
72
+ timestamp: id,
73
+ max,
74
+ min,
75
+ uri: storageObjectKey
76
+ }
77
+ ]);
72
78
  }
73
- ]);
74
- }
75
79
 
76
- return (await storage.query(storageObject, query)).map(it => ({
77
- timestamp: it.timestamp,
78
- payload: JSON.parse(it.payload) as V
79
- }));
80
- }
80
+ return (await storage.query(storageObject, query)).map(it => ({
81
+ timestamp: it.timestamp,
82
+ payload: JSON.parse(it.payload) as V
83
+ }));
84
+ }
85
+ })
81
86
  };
82
87
  }
@@ -1,28 +1,7 @@
1
- import { Observable } from 'rxjs';
2
-
3
- import { Query, QueryObject } from '@lib/storage';
4
1
  import { useExecutionMode } from '@lib/use-execution-mode';
5
2
 
6
- import { useBacktestScheduler } from './use-backtest-scheduler';
7
-
8
- export interface BacktestStorage<V> {
9
- query(
10
- query: Query<QueryObject> & {
11
- where: { timestamp: { type: 'between'; min: number; max: number } };
12
- }
13
- ): Promise<{ timestamp: number; payload: V }[]>;
14
- }
15
-
16
- export function useBacktest<T>(
17
- input: Observable<{ timestamp: number; payload: T }>,
18
- query: BacktestStorage<T>
19
- ) {
3
+ export function useBacktest<T>(backtest: T, real: T) {
20
4
  const { isReplay } = useExecutionMode();
21
5
 
22
- if (isReplay) {
23
- const { watch } = useBacktestScheduler();
24
- return watch<T>(query);
25
- }
26
-
27
- return input;
6
+ return isReplay ? backtest : real;
28
7
  }
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-backtest-query-buffer.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-query-buffer.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGjD,wBAAgB,sBAAsB,CAAC,CAAC,EAAE,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC;;;mBAGtC,MAAM;iBAAW,CAAC;;;mBAAlB,MAAM;iBAAW,CAAC;;;;EAwChD"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"use-backtest-query-cursor.d.ts","sourceRoot":"","sources":["../../src/backtest/use-backtest-query-cursor.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGjD,eAAO,MAAM,sBAAsB;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkCjC,CAAC"}
@@ -1,40 +0,0 @@
1
- import { withMemo } from '@lib/with-memo';
2
-
3
- import { BacktestStorage } from './use-backtest';
4
- import { useBacktestQueryBuffer } from './use-backtest-query-buffer';
5
-
6
- export const useBacktestQueryCursor = withMemo(() => {
7
- const cursors = Array.of<ReturnType<typeof useBacktestQueryBuffer<any>>>();
8
-
9
- return {
10
- get<T>(query: BacktestStorage<T>) {
11
- const buffer = useBacktestQueryBuffer<T>(query);
12
-
13
- cursors.push(buffer);
14
-
15
- return buffer;
16
- },
17
-
18
- async cursor() {
19
- let current: ReturnType<typeof useBacktestQueryBuffer<any>> | undefined;
20
-
21
- for (const cursor of cursors) {
22
- if (cursor.completed()) {
23
- continue;
24
- }
25
-
26
- if (cursor.size() == 0) {
27
- await cursor.fetchNextPage();
28
- }
29
-
30
- if (cursor.peek()) {
31
- if (!current || current.peek().timestamp > cursor.peek().timestamp) {
32
- current = cursor;
33
- }
34
- }
35
- }
36
-
37
- return current;
38
- }
39
- };
40
- });