@quantform/sqlite 0.6.7 → 0.7.0-beta.5

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.
@@ -1,120 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const fs_1 = require("fs");
13
- const core_1 = require("@quantform/core");
14
- const sqlite_storage_1 = require("./sqlite-storage");
15
- describe(sqlite_storage_1.SQLiteStorage.name, () => {
16
- const dbName = 'test.db';
17
- const instrument = new core_1.Instrument(0, new core_1.Asset('btc', 'binance', 8), new core_1.Asset('usdt', 'binance', 2), '', core_1.Commission.Zero);
18
- afterEach(() => {
19
- if ((0, fs_1.existsSync)(dbName)) {
20
- (0, fs_1.unlinkSync)(dbName);
21
- }
22
- });
23
- test('should create db file in user directory', () => __awaiter(void 0, void 0, void 0, function* () {
24
- const feed = new core_1.Feed(new sqlite_storage_1.SQLiteStorage(dbName));
25
- const input = [
26
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(1.12345678), 1616175004063)
27
- ];
28
- yield feed.save(input);
29
- const output = yield feed.query(instrument, {
30
- from: 1616175004062,
31
- to: 1616234152109,
32
- count: 100
33
- });
34
- expect(output.length).toBe(1);
35
- }));
36
- test('should insert multiple rows', () => __awaiter(void 0, void 0, void 0, function* () {
37
- const feed = new core_1.Feed(new sqlite_storage_1.SQLiteStorage(dbName));
38
- const input = [
39
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(1.12345678), 1616175004063),
40
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(2.12345678), 1616221874143),
41
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(3.12345678), 1616234152108)
42
- ];
43
- yield feed.save(input);
44
- const output = yield feed.query(instrument, {
45
- from: 1616175004062,
46
- to: 1616234152109,
47
- count: 100
48
- });
49
- expect(output.length).toBe(3);
50
- for (let i = 0; i < 3; i++) {
51
- expect(output[i]).toEqual(input[i]);
52
- }
53
- }));
54
- test('should limit result', () => __awaiter(void 0, void 0, void 0, function* () {
55
- const feed = new core_1.Feed(new sqlite_storage_1.SQLiteStorage(dbName));
56
- const input = [
57
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(1.12345678), 1616175004063),
58
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(2.12345678), 1616221874143),
59
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(3.12345678), 1616234152108)
60
- ];
61
- yield feed.save(input);
62
- const output = yield feed.query(instrument, {
63
- from: 1616175004062,
64
- to: 1616234152109,
65
- count: 2
66
- });
67
- expect(output.length).toBe(2);
68
- expect(output[0].timestamp).toBe(input[0].timestamp);
69
- expect(output[1].timestamp).toBe(input[1].timestamp);
70
- }));
71
- test('should override duplicated rows', () => __awaiter(void 0, void 0, void 0, function* () {
72
- const feed = new core_1.Feed(new sqlite_storage_1.SQLiteStorage(dbName));
73
- yield feed.save([
74
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(1.12345678), 1616175004063),
75
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(2.12345678), 1616221874143),
76
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1234.56789), (0, core_1.d)(3.12345678), 1616234152108)
77
- ]);
78
- const result = yield feed.query(instrument, {
79
- from: 1616175004062,
80
- to: 1616175004064,
81
- count: 100
82
- });
83
- expect(result.length).toBe(1);
84
- }));
85
- test('should patch a store with events', done => {
86
- const feed = new core_1.Feed(new sqlite_storage_1.SQLiteStorage(dbName));
87
- const store = new core_1.Store();
88
- store.snapshot.universe.instrument.upsert(instrument);
89
- store.snapshot.subscription.instrument.upsert(instrument);
90
- const streamer = new core_1.BacktesterStreamer(store, feed, {
91
- from: 0,
92
- to: 10
93
- }, {
94
- onBacktestCompleted: () => {
95
- var _a;
96
- const trade = (_a = store.snapshot.trade.get(instrument.id)) !== null && _a !== void 0 ? _a : fail();
97
- expect(trade.timestamp).toEqual(8);
98
- expect(trade.rate).toEqual((0, core_1.d)(8));
99
- expect(trade.quantity).toEqual((0, core_1.d)(8));
100
- expect(store.snapshot.timestamp).toEqual(8);
101
- done();
102
- }
103
- });
104
- feed
105
- .save([
106
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(1), (0, core_1.d)(1), 1),
107
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(2), (0, core_1.d)(2), 2),
108
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(3), (0, core_1.d)(3), 3),
109
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(4), (0, core_1.d)(4), 4),
110
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(5), (0, core_1.d)(5), 5),
111
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(6), (0, core_1.d)(6), 6),
112
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(7), (0, core_1.d)(7), 7),
113
- new core_1.TradePatchEvent(instrument, (0, core_1.d)(8), (0, core_1.d)(8), 8)
114
- ])
115
- .then(() => {
116
- streamer.subscribe(instrument);
117
- streamer.tryContinue();
118
- });
119
- });
120
- });
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=sqlite-measurement.spec.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"sqlite-measurement.spec.d.ts","sourceRoot":"","sources":["../src/sqlite-measurement.spec.ts"],"names":[],"mappings":""}
@@ -1,122 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const fs_1 = require("fs");
13
- const core_1 = require("@quantform/core");
14
- const sqlite_storage_1 = require("./sqlite-storage");
15
- describe('sqlite measurement tests', () => {
16
- const dbName = 'measurement.db';
17
- beforeEach(() => {
18
- if ((0, fs_1.existsSync)(dbName)) {
19
- (0, fs_1.unlinkSync)(dbName);
20
- }
21
- });
22
- afterAll(() => {
23
- if ((0, fs_1.existsSync)(dbName)) {
24
- (0, fs_1.unlinkSync)(dbName);
25
- }
26
- });
27
- test('should return empty array for unknown session', () => __awaiter(void 0, void 0, void 0, function* () {
28
- const measurement = new core_1.Measurement(new sqlite_storage_1.SQLiteStorage(dbName));
29
- const measure = yield measurement.query(0, {
30
- from: 0,
31
- count: 100
32
- });
33
- expect(measure).toEqual([]);
34
- }));
35
- test('should list written sessions', () => __awaiter(void 0, void 0, void 0, function* () {
36
- const measurement = new core_1.Measurement(new sqlite_storage_1.SQLiteStorage(dbName));
37
- yield measurement.save(1, [...Array(10).keys()].map(it => ({
38
- timestamp: it + 1,
39
- kind: 'spread',
40
- payload: { value: it + 1 }
41
- })));
42
- yield measurement.save(2, [...Array(10).keys()].map(it => ({
43
- timestamp: it + 1,
44
- kind: 'spread',
45
- payload: { value: it + 1 }
46
- })));
47
- const index = yield measurement.index();
48
- expect(index.length).toBe(2);
49
- expect(index[0]).toBe(1);
50
- expect(index[1]).toBe(2);
51
- }));
52
- test('should read and write measurement', () => __awaiter(void 0, void 0, void 0, function* () {
53
- const measurement = new core_1.Measurement(new sqlite_storage_1.SQLiteStorage(dbName));
54
- const session = 1;
55
- yield measurement.save(session, [...Array(10).keys()].map(it => ({
56
- timestamp: it + 1,
57
- kind: 'spread',
58
- payload: { value: it + 1 }
59
- })));
60
- const after = yield measurement.query(session, {
61
- from: 5,
62
- count: 100
63
- });
64
- expect(after.length).toBe(5);
65
- expect(after[0].timestamp).toBe(6);
66
- expect(after[0].kind).toBe('spread');
67
- expect(after[0].payload.value).toBe(6);
68
- expect(after[1].timestamp).toBe(7);
69
- expect(after[1].kind).toBe('spread');
70
- expect(after[1].payload.value).toBe(7);
71
- expect(after[2].timestamp).toBe(8);
72
- expect(after[2].kind).toBe('spread');
73
- expect(after[2].payload.value).toBe(8);
74
- const before = yield measurement.query(session, {
75
- to: 6,
76
- count: 100
77
- });
78
- expect(before.length).toBe(5);
79
- expect(before[0].timestamp).toBe(1);
80
- expect(before[0].kind).toBe('spread');
81
- expect(before[0].payload.value).toBe(1);
82
- expect(before[1].timestamp).toBe(2);
83
- expect(before[1].kind).toBe('spread');
84
- expect(before[1].payload.value).toBe(2);
85
- expect(before[2].timestamp).toBe(3);
86
- expect(before[2].kind).toBe('spread');
87
- expect(before[2].payload.value).toBe(3);
88
- }));
89
- test('should read and write specific measurement (state)', () => __awaiter(void 0, void 0, void 0, function* () {
90
- const measurement = new core_1.Measurement(new sqlite_storage_1.SQLiteStorage(dbName));
91
- const session = 1;
92
- yield measurement.save(session, [
93
- {
94
- timestamp: 1,
95
- kind: 'order-completed',
96
- payload: { rate: 100 }
97
- },
98
- {
99
- timestamp: 5,
100
- kind: 'order-completed',
101
- payload: { rate: 105 }
102
- }
103
- ]);
104
- let measure = yield measurement.query(session, {
105
- to: 2,
106
- count: 1,
107
- kind: 'order-completed'
108
- });
109
- expect(measure.length).toBe(1);
110
- expect(measure[0].timestamp).toBe(1);
111
- expect(measure[0].kind).toBe('order-completed');
112
- expect(measure[0].payload.rate).toBe(100);
113
- measure = yield measurement.query(session, {
114
- to: 6,
115
- count: 1
116
- });
117
- expect(measure.length).toBe(1);
118
- expect(measure[0].timestamp).toBe(5);
119
- expect(measure[0].kind).toBe('order-completed');
120
- expect(measure[0].payload.rate).toBe(105);
121
- }));
122
- });
package/src/error.ts DELETED
@@ -1,5 +0,0 @@
1
- export class NoConnectionError extends Error {
2
- constructor() {
3
- super('Missing database connection connection!');
4
- }
5
- }
@@ -1,158 +0,0 @@
1
- import { existsSync, unlinkSync } from 'fs';
2
-
3
- import {
4
- Asset,
5
- BacktesterStreamer,
6
- Commission,
7
- d,
8
- Feed,
9
- Instrument,
10
- Store,
11
- TradePatchEvent
12
- } from '@quantform/core';
13
-
14
- import { SQLiteStorage } from '@lib/sqlite-storage';
15
-
16
- describe(SQLiteStorage.name, () => {
17
- const dbName = 'test.db';
18
- const instrument = new Instrument(
19
- 0,
20
- new Asset('btc', 'binance', 8),
21
- new Asset('usdt', 'binance', 2),
22
- '',
23
- Commission.Zero
24
- );
25
-
26
- afterEach(() => {
27
- if (existsSync(dbName)) {
28
- unlinkSync(dbName);
29
- }
30
- });
31
-
32
- test('should create db file in user directory', async () => {
33
- const feed = new Feed(new SQLiteStorage(dbName));
34
-
35
- const input = [
36
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063)
37
- ];
38
-
39
- await feed.save(input);
40
-
41
- const output = await feed.query(instrument, {
42
- from: 1616175004062,
43
- to: 1616234152109,
44
- count: 100
45
- });
46
-
47
- expect(output.length).toBe(1);
48
- });
49
-
50
- test('should insert multiple rows', async () => {
51
- const feed = new Feed(new SQLiteStorage(dbName));
52
-
53
- const input = [
54
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063),
55
- new TradePatchEvent(instrument, d(1234.56789), d(2.12345678), 1616221874143),
56
- new TradePatchEvent(instrument, d(1234.56789), d(3.12345678), 1616234152108)
57
- ];
58
-
59
- await feed.save(input);
60
-
61
- const output = await feed.query(instrument, {
62
- from: 1616175004062,
63
- to: 1616234152109,
64
- count: 100
65
- });
66
-
67
- expect(output.length).toBe(3);
68
-
69
- for (let i = 0; i < 3; i++) {
70
- expect(output[i]).toEqual(input[i]);
71
- }
72
- });
73
-
74
- test('should limit result', async () => {
75
- const feed = new Feed(new SQLiteStorage(dbName));
76
-
77
- const input = [
78
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063),
79
- new TradePatchEvent(instrument, d(1234.56789), d(2.12345678), 1616221874143),
80
- new TradePatchEvent(instrument, d(1234.56789), d(3.12345678), 1616234152108)
81
- ];
82
-
83
- await feed.save(input);
84
-
85
- const output = await feed.query(instrument, {
86
- from: 1616175004062,
87
- to: 1616234152109,
88
- count: 2
89
- });
90
-
91
- expect(output.length).toBe(2);
92
- expect(output[0].timestamp).toBe(input[0].timestamp);
93
- expect(output[1].timestamp).toBe(input[1].timestamp);
94
- });
95
-
96
- test('should override duplicated rows', async () => {
97
- const feed = new Feed(new SQLiteStorage(dbName));
98
-
99
- await feed.save([
100
- new TradePatchEvent(instrument, d(1234.56789), d(1.12345678), 1616175004063),
101
- new TradePatchEvent(instrument, d(1234.56789), d(2.12345678), 1616221874143),
102
- new TradePatchEvent(instrument, d(1234.56789), d(3.12345678), 1616234152108)
103
- ]);
104
-
105
- const result = await feed.query(instrument, {
106
- from: 1616175004062,
107
- to: 1616175004064,
108
- count: 100
109
- });
110
-
111
- expect(result.length).toBe(1);
112
- });
113
-
114
- test('should patch a store with events', done => {
115
- const feed = new Feed(new SQLiteStorage(dbName));
116
- const store = new Store();
117
-
118
- store.snapshot.universe.instrument.upsert(instrument);
119
- store.snapshot.subscription.instrument.upsert(instrument);
120
-
121
- const streamer = new BacktesterStreamer(
122
- store,
123
- feed,
124
- {
125
- from: 0,
126
- to: 10
127
- },
128
- {
129
- onBacktestCompleted: () => {
130
- const trade = store.snapshot.trade.get(instrument.id) ?? fail();
131
-
132
- expect(trade.timestamp).toEqual(8);
133
- expect(trade.rate).toEqual(d(8));
134
- expect(trade.quantity).toEqual(d(8));
135
- expect(store.snapshot.timestamp).toEqual(8);
136
-
137
- done();
138
- }
139
- }
140
- );
141
-
142
- feed
143
- .save([
144
- new TradePatchEvent(instrument, d(1), d(1), 1),
145
- new TradePatchEvent(instrument, d(2), d(2), 2),
146
- new TradePatchEvent(instrument, d(3), d(3), 3),
147
- new TradePatchEvent(instrument, d(4), d(4), 4),
148
- new TradePatchEvent(instrument, d(5), d(5), 5),
149
- new TradePatchEvent(instrument, d(6), d(6), 6),
150
- new TradePatchEvent(instrument, d(7), d(7), 7),
151
- new TradePatchEvent(instrument, d(8), d(8), 8)
152
- ])
153
- .then(() => {
154
- streamer.subscribe(instrument);
155
- streamer.tryContinue();
156
- });
157
- });
158
- });
@@ -1,151 +0,0 @@
1
- import { existsSync, unlinkSync } from 'fs';
2
-
3
- import { Measurement } from '@quantform/core';
4
-
5
- import { SQLiteStorage } from '@lib/sqlite-storage';
6
-
7
- describe('sqlite measurement tests', () => {
8
- const dbName = 'measurement.db';
9
-
10
- beforeEach(() => {
11
- if (existsSync(dbName)) {
12
- unlinkSync(dbName);
13
- }
14
- });
15
-
16
- afterAll(() => {
17
- if (existsSync(dbName)) {
18
- unlinkSync(dbName);
19
- }
20
- });
21
-
22
- test('should return empty array for unknown session', async () => {
23
- const measurement = new Measurement(new SQLiteStorage(dbName));
24
-
25
- const measure = await measurement.query(0, {
26
- from: 0,
27
- count: 100
28
- });
29
-
30
- expect(measure).toEqual([]);
31
- });
32
-
33
- test('should list written sessions', async () => {
34
- const measurement = new Measurement(new SQLiteStorage(dbName));
35
-
36
- await measurement.save(
37
- 1,
38
- [...Array(10).keys()].map(it => ({
39
- timestamp: it + 1,
40
- kind: 'spread',
41
- payload: { value: it + 1 }
42
- }))
43
- );
44
-
45
- await measurement.save(
46
- 2,
47
- [...Array(10).keys()].map(it => ({
48
- timestamp: it + 1,
49
- kind: 'spread',
50
- payload: { value: it + 1 }
51
- }))
52
- );
53
-
54
- const index = await measurement.index();
55
-
56
- expect(index.length).toBe(2);
57
- expect(index[0]).toBe(1);
58
- expect(index[1]).toBe(2);
59
- });
60
-
61
- test('should read and write measurement', async () => {
62
- const measurement = new Measurement(new SQLiteStorage(dbName));
63
-
64
- const session = 1;
65
-
66
- await measurement.save(
67
- session,
68
- [...Array(10).keys()].map(it => ({
69
- timestamp: it + 1,
70
- kind: 'spread',
71
- payload: { value: it + 1 }
72
- }))
73
- );
74
-
75
- const after = await measurement.query(session, {
76
- from: 5,
77
- count: 100
78
- });
79
-
80
- expect(after.length).toBe(5);
81
- expect(after[0].timestamp).toBe(6);
82
- expect(after[0].kind).toBe('spread');
83
- expect(after[0].payload.value).toBe(6);
84
-
85
- expect(after[1].timestamp).toBe(7);
86
- expect(after[1].kind).toBe('spread');
87
- expect(after[1].payload.value).toBe(7);
88
-
89
- expect(after[2].timestamp).toBe(8);
90
- expect(after[2].kind).toBe('spread');
91
- expect(after[2].payload.value).toBe(8);
92
-
93
- const before = await measurement.query(session, {
94
- to: 6,
95
- count: 100
96
- });
97
-
98
- expect(before.length).toBe(5);
99
- expect(before[0].timestamp).toBe(1);
100
- expect(before[0].kind).toBe('spread');
101
- expect(before[0].payload.value).toBe(1);
102
-
103
- expect(before[1].timestamp).toBe(2);
104
- expect(before[1].kind).toBe('spread');
105
- expect(before[1].payload.value).toBe(2);
106
-
107
- expect(before[2].timestamp).toBe(3);
108
- expect(before[2].kind).toBe('spread');
109
- expect(before[2].payload.value).toBe(3);
110
- });
111
-
112
- test('should read and write specific measurement (state)', async () => {
113
- const measurement = new Measurement(new SQLiteStorage(dbName));
114
-
115
- const session = 1;
116
-
117
- await measurement.save(session, [
118
- {
119
- timestamp: 1,
120
- kind: 'order-completed',
121
- payload: { rate: 100 }
122
- },
123
- {
124
- timestamp: 5,
125
- kind: 'order-completed',
126
- payload: { rate: 105 }
127
- }
128
- ]);
129
-
130
- let measure = await measurement.query(session, {
131
- to: 2,
132
- count: 1,
133
- kind: 'order-completed'
134
- });
135
-
136
- expect(measure.length).toBe(1);
137
- expect(measure[0].timestamp).toBe(1);
138
- expect(measure[0].kind).toBe('order-completed');
139
- expect(measure[0].payload.rate).toBe(100);
140
-
141
- measure = await measurement.query(session, {
142
- to: 6,
143
- count: 1
144
- });
145
-
146
- expect(measure.length).toBe(1);
147
- expect(measure[0].timestamp).toBe(5);
148
- expect(measure[0].kind).toBe('order-completed');
149
- expect(measure[0].payload.rate).toBe(105);
150
- });
151
- });