@hitrading/ctp-node 1.2.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 (104) hide show
  1. package/CHANGELOG.md +67 -0
  2. package/README.md +52 -31
  3. package/README.zh-CN.md +42 -26
  4. package/RELEASING.md +78 -0
  5. package/binding.gyp +13 -13
  6. package/dist/client.d.ts +40 -15
  7. package/dist/client.d.ts.map +1 -1
  8. package/dist/client.js +241 -62
  9. package/dist/client.js.map +1 -1
  10. package/dist/codec.d.ts.map +1 -1
  11. package/dist/codec.js +84 -19
  12. package/dist/codec.js.map +1 -1
  13. package/dist/generated/structs.gen.d.ts +5461 -5461
  14. package/dist/generated/structs.gen.d.ts.map +1 -1
  15. package/dist/generated/trader.gen.d.ts.map +1 -1
  16. package/dist/generated/trader.gen.js +788 -94
  17. package/dist/generated/trader.gen.js.map +1 -1
  18. package/dist/index.cjs +1896 -420
  19. package/dist/index.d.cts +1 -1
  20. package/dist/index.d.ts +1 -1
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js.map +1 -1
  23. package/dist/market-data.d.ts +48 -21
  24. package/dist/market-data.d.ts.map +1 -1
  25. package/dist/market-data.js +165 -26
  26. package/dist/market-data.js.map +1 -1
  27. package/dist/native-binding.d.ts.map +1 -1
  28. package/dist/native-binding.js +5 -1
  29. package/dist/native-binding.js.map +1 -1
  30. package/dist/trader.d.ts +127 -79
  31. package/dist/trader.d.ts.map +1 -1
  32. package/dist/trader.js +788 -200
  33. package/dist/trader.js.map +1 -1
  34. package/dist/validation.d.ts +27 -0
  35. package/dist/validation.d.ts.map +1 -0
  36. package/dist/validation.js +85 -0
  37. package/dist/validation.js.map +1 -0
  38. package/docs/API.md +1437 -0
  39. package/docs/API.zh-CN.md +1278 -0
  40. package/docs/MIGRATION.md +54 -0
  41. package/docs/assets/validation.svg +27 -0
  42. package/docs/integration.md +60 -0
  43. package/docs/native-hooks.md +281 -0
  44. package/docs/native-hooks.zh-CN.md +205 -0
  45. package/docs/troubleshooting.md +257 -0
  46. package/docs/troubleshooting.zh-CN.md +220 -0
  47. package/package.json +14 -6
  48. package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
  49. package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
  50. package/scripts/bench-decode.mjs +37 -0
  51. package/scripts/bench-fullmarket-offline.mjs +207 -0
  52. package/scripts/stress-fullmarket.mjs +255 -0
  53. package/src/addon.cc +2 -2
  54. package/src/client.ts +263 -65
  55. package/src/codec.ts +86 -19
  56. package/src/generated/structs.gen.ts +5461 -5461
  57. package/src/generated/trader.gen.ts +309 -94
  58. package/src/generated/traderreq.gen.cc +194 -18
  59. package/src/generated/traderreq.gen.h +4 -0
  60. package/src/generated/traderspi.gen.cc +130 -10
  61. package/src/generated/traderspi.gen.h +4 -2
  62. package/src/index.ts +8 -1
  63. package/src/market-data.ts +219 -43
  64. package/src/native/arm.cc +114 -30
  65. package/src/native/arm.h +16 -4
  66. package/src/native/channel.cc +209 -81
  67. package/src/native/channel.h +65 -24
  68. package/src/native/ctp_field.h +23 -0
  69. package/src/native/mdapi.cc +621 -61
  70. package/src/native/mdspi.cc +221 -14
  71. package/src/native/mdspi.h +62 -9
  72. package/src/native/order_capability.h +75 -0
  73. package/src/native/risk.cc +2051 -205
  74. package/src/native/risk.h +450 -57
  75. package/src/native/shared_handle.h +37 -0
  76. package/src/native/snapshot.h +120 -0
  77. package/src/native/traderapi.cc +749 -170
  78. package/src/native-binding.ts +5 -1
  79. package/src/trader.ts +993 -229
  80. package/src/validation.ts +96 -0
  81. /package/{tradeapi → ctpsdk}/ThostFtdcMdApi.h +0 -0
  82. /package/{tradeapi → ctpsdk}/ThostFtdcTraderApi.h +0 -0
  83. /package/{tradeapi → ctpsdk}/ThostFtdcUserApiDataType.h +0 -0
  84. /package/{tradeapi → ctpsdk}/ThostFtdcUserApiStruct.h +0 -0
  85. /package/{tradeapi → ctpsdk}/error.dtd +0 -0
  86. /package/{tradeapi → ctpsdk}/error.xml +0 -0
  87. /package/{tradeapi → ctpsdk}/linux/thostmduserapi_se.so +0 -0
  88. /package/{tradeapi → ctpsdk}/linux/thosttraderapi_se.so +0 -0
  89. /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcMdApi.h +0 -0
  90. /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +0 -0
  91. /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +0 -0
  92. /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Resources/Info.plist +0 -0
  93. /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/_CodeSignature/CodeResources +0 -0
  94. /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/thostmduserapi_se +0 -0
  95. /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcTraderApi.h +0 -0
  96. /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +0 -0
  97. /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +0 -0
  98. /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Resources/Info.plist +0 -0
  99. /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/_CodeSignature/CodeResources +0 -0
  100. /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/thosttraderapi_se +0 -0
  101. /package/{tradeapi → ctpsdk}/windows/thostmduserapi_se.dll +0 -0
  102. /package/{tradeapi → ctpsdk}/windows/thostmduserapi_se.lib +0 -0
  103. /package/{tradeapi → ctpsdk}/windows/thosttraderapi_se.dll +0 -0
  104. /package/{tradeapi → ctpsdk}/windows/thosttraderapi_se.lib +0 -0
