@quantform/core 0.6.6 → 0.6.7
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/dist/adapter/adapter-aggregate.d.ts +2 -2
- package/dist/adapter/adapter-aggregate.d.ts.map +1 -1
- package/dist/adapter/adapter-aggregate.js +12 -11
- package/dist/adapter/adapter.d.ts +2 -3
- package/dist/adapter/adapter.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-adapter.d.ts +1 -5
- package/dist/adapter/backtester/backtester-adapter.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-adapter.js +2 -2
- package/dist/adapter/backtester/backtester-cursor.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-cursor.js +2 -2
- package/dist/adapter/backtester/backtester-cursor.spec.d.ts +2 -0
- package/dist/adapter/backtester/backtester-cursor.spec.d.ts.map +1 -0
- package/dist/adapter/backtester/backtester-cursor.spec.js +47 -0
- package/dist/adapter/backtester/backtester-streamer.d.ts +1 -1
- package/dist/adapter/backtester/backtester-streamer.d.ts.map +1 -1
- package/dist/adapter/backtester/backtester-streamer.js +4 -5
- package/dist/adapter/backtester/backtester-streamer.spec.d.ts +2 -0
- package/dist/adapter/backtester/backtester-streamer.spec.d.ts.map +1 -0
- package/dist/adapter/backtester/backtester-streamer.spec.js +45 -0
- package/dist/adapter/backtester/error.d.ts +6 -2
- package/dist/adapter/backtester/error.d.ts.map +1 -1
- package/dist/adapter/backtester/error.js +11 -7
- package/dist/adapter/backtester/index.d.ts +4 -2
- package/dist/adapter/backtester/index.d.ts.map +1 -1
- package/dist/adapter/backtester/index.js +4 -2
- package/dist/adapter/error.d.ts +9 -3
- package/dist/adapter/error.d.ts.map +1 -1
- package/dist/adapter/error.js +16 -10
- package/dist/adapter/index.d.ts +5 -4
- package/dist/adapter/index.d.ts.map +1 -1
- package/dist/adapter/index.js +5 -4
- package/dist/adapter/paper/engine/paper-engine.d.ts.map +1 -1
- package/dist/adapter/paper/engine/paper-engine.js +7 -8
- package/dist/adapter/paper/engine/paper-engine.spec.d.ts +2 -0
- package/dist/adapter/paper/engine/paper-engine.spec.d.ts.map +1 -0
- package/dist/adapter/paper/engine/paper-engine.spec.js +63 -0
- package/dist/adapter/paper/index.d.ts +2 -2
- package/dist/adapter/paper/index.d.ts.map +1 -1
- package/dist/adapter/paper/index.js +2 -2
- package/dist/adapter/paper/paper-adapter.d.ts +1 -3
- package/dist/adapter/paper/paper-adapter.d.ts.map +1 -1
- package/dist/adapter/paper/paper-adapter.js +8 -7
- package/dist/cli/build.js +1 -1
- package/dist/cli/dev.js +6 -6
- package/dist/cli/index.js +5 -5
- package/dist/cli/internal/workspace.js +1 -1
- package/dist/cli/pull.js +6 -6
- package/dist/cli/run.js +6 -6
- package/dist/cli/test.d.ts.map +1 -1
- package/dist/cli/test.js +9 -8
- package/dist/domain/asset.d.ts +5 -1
- package/dist/domain/asset.d.ts.map +1 -1
- package/dist/domain/asset.js +13 -7
- package/dist/domain/asset.spec.d.ts +2 -0
- package/dist/domain/asset.spec.d.ts.map +1 -0
- package/dist/domain/asset.spec.js +54 -0
- package/dist/domain/balance-operator.d.ts +1 -3
- package/dist/domain/balance-operator.d.ts.map +1 -1
- package/dist/domain/balance-operator.js +2 -2
- package/dist/domain/balance-operator.spec.d.ts +2 -0
- package/dist/domain/balance-operator.spec.d.ts.map +1 -0
- package/dist/domain/balance-operator.spec.js +21 -0
- package/dist/domain/balance.d.ts +14 -15
- package/dist/domain/balance.d.ts.map +1 -1
- package/dist/domain/balance.js +30 -48
- package/dist/domain/balance.spec.d.ts +2 -0
- package/dist/domain/balance.spec.d.ts.map +1 -0
- package/dist/domain/balance.spec.js +32 -0
- package/dist/domain/commission.d.ts.map +1 -1
- package/dist/domain/commission.spec.d.ts +2 -0
- package/dist/domain/commission.spec.d.ts.map +1 -0
- package/dist/domain/commission.spec.js +30 -0
- package/dist/domain/component.d.ts +2 -2
- package/dist/domain/component.d.ts.map +1 -1
- package/dist/domain/error.d.ts +15 -5
- package/dist/domain/error.d.ts.map +1 -1
- package/dist/domain/error.js +26 -16
- package/dist/domain/index.d.ts +21 -21
- package/dist/domain/index.d.ts.map +1 -1
- package/dist/domain/index.js +21 -21
- package/dist/domain/instrument-operator.d.ts +1 -2
- package/dist/domain/instrument-operator.d.ts.map +1 -1
- package/dist/domain/instrument-operator.js +3 -3
- package/dist/domain/instrument-operator.spec.d.ts +2 -0
- package/dist/domain/instrument-operator.spec.d.ts.map +1 -0
- package/dist/domain/instrument-operator.spec.js +21 -0
- package/dist/domain/instrument.d.ts +3 -3
- package/dist/domain/instrument.d.ts.map +1 -1
- package/dist/domain/instrument.js +10 -8
- package/dist/domain/instrument.spec.d.ts +2 -0
- package/dist/domain/instrument.spec.d.ts.map +1 -0
- package/dist/domain/instrument.spec.js +51 -0
- package/dist/domain/ohlc-operator.d.ts +1 -1
- package/dist/domain/ohlc-operator.d.ts.map +1 -1
- package/dist/domain/ohlc-operator.js +4 -5
- package/dist/domain/ohlc-operator.spec.d.ts +2 -0
- package/dist/domain/ohlc-operator.spec.d.ts.map +1 -0
- package/dist/domain/ohlc-operator.spec.js +110 -0
- package/dist/domain/ohlc.d.ts.map +1 -1
- package/dist/domain/ohlc.spec.d.ts +2 -0
- package/dist/domain/ohlc.spec.d.ts.map +1 -0
- package/dist/domain/ohlc.spec.js +25 -0
- package/dist/domain/order-operator.d.ts +1 -3
- package/dist/domain/order-operator.d.ts.map +1 -1
- package/dist/domain/order-operator.js +3 -3
- package/dist/domain/order-operator.spec.d.ts +2 -0
- package/dist/domain/order-operator.spec.d.ts.map +1 -0
- package/dist/domain/order-operator.spec.js +61 -0
- package/dist/domain/order.d.ts +5 -8
- package/dist/domain/order.d.ts.map +1 -1
- package/dist/domain/order.js +14 -22
- package/dist/domain/order.spec.d.ts +2 -0
- package/dist/domain/order.spec.d.ts.map +1 -0
- package/dist/domain/order.spec.js +27 -0
- package/dist/domain/orderbook-operator.d.ts +1 -3
- package/dist/domain/orderbook-operator.d.ts.map +1 -1
- package/dist/domain/orderbook-operator.js +2 -2
- package/dist/domain/orderbook-operator.spec.d.ts +2 -0
- package/dist/domain/orderbook-operator.spec.d.ts.map +1 -0
- package/dist/domain/orderbook-operator.spec.js +19 -0
- package/dist/domain/orderbook.d.ts +3 -2
- package/dist/domain/orderbook.d.ts.map +1 -1
- package/dist/domain/orderbook.js +3 -0
- package/dist/domain/orderbook.spec.d.ts +2 -0
- package/dist/domain/orderbook.spec.d.ts.map +1 -0
- package/dist/domain/orderbook.spec.js +11 -0
- package/dist/domain/position-operator.d.ts +1 -3
- package/dist/domain/position-operator.d.ts.map +1 -1
- package/dist/domain/position-operator.js +3 -4
- package/dist/domain/position-operator.spec.d.ts +2 -0
- package/dist/domain/position-operator.spec.d.ts.map +1 -0
- package/dist/domain/position-operator.spec.js +44 -0
- package/dist/domain/position.d.ts +3 -2
- package/dist/domain/position.d.ts.map +1 -1
- package/dist/domain/position.js +2 -0
- package/dist/domain/position.spec.d.ts +2 -0
- package/dist/domain/position.spec.d.ts.map +1 -0
- package/dist/domain/position.spec.js +28 -0
- package/dist/domain/session-builder.d.ts +8 -4
- package/dist/domain/session-builder.d.ts.map +1 -1
- package/dist/domain/session-builder.js +10 -15
- package/dist/domain/session.d.ts +1 -1
- package/dist/domain/session.d.ts.map +1 -1
- package/dist/domain/session.js +11 -17
- package/dist/domain/session.spec.d.ts +2 -0
- package/dist/domain/session.spec.d.ts.map +1 -0
- package/dist/domain/session.spec.js +17 -0
- package/dist/domain/trade-operator.d.ts +1 -3
- package/dist/domain/trade-operator.d.ts.map +1 -1
- package/dist/domain/trade-operator.js +2 -2
- package/dist/domain/trade-operator.spec.d.ts +2 -0
- package/dist/domain/trade-operator.spec.d.ts.map +1 -0
- package/dist/domain/trade-operator.spec.js +21 -0
- package/dist/domain/trade.d.ts +3 -2
- package/dist/domain/trade.d.ts.map +1 -1
- package/dist/domain/trade.js +3 -0
- package/dist/domain/trade.spec.d.ts +2 -0
- package/dist/domain/trade.spec.d.ts.map +1 -0
- package/dist/domain/trade.spec.js +11 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -8
- package/dist/shared/collections.spec.d.ts +2 -0
- package/dist/shared/collections.spec.d.ts.map +1 -0
- package/dist/shared/collections.spec.js +29 -0
- package/dist/shared/decimals.spec.d.ts +2 -0
- package/dist/shared/decimals.spec.d.ts.map +1 -0
- package/dist/shared/decimals.spec.js +28 -0
- package/dist/shared/hash.d.ts +2 -0
- package/dist/shared/hash.d.ts.map +1 -0
- package/dist/shared/hash.js +12 -0
- package/dist/shared/index.d.ts +7 -7
- package/dist/shared/index.d.ts.map +1 -1
- package/dist/shared/index.js +7 -7
- package/dist/shared/logger.d.ts +8 -5
- package/dist/shared/logger.d.ts.map +1 -1
- package/dist/shared/logger.js +23 -16
- package/dist/storage/cache.d.ts +1 -1
- package/dist/storage/cache.d.ts.map +1 -1
- package/dist/storage/cache.spec.d.ts +2 -0
- package/dist/storage/cache.spec.d.ts.map +1 -0
- package/dist/storage/cache.spec.js +25 -0
- package/dist/storage/feed.d.ts +1 -1
- package/dist/storage/feed.d.ts.map +1 -1
- package/dist/storage/index.d.ts +4 -4
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/storage/index.js +4 -4
- package/dist/storage/measurement.d.ts +1 -1
- package/dist/storage/measurement.d.ts.map +1 -1
- package/dist/store/error.d.ts +21 -7
- package/dist/store/error.d.ts.map +1 -1
- package/dist/store/error.js +36 -22
- package/dist/store/index.d.ts +10 -9
- package/dist/store/index.d.ts.map +1 -1
- package/dist/store/index.js +10 -9
- package/dist/store/store-balance-event.d.ts +10 -30
- package/dist/store/store-balance-event.d.ts.map +1 -1
- package/dist/store/store-balance-event.js +19 -94
- package/dist/store/store-balance-event.spec.d.ts +2 -0
- package/dist/store/store-balance-event.spec.d.ts.map +1 -0
- package/dist/store/store-balance-event.spec.js +24 -0
- package/dist/store/store-event.d.ts +1 -1
- package/dist/store/store-event.d.ts.map +1 -1
- package/dist/store/store-instrument-event.d.ts +1 -2
- package/dist/store/store-instrument-event.d.ts.map +1 -1
- package/dist/store/store-instrument-event.js +5 -6
- package/dist/store/store-instrument-event.spec.d.ts +2 -0
- package/dist/store/store-instrument-event.spec.d.ts.map +1 -0
- package/dist/store/store-instrument-event.spec.js +22 -0
- package/dist/store/store-order-event.d.ts +1 -2
- package/dist/store/store-order-event.d.ts.map +1 -1
- package/dist/store/store-order-event.js +60 -22
- package/dist/store/store-order-event.spec.d.ts +2 -0
- package/dist/store/store-order-event.spec.d.ts.map +1 -0
- package/dist/store/store-order-event.spec.js +23 -0
- package/dist/store/store-orderbook-event.d.ts +1 -2
- package/dist/store/store-orderbook-event.d.ts.map +1 -1
- package/dist/store/store-orderbook-event.js +3 -3
- package/dist/store/store-orderbook-event.spec.d.ts +2 -0
- package/dist/store/store-orderbook-event.spec.d.ts.map +1 -0
- package/dist/store/store-orderbook-event.spec.js +27 -0
- package/dist/store/store-position-event.d.ts +1 -2
- package/dist/store/store-position-event.d.ts.map +1 -1
- package/dist/store/store-position-event.js +5 -5
- package/dist/store/store-state.d.ts.map +1 -1
- package/dist/store/store-trade-event.d.ts +1 -2
- package/dist/store/store-trade-event.d.ts.map +1 -1
- package/dist/store/store-trade-event.js +3 -3
- package/dist/store/store-trade-event.spec.d.ts +2 -0
- package/dist/store/store-trade-event.spec.d.ts.map +1 -0
- package/dist/store/store-trade-event.spec.js +44 -0
- package/dist/store/store.d.ts +1 -2
- package/dist/store/store.d.ts.map +1 -1
- package/dist/store/store.js +5 -2
- package/dist/store/store.spec.d.ts +2 -0
- package/dist/store/store.spec.d.ts.map +1 -0
- package/dist/store/store.spec.js +122 -0
- package/jest.config.ts +23 -2
- package/package.json +2 -2
- package/src/adapter/adapter-aggregate.ts +35 -21
- package/src/adapter/adapter.ts +6 -7
- package/src/adapter/backtester/backtester-adapter.ts +12 -8
- package/src/adapter/backtester/backtester-cursor.spec.ts +6 -6
- package/src/adapter/backtester/backtester-cursor.ts +5 -5
- package/src/adapter/backtester/backtester-streamer.spec.ts +6 -6
- package/src/adapter/backtester/backtester-streamer.ts +12 -9
- package/src/adapter/backtester/error.ts +8 -4
- package/src/adapter/backtester/index.ts +4 -2
- package/src/adapter/error.ts +14 -8
- package/src/adapter/index.ts +5 -4
- package/src/adapter/paper/engine/paper-engine.spec.ts +6 -12
- package/src/adapter/paper/engine/paper-engine.ts +13 -28
- package/src/adapter/paper/index.ts +2 -2
- package/src/adapter/paper/paper-adapter.ts +18 -11
- package/src/cli/build.ts +1 -1
- package/src/cli/dev.ts +5 -5
- package/src/cli/index.ts +5 -5
- package/src/cli/internal/workspace.ts +1 -1
- package/src/cli/pull.ts +7 -7
- package/src/cli/run.ts +5 -5
- package/src/cli/test.ts +8 -7
- package/src/domain/asset.spec.ts +4 -4
- package/src/domain/asset.ts +13 -6
- package/src/domain/balance-operator.spec.ts +3 -6
- package/src/domain/balance-operator.ts +5 -6
- package/src/domain/balance.spec.ts +7 -77
- package/src/domain/balance.ts +51 -64
- package/src/domain/commission.spec.ts +3 -3
- package/src/domain/commission.ts +1 -1
- package/src/domain/component.ts +3 -3
- package/src/domain/error.ts +23 -18
- package/src/domain/index.ts +21 -21
- package/src/domain/instrument-operator.spec.ts +10 -7
- package/src/domain/instrument-operator.ts +6 -6
- package/src/domain/instrument.spec.ts +11 -6
- package/src/domain/instrument.ts +16 -7
- package/src/domain/ohlc-operator.spec.ts +5 -6
- package/src/domain/ohlc-operator.ts +2 -3
- package/src/domain/ohlc.spec.ts +2 -2
- package/src/domain/ohlc.ts +2 -2
- package/src/domain/order-operator.spec.ts +14 -10
- package/src/domain/order-operator.ts +8 -7
- package/src/domain/order.spec.ts +3 -6
- package/src/domain/order.ts +26 -29
- package/src/domain/orderbook-operator.spec.ts +11 -9
- package/src/domain/orderbook-operator.ts +5 -6
- package/src/domain/orderbook.spec.ts +3 -6
- package/src/domain/orderbook.ts +5 -3
- package/src/domain/position-operator.spec.ts +14 -11
- package/src/domain/position-operator.ts +12 -9
- package/src/domain/position.spec.ts +3 -6
- package/src/domain/position.ts +5 -3
- package/src/domain/session-builder.ts +14 -14
- package/src/domain/session.spec.ts +4 -4
- package/src/domain/session.ts +20 -15
- package/src/domain/trade-operator.spec.ts +4 -9
- package/src/domain/trade-operator.ts +5 -6
- package/src/domain/trade.spec.ts +3 -6
- package/src/domain/trade.ts +5 -3
- package/src/index.ts +15 -14
- package/src/shared/collections.spec.ts +2 -2
- package/src/shared/decimals.spec.ts +1 -1
- package/src/shared/hash.ts +10 -0
- package/src/shared/index.ts +7 -7
- package/src/shared/logger.ts +22 -15
- package/src/storage/cache.spec.ts +2 -3
- package/src/storage/cache.ts +2 -2
- package/src/storage/feed.ts +4 -4
- package/src/storage/index.ts +4 -4
- package/src/storage/measurement.ts +2 -2
- package/src/store/error.ts +33 -22
- package/src/store/index.ts +10 -9
- package/src/store/store-balance-event.spec.ts +5 -7
- package/src/store/store-balance-event.ts +20 -115
- package/src/store/store-event.ts +2 -2
- package/src/store/store-instrument-event.spec.ts +4 -5
- package/src/store/store-instrument-event.ts +13 -9
- package/src/store/store-order-event.spec.ts +6 -5
- package/src/store/store-order-event.ts +92 -23
- package/src/store/store-orderbook-event.spec.ts +4 -5
- package/src/store/store-orderbook-event.ts +11 -7
- package/src/store/store-position-event.ts +13 -9
- package/src/store/store-state.ts +2 -2
- package/src/store/store-trade-event.spec.ts +4 -5
- package/src/store/store-trade-event.ts +11 -7
- package/src/store/store.spec.ts +45 -31
- package/src/store/store.ts +6 -3
- package/tsconfig.json +7 -4
- package/dist/cli/error.d.ts +0 -2
- package/dist/cli/error.d.ts.map +0 -1
- package/dist/cli/error.js +0 -7
- package/dist/shared/pipe.d.ts +0 -5
- package/dist/shared/pipe.d.ts.map +0 -1
- package/dist/shared/pipe.js +0 -8
- package/src/cli/error.ts +0 -5
- package/src/shared/pipe.ts +0 -12
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
1
|
+
export class MissingPeriodParametersError extends Error {
|
|
2
|
+
constructor() {
|
|
3
|
+
super('invalid backtest options, please provide from and to period.');
|
|
4
|
+
}
|
|
3
5
|
}
|
|
4
6
|
|
|
5
|
-
export
|
|
6
|
-
|
|
7
|
+
export class InvalidEventSequenceError extends Error {
|
|
8
|
+
constructor() {
|
|
9
|
+
super('invalid event to consume');
|
|
10
|
+
}
|
|
7
11
|
}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export * from '
|
|
1
|
+
export * from '@lib/adapter/backtester/backtester-adapter';
|
|
2
|
+
export * from '@lib/adapter/backtester/backtester-streamer';
|
|
3
|
+
export * from '@lib/adapter/backtester/backtester-cursor';
|
|
4
|
+
export * from '@lib/adapter/backtester/error';
|
package/src/adapter/error.ts
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
export
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export class AdapterNotFoundError extends Error {
|
|
2
|
+
constructor(adapterName: string) {
|
|
3
|
+
super(
|
|
4
|
+
`Unknown adapter: ${adapterName}. You should provide adapter in session descriptor.`
|
|
5
|
+
);
|
|
6
|
+
}
|
|
5
7
|
}
|
|
6
8
|
|
|
7
|
-
export
|
|
8
|
-
|
|
9
|
+
export class NoPaperEngineProvidedError extends Error {
|
|
10
|
+
constructor() {
|
|
11
|
+
super('No paper engine provided.');
|
|
12
|
+
}
|
|
9
13
|
}
|
|
10
14
|
|
|
11
|
-
export
|
|
12
|
-
|
|
15
|
+
export class BacktestPageNotEmpty extends Error {
|
|
16
|
+
constructor() {
|
|
17
|
+
super('Backtest page is not empty');
|
|
18
|
+
}
|
|
13
19
|
}
|
package/src/adapter/index.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export * from '
|
|
3
|
-
export * from '
|
|
4
|
-
export * from '
|
|
1
|
+
export * from '@lib/adapter/adapter';
|
|
2
|
+
export * from '@lib/adapter/adapter-aggregate';
|
|
3
|
+
export * from '@lib/adapter/backtester';
|
|
4
|
+
export * from '@lib/adapter/paper';
|
|
5
|
+
export * from '@lib/adapter/error';
|
|
@@ -1,19 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
commissionPercentOf,
|
|
5
|
-
Instrument,
|
|
6
|
-
Order
|
|
7
|
-
} from '../../../domain';
|
|
8
|
-
import { d, now } from '../../../shared';
|
|
1
|
+
import { PaperEngine } from '@lib/adapter';
|
|
2
|
+
import { Asset, Commission, commissionPercentOf, Instrument, Order } from '@lib/domain';
|
|
3
|
+
import { d, now } from '@lib/shared';
|
|
9
4
|
import {
|
|
10
5
|
BalancePatchEvent,
|
|
11
6
|
InstrumentPatchEvent,
|
|
12
7
|
InstrumentSubscriptionEvent,
|
|
13
8
|
OrderbookPatchEvent,
|
|
14
9
|
Store
|
|
15
|
-
} from '
|
|
16
|
-
import { PaperEngine } from './paper-engine';
|
|
10
|
+
} from '@lib/store';
|
|
17
11
|
|
|
18
12
|
describe('PaperEngine', () => {
|
|
19
13
|
const instrument = new Instrument(
|
|
@@ -109,8 +103,8 @@ describe('PaperEngine', () => {
|
|
|
109
103
|
expect(store.snapshot.order.asReadonlyArray().length).toEqual(1);
|
|
110
104
|
expect(pendingOrder.state).toEqual('FILLED');
|
|
111
105
|
expect(baseBalance.free).toEqual(d(0.4));
|
|
112
|
-
expect(baseBalance.locked).toEqual(d.
|
|
113
|
-
expect(quoteBalance.free).toEqual(d(
|
|
106
|
+
expect(baseBalance.locked).toEqual(d(0.6));
|
|
107
|
+
expect(quoteBalance.free).toEqual(d(1000));
|
|
114
108
|
expect(quoteBalance.locked).toEqual(d.Zero);
|
|
115
109
|
});
|
|
116
110
|
});
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
import { tap } from 'rxjs';
|
|
2
2
|
|
|
3
|
-
import { Order, Orderbook, Trade } from '
|
|
4
|
-
import { d, decimal } from '
|
|
3
|
+
import { Order, Orderbook, Trade } from '@lib/domain';
|
|
4
|
+
import { d, decimal } from '@lib/shared';
|
|
5
5
|
import {
|
|
6
|
-
|
|
7
|
-
BalanceTransactEvent,
|
|
8
|
-
BalanceUnlockOrderEvent,
|
|
6
|
+
InstrumentNotSupportedError,
|
|
9
7
|
OrderCanceledEvent,
|
|
10
8
|
OrderCancelingEvent,
|
|
11
9
|
OrderFilledEvent,
|
|
@@ -13,18 +11,17 @@ import {
|
|
|
13
11
|
OrderPendingEvent,
|
|
14
12
|
OrderRejectedEvent,
|
|
15
13
|
Store
|
|
16
|
-
} from '
|
|
17
|
-
import { instrumentNotSupportedError } from '../../../store/error';
|
|
14
|
+
} from '@lib/store';
|
|
18
15
|
|
|
19
16
|
export class PaperEngine {
|
|
20
17
|
constructor(private readonly store: Store) {
|
|
21
18
|
store.changes$
|
|
22
19
|
.pipe(
|
|
23
20
|
tap(it => {
|
|
24
|
-
if (it
|
|
25
|
-
this.onOrderbook(it);
|
|
26
|
-
} else if (it
|
|
27
|
-
this.onTrade(it);
|
|
21
|
+
if (it.type === Orderbook.type) {
|
|
22
|
+
this.onOrderbook(it as Orderbook);
|
|
23
|
+
} else if (it.type === Trade.type) {
|
|
24
|
+
this.onTrade(it as Trade);
|
|
28
25
|
}
|
|
29
26
|
})
|
|
30
27
|
)
|
|
@@ -35,17 +32,11 @@ export class PaperEngine {
|
|
|
35
32
|
const { timestamp } = this.store.snapshot;
|
|
36
33
|
|
|
37
34
|
try {
|
|
38
|
-
this.store.dispatch(
|
|
39
|
-
new OrderNewEvent(order, timestamp),
|
|
40
|
-
new BalanceLockOrderEvent(order.id, order.instrument, timestamp)
|
|
41
|
-
);
|
|
35
|
+
this.store.dispatch(new OrderNewEvent(order, timestamp));
|
|
42
36
|
|
|
43
37
|
this.store.dispatch(new OrderPendingEvent(order.id, order.instrument, timestamp));
|
|
44
38
|
} catch (error) {
|
|
45
|
-
this.store.dispatch(
|
|
46
|
-
new BalanceUnlockOrderEvent(order.id, order.instrument, timestamp),
|
|
47
|
-
new OrderRejectedEvent(order.id, order.instrument, timestamp)
|
|
48
|
-
);
|
|
39
|
+
this.store.dispatch(new OrderRejectedEvent(order.id, order.instrument, timestamp));
|
|
49
40
|
}
|
|
50
41
|
}
|
|
51
42
|
|
|
@@ -54,10 +45,7 @@ export class PaperEngine {
|
|
|
54
45
|
|
|
55
46
|
this.store.dispatch(new OrderCancelingEvent(order.id, order.instrument, timestamp));
|
|
56
47
|
|
|
57
|
-
this.store.dispatch(
|
|
58
|
-
new BalanceUnlockOrderEvent(order.id, order.instrument, timestamp),
|
|
59
|
-
new OrderCanceledEvent(order.id, order.instrument, timestamp)
|
|
60
|
-
);
|
|
48
|
+
this.store.dispatch(new OrderCanceledEvent(order.id, order.instrument, timestamp));
|
|
61
49
|
}
|
|
62
50
|
|
|
63
51
|
private onOrderbook(orderbook: Orderbook) {
|
|
@@ -115,7 +103,7 @@ export class PaperEngine {
|
|
|
115
103
|
|
|
116
104
|
const instrument = this.store.snapshot.universe.instrument.get(order.instrument.id);
|
|
117
105
|
if (!instrument) {
|
|
118
|
-
throw
|
|
106
|
+
throw new InstrumentNotSupportedError(order.instrument);
|
|
119
107
|
}
|
|
120
108
|
|
|
121
109
|
const transacted = {
|
|
@@ -142,10 +130,7 @@ export class PaperEngine {
|
|
|
142
130
|
}
|
|
143
131
|
|
|
144
132
|
this.store.dispatch(
|
|
145
|
-
new
|
|
146
|
-
new OrderFilledEvent(order.id, order.instrument, averageExecutionRate, timestamp),
|
|
147
|
-
new BalanceTransactEvent(instrument.base, transacted.base, timestamp),
|
|
148
|
-
new BalanceTransactEvent(instrument.quote, transacted.quote, timestamp)
|
|
133
|
+
new OrderFilledEvent(order.id, order.instrument, averageExecutionRate, timestamp)
|
|
149
134
|
);
|
|
150
135
|
}
|
|
151
136
|
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export * from '
|
|
1
|
+
export * from '@lib/adapter/paper/paper-adapter';
|
|
2
|
+
export * from '@lib/adapter/paper/engine/paper-engine';
|
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import {
|
|
2
|
+
Adapter,
|
|
3
|
+
AdapterFactory,
|
|
4
|
+
FeedAsyncCallback,
|
|
5
|
+
NoPaperEngineProvidedError,
|
|
6
|
+
PaperEngine
|
|
7
|
+
} from '@lib/adapter';
|
|
8
|
+
import { assetOf, InstrumentSelector, Ohlc, Order } from '@lib/domain';
|
|
9
|
+
import { d, decimal, timestamp } from '@lib/shared';
|
|
10
|
+
import { BalanceLoadEvent, OrderNewEvent, Store } from '@lib/store';
|
|
8
11
|
|
|
9
12
|
export interface PaperOptions {
|
|
10
13
|
balance: { [key: string]: decimal };
|
|
@@ -62,27 +65,31 @@ export class PaperAdapter extends Adapter {
|
|
|
62
65
|
|
|
63
66
|
subscribed = subscribed.filter(it => it.id != asset.id);
|
|
64
67
|
|
|
65
|
-
this.store.dispatch(new
|
|
68
|
+
this.store.dispatch(new BalanceLoadEvent(asset, free, d.Zero, this.timestamp()));
|
|
66
69
|
}
|
|
67
70
|
|
|
68
71
|
for (const missingAsset of subscribed) {
|
|
69
72
|
this.store.dispatch(
|
|
70
|
-
new
|
|
73
|
+
new BalanceLoadEvent(missingAsset, d.Zero, d.Zero, this.timestamp())
|
|
71
74
|
);
|
|
72
75
|
}
|
|
73
76
|
}
|
|
74
77
|
|
|
75
78
|
async open(order: Order): Promise<void> {
|
|
76
79
|
if (!this.engine) {
|
|
77
|
-
throw
|
|
80
|
+
throw new NoPaperEngineProvidedError();
|
|
78
81
|
}
|
|
79
82
|
|
|
83
|
+
const { timestamp } = this.store.snapshot;
|
|
84
|
+
|
|
85
|
+
this.store.dispatch(new OrderNewEvent(order, timestamp));
|
|
86
|
+
|
|
80
87
|
this.engine.open(order);
|
|
81
88
|
}
|
|
82
89
|
|
|
83
90
|
async cancel(order: Order): Promise<void> {
|
|
84
91
|
if (!this.engine) {
|
|
85
|
-
throw
|
|
92
|
+
throw new NoPaperEngineProvidedError();
|
|
86
93
|
}
|
|
87
94
|
|
|
88
95
|
this.engine.cancel(order);
|
package/src/cli/build.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawn } from 'child_process';
|
|
2
2
|
|
|
3
|
-
import { buildDirectory } from '
|
|
3
|
+
import { buildDirectory } from '@lib/cli/internal/workspace';
|
|
4
4
|
|
|
5
5
|
export default async function (): Promise<number> {
|
|
6
6
|
return new Promise<number>((resolve, reject) => {
|
package/src/cli/dev.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
3
|
+
import build from '@lib/cli/build';
|
|
4
|
+
import { buildDirectory } from '@lib/cli/internal/workspace';
|
|
5
|
+
import { SessionBuilder } from '@lib/domain';
|
|
6
|
+
import { spawn } from '@lib/index';
|
|
7
|
+
import { now } from '@lib/shared';
|
|
8
8
|
|
|
9
9
|
export default async function (name: string, options: any) {
|
|
10
10
|
if (await build()) {
|
package/src/cli/index.ts
CHANGED
|
@@ -4,11 +4,11 @@ import { ChildProcess, spawn } from 'child_process';
|
|
|
4
4
|
import { program } from 'commander';
|
|
5
5
|
import watch from 'node-watch';
|
|
6
6
|
|
|
7
|
-
import build from '
|
|
8
|
-
import dev from '
|
|
9
|
-
import pull from '
|
|
10
|
-
import run from '
|
|
11
|
-
import test from '
|
|
7
|
+
import build from '@lib/cli/build';
|
|
8
|
+
import dev from '@lib/cli/dev';
|
|
9
|
+
import pull from '@lib/cli/pull';
|
|
10
|
+
import run from '@lib/cli/run';
|
|
11
|
+
import test from '@lib/cli/test';
|
|
12
12
|
|
|
13
13
|
program
|
|
14
14
|
.command('build')
|
package/src/cli/pull.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Presets, SingleBar } from 'cli-progress';
|
|
2
2
|
import { join } from 'path';
|
|
3
3
|
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import {
|
|
4
|
+
import build from '@lib/cli/build';
|
|
5
|
+
import { buildDirectory } from '@lib/cli/internal/workspace';
|
|
6
|
+
import { instrumentOf } from '@lib/domain';
|
|
7
|
+
import { SessionBuilder } from '@lib/domain';
|
|
8
|
+
import { spawn } from '@lib/index';
|
|
9
|
+
import { now } from '@lib/shared';
|
|
10
|
+
import { Feed } from '@lib/storage';
|
|
11
11
|
|
|
12
12
|
export default async function (name: string, instrument: string, options: any) {
|
|
13
13
|
if (await build()) {
|
package/src/cli/run.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
3
|
+
import build from '@lib/cli/build';
|
|
4
|
+
import { buildDirectory } from '@lib/cli/internal/workspace';
|
|
5
|
+
import { SessionBuilder } from '@lib/domain';
|
|
6
|
+
import { spawn } from '@lib/index';
|
|
7
|
+
import { now } from '@lib/shared';
|
|
8
8
|
|
|
9
9
|
export default async function (name: string, options: any) {
|
|
10
10
|
if (await build()) {
|
package/src/cli/test.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { join } from 'path';
|
|
2
2
|
|
|
3
|
-
import
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
3
|
+
import build from '@lib/cli/build';
|
|
4
|
+
import { buildDirectory } from '@lib/cli/internal/workspace';
|
|
5
|
+
import { SessionBuilder } from '@lib/domain';
|
|
6
|
+
import { spawn } from '@lib/index';
|
|
7
|
+
import { log, now } from '@lib/shared';
|
|
8
8
|
|
|
9
9
|
export default async function (name: string, options: any) {
|
|
10
10
|
if (await build()) {
|
|
@@ -18,17 +18,18 @@ export default async function (name: string, options: any) {
|
|
|
18
18
|
);
|
|
19
19
|
|
|
20
20
|
const rules = await spawn(name, builder);
|
|
21
|
+
const logger = log('backtester');
|
|
21
22
|
|
|
22
23
|
const startTime = performance.now();
|
|
23
24
|
|
|
24
25
|
const [session, backtester] = builder.backtest({
|
|
25
|
-
onBacktestStarted: () =>
|
|
26
|
+
onBacktestStarted: () => logger.info(`new session ${session.id} started`),
|
|
26
27
|
onBacktestCompleted: async () => {
|
|
27
28
|
await session.dispose();
|
|
28
29
|
|
|
29
30
|
const seconds = ((performance.now() - startTime) / 1000).toFixed(3);
|
|
30
31
|
|
|
31
|
-
|
|
32
|
+
logger.info(`session ${session.id} completed in ${seconds}s`);
|
|
32
33
|
}
|
|
33
34
|
});
|
|
34
35
|
|
package/src/domain/asset.spec.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { Asset, assetOf, AssetSelector } from '@lib/domain';
|
|
2
|
+
import { d } from '@lib/shared';
|
|
3
3
|
|
|
4
|
-
describe(
|
|
4
|
+
describe(Asset.name, () => {
|
|
5
5
|
test('should construct a new asset', () => {
|
|
6
6
|
const sut = new Asset('abc', 'xyz', 4);
|
|
7
7
|
|
|
@@ -27,7 +27,7 @@ describe('Asset', () => {
|
|
|
27
27
|
});
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
describe(
|
|
30
|
+
describe(AssetSelector.name, () => {
|
|
31
31
|
test('should construct a new asset selector from unified string', () => {
|
|
32
32
|
const sut = assetOf('xyz:abc');
|
|
33
33
|
|
package/src/domain/asset.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { InvalidArgumentsError, InvalidAssetSelectorError } from '@lib/domain';
|
|
2
|
+
import { d, decimal } from '@lib/shared';
|
|
3
3
|
|
|
4
4
|
export const AssetSelectorSeparator = ':';
|
|
5
5
|
|
|
@@ -13,11 +13,11 @@ export class AssetSelector {
|
|
|
13
13
|
|
|
14
14
|
constructor(name: string, adapterName: string) {
|
|
15
15
|
if (!name?.length) {
|
|
16
|
-
throw
|
|
16
|
+
throw new InvalidArgumentsError({ name });
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
if (!adapterName?.length) {
|
|
20
|
-
throw
|
|
20
|
+
throw new InvalidArgumentsError({ adapterName });
|
|
21
21
|
}
|
|
22
22
|
|
|
23
23
|
this.name = name.toLowerCase();
|
|
@@ -33,7 +33,7 @@ export function assetOf(selector: string): AssetSelector {
|
|
|
33
33
|
const [adapterName, name, ...rest] = selector.split(AssetSelectorSeparator);
|
|
34
34
|
|
|
35
35
|
if (!adapterName || !name || rest.length) {
|
|
36
|
-
throw
|
|
36
|
+
throw new InvalidAssetSelectorError(selector);
|
|
37
37
|
}
|
|
38
38
|
|
|
39
39
|
return new AssetSelector(name, adapterName);
|
|
@@ -50,12 +50,19 @@ export class Asset extends AssetSelector {
|
|
|
50
50
|
super(name, adapterName);
|
|
51
51
|
|
|
52
52
|
if (scale && (scale < 0 || Number.isNaN(scale))) {
|
|
53
|
-
throw
|
|
53
|
+
throw new InvalidArgumentsError({ scale });
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
this.tickSize = d(1.0).div(Math.pow(10, this.scale));
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Formats a number to string with fixed number of decimal places.
|
|
61
|
+
*/
|
|
62
|
+
fixed(number: decimal): string {
|
|
63
|
+
return number.toFixed(this.scale);
|
|
64
|
+
}
|
|
65
|
+
|
|
59
66
|
/**
|
|
60
67
|
* Rounds down a number to the asset precision.
|
|
61
68
|
*/
|
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
import { Subject } from 'rxjs';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { Balance } from './balance';
|
|
6
|
-
import { balance } from './balance-operator';
|
|
7
|
-
import { Component } from './component';
|
|
3
|
+
import { Asset, Balance, balance, Component } from '@lib/domain';
|
|
4
|
+
import { State } from '@lib/store';
|
|
8
5
|
|
|
9
|
-
describe(
|
|
6
|
+
describe(balance.name, () => {
|
|
10
7
|
const timestamp = 0;
|
|
11
8
|
const asset = new Asset('abc', 'xyz', 4);
|
|
12
9
|
const state = new State();
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { filter, map, Observable, startWith } from 'rxjs';
|
|
2
2
|
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { Balance } from './balance';
|
|
6
|
-
import { Component } from './component';
|
|
3
|
+
import { AssetSelector, Balance, Component } from '@lib/domain';
|
|
4
|
+
import { State } from '@lib/store';
|
|
7
5
|
|
|
8
6
|
export function balance(selector: AssetSelector, state: State) {
|
|
9
7
|
return (source$: Observable<Component>) =>
|
|
10
8
|
source$.pipe(
|
|
11
9
|
startWith(state.balance.get(selector.id)),
|
|
12
|
-
filter(it => it
|
|
13
|
-
map(it => it as Balance)
|
|
10
|
+
filter(it => it !== undefined && it.type === Balance.type),
|
|
11
|
+
map(it => it as Balance),
|
|
12
|
+
filter(it => it.asset.id === selector.id)
|
|
14
13
|
);
|
|
15
14
|
}
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { Balance } from './balance';
|
|
4
|
-
import { Commission } from './commission';
|
|
5
|
-
import { Instrument } from './instrument';
|
|
6
|
-
import { Position } from './position';
|
|
1
|
+
import { Asset, Balance, Commission, Instrument, Position } from '@lib/domain';
|
|
2
|
+
import { d } from '@lib/shared';
|
|
7
3
|
|
|
8
|
-
describe(
|
|
4
|
+
describe(Balance.name, () => {
|
|
9
5
|
const asset = new Asset('abc', 'xyz', 4);
|
|
10
6
|
const instrument = new Instrument(
|
|
11
7
|
0,
|
|
@@ -25,78 +21,13 @@ describe('Balance', () => {
|
|
|
25
21
|
});
|
|
26
22
|
|
|
27
23
|
test('should construct balance', () => {
|
|
28
|
-
const sut = new Balance(1, asset);
|
|
29
|
-
|
|
30
|
-
sut.set(d(100), d(50));
|
|
24
|
+
const sut = new Balance(1, asset, d(100));
|
|
31
25
|
|
|
32
26
|
expect(sut.asset.id).toEqual('xyz:abc');
|
|
33
27
|
expect(sut.free).toEqual(d(100));
|
|
34
|
-
expect(sut.locked).toEqual(d(
|
|
35
|
-
expect(sut.total).toEqual(d(150));
|
|
36
|
-
expect(Object.keys(sut.position).length).toEqual(0);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('should account positive amount', () => {
|
|
40
|
-
const sut = new Balance(0, asset);
|
|
41
|
-
|
|
42
|
-
sut.set(d(100), d.Zero);
|
|
43
|
-
sut.account(d(10));
|
|
44
|
-
|
|
45
|
-
expect(sut.free).toEqual(d(110));
|
|
46
|
-
expect(sut.locked).toEqual(d.Zero);
|
|
47
|
-
expect(sut.total).toEqual(d(110));
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('should account negative amount', () => {
|
|
51
|
-
const sut = new Balance(0, asset);
|
|
52
|
-
|
|
53
|
-
sut.set(d(100), d.Zero);
|
|
54
|
-
sut.account(d(-10));
|
|
55
|
-
|
|
56
|
-
expect(sut.free).toEqual(d(90));
|
|
57
|
-
expect(sut.locked).toEqual(d.Zero);
|
|
58
|
-
expect(sut.total).toEqual(d(90));
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
test('should throw for insufficient balance account amount', () => {
|
|
62
|
-
const sut = new Balance(0, asset);
|
|
63
|
-
|
|
64
|
-
sut.set(d(100), d.Zero);
|
|
65
|
-
const fn = () => sut.account(d(-120));
|
|
66
|
-
|
|
67
|
-
expect(fn).toThrowError();
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
test('should lock specific amount of available balance', () => {
|
|
71
|
-
const sut = new Balance(0, asset);
|
|
72
|
-
|
|
73
|
-
sut.set(d(100), d.Zero);
|
|
74
|
-
sut.lock('key', d(10));
|
|
75
|
-
|
|
76
|
-
expect(sut.free).toEqual(d(90));
|
|
77
|
-
expect(sut.locked).toEqual(d(10));
|
|
78
|
-
expect(sut.total).toEqual(d(100));
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('should throw for lock insufficient amount of available balance', () => {
|
|
82
|
-
const sut = new Balance(0, asset);
|
|
83
|
-
|
|
84
|
-
sut.set(d(30), d(50));
|
|
85
|
-
const fn = () => sut.lock('key', d(100));
|
|
86
|
-
|
|
87
|
-
expect(fn).toThrowError();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
test('should lock and unlock specific amount of available balance', () => {
|
|
91
|
-
const sut = new Balance(0, asset);
|
|
92
|
-
|
|
93
|
-
sut.set(d(100), d.Zero);
|
|
94
|
-
sut.lock('key', d(40));
|
|
95
|
-
sut.tryUnlock('key');
|
|
96
|
-
|
|
97
|
-
expect(sut.free).toEqual(d(100));
|
|
98
|
-
expect(sut.locked).toEqual(d.Zero);
|
|
28
|
+
expect(sut.locked).toEqual(d(0));
|
|
99
29
|
expect(sut.total).toEqual(d(100));
|
|
30
|
+
expect(Object.keys(sut.position).length).toEqual(0);
|
|
100
31
|
});
|
|
101
32
|
|
|
102
33
|
test('should return correct estimated unrealized pnl', () => {
|
|
@@ -104,9 +35,8 @@ describe('Balance', () => {
|
|
|
104
35
|
|
|
105
36
|
position.calculateEstimatedUnrealizedPnL(d(2576.44));
|
|
106
37
|
|
|
107
|
-
const sut = new Balance(0, asset);
|
|
38
|
+
const sut = new Balance(0, asset, d(100));
|
|
108
39
|
|
|
109
|
-
sut.set(d(100), d.Zero);
|
|
110
40
|
sut.position['1'] = position;
|
|
111
41
|
|
|
112
42
|
expect(sut.getEstimatedUnrealizedPnL()).toEqual(d(0.2652));
|