@yuants/exchange 0.7.1 → 0.8.0
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/exchange.d.ts +90 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/interest_rate.js +129 -0
- package/dist/interest_rate.js.map +1 -0
- package/dist/interest_rate.test.js +24 -0
- package/dist/interest_rate.test.js.map +1 -0
- package/dist/ohlc.js +152 -0
- package/dist/ohlc.js.map +1 -0
- package/dist/ohlc.test.js +26 -0
- package/dist/ohlc.test.js.map +1 -0
- package/dist/quote.test.js +31 -0
- package/dist/quote.test.js.map +1 -0
- package/dist/types.js.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/interest_rate.d.ts +34 -0
- package/lib/interest_rate.d.ts.map +1 -0
- package/lib/interest_rate.js +134 -0
- package/lib/interest_rate.js.map +1 -0
- package/lib/interest_rate.test.d.ts +2 -0
- package/lib/interest_rate.test.d.ts.map +1 -0
- package/lib/interest_rate.test.js +26 -0
- package/lib/interest_rate.test.js.map +1 -0
- package/lib/ohlc.d.ts +36 -0
- package/lib/ohlc.d.ts.map +1 -0
- package/lib/ohlc.js +157 -0
- package/lib/ohlc.js.map +1 -0
- package/lib/ohlc.test.d.ts +2 -0
- package/lib/ohlc.test.d.ts.map +1 -0
- package/lib/ohlc.test.js +28 -0
- package/lib/ohlc.test.js.map +1 -0
- package/lib/quote.test.d.ts +2 -0
- package/lib/quote.test.d.ts.map +1 -0
- package/lib/quote.test.js +33 -0
- package/lib/quote.test.js.map +1 -0
- package/lib/types.d.ts +16 -0
- package/lib/types.d.ts.map +1 -1
- package/lib/types.js.map +1 -1
- package/package.json +3 -1
- package/temp/exchange.api.json +752 -0
- package/temp/exchange.api.md +80 -0
- package/temp/package-deps.json +14 -7
package/dist/exchange.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { IInterestRate } from '@yuants/data-interest-rate';
|
|
2
|
+
import { IOHLC } from '@yuants/data-ohlc';
|
|
1
3
|
import { IOrder } from '@yuants/data-order';
|
|
2
4
|
import { IPosition } from '@yuants/data-account';
|
|
3
5
|
import { IProduct } from '@yuants/data-product';
|
|
@@ -122,6 +124,48 @@ export declare interface IExchange<T = any> {
|
|
|
122
124
|
cancelOrder(credential: T, order: IOrder): Promise<void>;
|
|
123
125
|
}
|
|
124
126
|
|
|
127
|
+
/**
|
|
128
|
+
* Interest Rate Service Request from VEX to Vendor
|
|
129
|
+
* @public
|
|
130
|
+
*/
|
|
131
|
+
export declare interface IIngestInterestRateRequest {
|
|
132
|
+
product_id: string;
|
|
133
|
+
limit?: number;
|
|
134
|
+
direction: SeriesFetchDirection;
|
|
135
|
+
time: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* OHLC Service Request from VEX to Vendor
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
export declare interface IIngestOHLCRequest {
|
|
143
|
+
product_id: string;
|
|
144
|
+
duration: string;
|
|
145
|
+
limit?: number;
|
|
146
|
+
direction: SeriesFetchDirection;
|
|
147
|
+
time: string;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Interest Rate Service Metadata
|
|
152
|
+
* @public
|
|
153
|
+
*/
|
|
154
|
+
export declare interface IInterestRateServiceMetadata {
|
|
155
|
+
product_id_prefix: string;
|
|
156
|
+
direction: SeriesFetchDirection;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* OHLC Service Metadata
|
|
161
|
+
* @public
|
|
162
|
+
*/
|
|
163
|
+
export declare interface IOHLCServiceMetadata {
|
|
164
|
+
product_id_prefix: string;
|
|
165
|
+
duration_list: string[];
|
|
166
|
+
direction: SeriesFetchDirection;
|
|
167
|
+
}
|
|
168
|
+
|
|
125
169
|
/**
|
|
126
170
|
* Quote 字段类型,排除掉 product_id, updated_at, datasource_id 三个字段
|
|
127
171
|
* @public
|
|
@@ -169,6 +213,18 @@ export declare interface IQuoteServiceRequestByVEX {
|
|
|
169
213
|
*/
|
|
170
214
|
export declare type IQuoteUpdateAction<K extends IQuoteField = IQuoteField> = Record<string, Partial<Record<K, [value: string, updated_at: number]>>>;
|
|
171
215
|
|
|
216
|
+
/**
|
|
217
|
+
* 历史数据写库结果(不返回数据本体)
|
|
218
|
+
* @public
|
|
219
|
+
*/
|
|
220
|
+
export declare interface ISeriesIngestResult {
|
|
221
|
+
wrote_count: number;
|
|
222
|
+
range?: {
|
|
223
|
+
start_time: string;
|
|
224
|
+
end_time: string;
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
|
|
172
228
|
/**
|
|
173
229
|
* List products
|
|
174
230
|
*
|
|
@@ -186,6 +242,16 @@ export declare const modifyOrder: <T>(terminal: Terminal, credential: {
|
|
|
186
242
|
payload: T;
|
|
187
243
|
}, order: IOrder) => Promise<IResponse<void>>;
|
|
188
244
|
|
|
245
|
+
/**
|
|
246
|
+
* @public
|
|
247
|
+
*/
|
|
248
|
+
export declare const parseInterestRateServiceMetadataFromSchema: (schema: any) => IInterestRateServiceMetadata;
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* @public
|
|
252
|
+
*/
|
|
253
|
+
export declare const parseOHLCServiceMetadataFromSchema: (schema: any) => IOHLCServiceMetadata;
|
|
254
|
+
|
|
189
255
|
/**
|
|
190
256
|
* Extract Quote Service Metadata from JSON Schema
|
|
191
257
|
* @public
|
|
@@ -199,6 +265,24 @@ export declare const parseQuoteServiceMetadataFromSchema: (schema: any) => IQuot
|
|
|
199
265
|
*/
|
|
200
266
|
export declare const provideExchangeServices: <T>(terminal: Terminal, exchange: IExchange<T>) => void;
|
|
201
267
|
|
|
268
|
+
/**
|
|
269
|
+
* @public
|
|
270
|
+
*/
|
|
271
|
+
export declare const provideInterestRateService: (terminal: Terminal, metadata: IInterestRateServiceMetadata, fetchPage: (request: IIngestInterestRateRequest & {
|
|
272
|
+
series_id: string;
|
|
273
|
+
}) => Promise<IInterestRate[]>, serviceOptions?: IServiceOptions) => {
|
|
274
|
+
dispose: () => void;
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* @public
|
|
279
|
+
*/
|
|
280
|
+
export declare const provideOHLCService: (terminal: Terminal, metadata: IOHLCServiceMetadata, fetchPage: (request: IIngestOHLCRequest & {
|
|
281
|
+
series_id: string;
|
|
282
|
+
}) => Promise<IOHLC[]>, serviceOptions?: IServiceOptions) => {
|
|
283
|
+
dispose: () => void;
|
|
284
|
+
};
|
|
285
|
+
|
|
202
286
|
/**
|
|
203
287
|
* Provide Quote Service
|
|
204
288
|
* @public
|
|
@@ -210,6 +294,12 @@ export declare const provideQuoteService: <K extends IQuoteField>(terminal: Term
|
|
|
210
294
|
dispose: () => void;
|
|
211
295
|
};
|
|
212
296
|
|
|
297
|
+
/**
|
|
298
|
+
* 历史数据翻页方向
|
|
299
|
+
* @public
|
|
300
|
+
*/
|
|
301
|
+
export declare type SeriesFetchDirection = 'backward' | 'forward';
|
|
302
|
+
|
|
213
303
|
/**
|
|
214
304
|
* Submit order
|
|
215
305
|
*
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAqFxB,MAAM,oBAAoB,GAAG,CAAC,IAAY,EAAE,aAA0B,EAAe,EAAE;IACrF,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;QAC7B,UAAU,EAAE;YACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE;YACrC,OAAO,EAAE,aAAa;SACvB;KACF,CAAC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U,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { IProduct } from '@yuants/data-product';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\nexport * from './types';\n\n/**\n * Exchange Interface\n *\n * @public\n */\nexport interface IExchange<T = any> {\n /**\n * Exchange name / type (e.g. 'Binance', 'OKX')\n */\n name: string;\n\n /**\n * JSON Schema for the credential payload\n */\n credentialSchema: JSONSchema7;\n\n /**\n * Get credential ID from credential\n *\n * @param credential - The credential object\n */\n getCredentialId(credential: T): Promise<string>;\n\n /**\n * List all products\n */\n listProducts(): Promise<IProduct[]>;\n\n /**\n * Get all positions\n *\n * @param credential - The credential object\n */\n getPositions(credential: T): Promise<IPosition[]>;\n\n /**\n * Get all orders\n *\n * @param credential - The credential object\n */\n getOrders(credential: T): Promise<IOrder[]>;\n\n /**\n * Get positions by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;\n\n /**\n * Get orders by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;\n\n /**\n * Submit an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n submitOrder(credential: T, order: IOrder): Promise<{ order_id: string }>;\n\n /**\n * Modify an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n modifyOrder(credential: T, order: IOrder): Promise<void>;\n\n /**\n * Cancel an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n cancelOrder(credential: T, order: IOrder): Promise<void>;\n}\n\nconst makeCredentialSchema = (type: string, payloadSchema: JSONSchema7): JSONSchema7 => {\n return {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: type },\n payload: payloadSchema,\n },\n };\n};\n\n/**\n * Provide exchange services\n *\n * @public\n */\nexport const provideExchangeServices = <T>(terminal: Terminal, exchange: IExchange<T>) => {\n const { name: type, credentialSchema } = exchange;\n\n // GetCredentialId\n terminal.server.provideService<{ credential: { type: string; payload: T } }, string>(\n 'GetCredentialId',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n },\n },\n async (msg) => {\n const credentialId = await exchange.getCredentialId(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: credentialId } };\n },\n );\n\n // ListProducts\n terminal.server.provideService<void, IProduct[]>(\n 'ListProducts',\n {\n type: 'object',\n required: ['type'],\n properties: {\n type: { const: exchange.name },\n },\n },\n async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\n },\n );\n\n // GetPositions\n terminal.server.provideService<\n { credential: { type: string; payload: T }; product_id?: string },\n IPosition[]\n >(\n 'GetPositions',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const positions = await exchange.getPositionsByProductId(\n msg.req.credential.payload,\n msg.req.product_id,\n );\n return { res: { code: 0, message: 'OK', data: positions } };\n }\n const positions = await exchange.getPositions(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: positions } };\n },\n );\n\n // GetOrders\n terminal.server.provideService<{ credential: { type: string; payload: T }; product_id?: string }, IOrder[]>(\n 'GetOrders',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);\n return { res: { code: 0, message: 'OK', data: orders } };\n }\n const orders = await exchange.getOrders(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: orders } };\n },\n );\n\n // SubmitOrder\n terminal.server.provideService<\n { credential: { type: string; payload: T }; order: IOrder },\n { order_id: string }\n >(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK', data: result } };\n },\n );\n\n // ModifyOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n // CancelOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n};\n\n/**\n * Get credential ID\n *\n * @public\n */\nexport const getCredentialId = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n): Promise<IResponse<string>> => {\n return terminal.client.requestForResponse('GetCredentialId', { credential });\n};\n\n/**\n * List products\n *\n * @public\n */\nexport const listProducts = async (terminal: Terminal, type: string): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', { type });\n};\n\n/**\n * Get positions\n *\n * @public\n */\nexport const getPositions = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IPosition[]>> => {\n return terminal.client.requestForResponse('GetPositions', { credential, product_id });\n};\n\n/**\n * Get orders\n *\n * @public\n */\nexport const getOrders = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IOrder[]>> => {\n return terminal.client.requestForResponse('GetOrders', { credential, product_id });\n};\n\n/**\n * Submit order\n *\n * @public\n */\nexport const submitOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<{ order_id: string }>> => {\n return terminal.client.requestForResponse('SubmitOrder', { credential, order });\n};\n\n/**\n * Modify order\n *\n * @public\n */\nexport const modifyOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('ModifyOrder', { credential, order });\n};\n\n/**\n * Cancel order\n *\n * @public\n */\nexport const cancelOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('CancelOrder', { credential, order });\n};\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAKA,cAAc,SAAS,CAAC;AACxB,cAAc,QAAQ,CAAC;AACvB,cAAc,iBAAiB,CAAC;AAChC,cAAc,SAAS,CAAC;AAqFxB,MAAM,oBAAoB,GAAG,CAAC,IAAY,EAAE,aAA0B,EAAe,EAAE;IACrF,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,CAAC,MAAM,EAAE,SAAS,CAAC;QAC7B,UAAU,EAAE;YACV,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE;YACrC,OAAO,EAAE,aAAa;SACvB;KACF,CAAC;AACJ,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAI,QAAkB,EAAE,QAAsB,EAAE,EAAE;IACvF,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,gBAAgB,EAAE,GAAG,QAAQ,CAAC;IAElD,kBAAkB;IAClB,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,iBAAiB,EACjB;QACE,IAAI,EAAE,QAAQ;Q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EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;AACxF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,KAAK,EAC5B,QAAkB,EAClB,UAAwC,EACxC,UAAmB,EACW,EAAE;IAChC,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,WAAW,EAAE,EAAE,UAAU,EAAE,UAAU,EAAE,CAAC,CAAC;AACrF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EAC6B,EAAE;IAC5C,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,QAAkB,EAClB,UAAwC,EACxC,KAAa,EACa,EAAE;IAC5B,OAAO,QAAQ,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,EAAE,EAAE,UAAU,EAAE,KAAK,EAAE,CAAC,CAAC;AAClF,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { IProduct } from '@yuants/data-product';\nimport { IResponse, Terminal } from '@yuants/protocol';\nimport { JSONSchema7 } from 'json-schema';\nexport * from './quote';\nexport * from './ohlc';\nexport * from './interest_rate';\nexport * from './types';\n\n/**\n * Exchange Interface\n *\n * @public\n */\nexport interface IExchange<T = any> {\n /**\n * Exchange name / type (e.g. 'Binance', 'OKX')\n */\n name: string;\n\n /**\n * JSON Schema for the credential payload\n */\n credentialSchema: JSONSchema7;\n\n /**\n * Get credential ID from credential\n *\n * @param credential - The credential object\n */\n getCredentialId(credential: T): Promise<string>;\n\n /**\n * List all products\n */\n listProducts(): Promise<IProduct[]>;\n\n /**\n * Get all positions\n *\n * @param credential - The credential object\n */\n getPositions(credential: T): Promise<IPosition[]>;\n\n /**\n * Get all orders\n *\n * @param credential - The credential object\n */\n getOrders(credential: T): Promise<IOrder[]>;\n\n /**\n * Get positions by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getPositionsByProductId(credential: T, product_id: string): Promise<IPosition[]>;\n\n /**\n * Get orders by product_id\n *\n * @param credential - The credential object\n * @param product_id - The product ID\n */\n getOrdersByProductId(credential: T, product_id: string): Promise<IOrder[]>;\n\n /**\n * Submit an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n submitOrder(credential: T, order: IOrder): Promise<{ order_id: string }>;\n\n /**\n * Modify an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n modifyOrder(credential: T, order: IOrder): Promise<void>;\n\n /**\n * Cancel an order\n *\n * @param credential - The credential object\n * @param order - The order object\n */\n cancelOrder(credential: T, order: IOrder): Promise<void>;\n}\n\nconst makeCredentialSchema = (type: string, payloadSchema: JSONSchema7): JSONSchema7 => {\n return {\n type: 'object',\n required: ['type', 'payload'],\n properties: {\n type: { type: 'string', const: type },\n payload: payloadSchema,\n },\n };\n};\n\n/**\n * Provide exchange services\n *\n * @public\n */\nexport const provideExchangeServices = <T>(terminal: Terminal, exchange: IExchange<T>) => {\n const { name: type, credentialSchema } = exchange;\n\n // GetCredentialId\n terminal.server.provideService<{ credential: { type: string; payload: T } }, string>(\n 'GetCredentialId',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n },\n },\n async (msg) => {\n const credentialId = await exchange.getCredentialId(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: credentialId } };\n },\n );\n\n // ListProducts\n terminal.server.provideService<void, IProduct[]>(\n 'ListProducts',\n {\n type: 'object',\n required: ['type'],\n properties: {\n type: { const: exchange.name },\n },\n },\n async () => {\n const products = await exchange.listProducts();\n return { res: { code: 0, message: 'OK', data: products } };\n },\n );\n\n // GetPositions\n terminal.server.provideService<\n { credential: { type: string; payload: T }; product_id?: string },\n IPosition[]\n >(\n 'GetPositions',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const positions = await exchange.getPositionsByProductId(\n msg.req.credential.payload,\n msg.req.product_id,\n );\n return { res: { code: 0, message: 'OK', data: positions } };\n }\n const positions = await exchange.getPositions(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: positions } };\n },\n );\n\n // GetOrders\n terminal.server.provideService<{ credential: { type: string; payload: T }; product_id?: string }, IOrder[]>(\n 'GetOrders',\n {\n type: 'object',\n required: ['credential'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n product_id: { type: 'string' },\n },\n },\n async (msg) => {\n if (msg.req.product_id) {\n const orders = await exchange.getOrdersByProductId(msg.req.credential.payload, msg.req.product_id);\n return { res: { code: 0, message: 'OK', data: orders } };\n }\n const orders = await exchange.getOrders(msg.req.credential.payload);\n return { res: { code: 0, message: 'OK', data: orders } };\n },\n );\n\n // SubmitOrder\n terminal.server.provideService<\n { credential: { type: string; payload: T }; order: IOrder },\n { order_id: string }\n >(\n 'SubmitOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n const result = await exchange.submitOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK', data: result } };\n },\n );\n\n // ModifyOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'ModifyOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.modifyOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n // CancelOrder\n terminal.server.provideService<{ credential: { type: string; payload: T }; order: IOrder }, void>(\n 'CancelOrder',\n {\n type: 'object',\n required: ['credential', 'order'],\n properties: {\n credential: makeCredentialSchema(type, credentialSchema),\n order: { type: 'object' },\n },\n },\n async (msg) => {\n await exchange.cancelOrder(msg.req.credential.payload, msg.req.order);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n};\n\n/**\n * Get credential ID\n *\n * @public\n */\nexport const getCredentialId = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n): Promise<IResponse<string>> => {\n return terminal.client.requestForResponse('GetCredentialId', { credential });\n};\n\n/**\n * List products\n *\n * @public\n */\nexport const listProducts = async (terminal: Terminal, type: string): Promise<IResponse<IProduct[]>> => {\n return terminal.client.requestForResponse('ListProducts', { type });\n};\n\n/**\n * Get positions\n *\n * @public\n */\nexport const getPositions = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IPosition[]>> => {\n return terminal.client.requestForResponse('GetPositions', { credential, product_id });\n};\n\n/**\n * Get orders\n *\n * @public\n */\nexport const getOrders = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n product_id?: string,\n): Promise<IResponse<IOrder[]>> => {\n return terminal.client.requestForResponse('GetOrders', { credential, product_id });\n};\n\n/**\n * Submit order\n *\n * @public\n */\nexport const submitOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<{ order_id: string }>> => {\n return terminal.client.requestForResponse('SubmitOrder', { credential, order });\n};\n\n/**\n * Modify order\n *\n * @public\n */\nexport const modifyOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('ModifyOrder', { credential, order });\n};\n\n/**\n * Cancel order\n *\n * @public\n */\nexport const cancelOrder = async <T>(\n terminal: Terminal,\n credential: { type: string; payload: T },\n order: IOrder,\n): Promise<IResponse<void>> => {\n return terminal.client.requestForResponse('CancelOrder', { credential, order });\n};\n"]}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { createValidator } from '@yuants/protocol/lib/schema';
|
|
2
|
+
import { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';
|
|
3
|
+
import { encodePath } from '@yuants/utils';
|
|
4
|
+
import { newError } from '../../utils/lib';
|
|
5
|
+
const schemaValidator = createValidator({
|
|
6
|
+
type: 'object',
|
|
7
|
+
required: ['type', 'properties'],
|
|
8
|
+
properties: {
|
|
9
|
+
type: { type: 'string', const: 'object' },
|
|
10
|
+
required: { type: 'array', const: ['product_id', 'direction', 'time'] },
|
|
11
|
+
properties: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
required: ['product_id', 'direction', 'time'],
|
|
14
|
+
properties: {
|
|
15
|
+
product_id: {
|
|
16
|
+
type: 'object',
|
|
17
|
+
required: ['type', 'pattern'],
|
|
18
|
+
properties: {
|
|
19
|
+
type: { type: 'string', const: 'string' },
|
|
20
|
+
pattern: { type: 'string', pattern: '^\\^' },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
direction: {
|
|
24
|
+
type: 'object',
|
|
25
|
+
required: ['const'],
|
|
26
|
+
properties: {
|
|
27
|
+
const: { type: 'string', enum: ['backward', 'forward'] },
|
|
28
|
+
},
|
|
29
|
+
},
|
|
30
|
+
time: {
|
|
31
|
+
type: 'object',
|
|
32
|
+
required: ['type', 'format'],
|
|
33
|
+
properties: {
|
|
34
|
+
type: { type: 'string', const: 'string' },
|
|
35
|
+
format: { type: 'string', const: 'date-time' },
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
/**
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export const parseInterestRateServiceMetadataFromSchema = (schema) => {
|
|
46
|
+
if (!schema)
|
|
47
|
+
throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });
|
|
48
|
+
if (!schemaValidator(schema))
|
|
49
|
+
throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });
|
|
50
|
+
return {
|
|
51
|
+
product_id_prefix: schema.properties.product_id.pattern.slice(1),
|
|
52
|
+
direction: schema.properties.direction.const,
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
const INTEREST_RATE_INSERT_COLUMNS = [
|
|
56
|
+
'series_id',
|
|
57
|
+
'created_at',
|
|
58
|
+
'datasource_id',
|
|
59
|
+
'product_id',
|
|
60
|
+
'long_rate',
|
|
61
|
+
'short_rate',
|
|
62
|
+
'settlement_price',
|
|
63
|
+
];
|
|
64
|
+
const computeInterestRatePageRange = (items) => {
|
|
65
|
+
if (items.length === 0)
|
|
66
|
+
return undefined;
|
|
67
|
+
let start = items[0];
|
|
68
|
+
let startMs = Date.parse(start.created_at);
|
|
69
|
+
let end = items[0];
|
|
70
|
+
let endMs = Date.parse(end.created_at);
|
|
71
|
+
for (const item of items) {
|
|
72
|
+
const createdAtMs = Date.parse(item.created_at);
|
|
73
|
+
if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {
|
|
74
|
+
start = item;
|
|
75
|
+
startMs = createdAtMs;
|
|
76
|
+
}
|
|
77
|
+
if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {
|
|
78
|
+
end = item;
|
|
79
|
+
endMs = createdAtMs;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { start_time: start.created_at, end_time: end.created_at };
|
|
83
|
+
};
|
|
84
|
+
/**
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
export const provideInterestRateService = (terminal, metadata, fetchPage, serviceOptions) => {
|
|
88
|
+
return terminal.server.provideService('IngestInterestRate', {
|
|
89
|
+
type: 'object',
|
|
90
|
+
required: ['product_id', 'direction', 'time'],
|
|
91
|
+
properties: {
|
|
92
|
+
product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },
|
|
93
|
+
direction: { const: metadata.direction },
|
|
94
|
+
time: { type: 'string', format: 'date-time' },
|
|
95
|
+
},
|
|
96
|
+
}, async (msg) => {
|
|
97
|
+
try {
|
|
98
|
+
const series_id = encodePath(msg.req.product_id);
|
|
99
|
+
const items = await fetchPage(Object.assign(Object.assign({}, msg.req), { series_id }));
|
|
100
|
+
const normalized = items.map((x) => (Object.assign(Object.assign({}, x), { series_id, datasource_id: '', product_id: msg.req.product_id })));
|
|
101
|
+
if (normalized.length > 0) {
|
|
102
|
+
await requestSQL(terminal, buildInsertManyIntoTableSQL(normalized, 'interest_rate', {
|
|
103
|
+
columns: INTEREST_RATE_INSERT_COLUMNS,
|
|
104
|
+
conflictKeys: ['series_id', 'created_at'],
|
|
105
|
+
}));
|
|
106
|
+
}
|
|
107
|
+
const range = computeInterestRatePageRange(normalized);
|
|
108
|
+
if (range) {
|
|
109
|
+
await requestSQL(terminal, buildInsertManyIntoTableSQL([
|
|
110
|
+
{
|
|
111
|
+
series_id,
|
|
112
|
+
table_name: 'interest_rate',
|
|
113
|
+
start_time: range.start_time,
|
|
114
|
+
end_time: range.end_time,
|
|
115
|
+
},
|
|
116
|
+
], 'series_data_range', {
|
|
117
|
+
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
118
|
+
ignoreConflict: true,
|
|
119
|
+
}));
|
|
120
|
+
}
|
|
121
|
+
return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };
|
|
122
|
+
}
|
|
123
|
+
catch (error) {
|
|
124
|
+
const message = error instanceof Error ? error.message : `${error}`;
|
|
125
|
+
return { res: { code: 1, message } };
|
|
126
|
+
}
|
|
127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AAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IInterestRate } from '@yuants/data-interest-rate';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { encodePath } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * Interest Rate Service Metadata\n * @public\n */\nexport interface IInterestRateServiceMetadata {\n product_id_prefix: string;\n direction: SeriesFetchDirection;\n}\n\n/**\n * Interest Rate Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestInterestRateRequest {\n product_id: string;\n limit?: number;\n direction: SeriesFetchDirection;\n time: string;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type', 'format'],\n properties: {\n type: { type: 'string', const: 'string' },\n format: { type: 'string', const: 'date-time' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseInterestRateServiceMetadataFromSchema = (schema: any): IInterestRateServiceMetadata => {\n if (!schema) throw newError('INTEREST_RATE_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('INTEREST_RATE_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n direction: schema.properties.direction.const,\n };\n};\n\nconst INTEREST_RATE_INSERT_COLUMNS: Array<keyof IInterestRate> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'long_rate',\n 'short_rate',\n 'settlement_price',\n];\n\nconst computeInterestRatePageRange = (\n items: IInterestRate[],\n): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n if (!isNaN(createdAtMs) && (isNaN(endMs) || createdAtMs > endMs)) {\n end = item;\n endMs = createdAtMs;\n }\n }\n\n return { start_time: start.created_at, end_time: end.created_at };\n};\n\n/**\n * @public\n */\nexport const provideInterestRateService = (\n terminal: Terminal,\n metadata: IInterestRateServiceMetadata,\n fetchPage: (request: IIngestInterestRateRequest & { series_id: string }) => Promise<IInterestRate[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestInterestRateRequest, ISeriesIngestResult>(\n 'IngestInterestRate',\n {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n direction: { const: metadata.direction },\n time: { type: 'string', format: 'date-time' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodePath(msg.req.product_id);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IInterestRate[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n }));\n\n if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'interest_rate', {\n columns: INTEREST_RATE_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n const range = computeInterestRatePageRange(normalized);\n if (range) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'interest_rate',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n ),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { parseInterestRateServiceMetadataFromSchema } from './interest_rate';
|
|
2
|
+
describe('parseInterestRateServiceMetadataFromSchema', () => {
|
|
3
|
+
it('parses interest rate service schema', () => {
|
|
4
|
+
const schema = {
|
|
5
|
+
type: 'object',
|
|
6
|
+
required: ['product_id', 'direction', 'time'],
|
|
7
|
+
properties: {
|
|
8
|
+
product_id: { type: 'string', pattern: '^BINANCE/' },
|
|
9
|
+
direction: { const: 'forward' },
|
|
10
|
+
time: { type: 'string', format: 'date-time' },
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
const meta = parseInterestRateServiceMetadataFromSchema(schema);
|
|
14
|
+
expect(meta).toEqual({
|
|
15
|
+
product_id_prefix: 'BINANCE/',
|
|
16
|
+
direction: 'forward',
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
it('throws when schema missing/invalid', () => {
|
|
20
|
+
expect(() => parseInterestRateServiceMetadataFromSchema(undefined)).toThrow('INTEREST_RATE_SERVICE_SCHEMA_MISSING');
|
|
21
|
+
expect(() => parseInterestRateServiceMetadataFromSchema({})).toThrow('INTEREST_RATE_SERVICE_SCHEMA_INVALID');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
//# sourceMappingURL=interest_rate.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interest_rate.test.js","sourceRoot":"","sources":["../src/interest_rate.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0CAA0C,EAAE,MAAM,iBAAiB,CAAC;AAE7E,QAAQ,CAAC,4CAA4C,EAAE,GAAG,EAAE;IAC1D,EAAE,CAAC,qCAAqC,EAAE,GAAG,EAAE;QAC7C,MAAM,MAAM,GAAG;YACb,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,YAAY,EAAE,WAAW,EAAE,MAAM,CAAC;YAC7C,UAAU,EAAE;gBACV,UAAU,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,WAAW,EAAE;gBACpD,SAAS,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE;gBAC/B,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,WAAW,EAAE;aAC9C;SACF,CAAC;QAEF,MAAM,IAAI,GAAG,0CAA0C,CAAC,MAAM,CAAC,CAAC;QAChE,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;YACnB,iBAAiB,EAAE,UAAU;YAC7B,SAAS,EAAE,SAAS;SACrB,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,CAAC,GAAG,EAAE,CAAC,0CAA0C,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CACzE,sCAAsC,CACvC,CAAC;QACF,MAAM,CAAC,GAAG,EAAE,CAAC,0CAA0C,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAClE,sCAAsC,CACvC,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { parseInterestRateServiceMetadataFromSchema } from './interest_rate';\n\ndescribe('parseInterestRateServiceMetadataFromSchema', () => {\n it('parses interest rate service schema', () => {\n const schema = {\n type: 'object',\n required: ['product_id', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: '^BINANCE/' },\n direction: { const: 'forward' },\n time: { type: 'string', format: 'date-time' },\n },\n };\n\n const meta = parseInterestRateServiceMetadataFromSchema(schema);\n expect(meta).toEqual({\n product_id_prefix: 'BINANCE/',\n direction: 'forward',\n });\n });\n\n it('throws when schema missing/invalid', () => {\n expect(() => parseInterestRateServiceMetadataFromSchema(undefined)).toThrow(\n 'INTEREST_RATE_SERVICE_SCHEMA_MISSING',\n );\n expect(() => parseInterestRateServiceMetadataFromSchema({})).toThrow(\n 'INTEREST_RATE_SERVICE_SCHEMA_INVALID',\n );\n });\n});\n"]}
|
package/dist/ohlc.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { createValidator } from '@yuants/protocol/lib/schema';
|
|
2
|
+
import { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';
|
|
3
|
+
import { encodePath } from '@yuants/utils';
|
|
4
|
+
import { newError } from '../../utils/lib';
|
|
5
|
+
const schemaValidator = createValidator({
|
|
6
|
+
type: 'object',
|
|
7
|
+
required: ['type', 'properties'],
|
|
8
|
+
properties: {
|
|
9
|
+
type: { type: 'string', const: 'object' },
|
|
10
|
+
required: { type: 'array', const: ['product_id', 'duration', 'direction', 'time'] },
|
|
11
|
+
properties: {
|
|
12
|
+
type: 'object',
|
|
13
|
+
required: ['product_id', 'duration', 'direction', 'time'],
|
|
14
|
+
properties: {
|
|
15
|
+
product_id: {
|
|
16
|
+
type: 'object',
|
|
17
|
+
required: ['type', 'pattern'],
|
|
18
|
+
properties: {
|
|
19
|
+
type: { type: 'string', const: 'string' },
|
|
20
|
+
pattern: { type: 'string', pattern: '^\\^' },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
duration: {
|
|
24
|
+
type: 'object',
|
|
25
|
+
required: ['type', 'enum'],
|
|
26
|
+
properties: {
|
|
27
|
+
type: { type: 'string', const: 'string' },
|
|
28
|
+
enum: { type: 'array', items: { type: 'string' } },
|
|
29
|
+
},
|
|
30
|
+
},
|
|
31
|
+
direction: {
|
|
32
|
+
type: 'object',
|
|
33
|
+
required: ['const'],
|
|
34
|
+
properties: {
|
|
35
|
+
const: { type: 'string', enum: ['backward', 'forward'] },
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
time: {
|
|
39
|
+
type: 'object',
|
|
40
|
+
required: ['type', 'format'],
|
|
41
|
+
properties: {
|
|
42
|
+
type: { type: 'string', const: 'string' },
|
|
43
|
+
format: { type: 'string', const: 'date-time' },
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
});
|
|
50
|
+
/**
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
export const parseOHLCServiceMetadataFromSchema = (schema) => {
|
|
54
|
+
if (!schema)
|
|
55
|
+
throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });
|
|
56
|
+
if (!schemaValidator(schema))
|
|
57
|
+
throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });
|
|
58
|
+
return {
|
|
59
|
+
product_id_prefix: schema.properties.product_id.pattern.slice(1),
|
|
60
|
+
duration_list: schema.properties.duration.enum,
|
|
61
|
+
direction: schema.properties.direction.const,
|
|
62
|
+
};
|
|
63
|
+
};
|
|
64
|
+
const OHLC_INSERT_COLUMNS = [
|
|
65
|
+
'series_id',
|
|
66
|
+
'created_at',
|
|
67
|
+
'datasource_id',
|
|
68
|
+
'product_id',
|
|
69
|
+
'duration',
|
|
70
|
+
'closed_at',
|
|
71
|
+
'open',
|
|
72
|
+
'high',
|
|
73
|
+
'low',
|
|
74
|
+
'close',
|
|
75
|
+
'volume',
|
|
76
|
+
'open_interest',
|
|
77
|
+
];
|
|
78
|
+
const computeOHLCPageRange = (items) => {
|
|
79
|
+
if (items.length === 0)
|
|
80
|
+
return undefined;
|
|
81
|
+
let start = items[0];
|
|
82
|
+
let startMs = Date.parse(start.created_at);
|
|
83
|
+
let end = items[0];
|
|
84
|
+
let endMs = Date.parse(end.closed_at) || Date.parse(end.created_at);
|
|
85
|
+
for (const item of items) {
|
|
86
|
+
const createdAtMs = Date.parse(item.created_at);
|
|
87
|
+
if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {
|
|
88
|
+
start = item;
|
|
89
|
+
startMs = createdAtMs;
|
|
90
|
+
}
|
|
91
|
+
const closedAtMs = Date.parse(item.closed_at);
|
|
92
|
+
const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;
|
|
93
|
+
if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {
|
|
94
|
+
end = item;
|
|
95
|
+
endMs = candidateEndMs;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return {
|
|
99
|
+
start_time: start.created_at,
|
|
100
|
+
end_time: end.closed_at || end.created_at,
|
|
101
|
+
};
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
export const provideOHLCService = (terminal, metadata, fetchPage, serviceOptions) => {
|
|
107
|
+
return terminal.server.provideService('IngestOHLC', {
|
|
108
|
+
type: 'object',
|
|
109
|
+
required: ['product_id', 'duration', 'direction', 'time'],
|
|
110
|
+
properties: {
|
|
111
|
+
product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },
|
|
112
|
+
duration: { type: 'string', enum: metadata.duration_list },
|
|
113
|
+
direction: { const: metadata.direction },
|
|
114
|
+
time: { type: 'string', format: 'date-time' },
|
|
115
|
+
},
|
|
116
|
+
}, async (msg) => {
|
|
117
|
+
try {
|
|
118
|
+
const series_id = encodePath(msg.req.product_id, msg.req.duration);
|
|
119
|
+
const items = await fetchPage(Object.assign(Object.assign({}, msg.req), { series_id }));
|
|
120
|
+
const normalized = items.map((x) => {
|
|
121
|
+
var _a;
|
|
122
|
+
return (Object.assign(Object.assign({}, x), { series_id, datasource_id: '', product_id: msg.req.product_id, duration: msg.req.duration, open_interest: (_a = x.open_interest) !== null && _a !== void 0 ? _a : '0' }));
|
|
123
|
+
});
|
|
124
|
+
if (normalized.length > 0) {
|
|
125
|
+
await requestSQL(terminal, buildInsertManyIntoTableSQL(normalized, 'ohlc', {
|
|
126
|
+
columns: OHLC_INSERT_COLUMNS,
|
|
127
|
+
conflictKeys: ['series_id', 'created_at'],
|
|
128
|
+
}));
|
|
129
|
+
}
|
|
130
|
+
const range = computeOHLCPageRange(normalized);
|
|
131
|
+
if (range) {
|
|
132
|
+
await requestSQL(terminal, buildInsertManyIntoTableSQL([
|
|
133
|
+
{
|
|
134
|
+
series_id,
|
|
135
|
+
table_name: 'ohlc',
|
|
136
|
+
start_time: range.start_time,
|
|
137
|
+
end_time: range.end_time,
|
|
138
|
+
},
|
|
139
|
+
], 'series_data_range', {
|
|
140
|
+
columns: ['series_id', 'table_name', 'start_time', 'end_time'],
|
|
141
|
+
ignoreConflict: true,
|
|
142
|
+
}));
|
|
143
|
+
}
|
|
144
|
+
return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };
|
|
145
|
+
}
|
|
146
|
+
catch (error) {
|
|
147
|
+
const message = error instanceof Error ? error.message : `${error}`;
|
|
148
|
+
return { res: { code: 1, message } };
|
|
149
|
+
}
|
|
150
|
+
}, serviceOptions);
|
|
151
|
+
};
|
|
152
|
+
//# sourceMappingURL=ohlc.js.map
|
package/dist/ohlc.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,2BAA2B,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACtE,OAAO,EAAE,UAAU,EAAE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EAAE,mBAAmB;oBAC5B,YAAY,EAAE,CAAC,WAAW,EAAE,YAAY,CAAC;iBAC1C,CAAC,CACH,CAAC;aACH;YAED,MAAM,KAAK,GAAG,oBAAoB,CAAC,UAAU,CAAC,CAAC;YAC/C,IAAI,KAAK,EAAE;gBACT,MAAM,UAAU,CACd,QAAQ,EACR,2BAA2B,CACzB;oBACE;wBACE,SAAS;wBACT,UAAU,EAAE,MAAM;wBAClB,UAAU,EAAE,KAAK,CAAC,UAAU;wBAC5B,QAAQ,EAAE,KAAK,CAAC,QAAQ;qBACzB;iBACF,EACD,mBAAmB,EACnB;oBACE,OAAO,EAAE,CAAC,WAAW,EAAE,YAAY,EAAE,YAAY,EAAE,UAAU,CAAC;oBAC9D,cAAc,EAAE,IAAI;iBACrB,CACF,CACF,CAAC;aACH;YAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,EAAE,CAAC;SAC7F;QAAC,OAAO,KAAK,EAAE;YACd,MAAM,OAAO,GAAG,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,KAAK,EAAE,CAAC;YACpE,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,EAAE,CAAC;SACtC;IACH,CAAC,EACD,cAAc,CACf,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { IServiceOptions, Terminal } from '@yuants/protocol';\nimport { createValidator } from '@yuants/protocol/lib/schema';\nimport { buildInsertManyIntoTableSQL, requestSQL } from '@yuants/sql';\nimport { encodePath } from '@yuants/utils';\nimport { newError } from '../../utils/lib';\nimport { ISeriesIngestResult, SeriesFetchDirection } from './types';\n\n/**\n * OHLC Service Metadata\n * @public\n */\nexport interface IOHLCServiceMetadata {\n product_id_prefix: string;\n duration_list: string[];\n direction: SeriesFetchDirection;\n}\n\n/**\n * OHLC Service Request from VEX to Vendor\n * @public\n */\nexport interface IIngestOHLCRequest {\n product_id: string;\n duration: string;\n limit?: number;\n direction: SeriesFetchDirection;\n time: string;\n}\n\nconst schemaValidator = createValidator({\n type: 'object',\n required: ['type', 'properties'],\n properties: {\n type: { type: 'string', const: 'object' },\n required: { type: 'array', const: ['product_id', 'duration', 'direction', 'time'] },\n properties: {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: {\n type: 'object',\n required: ['type', 'pattern'],\n properties: {\n type: { type: 'string', const: 'string' },\n pattern: { type: 'string', pattern: '^\\\\^' },\n },\n },\n duration: {\n type: 'object',\n required: ['type', 'enum'],\n properties: {\n type: { type: 'string', const: 'string' },\n enum: { type: 'array', items: { type: 'string' } },\n },\n },\n direction: {\n type: 'object',\n required: ['const'],\n properties: {\n const: { type: 'string', enum: ['backward', 'forward'] },\n },\n },\n time: {\n type: 'object',\n required: ['type', 'format'],\n properties: {\n type: { type: 'string', const: 'string' },\n format: { type: 'string', const: 'date-time' },\n },\n },\n },\n },\n },\n});\n\n/**\n * @public\n */\nexport const parseOHLCServiceMetadataFromSchema = (schema: any): IOHLCServiceMetadata => {\n if (!schema) throw newError('OHLC_SERVICE_SCHEMA_MISSING', { schema });\n if (!schemaValidator(schema)) throw newError('OHLC_SERVICE_SCHEMA_INVALID', { schema });\n return {\n product_id_prefix: schema.properties.product_id.pattern.slice(1),\n duration_list: schema.properties.duration.enum,\n direction: schema.properties.direction.const,\n };\n};\n\nconst OHLC_INSERT_COLUMNS: Array<keyof IOHLC> = [\n 'series_id',\n 'created_at',\n 'datasource_id',\n 'product_id',\n 'duration',\n 'closed_at',\n 'open',\n 'high',\n 'low',\n 'close',\n 'volume',\n 'open_interest',\n];\n\nconst computeOHLCPageRange = (items: IOHLC[]): { start_time: string; end_time: string } | undefined => {\n if (items.length === 0) return undefined;\n\n let start = items[0];\n let startMs = Date.parse(start.created_at);\n let end = items[0];\n let endMs = Date.parse(end.closed_at) || Date.parse(end.created_at);\n\n for (const item of items) {\n const createdAtMs = Date.parse(item.created_at);\n if (!isNaN(createdAtMs) && (isNaN(startMs) || createdAtMs < startMs)) {\n start = item;\n startMs = createdAtMs;\n }\n const closedAtMs = Date.parse(item.closed_at);\n const candidateEndMs = !isNaN(closedAtMs) ? closedAtMs : createdAtMs;\n if (!isNaN(candidateEndMs) && (isNaN(endMs) || candidateEndMs > endMs)) {\n end = item;\n endMs = candidateEndMs;\n }\n }\n\n return {\n start_time: start.created_at,\n end_time: end.closed_at || end.created_at,\n };\n};\n\n/**\n * @public\n */\nexport const provideOHLCService = (\n terminal: Terminal,\n metadata: IOHLCServiceMetadata,\n fetchPage: (request: IIngestOHLCRequest & { series_id: string }) => Promise<IOHLC[]>,\n serviceOptions?: IServiceOptions,\n) => {\n return terminal.server.provideService<IIngestOHLCRequest, ISeriesIngestResult>(\n 'IngestOHLC',\n {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: `^${metadata.product_id_prefix}` },\n duration: { type: 'string', enum: metadata.duration_list },\n direction: { const: metadata.direction },\n time: { type: 'string', format: 'date-time' },\n },\n },\n async (msg) => {\n try {\n const series_id = encodePath(msg.req.product_id, msg.req.duration);\n\n const items = await fetchPage({ ...msg.req, series_id });\n\n const normalized: IOHLC[] = items.map((x) => ({\n ...x,\n series_id,\n datasource_id: '',\n product_id: msg.req.product_id,\n duration: msg.req.duration,\n open_interest: x.open_interest ?? '0',\n }));\n\n if (normalized.length > 0) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(normalized, 'ohlc', {\n columns: OHLC_INSERT_COLUMNS,\n conflictKeys: ['series_id', 'created_at'],\n }),\n );\n }\n\n const range = computeOHLCPageRange(normalized);\n if (range) {\n await requestSQL(\n terminal,\n buildInsertManyIntoTableSQL(\n [\n {\n series_id,\n table_name: 'ohlc',\n start_time: range.start_time,\n end_time: range.end_time,\n },\n ],\n 'series_data_range',\n {\n columns: ['series_id', 'table_name', 'start_time', 'end_time'],\n ignoreConflict: true,\n },\n ),\n );\n }\n\n return { res: { code: 0, message: 'OK', data: { wrote_count: normalized.length, range } } };\n } catch (error) {\n const message = error instanceof Error ? error.message : `${error}`;\n return { res: { code: 1, message } };\n }\n },\n serviceOptions,\n );\n};\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { parseOHLCServiceMetadataFromSchema } from './ohlc';
|
|
2
|
+
describe('parseOHLCServiceMetadataFromSchema', () => {
|
|
3
|
+
it('parses OHLC service schema', () => {
|
|
4
|
+
const schema = {
|
|
5
|
+
type: 'object',
|
|
6
|
+
required: ['product_id', 'duration', 'direction', 'time'],
|
|
7
|
+
properties: {
|
|
8
|
+
product_id: { type: 'string', pattern: '^BINANCE/USDT-FUTURE' },
|
|
9
|
+
duration: { type: 'string', enum: ['PT1M', 'PT5M'] },
|
|
10
|
+
direction: { const: 'backward' },
|
|
11
|
+
time: { type: 'string', format: 'date-time' },
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
const meta = parseOHLCServiceMetadataFromSchema(schema);
|
|
15
|
+
expect(meta).toEqual({
|
|
16
|
+
product_id_prefix: 'BINANCE/USDT-FUTURE',
|
|
17
|
+
duration_list: ['PT1M', 'PT5M'],
|
|
18
|
+
direction: 'backward',
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
it('throws when schema missing/invalid', () => {
|
|
22
|
+
expect(() => parseOHLCServiceMetadataFromSchema(undefined)).toThrow('OHLC_SERVICE_SCHEMA_MISSING');
|
|
23
|
+
expect(() => parseOHLCServiceMetadataFromSchema({})).toThrow('OHLC_SERVICE_SCHEMA_INVALID');
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
//# sourceMappingURL=ohlc.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ohlc.test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ataFromSchema } from './ohlc';\n\ndescribe('parseOHLCServiceMetadataFromSchema', () => {\n it('parses OHLC service schema', () => {\n const schema = {\n type: 'object',\n required: ['product_id', 'duration', 'direction', 'time'],\n properties: {\n product_id: { type: 'string', pattern: '^BINANCE/USDT-FUTURE' },\n duration: { type: 'string', enum: ['PT1M', 'PT5M'] },\n direction: { const: 'backward' },\n time: { type: 'string', format: 'date-time' },\n },\n };\n\n const meta = parseOHLCServiceMetadataFromSchema(schema);\n expect(meta).toEqual({\n product_id_prefix: 'BINANCE/USDT-FUTURE',\n duration_list: ['PT1M', 'PT5M'],\n direction: 'backward',\n });\n });\n\n it('throws when schema missing/invalid', () => {\n expect(() => parseOHLCServiceMetadataFromSchema(undefined)).toThrow('OHLC_SERVICE_SCHEMA_MISSING');\n expect(() => parseOHLCServiceMetadataFromSchema({})).toThrow('OHLC_SERVICE_SCHEMA_INVALID');\n });\n});\n"]}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { parseQuoteServiceMetadataFromSchema } from './quote';
|
|
2
|
+
describe('parseQuoteServiceMetadataFromSchema', () => {
|
|
3
|
+
it('parses quote service schema', () => {
|
|
4
|
+
const schema = {
|
|
5
|
+
type: 'object',
|
|
6
|
+
required: ['product_ids', 'fields'],
|
|
7
|
+
properties: {
|
|
8
|
+
product_ids: {
|
|
9
|
+
type: 'array',
|
|
10
|
+
maxItems: 20,
|
|
11
|
+
items: { type: 'string', pattern: '^BINANCE/USDT-FUTURE' },
|
|
12
|
+
},
|
|
13
|
+
fields: {
|
|
14
|
+
type: 'array',
|
|
15
|
+
const: ['last_price', 'bid_price'],
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
const meta = parseQuoteServiceMetadataFromSchema(schema);
|
|
20
|
+
expect(meta).toEqual({
|
|
21
|
+
product_id_prefix: 'BINANCE/USDT-FUTURE',
|
|
22
|
+
fields: ['last_price', 'bid_price'],
|
|
23
|
+
max_products_per_request: 20,
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
it('throws when schema missing/invalid', () => {
|
|
27
|
+
expect(() => parseQuoteServiceMetadataFromSchema(undefined)).toThrow('QUOTE_SERVICE_SCHEMA_MISSING');
|
|
28
|
+
expect(() => parseQuoteServiceMetadataFromSchema({})).toThrow('QUOTE_SERVICE_SCHEMA_INVALID');
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
//# sourceMappingURL=quote.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quote.test.js","sourceRoot":"","sources":["../src/quote.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mCAAmC,EAAE,MAAM,SAAS,CAAC;AAE9D,QAAQ,CAAC,qCAAqC,EAAE,GAAG,EAAE;IACnD,EAAE,CAAC,6BAA6B,EAAE,GAAG,EAAE;QACrC,MAAM,MAAM,GAAG;YACb,IAAI,EAAE,QAAQ;YACd,QAAQ,EAAE,CAAC,aAAa,EAAE,QAAQ,CAAC;YACnC,UAAU,EAAE;gBACV,WAAW,EAAE;oBACX,IAAI,EAAE,OAAO;oBACb,QAAQ,EAAE,EAAE;oBACZ,KAAK,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,OAAO,EAAE,sBAAsB,EAAE;iBAC3D;gBACD,MAAM,EAAE;oBACN,IAAI,EAAE,OAAO;oBACb,KAAK,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC;iBACnC;aACF;SACF,CAAC;QAEF,MAAM,IAAI,GAAG,mCAAmC,CAAC,MAAM,CAAC,CAAC;QACzD,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC;YACnB,iBAAiB,EAAE,qBAAqB;YACxC,MAAM,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC;YACnC,wBAAwB,EAAE,EAAE;SAC7B,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,oCAAoC,EAAE,GAAG,EAAE;QAC5C,MAAM,CAAC,GAAG,EAAE,CAAC,mCAAmC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC;QACrG,MAAM,CAAC,GAAG,EAAE,CAAC,mCAAmC,CAAC,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,8BAA8B,CAAC,CAAC;IAChG,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import { parseQuoteServiceMetadataFromSchema } from './quote';\n\ndescribe('parseQuoteServiceMetadataFromSchema', () => {\n it('parses quote service schema', () => {\n const schema = {\n type: 'object',\n required: ['product_ids', 'fields'],\n properties: {\n product_ids: {\n type: 'array',\n maxItems: 20,\n items: { type: 'string', pattern: '^BINANCE/USDT-FUTURE' },\n },\n fields: {\n type: 'array',\n const: ['last_price', 'bid_price'],\n },\n },\n };\n\n const meta = parseQuoteServiceMetadataFromSchema(schema);\n expect(meta).toEqual({\n product_id_prefix: 'BINANCE/USDT-FUTURE',\n fields: ['last_price', 'bid_price'],\n max_products_per_request: 20,\n });\n });\n\n it('throws when schema missing/invalid', () => {\n expect(() => parseQuoteServiceMetadataFromSchema(undefined)).toThrow('QUOTE_SERVICE_SCHEMA_MISSING');\n expect(() => parseQuoteServiceMetadataFromSchema({})).toThrow('QUOTE_SERVICE_SCHEMA_INVALID');\n });\n});\n"]}
|