@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.
- package/CHANGELOG.md +67 -0
- package/README.md +52 -31
- package/README.zh-CN.md +42 -26
- package/RELEASING.md +78 -0
- package/binding.gyp +13 -13
- package/dist/client.d.ts +40 -15
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +241 -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 +1896 -420
- 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 +48 -21
- package/dist/market-data.d.ts.map +1 -1
- package/dist/market-data.js +165 -26
- package/dist/market-data.js.map +1 -1
- package/dist/native-binding.d.ts.map +1 -1
- package/dist/native-binding.js +5 -1
- package/dist/native-binding.js.map +1 -1
- package/dist/trader.d.ts +127 -79
- package/dist/trader.d.ts.map +1 -1
- package/dist/trader.js +788 -200
- 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 +1437 -0
- package/docs/API.zh-CN.md +1278 -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 +14 -6
- 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 +263 -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 +130 -10
- package/src/generated/traderspi.gen.h +4 -2
- package/src/index.ts +8 -1
- package/src/market-data.ts +219 -43
- 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 +621 -61
- package/src/native/mdspi.cc +221 -14
- package/src/native/mdspi.h +62 -9
- package/src/native/order_capability.h +75 -0
- package/src/native/risk.cc +2051 -205
- package/src/native/risk.h +450 -57
- package/src/native/shared_handle.h +37 -0
- package/src/native/snapshot.h +120 -0
- package/src/native/traderapi.cc +749 -170
- package/src/native-binding.ts +5 -1
- package/src/trader.ts +993 -229
- package/src/validation.ts +96 -0
- /package/{tradeapi → ctpsdk}/ThostFtdcMdApi.h +0 -0
- /package/{tradeapi → ctpsdk}/ThostFtdcTraderApi.h +0 -0
- /package/{tradeapi → ctpsdk}/ThostFtdcUserApiDataType.h +0 -0
- /package/{tradeapi → ctpsdk}/ThostFtdcUserApiStruct.h +0 -0
- /package/{tradeapi → ctpsdk}/error.dtd +0 -0
- /package/{tradeapi → ctpsdk}/error.xml +0 -0
- /package/{tradeapi → ctpsdk}/linux/thostmduserapi_se.so +0 -0
- /package/{tradeapi → ctpsdk}/linux/thosttraderapi_se.so +0 -0
- /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcMdApi.h +0 -0
- /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +0 -0
- /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +0 -0
- /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/Resources/Info.plist +0 -0
- /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/_CodeSignature/CodeResources +0 -0
- /package/{tradeapi → ctpsdk}/macos/thostmduserapi_se.framework/Versions/A/thostmduserapi_se +0 -0
- /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcTraderApi.h +0 -0
- /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +0 -0
- /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +0 -0
- /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/Resources/Info.plist +0 -0
- /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/_CodeSignature/CodeResources +0 -0
- /package/{tradeapi → ctpsdk}/macos/thosttraderapi_se.framework/Versions/A/thosttraderapi_se +0 -0
- /package/{tradeapi → ctpsdk}/windows/thostmduserapi_se.dll +0 -0
- /package/{tradeapi → ctpsdk}/windows/thostmduserapi_se.lib +0 -0
- /package/{tradeapi → ctpsdk}/windows/thosttraderapi_se.dll +0 -0
- /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 */
|