@hitrading/ctp-node 1.0.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/LICENSE +201 -0
- package/README.md +208 -0
- package/README.zh-CN.md +185 -0
- package/binding.gyp +90 -0
- package/dist/client.d.ts +115 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +466 -0
- package/dist/client.js.map +1 -0
- package/dist/codec.d.ts +29 -0
- package/dist/codec.d.ts.map +1 -0
- package/dist/codec.js +131 -0
- package/dist/codec.js.map +1 -0
- package/dist/generated/enums.gen.d.ts +2257 -0
- package/dist/generated/enums.gen.d.ts.map +1 -0
- package/dist/generated/enums.gen.js +2576 -0
- package/dist/generated/enums.gen.js.map +1 -0
- package/dist/generated/structs.gen.d.ts +11041 -0
- package/dist/generated/structs.gen.d.ts.map +1 -0
- package/dist/generated/structs.gen.js +939 -0
- package/dist/generated/structs.gen.js.map +1 -0
- package/dist/generated/trader.gen.d.ts +273 -0
- package/dist/generated/trader.gen.d.ts.map +1 -0
- package/dist/generated/trader.gen.js +653 -0
- package/dist/generated/trader.gen.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/market-data.d.ts +66 -0
- package/dist/market-data.d.ts.map +1 -0
- package/dist/market-data.js +105 -0
- package/dist/market-data.js.map +1 -0
- package/dist/native-binding.d.ts +2 -0
- package/dist/native-binding.d.ts.map +1 -0
- package/dist/native-binding.js +22 -0
- package/dist/native-binding.js.map +1 -0
- package/dist/trader.d.ts +234 -0
- package/dist/trader.d.ts.map +1 -0
- package/dist/trader.js +388 -0
- package/dist/trader.js.map +1 -0
- package/package.json +69 -0
- package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
- package/prebuilds/linux-x64/thostmduserapi_se.so +0 -0
- package/prebuilds/linux-x64/thosttraderapi_se.so +0 -0
- package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
- package/prebuilds/win32-x64/thostmduserapi_se.dll +0 -0
- package/prebuilds/win32-x64/thosttraderapi_se.dll +0 -0
- package/src/addon.cc +171 -0
- package/src/client.ts +539 -0
- package/src/codec.ts +160 -0
- package/src/generated/enums.gen.ts +2576 -0
- package/src/generated/layout.gen.cc +5771 -0
- package/src/generated/layout.gen.h +8 -0
- package/src/generated/structids.gen.h +470 -0
- package/src/generated/structs.gen.ts +12437 -0
- package/src/generated/trader.gen.ts +656 -0
- package/src/generated/traderreq.gen.cc +706 -0
- package/src/generated/traderreq.gen.h +127 -0
- package/src/generated/traderspi.gen.cc +636 -0
- package/src/generated/traderspi.gen.h +334 -0
- package/src/index.ts +14 -0
- package/src/market-data.ts +145 -0
- package/src/native/arm.cc +77 -0
- package/src/native/arm.h +71 -0
- package/src/native/channel.cc +175 -0
- package/src/native/channel.h +117 -0
- package/src/native/gbk.cc +54 -0
- package/src/native/gbk.h +17 -0
- package/src/native/mdapi.cc +412 -0
- package/src/native/mdapi.h +14 -0
- package/src/native/mdspi.cc +103 -0
- package/src/native/mdspi.h +81 -0
- package/src/native/risk.cc +461 -0
- package/src/native/risk.h +183 -0
- package/src/native/traderapi.cc +533 -0
- package/src/native/traderapi.h +14 -0
- package/src/native-binding.ts +25 -0
- package/src/trader.ts +522 -0
- package/tradeapi/ThostFtdcMdApi.h +168 -0
- package/tradeapi/ThostFtdcTraderApi.h +914 -0
- package/tradeapi/ThostFtdcUserApiDataType.h +7068 -0
- package/tradeapi/ThostFtdcUserApiStruct.h +12386 -0
- package/tradeapi/error.dtd +9 -0
- package/tradeapi/error.xml +284 -0
- package/tradeapi/linux/thostmduserapi_se.so +0 -0
- package/tradeapi/linux/thosttraderapi_se.so +0 -0
- package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcMdApi.h +168 -0
- package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +7068 -0
- package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +12386 -0
- package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Resources/Info.plist +46 -0
- package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/_CodeSignature/CodeResources +149 -0
- package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/thostmduserapi_se +0 -0
- package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcTraderApi.h +914 -0
- package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +7068 -0
- package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +12386 -0
- package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Resources/Info.plist +46 -0
- package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/_CodeSignature/CodeResources +149 -0
- package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/thosttraderapi_se +0 -0
- package/tradeapi/windows/thostmduserapi_se.dll +0 -0
- package/tradeapi/windows/thostmduserapi_se.lib +0 -0
- package/tradeapi/windows/thosttraderapi_se.dll +0 -0
- package/tradeapi/windows/thosttraderapi_se.lib +0 -0
|
@@ -0,0 +1,533 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* traderapi.cc - Trader N-API class (ObjectWrap).
|
|
3
|
+
*
|
|
4
|
+
* All requests funnel through _req(methodId, bytes, requestId) -> the generated
|
|
5
|
+
* traderReq() switch, which builds the CTP struct from the JS-encoded bytes and
|
|
6
|
+
* calls the matching ReqXxx. Pre-trade risk (kill-switch / max volume / max
|
|
7
|
+
* notional / rate limit) is enforced in C++ on the order-insert path.
|
|
8
|
+
*
|
|
9
|
+
* Init() is deferred to _start() (after the JS drain is wired) so the
|
|
10
|
+
* front-connected event can't be missed.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
#include "traderapi.h"
|
|
14
|
+
|
|
15
|
+
#include <algorithm>
|
|
16
|
+
#include <atomic>
|
|
17
|
+
#include <cstring>
|
|
18
|
+
#include <memory>
|
|
19
|
+
#include <string>
|
|
20
|
+
#include <vector>
|
|
21
|
+
|
|
22
|
+
#include "../generated/layout.gen.h"
|
|
23
|
+
#include "../generated/traderreq.gen.h"
|
|
24
|
+
#include "../generated/traderspi.gen.h"
|
|
25
|
+
#include "ThostFtdcTraderApi.h"
|
|
26
|
+
#include "arm.h"
|
|
27
|
+
#include "channel.h"
|
|
28
|
+
#include "risk.h"
|
|
29
|
+
|
|
30
|
+
namespace ctp {
|
|
31
|
+
|
|
32
|
+
namespace {
|
|
33
|
+
|
|
34
|
+
std::vector<std::string> toStringList(const Napi::Value &v) {
|
|
35
|
+
std::vector<std::string> out;
|
|
36
|
+
if (v.IsArray()) {
|
|
37
|
+
Napi::Array a = v.As<Napi::Array>();
|
|
38
|
+
for (uint32_t i = 0; i < a.Length(); ++i)
|
|
39
|
+
out.push_back(a.Get(i).As<Napi::String>().Utf8Value());
|
|
40
|
+
} else if (v.IsString()) {
|
|
41
|
+
out.push_back(v.As<Napi::String>().Utf8Value());
|
|
42
|
+
}
|
|
43
|
+
return out;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
int getInt(const Napi::Object &o, const char *k, int d) {
|
|
47
|
+
if (o.Has(k)) {
|
|
48
|
+
Napi::Value v = o.Get(k);
|
|
49
|
+
if (v.IsNumber())
|
|
50
|
+
return v.As<Napi::Number>().Int32Value();
|
|
51
|
+
}
|
|
52
|
+
return d;
|
|
53
|
+
}
|
|
54
|
+
double getDbl(const Napi::Object &o, const char *k, double d) {
|
|
55
|
+
if (o.Has(k)) {
|
|
56
|
+
Napi::Value v = o.Get(k);
|
|
57
|
+
if (v.IsNumber())
|
|
58
|
+
return v.As<Napi::Number>().DoubleValue();
|
|
59
|
+
}
|
|
60
|
+
return d;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
} // namespace
|
|
64
|
+
|
|
65
|
+
class Trader : public Napi::ObjectWrap<Trader>, public OrderSink {
|
|
66
|
+
public:
|
|
67
|
+
static Napi::Function Init(Napi::Env env);
|
|
68
|
+
Trader(const Napi::CallbackInfo &info);
|
|
69
|
+
~Trader();
|
|
70
|
+
|
|
71
|
+
// OrderSink: called on the MD callback thread when an armed trigger hits.
|
|
72
|
+
int fireArmed(const ArmSpec &spec) override;
|
|
73
|
+
|
|
74
|
+
private:
|
|
75
|
+
void doClose();
|
|
76
|
+
|
|
77
|
+
Napi::Value GetApiVersion(const Napi::CallbackInfo &info);
|
|
78
|
+
Napi::Value GetTradingDay(const Napi::CallbackInfo &info);
|
|
79
|
+
Napi::Value Req(const Napi::CallbackInfo &info);
|
|
80
|
+
Napi::Value RiskSet(const Napi::CallbackInfo &info);
|
|
81
|
+
Napi::Value RiskHalt(const Napi::CallbackInfo &info);
|
|
82
|
+
Napi::Value RiskResume(const Napi::CallbackInfo &info);
|
|
83
|
+
Napi::Value LastRiskReason(const Napi::CallbackInfo &info);
|
|
84
|
+
Napi::Value SetRefPrice(const Napi::CallbackInfo &info);
|
|
85
|
+
Napi::Value SetMultiplier(const Napi::CallbackInfo &info);
|
|
86
|
+
Napi::Value SetMaxPositionVolume(const Napi::CallbackInfo &info);
|
|
87
|
+
Napi::Value SetMaxInstrumentCost(const Napi::CallbackInfo &info);
|
|
88
|
+
Napi::Value SetSession(const Napi::CallbackInfo &info);
|
|
89
|
+
Napi::Value RebuildReservations(const Napi::CallbackInfo &info);
|
|
90
|
+
Napi::Value SeedPosition(const Napi::CallbackInfo &info);
|
|
91
|
+
Napi::Value PositionCost(const Napi::CallbackInfo &info);
|
|
92
|
+
Napi::Value ResetPositions(const Napi::CallbackInfo &info);
|
|
93
|
+
Napi::Value ApplyTestTrade(const Napi::CallbackInfo &info);
|
|
94
|
+
Napi::Value ApplyTestOrder(const Napi::CallbackInfo &info);
|
|
95
|
+
Napi::Value Start(const Napi::CallbackInfo &info);
|
|
96
|
+
Napi::Value Buffer(const Napi::CallbackInfo &info);
|
|
97
|
+
Napi::Value ClaimBatch(const Napi::CallbackInfo &info);
|
|
98
|
+
Napi::Value ReleaseBatch(const Napi::CallbackInfo &info);
|
|
99
|
+
Napi::Value DropCount(const Napi::CallbackInfo &info);
|
|
100
|
+
Napi::Value ChannelInfo(const Napi::CallbackInfo &info);
|
|
101
|
+
Napi::Value Close(const Napi::CallbackInfo &info);
|
|
102
|
+
Napi::Value ArmRegistryExternal(const Napi::CallbackInfo &info);
|
|
103
|
+
Napi::Value Arm(const Napi::CallbackInfo &info);
|
|
104
|
+
Napi::Value Disarm(const Napi::CallbackInfo &info);
|
|
105
|
+
Napi::Value ArmFireCount(const Napi::CallbackInfo &info);
|
|
106
|
+
Napi::Value ArmBlockedCount(const Napi::CallbackInfo &info);
|
|
107
|
+
|
|
108
|
+
CThostFtdcTraderApi *api_ = nullptr;
|
|
109
|
+
TraderSpi *spi_ = nullptr;
|
|
110
|
+
EventChannel *ch_ = nullptr;
|
|
111
|
+
RiskEngine risk_;
|
|
112
|
+
std::shared_ptr<ArmRegistry> arm_ = std::make_shared<ArmRegistry>();
|
|
113
|
+
std::atomic<int> armReqId_{1000000};
|
|
114
|
+
bool started_ = false;
|
|
115
|
+
bool closed_ = false;
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
Napi::Function Trader::Init(Napi::Env env) {
|
|
119
|
+
return DefineClass(
|
|
120
|
+
env, "Trader",
|
|
121
|
+
{
|
|
122
|
+
InstanceMethod("getApiVersion", &Trader::GetApiVersion),
|
|
123
|
+
InstanceMethod("getTradingDay", &Trader::GetTradingDay),
|
|
124
|
+
InstanceMethod("_req", &Trader::Req),
|
|
125
|
+
InstanceMethod("riskSet", &Trader::RiskSet),
|
|
126
|
+
InstanceMethod("riskHalt", &Trader::RiskHalt),
|
|
127
|
+
InstanceMethod("riskResume", &Trader::RiskResume),
|
|
128
|
+
InstanceMethod("lastRiskReason", &Trader::LastRiskReason),
|
|
129
|
+
InstanceMethod("setRefPrice", &Trader::SetRefPrice),
|
|
130
|
+
InstanceMethod("setMultiplier", &Trader::SetMultiplier),
|
|
131
|
+
InstanceMethod("setMaxPositionVolume", &Trader::SetMaxPositionVolume),
|
|
132
|
+
InstanceMethod("setMaxInstrumentCost", &Trader::SetMaxInstrumentCost),
|
|
133
|
+
InstanceMethod("setSession", &Trader::SetSession),
|
|
134
|
+
InstanceMethod("rebuildReservations", &Trader::RebuildReservations),
|
|
135
|
+
InstanceMethod("seedPosition", &Trader::SeedPosition),
|
|
136
|
+
InstanceMethod("positionCost", &Trader::PositionCost),
|
|
137
|
+
InstanceMethod("resetPositions", &Trader::ResetPositions),
|
|
138
|
+
InstanceMethod("_applyTestTrade", &Trader::ApplyTestTrade),
|
|
139
|
+
InstanceMethod("_applyTestOrder", &Trader::ApplyTestOrder),
|
|
140
|
+
InstanceMethod("_start", &Trader::Start),
|
|
141
|
+
InstanceMethod("_buffer", &Trader::Buffer),
|
|
142
|
+
InstanceMethod("_claim", &Trader::ClaimBatch),
|
|
143
|
+
InstanceMethod("_release", &Trader::ReleaseBatch),
|
|
144
|
+
InstanceMethod("_dropCount", &Trader::DropCount),
|
|
145
|
+
InstanceMethod("_info", &Trader::ChannelInfo),
|
|
146
|
+
InstanceMethod("_armRegistry", &Trader::ArmRegistryExternal),
|
|
147
|
+
InstanceMethod("arm", &Trader::Arm),
|
|
148
|
+
InstanceMethod("disarm", &Trader::Disarm),
|
|
149
|
+
InstanceMethod("_armFireCount", &Trader::ArmFireCount),
|
|
150
|
+
InstanceMethod("_armBlockedCount", &Trader::ArmBlockedCount),
|
|
151
|
+
InstanceMethod("close", &Trader::Close),
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
Trader::Trader(const Napi::CallbackInfo &info) : Napi::ObjectWrap<Trader>(info) {
|
|
156
|
+
Napi::Env env = info.Env();
|
|
157
|
+
if (info.Length() < 2 || !info[0].IsString()) {
|
|
158
|
+
Napi::TypeError::New(env, "Trader(flowPath, fronts)")
|
|
159
|
+
.ThrowAsJavaScriptException();
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
std::string flowPath = info[0].As<Napi::String>().Utf8Value();
|
|
163
|
+
|
|
164
|
+
// Validate fronts BEFORE allocating anything: RegisterFront("") faults deep
|
|
165
|
+
// inside the CTP DLL, so drop empty addresses and reject an empty result with
|
|
166
|
+
// a clean JS error rather than crashing the process.
|
|
167
|
+
std::vector<std::string> fronts;
|
|
168
|
+
for (auto &addr : toStringList(info[1]))
|
|
169
|
+
if (!addr.empty())
|
|
170
|
+
fronts.push_back(addr);
|
|
171
|
+
if (fronts.empty()) {
|
|
172
|
+
Napi::TypeError::New(
|
|
173
|
+
env, "Trader: at least one non-empty front address is required")
|
|
174
|
+
.ThrowAsJavaScriptException();
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
api_ = CThostFtdcTraderApi::CreateFtdcTraderApi(flowPath.c_str());
|
|
179
|
+
if (!api_) {
|
|
180
|
+
Napi::Error::New(env, "CreateFtdcTraderApi failed")
|
|
181
|
+
.ThrowAsJavaScriptException();
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
// Trader uses DropPolicy::Newest (reliable): order/trade returns must never be
|
|
185
|
+
// silently discarded under backpressure.
|
|
186
|
+
ch_ = new EventChannel(4096, static_cast<size_t>(ctpMaxStructSize()),
|
|
187
|
+
DropPolicy::Newest);
|
|
188
|
+
spi_ = new TraderSpi(api_, ch_);
|
|
189
|
+
spi_->setRisk(&risk_); // fills update the position-cost tracker
|
|
190
|
+
api_->RegisterSpi(spi_);
|
|
191
|
+
arm_->setSink(this); // armed triggers fire orders through this Trader
|
|
192
|
+
api_->SubscribePublicTopic(THOST_TERT_QUICK);
|
|
193
|
+
api_->SubscribePrivateTopic(THOST_TERT_QUICK);
|
|
194
|
+
|
|
195
|
+
for (auto &addr : fronts)
|
|
196
|
+
api_->RegisterFront(const_cast<char *>(addr.c_str()));
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
Trader::~Trader() { doClose(); }
|
|
200
|
+
|
|
201
|
+
void Trader::doClose() {
|
|
202
|
+
if (closed_)
|
|
203
|
+
return;
|
|
204
|
+
closed_ = true;
|
|
205
|
+
// Order matters: clearSink() (under the arm lock) guarantees no fireArmed is
|
|
206
|
+
// running or will start, so the non-atomic api_ below is never read after it
|
|
207
|
+
// is nulled. Do NOT move the api_ teardown before clearSink().
|
|
208
|
+
arm_->clearSink();
|
|
209
|
+
if (api_) {
|
|
210
|
+
api_->Release();
|
|
211
|
+
api_ = nullptr;
|
|
212
|
+
}
|
|
213
|
+
// Free the SPI and the ring here, on close(), not in the GC finalizer: the
|
|
214
|
+
// ring (~maxStructSize * 4096 slots, ~10 MB) is off-heap so V8 feels no GC
|
|
215
|
+
// pressure from it, and a create/close loop leaks it (measured unreclaimed
|
|
216
|
+
// even by a forced GC) until finalizers eventually run. Release() above
|
|
217
|
+
// stopped the CTP callback thread, so no more pushes; accessors null-check.
|
|
218
|
+
if (spi_) {
|
|
219
|
+
delete spi_;
|
|
220
|
+
spi_ = nullptr;
|
|
221
|
+
}
|
|
222
|
+
if (ch_) {
|
|
223
|
+
ch_->stop();
|
|
224
|
+
delete ch_;
|
|
225
|
+
ch_ = nullptr;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
int Trader::fireArmed(const ArmSpec &spec) {
|
|
230
|
+
CThostFtdcTraderApi *api = api_;
|
|
231
|
+
if (!api)
|
|
232
|
+
return -1;
|
|
233
|
+
CThostFtdcInputOrderField f;
|
|
234
|
+
std::memset(&f, 0, sizeof(f));
|
|
235
|
+
std::memcpy(&f, spec.orderTemplate.data(),
|
|
236
|
+
std::min(spec.orderTemplate.size(), sizeof(f)));
|
|
237
|
+
// Deviation check is intentionally skipped (refPrice 0) for armed orders:
|
|
238
|
+
// they fire on fast moves where the limit can legitimately differ from the
|
|
239
|
+
// last print, and silently blocking the armed order would be worse. Volume,
|
|
240
|
+
// notional (multiplier-aware), kill-switch and rate limit still apply.
|
|
241
|
+
RiskVerdict v = risk_.check(f.InstrumentID, f.LimitPrice, 0.0,
|
|
242
|
+
f.VolumeTotalOriginal);
|
|
243
|
+
if (!v.ok)
|
|
244
|
+
return CTP_RISK_BLOCKED;
|
|
245
|
+
// Position caps (incl. in-flight reservation) apply to armed orders too -
|
|
246
|
+
// safety, not latency-sensitive.
|
|
247
|
+
const bool isOpen = (f.CombOffsetFlag[0] == '0');
|
|
248
|
+
if (isOpen) {
|
|
249
|
+
OpenGate g = risk_.tryReserveOpen(f.OrderRef, f.InstrumentID,
|
|
250
|
+
f.Direction == '0', f.LimitPrice,
|
|
251
|
+
f.VolumeTotalOriginal);
|
|
252
|
+
if (g == OpenGate::VolumeLimit)
|
|
253
|
+
return CTP_POSITION_VOLUME_LIMIT;
|
|
254
|
+
if (g == OpenGate::CostLimit)
|
|
255
|
+
return CTP_POSITION_LIMIT;
|
|
256
|
+
}
|
|
257
|
+
if (!risk_.allowRate()) {
|
|
258
|
+
if (isOpen)
|
|
259
|
+
risk_.releaseReservation(f.OrderRef);
|
|
260
|
+
return CTP_RATE_LIMITED;
|
|
261
|
+
}
|
|
262
|
+
int sendRc = api->ReqOrderInsert(&f, armReqId_.fetch_add(1));
|
|
263
|
+
if (sendRc != 0 && isOpen)
|
|
264
|
+
risk_.releaseReservation(f.OrderRef); // send failed -> release the reservation
|
|
265
|
+
return sendRc;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
Napi::Value Trader::ArmRegistryExternal(const Napi::CallbackInfo &info) {
|
|
269
|
+
auto *sp = new std::shared_ptr<ArmRegistry>(arm_);
|
|
270
|
+
return Napi::External<std::shared_ptr<ArmRegistry>>::New(
|
|
271
|
+
info.Env(), sp,
|
|
272
|
+
[](Napi::Env, std::shared_ptr<ArmRegistry> *p) { delete p; });
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
Napi::Value Trader::Arm(const Napi::CallbackInfo &info) {
|
|
276
|
+
Napi::Env env = info.Env();
|
|
277
|
+
ArmSpec spec;
|
|
278
|
+
spec.instrumentId = info[0].As<Napi::String>().Utf8Value();
|
|
279
|
+
std::string side = info[1].As<Napi::String>().Utf8Value();
|
|
280
|
+
spec.side = side.empty() ? '0' : side[0];
|
|
281
|
+
spec.triggerPrice = info[2].As<Napi::Number>().DoubleValue();
|
|
282
|
+
Napi::Uint8Array bytes = info[3].As<Napi::Uint8Array>();
|
|
283
|
+
spec.orderTemplate.assign(bytes.Data(), bytes.Data() + bytes.ByteLength());
|
|
284
|
+
uint64_t id = arm_->arm(std::move(spec));
|
|
285
|
+
return Napi::Number::New(env, static_cast<double>(id));
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
Napi::Value Trader::Disarm(const Napi::CallbackInfo &info) {
|
|
289
|
+
uint64_t id = static_cast<uint64_t>(info[0].As<Napi::Number>().DoubleValue());
|
|
290
|
+
return Napi::Boolean::New(info.Env(), arm_->disarm(id));
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
Napi::Value Trader::ArmBlockedCount(const Napi::CallbackInfo &info) {
|
|
294
|
+
return Napi::Number::New(info.Env(),
|
|
295
|
+
static_cast<double>(arm_->blockedCount()));
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
Napi::Value Trader::ArmFireCount(const Napi::CallbackInfo &info) {
|
|
299
|
+
return Napi::Number::New(info.Env(),
|
|
300
|
+
static_cast<double>(arm_->fireCount()));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
Napi::Value Trader::GetApiVersion(const Napi::CallbackInfo &info) {
|
|
304
|
+
return Napi::String::New(info.Env(), CThostFtdcTraderApi::GetApiVersion());
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
Napi::Value Trader::GetTradingDay(const Napi::CallbackInfo &info) {
|
|
308
|
+
Napi::Env env = info.Env();
|
|
309
|
+
if (!api_)
|
|
310
|
+
return env.Null();
|
|
311
|
+
const char *d = api_->GetTradingDay();
|
|
312
|
+
return Napi::String::New(env, d ? d : "");
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
Napi::Value Trader::Req(const Napi::CallbackInfo &info) {
|
|
316
|
+
Napi::Env env = info.Env();
|
|
317
|
+
if (!api_)
|
|
318
|
+
return Napi::Number::New(env, -1);
|
|
319
|
+
int methodId = info[0].As<Napi::Number>().Int32Value();
|
|
320
|
+
Napi::Uint8Array bytes = info[1].As<Napi::Uint8Array>();
|
|
321
|
+
int id = info[2].As<Napi::Number>().Int32Value();
|
|
322
|
+
int rc = traderReq(api_, methodId, bytes.Data(),
|
|
323
|
+
static_cast<int>(bytes.ByteLength()), id, &risk_);
|
|
324
|
+
return Napi::Number::New(env, rc);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
Napi::Value Trader::RiskSet(const Napi::CallbackInfo &info) {
|
|
328
|
+
if (info[0].IsObject()) {
|
|
329
|
+
Napi::Object o = info[0].As<Napi::Object>();
|
|
330
|
+
RiskConfig cfg;
|
|
331
|
+
cfg.maxOrderVolume = getInt(o, "maxOrderVolume", 0);
|
|
332
|
+
cfg.maxPriceDeviation = getDbl(o, "maxPriceDeviation", 0.0);
|
|
333
|
+
cfg.maxNotional = getDbl(o, "maxNotional", 0.0);
|
|
334
|
+
cfg.maxOrdersPerSec = getDbl(o, "maxOrdersPerSec", 0.0);
|
|
335
|
+
cfg.orderBurst = getDbl(o, "orderBurst", 0.0);
|
|
336
|
+
cfg.maxPositionCost = getDbl(o, "maxPositionCost", 0.0);
|
|
337
|
+
risk_.configure(cfg);
|
|
338
|
+
}
|
|
339
|
+
return info.Env().Undefined();
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
Napi::Value Trader::RiskHalt(const Napi::CallbackInfo &info) {
|
|
343
|
+
risk_.halt();
|
|
344
|
+
return info.Env().Undefined();
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
Napi::Value Trader::RiskResume(const Napi::CallbackInfo &info) {
|
|
348
|
+
risk_.resume();
|
|
349
|
+
return info.Env().Undefined();
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Reason string of the most recent pre-trade-risk (check) rejection, "" if none.
|
|
353
|
+
Napi::Value Trader::LastRiskReason(const Napi::CallbackInfo &info) {
|
|
354
|
+
const char *r = risk_.lastBlockReason();
|
|
355
|
+
return Napi::String::New(info.Env(), r ? r : "");
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
Napi::Value Trader::SetRefPrice(const Napi::CallbackInfo &info) {
|
|
359
|
+
if (info.Length() >= 2 && info[0].IsString() && info[1].IsNumber()) {
|
|
360
|
+
risk_.setRefPrice(info[0].As<Napi::String>().Utf8Value(),
|
|
361
|
+
info[1].As<Napi::Number>().DoubleValue());
|
|
362
|
+
}
|
|
363
|
+
return info.Env().Undefined();
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
Napi::Value Trader::SetMultiplier(const Napi::CallbackInfo &info) {
|
|
367
|
+
if (info.Length() >= 2 && info[0].IsString() && info[1].IsNumber()) {
|
|
368
|
+
risk_.setMultiplier(info[0].As<Napi::String>().Utf8Value(),
|
|
369
|
+
info[1].As<Napi::Number>().DoubleValue());
|
|
370
|
+
}
|
|
371
|
+
return info.Env().Undefined();
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
// (instrumentId, isLong, maxVolume) - per-side open-position lot cap; 0 = off.
|
|
375
|
+
Napi::Value Trader::SetMaxPositionVolume(const Napi::CallbackInfo &info) {
|
|
376
|
+
if (info.Length() >= 3 && info[0].IsString() && info[2].IsNumber()) {
|
|
377
|
+
risk_.setMaxPositionVolume(info[0].As<Napi::String>().Utf8Value(),
|
|
378
|
+
info[1].ToBoolean().Value(),
|
|
379
|
+
info[2].As<Napi::Number>().DoubleValue());
|
|
380
|
+
}
|
|
381
|
+
return info.Env().Undefined();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
// (instrumentId, maxCost) - per-instrument open-position cost cap; 0 = off.
|
|
385
|
+
Napi::Value Trader::SetMaxInstrumentCost(const Napi::CallbackInfo &info) {
|
|
386
|
+
if (info.Length() >= 2 && info[0].IsString() && info[1].IsNumber()) {
|
|
387
|
+
risk_.setMaxInstrumentCost(info[0].As<Napi::String>().Utf8Value(),
|
|
388
|
+
info[1].As<Napi::Number>().DoubleValue());
|
|
389
|
+
}
|
|
390
|
+
return info.Env().Undefined();
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
// (frontId, sessionId) - our login session, for reservation keys.
|
|
394
|
+
Napi::Value Trader::SetSession(const Napi::CallbackInfo &info) {
|
|
395
|
+
if (info.Length() >= 2 && info[0].IsNumber() && info[1].IsNumber()) {
|
|
396
|
+
risk_.setSession(info[0].As<Napi::Number>().Int32Value(),
|
|
397
|
+
info[1].As<Napi::Number>().Int32Value());
|
|
398
|
+
}
|
|
399
|
+
return info.Env().Undefined();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// (Array<{frontId, sessionId, orderRef, instrumentId, isLong, vol, price}>) -
|
|
403
|
+
// rebuild in-flight reservations from CTP's working orders (reqQryOrder).
|
|
404
|
+
Napi::Value Trader::RebuildReservations(const Napi::CallbackInfo &info) {
|
|
405
|
+
std::vector<OpenOrderInfo> orders;
|
|
406
|
+
if (info.Length() >= 1 && info[0].IsArray()) {
|
|
407
|
+
Napi::Array arr = info[0].As<Napi::Array>();
|
|
408
|
+
for (uint32_t i = 0; i < arr.Length(); ++i) {
|
|
409
|
+
Napi::Value v = arr.Get(i);
|
|
410
|
+
if (!v.IsObject())
|
|
411
|
+
continue;
|
|
412
|
+
Napi::Object o = v.As<Napi::Object>();
|
|
413
|
+
OpenOrderInfo r;
|
|
414
|
+
r.frontId = o.Get("frontId").ToNumber().Int32Value();
|
|
415
|
+
r.sessionId = o.Get("sessionId").ToNumber().Int32Value();
|
|
416
|
+
r.orderRef = o.Get("orderRef").ToString().Utf8Value();
|
|
417
|
+
r.instrumentId = o.Get("instrumentId").ToString().Utf8Value();
|
|
418
|
+
r.isLong = o.Get("isLong").ToBoolean().Value();
|
|
419
|
+
r.vol = o.Get("vol").ToNumber().DoubleValue();
|
|
420
|
+
r.price = o.Get("price").ToNumber().DoubleValue();
|
|
421
|
+
orders.push_back(std::move(r));
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
risk_.rebuildOpenReservations(orders);
|
|
425
|
+
return info.Env().Undefined();
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
// (instrumentId, isLong, volume, openCost) - seed a pre-existing position.
|
|
429
|
+
Napi::Value Trader::SeedPosition(const Napi::CallbackInfo &info) {
|
|
430
|
+
if (info.Length() >= 4) {
|
|
431
|
+
risk_.seedPosition(info[0].As<Napi::String>().Utf8Value(),
|
|
432
|
+
info[1].ToBoolean().Value(),
|
|
433
|
+
info[2].As<Napi::Number>().DoubleValue(),
|
|
434
|
+
info[3].As<Napi::Number>().DoubleValue());
|
|
435
|
+
}
|
|
436
|
+
return info.Env().Undefined();
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
Napi::Value Trader::PositionCost(const Napi::CallbackInfo &info) {
|
|
440
|
+
return Napi::Number::New(info.Env(), risk_.currentPositionCost());
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
Napi::Value Trader::ResetPositions(const Napi::CallbackInfo &info) {
|
|
444
|
+
risk_.resetPositions();
|
|
445
|
+
return info.Env().Undefined();
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// test-only: (instrumentId, isBuy, isOpen, price, volume)
|
|
449
|
+
Napi::Value Trader::ApplyTestTrade(const Napi::CallbackInfo &info) {
|
|
450
|
+
if (info.Length() >= 5) {
|
|
451
|
+
risk_.onTrade(info[0].As<Napi::String>().Utf8Value(),
|
|
452
|
+
info[1].ToBoolean().Value(), info[2].ToBoolean().Value(),
|
|
453
|
+
info[3].As<Napi::Number>().DoubleValue(),
|
|
454
|
+
info[4].As<Napi::Number>().DoubleValue());
|
|
455
|
+
}
|
|
456
|
+
return info.Env().Undefined();
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
// (frontId, sessionId, orderRef, instrumentId, isOpen, isLong, status,
|
|
460
|
+
// limitPrice, volTotal, volTraded) - drive the reservation tracker (OnRtnOrder).
|
|
461
|
+
Napi::Value Trader::ApplyTestOrder(const Napi::CallbackInfo &info) {
|
|
462
|
+
if (info.Length() >= 10) {
|
|
463
|
+
std::string status = info[6].As<Napi::String>().Utf8Value();
|
|
464
|
+
risk_.onOrderUpdate(info[0].As<Napi::Number>().Int32Value(),
|
|
465
|
+
info[1].As<Napi::Number>().Int32Value(),
|
|
466
|
+
info[2].As<Napi::String>().Utf8Value(),
|
|
467
|
+
info[3].As<Napi::String>().Utf8Value(),
|
|
468
|
+
info[4].ToBoolean().Value(), info[5].ToBoolean().Value(),
|
|
469
|
+
status.empty() ? ' ' : status[0],
|
|
470
|
+
info[7].As<Napi::Number>().DoubleValue(),
|
|
471
|
+
info[8].As<Napi::Number>().DoubleValue(),
|
|
472
|
+
info[9].As<Napi::Number>().DoubleValue());
|
|
473
|
+
}
|
|
474
|
+
return info.Env().Undefined();
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
Napi::Value Trader::Start(const Napi::CallbackInfo &info) {
|
|
478
|
+
Napi::Env env = info.Env();
|
|
479
|
+
if (started_ || !api_)
|
|
480
|
+
return env.Undefined();
|
|
481
|
+
napi_status st = ch_->start(env, info[0]);
|
|
482
|
+
if (st != napi_ok) {
|
|
483
|
+
Napi::Error::New(env, "failed to start event channel")
|
|
484
|
+
.ThrowAsJavaScriptException();
|
|
485
|
+
return env.Undefined();
|
|
486
|
+
}
|
|
487
|
+
api_->Init();
|
|
488
|
+
started_ = true;
|
|
489
|
+
return env.Undefined();
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
Napi::Value Trader::Buffer(const Napi::CallbackInfo &info) {
|
|
493
|
+
if (!ch_)
|
|
494
|
+
return info.Env().Undefined();
|
|
495
|
+
return Napi::ArrayBuffer::New(info.Env(), ch_->data(), ch_->byteLength());
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
Napi::Value Trader::ClaimBatch(const Napi::CallbackInfo &info) {
|
|
499
|
+
return Napi::Number::New(info.Env(), ch_ ? ch_->claim() : 0);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
Napi::Value Trader::ReleaseBatch(const Napi::CallbackInfo &info) {
|
|
503
|
+
if (ch_)
|
|
504
|
+
ch_->release(info[0].As<Napi::Number>().Uint32Value());
|
|
505
|
+
return info.Env().Undefined();
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
Napi::Value Trader::DropCount(const Napi::CallbackInfo &info) {
|
|
509
|
+
return Napi::Number::New(info.Env(),
|
|
510
|
+
static_cast<double>(ch_ ? ch_->dropCount() : 0));
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
Napi::Value Trader::ChannelInfo(const Napi::CallbackInfo &info) {
|
|
514
|
+
Napi::Env env = info.Env();
|
|
515
|
+
Napi::Object o = Napi::Object::New(env);
|
|
516
|
+
const bool live = ch_ != nullptr;
|
|
517
|
+
o.Set("slotSize", static_cast<double>(live ? ch_->slotSize() : 0));
|
|
518
|
+
o.Set("numSlots", static_cast<double>(live ? ch_->numSlots() : 0));
|
|
519
|
+
o.Set("headerSize", static_cast<double>(live ? ch_->headerSize() : 0));
|
|
520
|
+
// Trader keeps DropPolicy::Newest: an order/trade return must never be silently
|
|
521
|
+
// dropped in favour of a newer record.
|
|
522
|
+
o.Set("dropOldest", false);
|
|
523
|
+
return o;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
Napi::Value Trader::Close(const Napi::CallbackInfo &info) {
|
|
527
|
+
doClose();
|
|
528
|
+
return info.Env().Undefined();
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
Napi::Function InitTrader(Napi::Env env) { return Trader::Init(env); }
|
|
532
|
+
|
|
533
|
+
} // namespace ctp
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* traderapi.h - exposes the Trader N-API class constructor.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
#ifndef CTP_NATIVE_TRADERAPI_H
|
|
6
|
+
#define CTP_NATIVE_TRADERAPI_H
|
|
7
|
+
|
|
8
|
+
#include <napi.h>
|
|
9
|
+
|
|
10
|
+
namespace ctp {
|
|
11
|
+
Napi::Function InitTrader(Napi::Env env);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
#endif /* CTP_NATIVE_TRADERAPI_H */
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* native-binding.ts - load the addon via node-gyp-build (prebuilt binary if
|
|
3
|
+
* available, else the local build/), and make the CTP shared libraries findable
|
|
4
|
+
* by the dynamic loader.
|
|
5
|
+
*/
|
|
6
|
+
import { createRequire } from "node:module";
|
|
7
|
+
import { fileURLToPath } from "node:url";
|
|
8
|
+
import { dirname, join } from "node:path";
|
|
9
|
+
import process from "node:process";
|
|
10
|
+
|
|
11
|
+
const require = createRequire(import.meta.url);
|
|
12
|
+
const pkgRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
|
|
13
|
+
|
|
14
|
+
// On Windows the CTP .dll must be on the search path; prepend the bundled dir
|
|
15
|
+
// so it is found regardless of where the .node lives (build/ or prebuilds/).
|
|
16
|
+
// Linux/macOS resolve their .so/.framework via the rpath baked at build time.
|
|
17
|
+
if (process.platform === "win32") {
|
|
18
|
+
process.env.PATH = `${join(pkgRoot, "tradeapi", "windows")};${process.env.PATH ?? ""}`;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
22
|
+
const loadAddon = require("node-gyp-build") as (root: string) => any;
|
|
23
|
+
|
|
24
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
25
|
+
export const native = loadAddon(pkgRoot) as any;
|