@@ -0,0 +1,120 @@
1
+ /*
2
+ * snapshot.h - last-value cache (LVC): the latest full depth tick per instrument.
3
+ *
4
+ * Written on the CTP MD callback thread (update), read on the JS thread
5
+ * (get/last) and the Trader's order thread (quote(), via RefPriceSource, for the
6
+ * deviation reference). One mutex; entries are full DepthMarketDataField copies.
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.
13
+ */
14
+ #ifndef CTP_NATIVE_SNAPSHOT_H
15
+ #define CTP_NATIVE_SNAPSHOT_H
16
+
17
+ #include "ThostFtdcUserApiStruct.h"
18
+ #include "risk.h" // RefPriceSource
19
+ #include <cmath>
20
+ #include <chrono>
21
+ #include <limits>
22
+ #include <mutex>
23
+ #include <string>
24
+ #include <unordered_map>
25
+
26
+ namespace ctp {
27
+
28
+ class SnapshotCache : public RefPriceSource {
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
+
46
+ // MD callback thread: copy the whole latest tick in, keyed by instrument id.
47
+ bool update(const std::string &instrumentId,
48
+ const CThostFtdcDepthMarketDataField &t) {
49
+ std::lock_guard<std::mutex> lk(m_);
50
+ if (!active_ || instrumentId.empty())
51
+ return false;
52
+ map_[instrumentId] = Entry{t, std::chrono::steady_clock::now()};
53
+ return true;
54
+ }
55
+ // Reader (JS thread): copy the latest tick out. False if none cached.
56
+ bool get(const std::string &id, CThostFtdcDepthMarketDataField &out) const {
57
+ std::lock_guard<std::mutex> lk(m_);
58
+ if (!active_)
59
+ return false;
60
+ auto it = map_.find(id);
61
+ if (it == map_.end())
62
+ return false;
63
+ out = it->second.tick;
64
+ return true;
65
+ }
66
+ // JS convenience: latest usable price, or 0 if absent/unset.
67
+ double last(const std::string &id) const {
68
+ std::lock_guard<std::mutex> lk(m_);
69
+ if (!active_)
70
+ return 0.0;
71
+ auto it = map_.find(id);
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;
76
+ }
77
+ uint64_t ageMillis(const std::string &id) const {
78
+ std::lock_guard<std::mutex> lk(m_);
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());
87
+ }
88
+ bool quote(const std::string &id, double &price,
89
+ uint64_t &ageMillisOut) const override {
90
+ std::lock_guard<std::mutex> lk(m_);
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);
105
+ }
106
+ void clear() { invalidate(); } // close
107
+
108
+ private:
109
+ struct Entry {
110
+ CThostFtdcDepthMarketDataField tick;
111
+ std::chrono::steady_clock::time_point updated;
112
+ };
113
+ mutable std::mutex m_;
114
+ bool active_ = false;
115
+ std::unordered_map<std::string, Entry> map_;
116
+ };
117
+
118
+ } // namespace ctp
119
+
120
+ #endif /* CTP_NATIVE_SNAPSHOT_H */