hft-js
Version:
High-Frequency Trading in Node.js
56 lines • 1.47 kB
JavaScript
/*
* provider.ts
*
* Copyright (c) 2025 Xiongfei Shi
*
* Author: Xiongfei Shi <xiongfei.shi(a)icloud.com>
* License: Apache-2.0
*
* https://github.com/shixiongfei/hft.js
*/
import fs from "node:fs";
import ctp, {} from "napi-ctp";
export class CTPProvider {
flowPath;
frontAddrs;
constructor(flowPath, frontAddrs) {
this.flowPath = flowPath;
this.frontAddrs = frontAddrs;
try {
fs.accessSync(this.flowPath);
}
catch {
fs.mkdirSync(this.flowPath, { recursive: true });
}
}
_sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async _withRetry(request, ms = 100) {
for (;;) {
const retval = request();
if (retval === 0) {
return ctp.getLastRequestId();
}
if (-2 === retval || -3 === retval) {
await this._sleep(ms);
continue;
}
return retval;
}
}
_isErrorResp(lifecycle, options, error) {
if (!options.rspInfo) {
return false;
}
lifecycle.onError(error, `${options.rspInfo.ErrorID}:${options.rspInfo.ErrorMsg}`);
return true;
}
_parseTime(time) {
const [hh = 0, mm = 0, ss = 0] = time.split(":").map((x) => parseInt(x));
return hh * 10000 + mm * 100 + ss;
}
}
//# sourceMappingURL=provider.js.map