@yuants/app-virtual-exchange 0.7.1 → 0.7.2
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/position.js +16 -2
- package/dist/position.js.map +1 -1
- package/lib/position.d.ts.map +1 -1
- package/lib/position.js +16 -2
- package/lib/position.js.map +1 -1
- package/package.json +1 -1
- package/temp/package-deps.json +4 -4
package/dist/position.js
CHANGED
|
@@ -14,7 +14,16 @@ const interestRateIntervalCache = createCache(async (product_id) => {
|
|
|
14
14
|
const rates = await requestSQL(terminal, sql);
|
|
15
15
|
if (rates.length < 2)
|
|
16
16
|
return undefined;
|
|
17
|
-
|
|
17
|
+
const prev = new Date(rates[0].created_at).getTime();
|
|
18
|
+
const prevOfPrev = new Date(rates[1].created_at).getTime();
|
|
19
|
+
const interval = prev - prevOfPrev;
|
|
20
|
+
const next = prev + interval;
|
|
21
|
+
return {
|
|
22
|
+
prev,
|
|
23
|
+
prevOfPrev,
|
|
24
|
+
interval,
|
|
25
|
+
next,
|
|
26
|
+
};
|
|
18
27
|
});
|
|
19
28
|
export const polyfillPosition = async (positions) => {
|
|
20
29
|
// TODO: 使用 batch query SQL 优化 product / quote 查询性能
|
|
@@ -44,8 +53,13 @@ export const polyfillPosition = async (positions) => {
|
|
|
44
53
|
// 利率相关信息的追加
|
|
45
54
|
if (quote) {
|
|
46
55
|
if (quote.interest_rate_next_settled_at !== null) {
|
|
56
|
+
// 优先使用行情数据中的下一个结算时间
|
|
47
57
|
pos.settlement_scheduled_at = new Date(quote.interest_rate_next_settled_at).getTime();
|
|
48
58
|
}
|
|
59
|
+
else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {
|
|
60
|
+
// 估算下一个结算时间
|
|
61
|
+
pos.settlement_scheduled_at = interestRateInterval.next;
|
|
62
|
+
}
|
|
49
63
|
if (pos.direction === 'LONG') {
|
|
50
64
|
if (quote.interest_rate_long !== null) {
|
|
51
65
|
pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;
|
|
@@ -58,7 +72,7 @@ export const polyfillPosition = async (positions) => {
|
|
|
58
72
|
}
|
|
59
73
|
}
|
|
60
74
|
if (interestRateInterval) {
|
|
61
|
-
pos.settlement_interval = interestRateInterval;
|
|
75
|
+
pos.settlement_interval = interestRateInterval.interval;
|
|
62
76
|
}
|
|
63
77
|
}
|
|
64
78
|
return positions;
|
package/dist/position.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"position.js","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,OAAO,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAEhE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEpD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AACxC,MAAM,YAAY,GAAG,wBAAwB,CAAC,QAAQ,CAAC,CAAC;AACxD,MAAM,UAAU,GAAG,WAAW,CAC5B,KAAK,EAAE,UAAU,EAAE,EAAE;IACnB,MAAM,GAAG,GAAG,0CAA0C,SAAS,CAAC,UAAU,CAAC,EAAE,CAAC;IAC9E,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,UAAU,CAAW,QAAQ,EAAE,GAAG,CAAC,CAAC;IAC1D,OAAO,KAAK,CAAC;AACf,CAAC,EACD,EAAE,MAAM,EAAE,KAAM,EAAE,CACnB,CAAC;AAEF,MAAM,yBAAyB,GAAG,WAAW,CAAC,KAAK,EAAE,UAAkB,EAAE,EAAE;IACzE,MAAM,GAAG,GAAG,0DAA0D,SAAS,CAC7E,UAAU,CACX,mCAAmC,CAAC;IACrC,MAAM,KAAK,GAAG,MAAM,UAAU,CAA2B,QAAQ,EAAE,GAAG,CAAC,CAAC;IACxE,IAAI,KAAK,CAAC,M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iBACrE;aACF;SACF;QAED,IAAI,oBAAoB,EAAE;YACxB,GAAG,CAAC,mBAAmB,GAAG,oBAAoB,CAAC,QAAQ,CAAC;SACzD;KACF;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { createClientProductCache } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\n\nconst terminal = Terminal.fromNodeEnv();\nconst productCache = createClientProductCache(terminal);\nconst quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n);\n\nconst interestRateIntervalCache = createCache(async (product_id: string) => {\n const sql = `select created_at from interest_rate where series_id = ${escapeSQL(\n product_id,\n )} order by created_at desc limit 2`;\n const rates = await requestSQL<{ created_at: string }[]>(terminal, sql);\n if (rates.length < 2) return undefined;\n const prev = new Date(rates[0].created_at).getTime();\n const prevOfPrev = new Date(rates[1].created_at).getTime();\n const interval = prev - prevOfPrev;\n const next = prev + interval;\n return {\n prev,\n prevOfPrev,\n interval,\n next,\n };\n});\n\nexport const polyfillPosition = async (positions: IPosition[]): Promise<IPosition[]> => {\n // TODO: 使用 batch query SQL 优化 product / quote 查询性能\n for (const pos of positions) {\n const [theProduct, quote, interestRateInterval] = await Promise.all([\n productCache.query(pos.product_id),\n quoteCache.query(pos.product_id),\n interestRateIntervalCache.query(pos.product_id),\n ]);\n\n // 估值 = value_scale * volume * closable_price\n if (theProduct) {\n if (theProduct.base_currency) {\n pos.base_currency = theProduct.base_currency;\n }\n if (theProduct.quote_currency) {\n pos.quote_currency = theProduct.quote_currency;\n }\n if (pos.size === undefined && pos.volume !== undefined && pos.direction !== undefined) {\n pos.size = (pos.direction === 'LONG' ? 1 : -1) * pos.volume * (theProduct.value_scale || 1) + '';\n }\n if (pos.free_size === undefined && pos.free_volume !== undefined && pos.direction !== undefined) {\n pos.free_size =\n (pos.direction === 'LONG' ? 1 : -1) * pos.free_volume * (theProduct.value_scale || 1) + '';\n }\n pos.valuation = Math.abs((theProduct.value_scale || 1) * pos.volume * pos.closable_price);\n }\n\n // 利率相关信息的追加\n if (quote) {\n if (quote.interest_rate_next_settled_at !== null) {\n // 优先使用行情数据中的下一个结算时间\n pos.settlement_scheduled_at = new Date(quote.interest_rate_next_settled_at).getTime();\n } else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {\n // 估算下一个结算时间\n pos.settlement_scheduled_at = interestRateInterval.next;\n }\n\n if (pos.direction === 'LONG') {\n if (quote.interest_rate_long !== null) {\n pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;\n }\n }\n if (pos.direction === 'SHORT') {\n if (quote.interest_rate_short !== null) {\n pos.interest_to_settle = +quote.interest_rate_short * pos.valuation;\n }\n }\n }\n\n if (interestRateInterval) {\n pos.settlement_interval = interestRateInterval.interval;\n }\n }\n return positions;\n};\n"]}
|
package/lib/position.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"position.d.ts","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAmCjD,eAAO,MAAM,gBAAgB,cAAqB,SAAS,EAAE,KAAG,QAAQ,SAAS,EAAE,CAsDlF,CAAC"}
|
package/lib/position.js
CHANGED
|
@@ -17,7 +17,16 @@ const interestRateIntervalCache = (0, cache_1.createCache)(async (product_id) =>
|
|
|
17
17
|
const rates = await (0, sql_1.requestSQL)(terminal, sql);
|
|
18
18
|
if (rates.length < 2)
|
|
19
19
|
return undefined;
|
|
20
|
-
|
|
20
|
+
const prev = new Date(rates[0].created_at).getTime();
|
|
21
|
+
const prevOfPrev = new Date(rates[1].created_at).getTime();
|
|
22
|
+
const interval = prev - prevOfPrev;
|
|
23
|
+
const next = prev + interval;
|
|
24
|
+
return {
|
|
25
|
+
prev,
|
|
26
|
+
prevOfPrev,
|
|
27
|
+
interval,
|
|
28
|
+
next,
|
|
29
|
+
};
|
|
21
30
|
});
|
|
22
31
|
const polyfillPosition = async (positions) => {
|
|
23
32
|
// TODO: 使用 batch query SQL 优化 product / quote 查询性能
|
|
@@ -47,8 +56,13 @@ const polyfillPosition = async (positions) => {
|
|
|
47
56
|
// 利率相关信息的追加
|
|
48
57
|
if (quote) {
|
|
49
58
|
if (quote.interest_rate_next_settled_at !== null) {
|
|
59
|
+
// 优先使用行情数据中的下一个结算时间
|
|
50
60
|
pos.settlement_scheduled_at = new Date(quote.interest_rate_next_settled_at).getTime();
|
|
51
61
|
}
|
|
62
|
+
else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {
|
|
63
|
+
// 估算下一个结算时间
|
|
64
|
+
pos.settlement_scheduled_at = interestRateInterval.next;
|
|
65
|
+
}
|
|
52
66
|
if (pos.direction === 'LONG') {
|
|
53
67
|
if (quote.interest_rate_long !== null) {
|
|
54
68
|
pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;
|
|
@@ -61,7 +75,7 @@ const polyfillPosition = async (positions) => {
|
|
|
61
75
|
}
|
|
62
76
|
}
|
|
63
77
|
if (interestRateInterval) {
|
|
64
|
-
pos.settlement_interval = interestRateInterval;
|
|
78
|
+
pos.settlement_interval = interestRateInterval.interval;
|
|
65
79
|
}
|
|
66
80
|
}
|
|
67
81
|
return positions;
|
package/lib/position.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"position.js","sourceRoot":"","sources":["../src/position.ts"],"names":[],"mappings":";;;AAAA,yCAA4C;AAE5C,uDAAgE;AAEhE,+CA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CAAC,KAAK,CAAC,6BAA6B,CAAC,CAAC,OAAO,EAAE,CAAC;aACvF;iBAAM,IAAI,KAAK,CAAC,6BAA6B,KAAK,IAAI,IAAI,oBAAoB,KAAK,SAAS,EAAE;gBAC7F,YAAY;gBACZ,GAAG,CAAC,uBAAuB,GAAG,oBAAoB,CAAC,IAAI,CAAC;aACzD;YAED,IAAI,GAAG,CAAC,SAAS,KAAK,MAAM,EAAE;gBAC5B,IAAI,KAAK,CAAC,kBAAkB,KAAK,IAAI,EAAE;oBACrC,GAAG,CAAC,kBAAkB,GAAG,CAAC,KAAK,CAAC,kBAAkB,GAAG,GAAG,CAAC,SAAS,CAAC;iBACpE;aACF;YACD,IAAI,GAAG,CAAC,SAAS,KAAK,OAAO,EAAE;gBAC7B,IAAI,KAAK,CAAC,mBAAmB,KAAK,IAAI,EAAE;oBACtC,GAAG,CAAC,kBAAkB,GAAG,CAAC,KAAK,CAAC,mBAAmB,GAAG,GAAG,CAAC,SAAS,CAAC;iBACrE;aACF;SACF;QAED,IAAI,oBAAoB,EAAE;YACxB,GAAG,CAAC,mBAAmB,GAAG,oBAAoB,CAAC,QAAQ,CAAC;SACzD;KACF;IACD,OAAO,SAAS,CAAC;AACnB,CAAC,CAAC;AAtDW,QAAA,gBAAgB,oBAsD3B","sourcesContent":["import { createCache } from '@yuants/cache';\nimport { IPosition } from '@yuants/data-account';\nimport { createClientProductCache } from '@yuants/data-product';\nimport { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { escapeSQL, requestSQL } from '@yuants/sql';\n\nconst terminal = Terminal.fromNodeEnv();\nconst productCache = createClientProductCache(terminal);\nconst quoteCache = createCache<IQuote>(\n async (product_id) => {\n const sql = `select * from quote where product_id = ${escapeSQL(product_id)}`;\n const [quote] = await requestSQL<IQuote[]>(terminal, sql);\n return quote;\n },\n { expire: 30_000 },\n);\n\nconst interestRateIntervalCache = createCache(async (product_id: string) => {\n const sql = `select created_at from interest_rate where series_id = ${escapeSQL(\n product_id,\n )} order by created_at desc limit 2`;\n const rates = await requestSQL<{ created_at: string }[]>(terminal, sql);\n if (rates.length < 2) return undefined;\n const prev = new Date(rates[0].created_at).getTime();\n const prevOfPrev = new Date(rates[1].created_at).getTime();\n const interval = prev - prevOfPrev;\n const next = prev + interval;\n return {\n prev,\n prevOfPrev,\n interval,\n next,\n };\n});\n\nexport const polyfillPosition = async (positions: IPosition[]): Promise<IPosition[]> => {\n // TODO: 使用 batch query SQL 优化 product / quote 查询性能\n for (const pos of positions) {\n const [theProduct, quote, interestRateInterval] = await Promise.all([\n productCache.query(pos.product_id),\n quoteCache.query(pos.product_id),\n interestRateIntervalCache.query(pos.product_id),\n ]);\n\n // 估值 = value_scale * volume * closable_price\n if (theProduct) {\n if (theProduct.base_currency) {\n pos.base_currency = theProduct.base_currency;\n }\n if (theProduct.quote_currency) {\n pos.quote_currency = theProduct.quote_currency;\n }\n if (pos.size === undefined && pos.volume !== undefined && pos.direction !== undefined) {\n pos.size = (pos.direction === 'LONG' ? 1 : -1) * pos.volume * (theProduct.value_scale || 1) + '';\n }\n if (pos.free_size === undefined && pos.free_volume !== undefined && pos.direction !== undefined) {\n pos.free_size =\n (pos.direction === 'LONG' ? 1 : -1) * pos.free_volume * (theProduct.value_scale || 1) + '';\n }\n pos.valuation = Math.abs((theProduct.value_scale || 1) * pos.volume * pos.closable_price);\n }\n\n // 利率相关信息的追加\n if (quote) {\n if (quote.interest_rate_next_settled_at !== null) {\n // 优先使用行情数据中的下一个结算时间\n pos.settlement_scheduled_at = new Date(quote.interest_rate_next_settled_at).getTime();\n } else if (quote.interest_rate_next_settled_at === null && interestRateInterval !== undefined) {\n // 估算下一个结算时间\n pos.settlement_scheduled_at = interestRateInterval.next;\n }\n\n if (pos.direction === 'LONG') {\n if (quote.interest_rate_long !== null) {\n pos.interest_to_settle = +quote.interest_rate_long * pos.valuation;\n }\n }\n if (pos.direction === 'SHORT') {\n if (quote.interest_rate_short !== null) {\n pos.interest_to_settle = +quote.interest_rate_short * pos.valuation;\n }\n }\n }\n\n if (interestRateInterval) {\n pos.settlement_interval = interestRateInterval.interval;\n }\n }\n return positions;\n};\n"]}
|
package/package.json
CHANGED
package/temp/package-deps.json
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
{
|
|
2
|
-
"apps/virtual-exchange/CHANGELOG.json": "
|
|
3
|
-
"apps/virtual-exchange/CHANGELOG.md": "
|
|
2
|
+
"apps/virtual-exchange/CHANGELOG.json": "0f489f52b36104e67e92d49b9d0be8a408a5eb83",
|
|
3
|
+
"apps/virtual-exchange/CHANGELOG.md": "524292bcec2f3059feeba1e59c96bd9ab14c769f",
|
|
4
4
|
"apps/virtual-exchange/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
|
|
5
5
|
"apps/virtual-exchange/config/jest.config.json": "4bb17bde3ee911163a3edb36a6eb71491d80b1bd",
|
|
6
6
|
"apps/virtual-exchange/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
|
|
7
7
|
"apps/virtual-exchange/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
|
|
8
8
|
"apps/virtual-exchange/etc/app-virtual-exchange.api.md": "6cb40ec1fa2d40a31a7b0dd3f02b8b24a4d7c4de",
|
|
9
|
-
"apps/virtual-exchange/package.json": "
|
|
9
|
+
"apps/virtual-exchange/package.json": "edab481fa519cf6191b0cd0e94a744ce4daff6e7",
|
|
10
10
|
"apps/virtual-exchange/src/credential.ts": "f839246750fcbddd9a6a29bf3de54631a92c88b7",
|
|
11
11
|
"apps/virtual-exchange/src/general.ts": "16a234c6ddbdf33490339272579cbcbac17e048c",
|
|
12
12
|
"apps/virtual-exchange/src/index.ts": "8d7f19a07e6be09c4d8fd4a49ddb3127d3fbf3de",
|
|
13
13
|
"apps/virtual-exchange/src/legacy-services.ts": "73f57566054e8cae899dc20f4d1547184e2c2e71",
|
|
14
|
-
"apps/virtual-exchange/src/position.ts": "
|
|
14
|
+
"apps/virtual-exchange/src/position.ts": "f054c43a4ed186c5ac176576bb23ba0f306faca4",
|
|
15
15
|
"apps/virtual-exchange/src/product-collector.ts": "15ba0a692d694d20b607eaad0287a864577ef30c",
|
|
16
16
|
"apps/virtual-exchange/tsconfig.json": "22f94ca28b507f8ddcc21b9053158eefd3f726a9",
|
|
17
17
|
"apps/virtual-exchange/.rush/temp/shrinkwrap-deps.json": "2c8344167a574161e8a89138bc1eb686bf6339de",
|