@hitrading/ctp-node 2.0.0 → 3.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.
Files changed (79) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +47 -32
  3. package/README.zh-CN.md +37 -27
  4. package/RELEASING.md +78 -0
  5. package/dist/client.d.ts +36 -15
  6. package/dist/client.d.ts.map +1 -1
  7. package/dist/client.js +234 -62
  8. package/dist/client.js.map +1 -1
  9. package/dist/codec.d.ts.map +1 -1
  10. package/dist/codec.js +84 -19
  11. package/dist/codec.js.map +1 -1
  12. package/dist/generated/structs.gen.d.ts +5461 -5461
  13. package/dist/generated/structs.gen.d.ts.map +1 -1
  14. package/dist/generated/trader.gen.d.ts.map +1 -1
  15. package/dist/generated/trader.gen.js +788 -94
  16. package/dist/generated/trader.gen.js.map +1 -1
  17. package/dist/index.cjs +1875 -415
  18. package/dist/index.d.cts +1 -1
  19. package/dist/index.d.ts +1 -1
  20. package/dist/index.d.ts.map +1 -1
  21. package/dist/index.js.map +1 -1
  22. package/dist/market-data.d.ts +41 -24
  23. package/dist/market-data.d.ts.map +1 -1
  24. package/dist/market-data.js +148 -29
  25. package/dist/market-data.js.map +1 -1
  26. package/dist/native-binding.d.ts.map +1 -1
  27. package/dist/native-binding.js +4 -0
  28. package/dist/native-binding.js.map +1 -1
  29. package/dist/trader.d.ts +111 -71
  30. package/dist/trader.d.ts.map +1 -1
  31. package/dist/trader.js +774 -180
  32. package/dist/trader.js.map +1 -1
  33. package/dist/validation.d.ts +27 -0
  34. package/dist/validation.d.ts.map +1 -0
  35. package/dist/validation.js +85 -0
  36. package/dist/validation.js.map +1 -0
  37. package/docs/API.md +1437 -0
  38. package/docs/API.zh-CN.md +1278 -0
  39. package/docs/MIGRATION.md +54 -0
  40. package/docs/assets/validation.svg +27 -0
  41. package/docs/integration.md +60 -0
  42. package/docs/native-hooks.md +281 -0
  43. package/docs/native-hooks.zh-CN.md +205 -0
  44. package/docs/troubleshooting.md +257 -0
  45. package/docs/troubleshooting.zh-CN.md +220 -0
  46. package/package.json +12 -4
  47. package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
  48. package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
  49. package/scripts/bench-decode.mjs +37 -0
  50. package/scripts/bench-fullmarket-offline.mjs +207 -0
  51. package/scripts/stress-fullmarket.mjs +255 -0
  52. package/src/addon.cc +2 -2
  53. package/src/client.ts +255 -65
  54. package/src/codec.ts +86 -19
  55. package/src/generated/structs.gen.ts +5461 -5461
  56. package/src/generated/trader.gen.ts +309 -94
  57. package/src/generated/traderreq.gen.cc +194 -18
  58. package/src/generated/traderreq.gen.h +4 -0
  59. package/src/generated/traderspi.gen.cc +129 -15
  60. package/src/generated/traderspi.gen.h +4 -2
  61. package/src/index.ts +8 -1
  62. package/src/market-data.ts +199 -46
  63. package/src/native/arm.cc +114 -30
  64. package/src/native/arm.h +16 -4
  65. package/src/native/channel.cc +209 -81
  66. package/src/native/channel.h +65 -24
  67. package/src/native/ctp_field.h +23 -0
  68. package/src/native/mdapi.cc +582 -69
  69. package/src/native/mdspi.cc +219 -16
  70. package/src/native/mdspi.h +59 -10
  71. package/src/native/order_capability.h +75 -0
  72. package/src/native/risk.cc +2030 -213
  73. package/src/native/risk.h +431 -78
  74. package/src/native/shared_handle.h +37 -0
  75. package/src/native/snapshot.h +75 -16
  76. package/src/native/traderapi.cc +753 -124
  77. package/src/native-binding.ts +4 -0
  78. package/src/trader.ts +971 -204
  79. 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 */
@@ -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 (last, via RefPriceSource, for the
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
- * Cleared on unsubscribe (erase) and close (clear). This is the in-C++ market-
8
- * data cache, so latest prices reach the risk engine without round-tripping
9
- * through JS, and JS can read a synchronous snapshot without waiting for a tick.
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
- void update(const CThostFtdcDepthMarketDataField &t) {
47
+ bool update(const std::string &instrumentId,
48
+ const CThostFtdcDepthMarketDataField &t) {
26
49
  std::lock_guard<std::mutex> lk(m_);
27
- map_[t.InstrumentID] = t;
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
- // RefPriceSource: latest price, or 0 if absent. Validity (the DBL_MAX "unset"
39
- // sentinel etc.) is filtered by the risk engine's refPrice(), not here.
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
- return it != map_.end() ? it->second.LastPrice : 0.0;
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
- void erase(const std::string &id) { // unsubscribe
77
+ uint64_t ageMillis(const std::string &id) const {
46
78
  std::lock_guard<std::mutex> lk(m_);
47
- map_.erase(id);
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
- void clear() { // close
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
- map_.clear();
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
- std::unordered_map<std::string, CThostFtdcDepthMarketDataField> map_;
114
+ bool active_ = false;
115
+ std::unordered_map<std::string, Entry> map_;
57
116
  };
58
117
 
59
118
  } // namespace ctp