@hitrading/ctp-node 2.0.0 → 4.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/CHANGELOG.md +82 -0
- package/README.md +54 -32
- package/README.zh-CN.md +44 -27
- package/RELEASING.md +78 -0
- package/dist/client.d.ts +36 -15
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +234 -62
- package/dist/client.js.map +1 -1
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +84 -19
- package/dist/codec.js.map +1 -1
- package/dist/generated/structs.gen.d.ts +5461 -5461
- package/dist/generated/structs.gen.d.ts.map +1 -1
- package/dist/generated/trader.gen.d.ts.map +1 -1
- package/dist/generated/trader.gen.js +788 -94
- package/dist/generated/trader.gen.js.map +1 -1
- package/dist/index.cjs +1909 -417
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/market-data.d.ts +41 -24
- package/dist/market-data.d.ts.map +1 -1
- package/dist/market-data.js +148 -29
- package/dist/market-data.js.map +1 -1
- package/dist/native-binding.d.ts.map +1 -1
- package/dist/native-binding.js +4 -0
- package/dist/native-binding.js.map +1 -1
- package/dist/trader.d.ts +136 -79
- package/dist/trader.d.ts.map +1 -1
- package/dist/trader.js +807 -182
- package/dist/trader.js.map +1 -1
- package/dist/validation.d.ts +27 -0
- package/dist/validation.d.ts.map +1 -0
- package/dist/validation.js +85 -0
- package/dist/validation.js.map +1 -0
- package/docs/API.md +1445 -0
- package/docs/API.zh-CN.md +1285 -0
- package/docs/MIGRATION.md +54 -0
- package/docs/assets/validation.svg +27 -0
- package/docs/integration.md +60 -0
- package/docs/native-hooks.md +281 -0
- package/docs/native-hooks.zh-CN.md +205 -0
- package/docs/troubleshooting.md +257 -0
- package/docs/troubleshooting.zh-CN.md +220 -0
- package/package.json +12 -4
- package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
- package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
- package/scripts/bench-decode.mjs +37 -0
- package/scripts/bench-fullmarket-offline.mjs +207 -0
- package/scripts/stress-fullmarket.mjs +255 -0
- package/src/addon.cc +2 -2
- package/src/client.ts +255 -65
- package/src/codec.ts +86 -19
- package/src/generated/structs.gen.ts +5461 -5461
- package/src/generated/trader.gen.ts +309 -94
- package/src/generated/traderreq.gen.cc +194 -18
- package/src/generated/traderreq.gen.h +4 -0
- package/src/generated/traderspi.gen.cc +129 -15
- package/src/generated/traderspi.gen.h +4 -2
- package/src/index.ts +11 -1
- package/src/market-data.ts +199 -46
- package/src/native/arm.cc +114 -30
- package/src/native/arm.h +16 -4
- package/src/native/channel.cc +209 -81
- package/src/native/channel.h +65 -24
- package/src/native/ctp_field.h +23 -0
- package/src/native/mdapi.cc +582 -69
- package/src/native/mdspi.cc +219 -16
- package/src/native/mdspi.h +59 -10
- package/src/native/order_capability.h +78 -0
- package/src/native/risk.cc +2030 -213
- package/src/native/risk.h +431 -78
- package/src/native/shared_handle.h +37 -0
- package/src/native/snapshot.h +75 -16
- package/src/native/traderapi.cc +769 -124
- package/src/native-binding.ts +4 -0
- package/src/trader.ts +1038 -207
- package/src/validation.ts +96 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* shared_handle.h - runtime-tagged N-API handles for cross-client shared state.
|
|
3
|
+
*
|
|
4
|
+
* Napi::External<T> does not preserve T at runtime. Both sides therefore use
|
|
5
|
+
* this one concrete payload and verify its magic/kind before touching either
|
|
6
|
+
* shared_ptr. This prevents an arm-registry External from being interpreted as
|
|
7
|
+
* a SnapshotCache External (or vice versa), which would otherwise be native
|
|
8
|
+
* type confusion rather than a recoverable JavaScript TypeError.
|
|
9
|
+
*/
|
|
10
|
+
#ifndef CTP_NATIVE_SHARED_HANDLE_H
|
|
11
|
+
#define CTP_NATIVE_SHARED_HANDLE_H
|
|
12
|
+
|
|
13
|
+
#include <cstdint>
|
|
14
|
+
#include <memory>
|
|
15
|
+
|
|
16
|
+
namespace ctp {
|
|
17
|
+
|
|
18
|
+
class ArmRegistry;
|
|
19
|
+
class SnapshotCache;
|
|
20
|
+
|
|
21
|
+
enum class SharedHandleKind : uint32_t {
|
|
22
|
+
ArmRegistry = 1,
|
|
23
|
+
SnapshotCache = 2,
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
struct SharedHandle {
|
|
27
|
+
static constexpr uint64_t kMagic = UINT64_C(0x4354504E4F444548); // CTPNODEH
|
|
28
|
+
|
|
29
|
+
uint64_t magic = kMagic;
|
|
30
|
+
SharedHandleKind kind = SharedHandleKind::ArmRegistry;
|
|
31
|
+
std::shared_ptr<ArmRegistry> arm;
|
|
32
|
+
std::shared_ptr<SnapshotCache> snapshot;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
} // namespace ctp
|
|
36
|
+
|
|
37
|
+
#endif /* CTP_NATIVE_SHARED_HANDLE_H */
|
package/src/native/snapshot.h
CHANGED
|
@@ -2,17 +2,23 @@
|
|
|
2
2
|
* snapshot.h - last-value cache (LVC): the latest full depth tick per instrument.
|
|
3
3
|
*
|
|
4
4
|
* Written on the CTP MD callback thread (update), read on the JS thread
|
|
5
|
-
* (get/last) and the Trader's order thread (
|
|
5
|
+
* (get/last) and the Trader's order thread (quote(), via RefPriceSource, for the
|
|
6
6
|
* deviation reference). One mutex; entries are full DepthMarketDataField copies.
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* A cache is invalid until the market-data login succeeds. It is invalidated
|
|
8
|
+
* (and cleared) on every front/session transition, so a reconnect can never
|
|
9
|
+
* make a quote from the previous session look fresh merely because its local
|
|
10
|
+
* steady-clock age is small. This is the in-C++ market-data cache, so latest
|
|
11
|
+
* prices reach the risk engine without round-tripping through JS, and JS can
|
|
12
|
+
* read a synchronous snapshot without waiting for a tick.
|
|
10
13
|
*/
|
|
11
14
|
#ifndef CTP_NATIVE_SNAPSHOT_H
|
|
12
15
|
#define CTP_NATIVE_SNAPSHOT_H
|
|
13
16
|
|
|
14
17
|
#include "ThostFtdcUserApiStruct.h"
|
|
15
18
|
#include "risk.h" // RefPriceSource
|
|
19
|
+
#include <cmath>
|
|
20
|
+
#include <chrono>
|
|
21
|
+
#include <limits>
|
|
16
22
|
#include <mutex>
|
|
17
23
|
#include <string>
|
|
18
24
|
#include <unordered_map>
|
|
@@ -21,39 +27,92 @@ namespace ctp {
|
|
|
21
27
|
|
|
22
28
|
class SnapshotCache : public RefPriceSource {
|
|
23
29
|
public:
|
|
30
|
+
// Start a newly authenticated MD session. Always discard prior entries before
|
|
31
|
+
// making the cache readable: activation itself must not resurrect stale quotes.
|
|
32
|
+
void activate() {
|
|
33
|
+
std::lock_guard<std::mutex> lk(m_);
|
|
34
|
+
map_.clear();
|
|
35
|
+
active_ = true;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// Close the current MD session immediately. update() also checks active_, so a
|
|
39
|
+
// callback racing just after disconnect/logout cannot repopulate the cache.
|
|
40
|
+
void invalidate() {
|
|
41
|
+
std::lock_guard<std::mutex> lk(m_);
|
|
42
|
+
active_ = false;
|
|
43
|
+
map_.clear();
|
|
44
|
+
}
|
|
45
|
+
|
|
24
46
|
// MD callback thread: copy the whole latest tick in, keyed by instrument id.
|
|
25
|
-
|
|
47
|
+
bool update(const std::string &instrumentId,
|
|
48
|
+
const CThostFtdcDepthMarketDataField &t) {
|
|
26
49
|
std::lock_guard<std::mutex> lk(m_);
|
|
27
|
-
|
|
50
|
+
if (!active_ || instrumentId.empty())
|
|
51
|
+
return false;
|
|
52
|
+
map_[instrumentId] = Entry{t, std::chrono::steady_clock::now()};
|
|
53
|
+
return true;
|
|
28
54
|
}
|
|
29
55
|
// Reader (JS thread): copy the latest tick out. False if none cached.
|
|
30
56
|
bool get(const std::string &id, CThostFtdcDepthMarketDataField &out) const {
|
|
31
57
|
std::lock_guard<std::mutex> lk(m_);
|
|
58
|
+
if (!active_)
|
|
59
|
+
return false;
|
|
32
60
|
auto it = map_.find(id);
|
|
33
61
|
if (it == map_.end())
|
|
34
62
|
return false;
|
|
35
|
-
out = it->second;
|
|
63
|
+
out = it->second.tick;
|
|
36
64
|
return true;
|
|
37
65
|
}
|
|
38
|
-
//
|
|
39
|
-
|
|
40
|
-
double last(const std::string &id) const override {
|
|
66
|
+
// JS convenience: latest usable price, or 0 if absent/unset.
|
|
67
|
+
double last(const std::string &id) const {
|
|
41
68
|
std::lock_guard<std::mutex> lk(m_);
|
|
69
|
+
if (!active_)
|
|
70
|
+
return 0.0;
|
|
42
71
|
auto it = map_.find(id);
|
|
43
|
-
|
|
72
|
+
if (it == map_.end())
|
|
73
|
+
return 0.0;
|
|
74
|
+
const double price = it->second.tick.LastPrice;
|
|
75
|
+
return std::isfinite(price) && price > 0.0 && price < 1e300 ? price : 0.0;
|
|
44
76
|
}
|
|
45
|
-
|
|
77
|
+
uint64_t ageMillis(const std::string &id) const {
|
|
46
78
|
std::lock_guard<std::mutex> lk(m_);
|
|
47
|
-
|
|
79
|
+
if (!active_)
|
|
80
|
+
return std::numeric_limits<uint64_t>::max();
|
|
81
|
+
auto it = map_.find(id);
|
|
82
|
+
if (it == map_.end())
|
|
83
|
+
return std::numeric_limits<uint64_t>::max();
|
|
84
|
+
const auto age = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
85
|
+
std::chrono::steady_clock::now() - it->second.updated);
|
|
86
|
+
return age.count() < 0 ? 0 : static_cast<uint64_t>(age.count());
|
|
48
87
|
}
|
|
49
|
-
|
|
88
|
+
bool quote(const std::string &id, double &price,
|
|
89
|
+
uint64_t &ageMillisOut) const override {
|
|
50
90
|
std::lock_guard<std::mutex> lk(m_);
|
|
51
|
-
|
|
91
|
+
if (!active_)
|
|
92
|
+
return false;
|
|
93
|
+
auto it = map_.find(id);
|
|
94
|
+
if (it == map_.end())
|
|
95
|
+
return false;
|
|
96
|
+
price = it->second.tick.LastPrice;
|
|
97
|
+
const auto age = std::chrono::duration_cast<std::chrono::milliseconds>(
|
|
98
|
+
std::chrono::steady_clock::now() - it->second.updated);
|
|
99
|
+
ageMillisOut = age.count() < 0 ? 0 : static_cast<uint64_t>(age.count());
|
|
100
|
+
return true;
|
|
101
|
+
}
|
|
102
|
+
void erase(const std::string &id) { // unsubscribe
|
|
103
|
+
std::lock_guard<std::mutex> lk(m_);
|
|
104
|
+
map_.erase(id);
|
|
52
105
|
}
|
|
106
|
+
void clear() { invalidate(); } // close
|
|
53
107
|
|
|
54
108
|
private:
|
|
109
|
+
struct Entry {
|
|
110
|
+
CThostFtdcDepthMarketDataField tick;
|
|
111
|
+
std::chrono::steady_clock::time_point updated;
|
|
112
|
+
};
|
|
55
113
|
mutable std::mutex m_;
|
|
56
|
-
|
|
114
|
+
bool active_ = false;
|
|
115
|
+
std::unordered_map<std::string, Entry> map_;
|
|
57
116
|
};
|
|
58
117
|
|
|
59
118
|
} // namespace ctp
|