@quantform/core 0.6.5 → 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
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const adapter_1 = require("../../../adapter");
|
|
4
|
+
const domain_1 = require("../../../domain");
|
|
5
|
+
const shared_1 = require("../../../shared");
|
|
6
|
+
const store_1 = require("../../../store");
|
|
7
|
+
describe('PaperEngine', () => {
|
|
8
|
+
const instrument = new domain_1.Instrument(0, new domain_1.Asset('btc', 'binance', 8), new domain_1.Asset('usdt', 'binance', 2), 'binance:btc-usdt', domain_1.Commission.Zero);
|
|
9
|
+
const commission = (0, domain_1.commissionPercentOf)({
|
|
10
|
+
maker: (0, shared_1.d)(0.1),
|
|
11
|
+
taker: (0, shared_1.d)(0.1)
|
|
12
|
+
});
|
|
13
|
+
test('should open a new buy market order', () => {
|
|
14
|
+
var _a, _b, _c;
|
|
15
|
+
const store = new store_1.Store();
|
|
16
|
+
const engine = new adapter_1.PaperEngine(store);
|
|
17
|
+
const order = new domain_1.Order(0, '1', instrument, (0, shared_1.d)(1.0), 0);
|
|
18
|
+
store.dispatch(new store_1.InstrumentPatchEvent((0, shared_1.now)(), instrument.base, instrument.quote, commission, ''), new store_1.BalancePatchEvent(instrument.base, (0, shared_1.d)(1), shared_1.d.Zero, (0, shared_1.now)()), new store_1.BalancePatchEvent(instrument.quote, (0, shared_1.d)(1000), shared_1.d.Zero, (0, shared_1.now)()));
|
|
19
|
+
engine.open(order);
|
|
20
|
+
const pendingOrder = (_b = (_a = store.snapshot.order.get(instrument.id)) === null || _a === void 0 ? void 0 : _a.get(order.id)) !== null && _b !== void 0 ? _b : fail();
|
|
21
|
+
const balance = (_c = store.snapshot.balance.get(instrument.quote.id)) !== null && _c !== void 0 ? _c : fail();
|
|
22
|
+
expect(store.snapshot.order.asReadonlyArray().length).toEqual(1);
|
|
23
|
+
expect(pendingOrder).toEqual(order);
|
|
24
|
+
expect(balance.free).toEqual(shared_1.d.Zero);
|
|
25
|
+
expect(balance.locked).toEqual((0, shared_1.d)(1000));
|
|
26
|
+
});
|
|
27
|
+
test('should open a new sell market order', () => {
|
|
28
|
+
var _a, _b, _c;
|
|
29
|
+
const store = new store_1.Store();
|
|
30
|
+
const engine = new adapter_1.PaperEngine(store);
|
|
31
|
+
const order = new domain_1.Order(0, '1', instrument, (0, shared_1.d)(-0.6), 0);
|
|
32
|
+
store.dispatch(new store_1.InstrumentPatchEvent((0, shared_1.now)(), instrument.base, instrument.quote, commission, ''), new store_1.BalancePatchEvent(instrument.base, (0, shared_1.d)(1), shared_1.d.Zero, (0, shared_1.now)()), new store_1.BalancePatchEvent(instrument.quote, (0, shared_1.d)(1000), shared_1.d.Zero, (0, shared_1.now)()));
|
|
33
|
+
engine.open(order);
|
|
34
|
+
const pendingOrder = (_b = (_a = store.snapshot.order.get(instrument.id)) === null || _a === void 0 ? void 0 : _a.get(order.id)) !== null && _b !== void 0 ? _b : fail();
|
|
35
|
+
const balance = (_c = store.snapshot.balance.get(instrument.base.id)) !== null && _c !== void 0 ? _c : fail();
|
|
36
|
+
expect(store.snapshot.order.asReadonlyArray().length).toEqual(1);
|
|
37
|
+
expect(pendingOrder).toEqual(order);
|
|
38
|
+
expect(balance.free).toEqual((0, shared_1.d)(0.4));
|
|
39
|
+
expect(balance.locked).toEqual((0, shared_1.d)(0.6));
|
|
40
|
+
});
|
|
41
|
+
test('should open and fill a new sell limit order', () => {
|
|
42
|
+
var _a, _b, _c, _d;
|
|
43
|
+
const store = new store_1.Store();
|
|
44
|
+
const engine = new adapter_1.PaperEngine(store);
|
|
45
|
+
const order = new domain_1.Order(0, '1', instrument, (0, shared_1.d)(-0.6), 0, (0, shared_1.d)(100));
|
|
46
|
+
store.dispatch(new store_1.InstrumentPatchEvent((0, shared_1.now)(), instrument.base, instrument.quote, commission, ''), new store_1.BalancePatchEvent(instrument.base, (0, shared_1.d)(1), shared_1.d.Zero, (0, shared_1.now)()), new store_1.BalancePatchEvent(instrument.quote, (0, shared_1.d)(1000), shared_1.d.Zero, (0, shared_1.now)()), new store_1.InstrumentSubscriptionEvent((0, shared_1.now)(), instrument, true));
|
|
47
|
+
engine.open(order);
|
|
48
|
+
const pendingOrder = (_b = (_a = store.snapshot.order.get(instrument.id)) === null || _a === void 0 ? void 0 : _a.get(order.id)) !== null && _b !== void 0 ? _b : fail();
|
|
49
|
+
const baseBalance = (_c = store.snapshot.balance.get(instrument.base.id)) !== null && _c !== void 0 ? _c : fail();
|
|
50
|
+
expect(store.snapshot.order.asReadonlyArray().length).toEqual(1);
|
|
51
|
+
expect(pendingOrder.state).toEqual('PENDING');
|
|
52
|
+
expect(baseBalance.free).toEqual((0, shared_1.d)(0.4));
|
|
53
|
+
expect(baseBalance.locked).toEqual((0, shared_1.d)(0.6));
|
|
54
|
+
store.dispatch(new store_1.OrderbookPatchEvent(instrument, { rate: (0, shared_1.d)(102), quantity: (0, shared_1.d)(1), next: undefined }, { rate: (0, shared_1.d)(101), quantity: (0, shared_1.d)(1), next: undefined }, (0, shared_1.now)()));
|
|
55
|
+
const quoteBalance = (_d = store.snapshot.balance.get(instrument.quote.id)) !== null && _d !== void 0 ? _d : fail();
|
|
56
|
+
expect(store.snapshot.order.asReadonlyArray().length).toEqual(1);
|
|
57
|
+
expect(pendingOrder.state).toEqual('FILLED');
|
|
58
|
+
expect(baseBalance.free).toEqual((0, shared_1.d)(0.4));
|
|
59
|
+
expect(baseBalance.locked).toEqual((0, shared_1.d)(0.6));
|
|
60
|
+
expect(quoteBalance.free).toEqual((0, shared_1.d)(1000));
|
|
61
|
+
expect(quoteBalance.locked).toEqual(shared_1.d.Zero);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from '
|
|
2
|
-
export * from '
|
|
1
|
+
export * from '../../adapter/paper/paper-adapter';
|
|
2
|
+
export * from '../../adapter/paper/engine/paper-engine';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapter/paper/index.ts"],"names":[],"mappings":"AAAA,cAAc,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/adapter/paper/index.ts"],"names":[],"mappings":"AAAA,cAAc,kCAAkC,CAAC;AACjD,cAAc,wCAAwC,CAAC"}
|
|
@@ -14,5 +14,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("
|
|
18
|
-
__exportStar(require("
|
|
17
|
+
__exportStar(require("../../adapter/paper/paper-adapter"), exports);
|
|
18
|
+
__exportStar(require("../../adapter/paper/engine/paper-engine"), exports);
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
+
import { Adapter, AdapterFactory, FeedAsyncCallback, PaperEngine } from '../../adapter';
|
|
1
2
|
import { InstrumentSelector, Ohlc, Order } from '../../domain';
|
|
2
3
|
import { decimal, timestamp } from '../../shared';
|
|
3
4
|
import { Store } from '../../store';
|
|
4
|
-
import { Adapter } from '..';
|
|
5
|
-
import { AdapterFactory, FeedAsyncCallback } from '../adapter';
|
|
6
|
-
import { PaperEngine } from './engine/paper-engine';
|
|
7
5
|
export interface PaperOptions {
|
|
8
6
|
balance: {
|
|
9
7
|
[key: string]: decimal;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"paper-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapter/paper/paper-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"paper-adapter.d.ts","sourceRoot":"","sources":["../../../src/adapter/paper/paper-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,cAAc,EACd,iBAAiB,EAEjB,WAAW,EACZ,MAAM,cAAc,CAAC;AACtB,OAAO,EAAW,kBAAkB,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAK,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACpD,OAAO,EAAmC,KAAK,EAAE,MAAM,YAAY,CAAC;AAEpE,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;KAAE,CAAC;CACrC;AAED,wBAAgB,yBAAyB,CACvC,uBAAuB,EAAE,cAAc,EACvC,OAAO,EAAE,YAAY,GACpB,cAAc,CAGhB;AAED,qBAAa,YAAa,SAAQ,OAAO;IAKrC,QAAQ,CAAC,gBAAgB,EAAE,OAAO;IAClC,QAAQ,CAAC,KAAK,EAAE,KAAK;IACrB,QAAQ,CAAC,OAAO,EAAE,YAAY;IANhC,QAAQ,CAAC,IAAI,SAA8B;IAC3C,OAAO,CAAC,MAAM,CAAC,CAAc;gBAGlB,gBAAgB,EAAE,OAAO,EACzB,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,YAAY;IAO1B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAM5B,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAIxB,SAAS,CAAC,WAAW,EAAE,kBAAkB,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAIrD,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IA0BxB,IAAI,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjC,MAAM,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC;IAQzC,OAAO,CACL,UAAU,EAAE,kBAAkB,EAC9B,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,IAAI,EAAE,CAAC;IAIlB,IAAI,CACF,UAAU,EAAE,kBAAkB,EAC9B,IAAI,EAAE,SAAS,EACf,EAAE,EAAE,SAAS,EACb,QAAQ,EAAE,iBAAiB,GAC1B,OAAO,CAAC,IAAI,CAAC;IAIhB,iBAAiB,CAAC,OAAO,EAAE,YAAY,GAAG,WAAW;CAGtD"}
|
|
@@ -10,16 +10,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.PaperAdapter = exports.createPaperAdapterFactory = void 0;
|
|
13
|
+
const adapter_1 = require("../../adapter");
|
|
13
14
|
const domain_1 = require("../../domain");
|
|
14
15
|
const shared_1 = require("../../shared");
|
|
15
16
|
const store_1 = require("../../store");
|
|
16
|
-
const __1 = require("..");
|
|
17
|
-
const error_1 = require("../error");
|
|
18
17
|
function createPaperAdapterFactory(decoratedAdapterFactory, options) {
|
|
19
18
|
return (timeProvider, store, cache) => new PaperAdapter(decoratedAdapterFactory(timeProvider, store, cache), store, options);
|
|
20
19
|
}
|
|
21
20
|
exports.createPaperAdapterFactory = createPaperAdapterFactory;
|
|
22
|
-
class PaperAdapter extends
|
|
21
|
+
class PaperAdapter extends adapter_1.Adapter {
|
|
23
22
|
constructor(decoratedAdapter, store, options) {
|
|
24
23
|
super({
|
|
25
24
|
timestamp: () => this.decoratedAdapter.timestamp()
|
|
@@ -51,25 +50,27 @@ class PaperAdapter extends __1.Adapter {
|
|
|
51
50
|
}
|
|
52
51
|
const free = this.options.balance[balance];
|
|
53
52
|
subscribed = subscribed.filter(it => it.id != asset.id);
|
|
54
|
-
this.store.dispatch(new store_1.
|
|
53
|
+
this.store.dispatch(new store_1.BalanceLoadEvent(asset, free, shared_1.d.Zero, this.timestamp()));
|
|
55
54
|
}
|
|
56
55
|
for (const missingAsset of subscribed) {
|
|
57
|
-
this.store.dispatch(new store_1.
|
|
56
|
+
this.store.dispatch(new store_1.BalanceLoadEvent(missingAsset, shared_1.d.Zero, shared_1.d.Zero, this.timestamp()));
|
|
58
57
|
}
|
|
59
58
|
});
|
|
60
59
|
}
|
|
61
60
|
open(order) {
|
|
62
61
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
62
|
if (!this.engine) {
|
|
64
|
-
throw
|
|
63
|
+
throw new adapter_1.NoPaperEngineProvidedError();
|
|
65
64
|
}
|
|
65
|
+
const { timestamp } = this.store.snapshot;
|
|
66
|
+
this.store.dispatch(new store_1.OrderNewEvent(order, timestamp));
|
|
66
67
|
this.engine.open(order);
|
|
67
68
|
});
|
|
68
69
|
}
|
|
69
70
|
cancel(order) {
|
|
70
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
72
|
if (!this.engine) {
|
|
72
|
-
throw
|
|
73
|
+
throw new adapter_1.NoPaperEngineProvidedError();
|
|
73
74
|
}
|
|
74
75
|
this.engine.cancel(order);
|
|
75
76
|
});
|
package/dist/cli/build.js
CHANGED
|
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
const child_process_1 = require("child_process");
|
|
13
|
-
const workspace_1 = require("
|
|
13
|
+
const workspace_1 = require("../cli/internal/workspace");
|
|
14
14
|
function default_1() {
|
|
15
15
|
return __awaiter(this, void 0, void 0, function* () {
|
|
16
16
|
return new Promise((resolve, reject) => {
|
package/dist/cli/dev.js
CHANGED
|
@@ -36,19 +36,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
const path_1 = require("path");
|
|
39
|
-
const
|
|
40
|
-
const
|
|
39
|
+
const build_1 = __importDefault(require("../cli/build"));
|
|
40
|
+
const workspace_1 = require("../cli/internal/workspace");
|
|
41
|
+
const domain_1 = require("../domain");
|
|
42
|
+
const index_1 = require("../index");
|
|
41
43
|
const shared_1 = require("../shared");
|
|
42
|
-
const build_1 = __importDefault(require("./build"));
|
|
43
|
-
const workspace_1 = require("./internal/workspace");
|
|
44
44
|
function default_1(name, options) {
|
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
46
|
if (yield (0, build_1.default)()) {
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
yield Promise.resolve().then(() => __importStar(require((0, path_1.join)((0, workspace_1.buildDirectory)(), 'index'))));
|
|
50
|
-
const builder = new
|
|
51
|
-
const rules = yield (0,
|
|
50
|
+
const builder = new domain_1.SessionBuilder().useSessionId(options.id ? Number(options.id) : (0, shared_1.now)());
|
|
51
|
+
const rules = yield (0, index_1.spawn)(name, builder);
|
|
52
52
|
const session = builder.paper();
|
|
53
53
|
yield session.awake();
|
|
54
54
|
rules(session).subscribe();
|
package/dist/cli/index.js
CHANGED
|
@@ -16,11 +16,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
16
16
|
const child_process_1 = require("child_process");
|
|
17
17
|
const commander_1 = require("commander");
|
|
18
18
|
const node_watch_1 = __importDefault(require("node-watch"));
|
|
19
|
-
const build_1 = __importDefault(require("
|
|
20
|
-
const dev_1 = __importDefault(require("
|
|
21
|
-
const pull_1 = __importDefault(require("
|
|
22
|
-
const run_1 = __importDefault(require("
|
|
23
|
-
const test_1 = __importDefault(require("
|
|
19
|
+
const build_1 = __importDefault(require("../cli/build"));
|
|
20
|
+
const dev_1 = __importDefault(require("../cli/dev"));
|
|
21
|
+
const pull_1 = __importDefault(require("../cli/pull"));
|
|
22
|
+
const run_1 = __importDefault(require("../cli/run"));
|
|
23
|
+
const test_1 = __importDefault(require("../cli/test"));
|
|
24
24
|
commander_1.program
|
|
25
25
|
.command('build')
|
|
26
26
|
.description('builds a production version of the app')
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.buildDirectory = void 0;
|
|
4
4
|
const path_1 = require("path");
|
|
5
|
-
const shared_1 = require("
|
|
5
|
+
const shared_1 = require("../../shared");
|
|
6
6
|
function buildDirectory() {
|
|
7
7
|
return (0, path_1.join)(process.cwd(), (0, shared_1.workingDirectory)(), 'build');
|
|
8
8
|
}
|
package/dist/cli/pull.js
CHANGED
|
@@ -37,21 +37,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
const cli_progress_1 = require("cli-progress");
|
|
39
39
|
const path_1 = require("path");
|
|
40
|
-
const
|
|
40
|
+
const build_1 = __importDefault(require("../cli/build"));
|
|
41
|
+
const workspace_1 = require("../cli/internal/workspace");
|
|
41
42
|
const domain_1 = require("../domain");
|
|
42
|
-
const
|
|
43
|
+
const domain_2 = require("../domain");
|
|
44
|
+
const index_1 = require("../index");
|
|
43
45
|
const shared_1 = require("../shared");
|
|
44
46
|
const storage_1 = require("../storage");
|
|
45
|
-
const build_1 = __importDefault(require("./build"));
|
|
46
|
-
const workspace_1 = require("./internal/workspace");
|
|
47
47
|
function default_1(name, instrument, options) {
|
|
48
48
|
return __awaiter(this, void 0, void 0, function* () {
|
|
49
49
|
if (yield (0, build_1.default)()) {
|
|
50
50
|
return;
|
|
51
51
|
}
|
|
52
52
|
yield Promise.resolve().then(() => __importStar(require((0, path_1.join)((0, workspace_1.buildDirectory)(), 'index'))));
|
|
53
|
-
const builder = new
|
|
54
|
-
yield (0,
|
|
53
|
+
const builder = new domain_2.SessionBuilder().useSessionId(options.id ? Number(options.id) : (0, shared_1.now)());
|
|
54
|
+
yield (0, index_1.spawn)(name, builder);
|
|
55
55
|
const session = builder.paper();
|
|
56
56
|
console.time('Pulling completed in');
|
|
57
57
|
yield session.awake();
|
package/dist/cli/run.js
CHANGED
|
@@ -36,19 +36,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
const path_1 = require("path");
|
|
39
|
-
const
|
|
40
|
-
const
|
|
39
|
+
const build_1 = __importDefault(require("../cli/build"));
|
|
40
|
+
const workspace_1 = require("../cli/internal/workspace");
|
|
41
|
+
const domain_1 = require("../domain");
|
|
42
|
+
const index_1 = require("../index");
|
|
41
43
|
const shared_1 = require("../shared");
|
|
42
|
-
const build_1 = __importDefault(require("./build"));
|
|
43
|
-
const workspace_1 = require("./internal/workspace");
|
|
44
44
|
function default_1(name, options) {
|
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
46
|
if (yield (0, build_1.default)()) {
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
yield Promise.resolve().then(() => __importStar(require((0, path_1.join)((0, workspace_1.buildDirectory)(), 'index'))));
|
|
50
|
-
const builder = new
|
|
51
|
-
const rules = yield (0,
|
|
50
|
+
const builder = new domain_1.SessionBuilder().useSessionId(options.id ? Number(options.id) : (0, shared_1.now)());
|
|
51
|
+
const rules = yield (0, index_1.spawn)(name, builder);
|
|
52
52
|
const session = builder.live();
|
|
53
53
|
yield session.awake();
|
|
54
54
|
rules(session).subscribe();
|
package/dist/cli/test.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/cli/test.ts"],"names":[],"mappings":"AAQA,yBAA+B,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,
|
|
1
|
+
{"version":3,"file":"test.d.ts","sourceRoot":"","sources":["../../src/cli/test.ts"],"names":[],"mappings":"AAQA,yBAA+B,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,GAAG,iBAgCxD"}
|
package/dist/cli/test.js
CHANGED
|
@@ -36,26 +36,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
36
36
|
};
|
|
37
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
38
|
const path_1 = require("path");
|
|
39
|
-
const
|
|
39
|
+
const build_1 = __importDefault(require("../cli/build"));
|
|
40
|
+
const workspace_1 = require("../cli/internal/workspace");
|
|
41
|
+
const domain_1 = require("../domain");
|
|
42
|
+
const index_1 = require("../index");
|
|
40
43
|
const shared_1 = require("../shared");
|
|
41
|
-
const __1 = require("..");
|
|
42
|
-
const build_1 = __importDefault(require("./build"));
|
|
43
|
-
const workspace_1 = require("./internal/workspace");
|
|
44
44
|
function default_1(name, options) {
|
|
45
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
46
|
if (yield (0, build_1.default)()) {
|
|
47
47
|
return;
|
|
48
48
|
}
|
|
49
49
|
yield Promise.resolve().then(() => __importStar(require((0, path_1.join)((0, workspace_1.buildDirectory)(), 'index'))));
|
|
50
|
-
const builder = new
|
|
51
|
-
const rules = yield (0,
|
|
50
|
+
const builder = new domain_1.SessionBuilder().useSessionId(options.id ? Number(options.id) : (0, shared_1.now)());
|
|
51
|
+
const rules = yield (0, index_1.spawn)(name, builder);
|
|
52
|
+
const logger = (0, shared_1.log)('backtester');
|
|
52
53
|
const startTime = performance.now();
|
|
53
54
|
const [session, backtester] = builder.backtest({
|
|
54
|
-
onBacktestStarted: () =>
|
|
55
|
+
onBacktestStarted: () => logger.info(`new session ${session.id} started`),
|
|
55
56
|
onBacktestCompleted: () => __awaiter(this, void 0, void 0, function* () {
|
|
56
57
|
yield session.dispose();
|
|
57
58
|
const seconds = ((performance.now() - startTime) / 1000).toFixed(3);
|
|
58
|
-
|
|
59
|
+
logger.info(`session ${session.id} completed in ${seconds}s`);
|
|
59
60
|
})
|
|
60
61
|
});
|
|
61
62
|
yield session.awake();
|
package/dist/domain/asset.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { decimal } from '
|
|
1
|
+
import { decimal } from '../shared';
|
|
2
2
|
export declare const AssetSelectorSeparator = ":";
|
|
3
3
|
/**
|
|
4
4
|
* Supposed to query specific @see Asset based on string notation.
|
|
@@ -21,6 +21,10 @@ export declare class Asset extends AssetSelector {
|
|
|
21
21
|
readonly scale: number;
|
|
22
22
|
readonly tickSize: decimal;
|
|
23
23
|
constructor(name: string, adapterName: string, scale: number);
|
|
24
|
+
/**
|
|
25
|
+
* Formats a number to string with fixed number of decimal places.
|
|
26
|
+
*/
|
|
27
|
+
fixed(number: decimal): string;
|
|
24
28
|
/**
|
|
25
29
|
* Rounds down a number to the asset precision.
|
|
26
30
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asset.d.ts","sourceRoot":"","sources":["../../src/domain/asset.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"asset.d.ts","sourceRoot":"","sources":["../../src/domain/asset.ts"],"names":[],"mappings":"AACA,OAAO,EAAK,OAAO,EAAE,MAAM,aAAa,CAAC;AAEzC,eAAO,MAAM,sBAAsB,MAAM,CAAC;AAE1C;;GAEG;AACH,qBAAa,aAAa;IACxB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;gBAEjB,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM;CAa9C;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,QAAQ,EAAE,MAAM,GAAG,aAAa,CAQvD;AAED;;;GAGG;AACH,qBAAa,KAAM,SAAQ,aAAa;aAGyB,KAAK,EAAE,MAAM;IAF5E,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;gBAEf,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,EAAkB,KAAK,EAAE,MAAM;IAU5E;;OAEG;IACH,KAAK,CAAC,MAAM,EAAE,OAAO,GAAG,MAAM;IAI9B;;OAEG;IACH,KAAK,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO;IAI/B;;OAEG;IACH,IAAI,CAAC,MAAM,EAAE,OAAO,GAAG,OAAO;CAG/B"}
|
package/dist/domain/asset.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Asset = exports.assetOf = exports.AssetSelector = exports.AssetSelectorSeparator = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const domain_1 = require("../domain");
|
|
5
|
+
const shared_1 = require("../shared");
|
|
6
6
|
exports.AssetSelectorSeparator = ':';
|
|
7
7
|
/**
|
|
8
8
|
* Supposed to query specific @see Asset based on string notation.
|
|
@@ -10,10 +10,10 @@ exports.AssetSelectorSeparator = ':';
|
|
|
10
10
|
class AssetSelector {
|
|
11
11
|
constructor(name, adapterName) {
|
|
12
12
|
if (!(name === null || name === void 0 ? void 0 : name.length)) {
|
|
13
|
-
throw
|
|
13
|
+
throw new domain_1.InvalidArgumentsError({ name });
|
|
14
14
|
}
|
|
15
15
|
if (!(adapterName === null || adapterName === void 0 ? void 0 : adapterName.length)) {
|
|
16
|
-
throw
|
|
16
|
+
throw new domain_1.InvalidArgumentsError({ adapterName });
|
|
17
17
|
}
|
|
18
18
|
this.name = name.toLowerCase();
|
|
19
19
|
this.adapterName = adapterName.toLowerCase();
|
|
@@ -27,7 +27,7 @@ exports.AssetSelector = AssetSelector;
|
|
|
27
27
|
function assetOf(selector) {
|
|
28
28
|
const [adapterName, name, ...rest] = selector.split(exports.AssetSelectorSeparator);
|
|
29
29
|
if (!adapterName || !name || rest.length) {
|
|
30
|
-
throw
|
|
30
|
+
throw new domain_1.InvalidAssetSelectorError(selector);
|
|
31
31
|
}
|
|
32
32
|
return new AssetSelector(name, adapterName);
|
|
33
33
|
}
|
|
@@ -41,9 +41,15 @@ class Asset extends AssetSelector {
|
|
|
41
41
|
super(name, adapterName);
|
|
42
42
|
this.scale = scale;
|
|
43
43
|
if (scale && (scale < 0 || Number.isNaN(scale))) {
|
|
44
|
-
throw
|
|
44
|
+
throw new domain_1.InvalidArgumentsError({ scale });
|
|
45
45
|
}
|
|
46
|
-
this.tickSize = (0,
|
|
46
|
+
this.tickSize = (0, shared_1.d)(1.0).div(Math.pow(10, this.scale));
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Formats a number to string with fixed number of decimal places.
|
|
50
|
+
*/
|
|
51
|
+
fixed(number) {
|
|
52
|
+
return number.toFixed(this.scale);
|
|
47
53
|
}
|
|
48
54
|
/**
|
|
49
55
|
* Rounds down a number to the asset precision.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"asset.spec.d.ts","sourceRoot":"","sources":["../../src/domain/asset.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const domain_1 = require("../domain");
|
|
4
|
+
const shared_1 = require("../shared");
|
|
5
|
+
describe(domain_1.Asset.name, () => {
|
|
6
|
+
test('should construct a new asset', () => {
|
|
7
|
+
const sut = new domain_1.Asset('abc', 'xyz', 4);
|
|
8
|
+
expect(sut.name).toEqual('abc');
|
|
9
|
+
expect(sut.adapterName).toEqual('xyz');
|
|
10
|
+
expect(sut.scale).toEqual(4);
|
|
11
|
+
expect(sut.tickSize).toEqual((0, shared_1.d)(0.0001));
|
|
12
|
+
expect(sut.floor((0, shared_1.d)(1.1234567))).toEqual((0, shared_1.d)(1.1234));
|
|
13
|
+
expect(sut.ceil((0, shared_1.d)(1.1234567))).toEqual((0, shared_1.d)(1.1235));
|
|
14
|
+
expect(sut.id).toEqual('xyz:abc');
|
|
15
|
+
});
|
|
16
|
+
test('should throw for missing asset name', () => {
|
|
17
|
+
const fn = () => new domain_1.Asset('xyz', '', 5);
|
|
18
|
+
expect(fn).toThrowError();
|
|
19
|
+
});
|
|
20
|
+
test('should throw for missing adapter name', () => {
|
|
21
|
+
const fn = () => new domain_1.Asset('', 'xyz', 5);
|
|
22
|
+
expect(fn).toThrowError();
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
describe(domain_1.AssetSelector.name, () => {
|
|
26
|
+
test('should construct a new asset selector from unified string', () => {
|
|
27
|
+
const sut = (0, domain_1.assetOf)('xyz:abc');
|
|
28
|
+
expect(sut.name).toEqual('abc');
|
|
29
|
+
expect(sut.adapterName).toEqual('xyz');
|
|
30
|
+
expect(sut.id).toEqual('xyz:abc');
|
|
31
|
+
});
|
|
32
|
+
test('should instantiate proper asset selector capital case', () => {
|
|
33
|
+
const sut = (0, domain_1.assetOf)('XYZ:ABC');
|
|
34
|
+
expect(sut.name).toEqual('abc');
|
|
35
|
+
expect(sut.adapterName).toEqual('xyz');
|
|
36
|
+
expect(sut.id).toEqual('xyz:abc');
|
|
37
|
+
});
|
|
38
|
+
test('should throw invalid format message for missing separator', () => {
|
|
39
|
+
const fn = () => (0, domain_1.assetOf)('xyzabc');
|
|
40
|
+
expect(fn).toThrowError();
|
|
41
|
+
});
|
|
42
|
+
test('should throw for multiple separators', () => {
|
|
43
|
+
const fn = () => (0, domain_1.assetOf)('xyz:abc:');
|
|
44
|
+
expect(fn).toThrowError();
|
|
45
|
+
});
|
|
46
|
+
test('should throw for missing asset name', () => {
|
|
47
|
+
const fn = () => (0, domain_1.assetOf)('xyz:');
|
|
48
|
+
expect(fn).toThrowError();
|
|
49
|
+
});
|
|
50
|
+
test('should throw for missing adapter name', () => {
|
|
51
|
+
const fn = () => (0, domain_1.assetOf)(':abc');
|
|
52
|
+
expect(fn).toThrowError();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import { Observable } from 'rxjs';
|
|
2
|
+
import { AssetSelector, Balance, Component } from '../domain';
|
|
2
3
|
import { State } from '../store';
|
|
3
|
-
import { AssetSelector } from './asset';
|
|
4
|
-
import { Balance } from './balance';
|
|
5
|
-
import { Component } from './component';
|
|
6
4
|
export declare function balance(selector: AssetSelector, state: State): (source$: Observable<Component>) => Observable<Balance>;
|
|
7
5
|
//# sourceMappingURL=balance-operator.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"balance-operator.d.ts","sourceRoot":"","sources":["../../src/domain/balance-operator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,UAAU,EAAa,MAAM,MAAM,CAAC;AAE1D,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"balance-operator.d.ts","sourceRoot":"","sources":["../../src/domain/balance-operator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,UAAU,EAAa,MAAM,MAAM,CAAC;AAE1D,OAAO,EAAE,aAAa,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAChE,OAAO,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAEnC,wBAAgB,OAAO,CAAC,QAAQ,EAAE,aAAa,EAAE,KAAK,EAAE,KAAK,aAC1C,WAAW,SAAS,CAAC,yBAOvC"}
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.balance = void 0;
|
|
4
4
|
const rxjs_1 = require("rxjs");
|
|
5
|
-
const
|
|
5
|
+
const domain_1 = require("../domain");
|
|
6
6
|
function balance(selector, state) {
|
|
7
|
-
return (source$) => source$.pipe((0, rxjs_1.startWith)(state.balance.get(selector.id)), (0, rxjs_1.filter)(it => it
|
|
7
|
+
return (source$) => source$.pipe((0, rxjs_1.startWith)(state.balance.get(selector.id)), (0, rxjs_1.filter)(it => it !== undefined && it.type === domain_1.Balance.type), (0, rxjs_1.map)(it => it), (0, rxjs_1.filter)(it => it.asset.id === selector.id));
|
|
8
8
|
}
|
|
9
9
|
exports.balance = balance;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance-operator.spec.d.ts","sourceRoot":"","sources":["../../src/domain/balance-operator.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const rxjs_1 = require("rxjs");
|
|
4
|
+
const domain_1 = require("../domain");
|
|
5
|
+
const store_1 = require("../store");
|
|
6
|
+
describe(domain_1.balance.name, () => {
|
|
7
|
+
const timestamp = 0;
|
|
8
|
+
const asset = new domain_1.Asset('abc', 'xyz', 4);
|
|
9
|
+
const state = new store_1.State();
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
state.balance.upsert(new domain_1.Balance(timestamp, asset));
|
|
12
|
+
});
|
|
13
|
+
test('should pipe a balance snapshot when subscribed', done => {
|
|
14
|
+
new rxjs_1.Subject().pipe((0, domain_1.balance)(asset, state)).subscribe({
|
|
15
|
+
next: it => {
|
|
16
|
+
expect(it.asset).toEqual(asset);
|
|
17
|
+
done();
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
});
|
package/dist/domain/balance.d.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
+
import { Asset, Component, Position, PositionMode } from '../domain';
|
|
1
2
|
import { decimal } from '../shared';
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
export interface Fundable {
|
|
4
|
+
id: string;
|
|
5
|
+
getFundingAmount(balance: Balance): decimal;
|
|
6
|
+
}
|
|
5
7
|
/**
|
|
6
8
|
* Represents single asset balance in your wallet.
|
|
7
9
|
*/
|
|
8
10
|
export declare class Balance implements Component {
|
|
9
11
|
timestamp: number;
|
|
10
12
|
readonly asset: Asset;
|
|
13
|
+
available: decimal;
|
|
14
|
+
unavailable: decimal;
|
|
15
|
+
static type: number;
|
|
16
|
+
readonly type: number;
|
|
11
17
|
id: string;
|
|
12
|
-
private
|
|
13
|
-
private available;
|
|
14
|
-
private unavailable;
|
|
18
|
+
private transientFunding;
|
|
15
19
|
/**
|
|
16
20
|
* Returns available amount to trade.
|
|
17
21
|
*/
|
|
@@ -29,19 +33,14 @@ export declare class Balance implements Component {
|
|
|
29
33
|
* Collection of opened positions backed by this balance.
|
|
30
34
|
*/
|
|
31
35
|
readonly position: Record<string, Position>;
|
|
32
|
-
constructor(timestamp: number, asset: Asset);
|
|
33
|
-
account(amount: decimal): void;
|
|
34
|
-
set(free: decimal, locked: decimal): void;
|
|
35
|
-
/**
|
|
36
|
-
* Lock specific amount of asset.
|
|
37
|
-
* If you place new pending order, you will lock your balance to fund order.
|
|
38
|
-
*/
|
|
39
|
-
lock(id: string, amount: decimal): void;
|
|
40
|
-
tryUnlock(id: string): boolean;
|
|
36
|
+
constructor(timestamp: number, asset: Asset, available?: decimal, unavailable?: decimal);
|
|
41
37
|
/**
|
|
42
38
|
* Returns unrealized profit and loss for all positions backed by this balance.
|
|
43
39
|
*/
|
|
44
40
|
getEstimatedUnrealizedPnL(mode?: PositionMode): decimal;
|
|
45
41
|
getEstimatedMargin(mode?: PositionMode): decimal;
|
|
42
|
+
tryAddTransientFunding(fundable: Fundable): boolean;
|
|
43
|
+
tryRemoveTransientFunding(fundable: Fundable): boolean;
|
|
44
|
+
clearTransientFunding(): void;
|
|
46
45
|
}
|
|
47
46
|
//# sourceMappingURL=balance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"balance.d.ts","sourceRoot":"","sources":["../../src/domain/balance.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"balance.d.ts","sourceRoot":"","sources":["../../src/domain/balance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACvE,OAAO,EAAK,OAAO,EAAQ,MAAM,aAAa,CAAC;AAE/C,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,MAAM,CAAC;IACX,gBAAgB,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAAC;CAC7C;AAED;;GAEG;AACH,qBAAa,OAAQ,YAAW,SAAS;IAoC9B,SAAS,EAAE,MAAM;aACR,KAAK,EAAE,KAAK;IACrB,SAAS,EAAE,OAAO;IAClB,WAAW,EAAE,OAAO;IAtC7B,MAAM,CAAC,IAAI,SAAsB;IACjC,QAAQ,CAAC,IAAI,SAAgB;IAE7B,EAAE,EAAE,MAAM,CAAC;IAEX,OAAO,CAAC,gBAAgB,CAAgC;IAExD;;OAEG;IACH,IAAI,IAAI,IAAI,OAAO,CAElB;IAED;;OAEG;IACH,IAAI,MAAM,IAAI,OAAO,CAEpB;IAED;;;OAGG;IACH,IAAI,KAAK,IAAI,OAAO,CAEnB;IAED;;OAEG;IACH,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAM;gBAGxC,SAAS,EAAE,MAAM,EACR,KAAK,EAAE,KAAK,EACrB,SAAS,GAAE,OAAgB,EAC3B,WAAW,GAAE,OAAgB;IAKtC;;OAEG;IACH,yBAAyB,CAAC,IAAI,CAAC,EAAE,YAAY,GAAG,OAAO;IAYvD,kBAAkB,CAAC,IAAI,CAAC,EAAE,YAAY,GAAG,OAAO;IAQhD,sBAAsB,CAAC,QAAQ,EAAE,QAAQ;IAezC,yBAAyB,CAAC,QAAQ,EAAE,QAAQ;IAa5C,qBAAqB;CAGtB"}
|