@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.
Files changed (102) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +208 -0
  3. package/README.zh-CN.md +185 -0
  4. package/binding.gyp +90 -0
  5. package/dist/client.d.ts +115 -0
  6. package/dist/client.d.ts.map +1 -0
  7. package/dist/client.js +466 -0
  8. package/dist/client.js.map +1 -0
  9. package/dist/codec.d.ts +29 -0
  10. package/dist/codec.d.ts.map +1 -0
  11. package/dist/codec.js +131 -0
  12. package/dist/codec.js.map +1 -0
  13. package/dist/generated/enums.gen.d.ts +2257 -0
  14. package/dist/generated/enums.gen.d.ts.map +1 -0
  15. package/dist/generated/enums.gen.js +2576 -0
  16. package/dist/generated/enums.gen.js.map +1 -0
  17. package/dist/generated/structs.gen.d.ts +11041 -0
  18. package/dist/generated/structs.gen.d.ts.map +1 -0
  19. package/dist/generated/structs.gen.js +939 -0
  20. package/dist/generated/structs.gen.js.map +1 -0
  21. package/dist/generated/trader.gen.d.ts +273 -0
  22. package/dist/generated/trader.gen.d.ts.map +1 -0
  23. package/dist/generated/trader.gen.js +653 -0
  24. package/dist/generated/trader.gen.js.map +1 -0
  25. package/dist/index.d.ts +9 -0
  26. package/dist/index.d.ts.map +1 -0
  27. package/dist/index.js +9 -0
  28. package/dist/index.js.map +1 -0
  29. package/dist/market-data.d.ts +66 -0
  30. package/dist/market-data.d.ts.map +1 -0
  31. package/dist/market-data.js +105 -0
  32. package/dist/market-data.js.map +1 -0
  33. package/dist/native-binding.d.ts +2 -0
  34. package/dist/native-binding.d.ts.map +1 -0
  35. package/dist/native-binding.js +22 -0
  36. package/dist/native-binding.js.map +1 -0
  37. package/dist/trader.d.ts +234 -0
  38. package/dist/trader.d.ts.map +1 -0
  39. package/dist/trader.js +388 -0
  40. package/dist/trader.js.map +1 -0
  41. package/package.json +69 -0
  42. package/prebuilds/linux-x64/@hitrading+ctp-node.node +0 -0
  43. package/prebuilds/linux-x64/thostmduserapi_se.so +0 -0
  44. package/prebuilds/linux-x64/thosttraderapi_se.so +0 -0
  45. package/prebuilds/win32-x64/@hitrading+ctp-node.node +0 -0
  46. package/prebuilds/win32-x64/thostmduserapi_se.dll +0 -0
  47. package/prebuilds/win32-x64/thosttraderapi_se.dll +0 -0
  48. package/src/addon.cc +171 -0
  49. package/src/client.ts +539 -0
  50. package/src/codec.ts +160 -0
  51. package/src/generated/enums.gen.ts +2576 -0
  52. package/src/generated/layout.gen.cc +5771 -0
  53. package/src/generated/layout.gen.h +8 -0
  54. package/src/generated/structids.gen.h +470 -0
  55. package/src/generated/structs.gen.ts +12437 -0
  56. package/src/generated/trader.gen.ts +656 -0
  57. package/src/generated/traderreq.gen.cc +706 -0
  58. package/src/generated/traderreq.gen.h +127 -0
  59. package/src/generated/traderspi.gen.cc +636 -0
  60. package/src/generated/traderspi.gen.h +334 -0
  61. package/src/index.ts +14 -0
  62. package/src/market-data.ts +145 -0
  63. package/src/native/arm.cc +77 -0
  64. package/src/native/arm.h +71 -0
  65. package/src/native/channel.cc +175 -0
  66. package/src/native/channel.h +117 -0
  67. package/src/native/gbk.cc +54 -0
  68. package/src/native/gbk.h +17 -0
  69. package/src/native/mdapi.cc +412 -0
  70. package/src/native/mdapi.h +14 -0
  71. package/src/native/mdspi.cc +103 -0
  72. package/src/native/mdspi.h +81 -0
  73. package/src/native/risk.cc +461 -0
  74. package/src/native/risk.h +183 -0
  75. package/src/native/traderapi.cc +533 -0
  76. package/src/native/traderapi.h +14 -0
  77. package/src/native-binding.ts +25 -0
  78. package/src/trader.ts +522 -0
  79. package/tradeapi/ThostFtdcMdApi.h +168 -0
  80. package/tradeapi/ThostFtdcTraderApi.h +914 -0
  81. package/tradeapi/ThostFtdcUserApiDataType.h +7068 -0
  82. package/tradeapi/ThostFtdcUserApiStruct.h +12386 -0
  83. package/tradeapi/error.dtd +9 -0
  84. package/tradeapi/error.xml +284 -0
  85. package/tradeapi/linux/thostmduserapi_se.so +0 -0
  86. package/tradeapi/linux/thosttraderapi_se.so +0 -0
  87. package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcMdApi.h +168 -0
  88. package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +7068 -0
  89. package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +12386 -0
  90. package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/Resources/Info.plist +46 -0
  91. package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/_CodeSignature/CodeResources +149 -0
  92. package/tradeapi/macos/thostmduserapi_se.framework/Versions/A/thostmduserapi_se +0 -0
  93. package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcTraderApi.h +914 -0
  94. package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiDataType.h +7068 -0
  95. package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Headers/ThostFtdcUserApiStruct.h +12386 -0
  96. package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/Resources/Info.plist +46 -0
  97. package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/_CodeSignature/CodeResources +149 -0
  98. package/tradeapi/macos/thosttraderapi_se.framework/Versions/A/thosttraderapi_se +0 -0
  99. package/tradeapi/windows/thostmduserapi_se.dll +0 -0
  100. package/tradeapi/windows/thostmduserapi_se.lib +0 -0
  101. package/tradeapi/windows/thosttraderapi_se.dll +0 -0
  102. package/tradeapi/windows/thosttraderapi_se.lib +0 -0
@@ -0,0 +1,334 @@
1
+ /* AUTO-GENERATED. Do not edit. */
2
+ #ifndef CTP_TRADERSPI_GEN_H
3
+ #define CTP_TRADERSPI_GEN_H
4
+ #include "ThostFtdcTraderApi.h"
5
+ #include "../native/channel.h"
6
+ #include "../native/risk.h"
7
+ namespace ctp {
8
+ enum {
9
+ ET_BASE = 8192,
10
+ ET_FrontConnected = ET_BASE + 1,
11
+ ET_FrontDisconnected = ET_BASE + 2,
12
+ ET_HeartBeatWarning = ET_BASE + 3,
13
+ ET_RspAuthenticate = ET_BASE + 4,
14
+ ET_RspUserLogin = ET_BASE + 5,
15
+ ET_RspUserLogout = ET_BASE + 6,
16
+ ET_RspUserPasswordUpdate = ET_BASE + 7,
17
+ ET_RspTradingAccountPasswordUpdate = ET_BASE + 8,
18
+ ET_RspUserAuthMethod = ET_BASE + 9,
19
+ ET_RspGenUserCaptcha = ET_BASE + 10,
20
+ ET_RspGenUserText = ET_BASE + 11,
21
+ ET_RspOrderInsert = ET_BASE + 12,
22
+ ET_RspParkedOrderInsert = ET_BASE + 13,
23
+ ET_RspParkedOrderAction = ET_BASE + 14,
24
+ ET_RspOrderAction = ET_BASE + 15,
25
+ ET_RspQryMaxOrderVolume = ET_BASE + 16,
26
+ ET_RspSettlementInfoConfirm = ET_BASE + 17,
27
+ ET_RspRemoveParkedOrder = ET_BASE + 18,
28
+ ET_RspRemoveParkedOrderAction = ET_BASE + 19,
29
+ ET_RspExecOrderInsert = ET_BASE + 20,
30
+ ET_RspExecOrderAction = ET_BASE + 21,
31
+ ET_RspForQuoteInsert = ET_BASE + 22,
32
+ ET_RspQuoteInsert = ET_BASE + 23,
33
+ ET_RspQuoteAction = ET_BASE + 24,
34
+ ET_RspBatchOrderAction = ET_BASE + 25,
35
+ ET_RspOptionSelfCloseInsert = ET_BASE + 26,
36
+ ET_RspOptionSelfCloseAction = ET_BASE + 27,
37
+ ET_RspCombActionInsert = ET_BASE + 28,
38
+ ET_RspQryOrder = ET_BASE + 29,
39
+ ET_RspQryTrade = ET_BASE + 30,
40
+ ET_RspQryInvestorPosition = ET_BASE + 31,
41
+ ET_RspQryTradingAccount = ET_BASE + 32,
42
+ ET_RspQryInvestor = ET_BASE + 33,
43
+ ET_RspQryTradingCode = ET_BASE + 34,
44
+ ET_RspQryInstrumentMarginRate = ET_BASE + 35,
45
+ ET_RspQryInstrumentCommissionRate = ET_BASE + 36,
46
+ ET_RspQryExchange = ET_BASE + 37,
47
+ ET_RspQryProduct = ET_BASE + 38,
48
+ ET_RspQryInstrument = ET_BASE + 39,
49
+ ET_RspQryDepthMarketData = ET_BASE + 40,
50
+ ET_RspQryTraderOffer = ET_BASE + 41,
51
+ ET_RspQrySettlementInfo = ET_BASE + 42,
52
+ ET_RspQryTransferBank = ET_BASE + 43,
53
+ ET_RspQryInvestorPositionDetail = ET_BASE + 44,
54
+ ET_RspQryNotice = ET_BASE + 45,
55
+ ET_RspQrySettlementInfoConfirm = ET_BASE + 46,
56
+ ET_RspQryInvestorPositionCombineDetail = ET_BASE + 47,
57
+ ET_RspQryCFMMCTradingAccountKey = ET_BASE + 48,
58
+ ET_RspQryEWarrantOffset = ET_BASE + 49,
59
+ ET_RspQryInvestorProductGroupMargin = ET_BASE + 50,
60
+ ET_RspQryExchangeMarginRate = ET_BASE + 51,
61
+ ET_RspQryExchangeMarginRateAdjust = ET_BASE + 52,
62
+ ET_RspQryExchangeRate = ET_BASE + 53,
63
+ ET_RspQrySecAgentACIDMap = ET_BASE + 54,
64
+ ET_RspQryProductExchRate = ET_BASE + 55,
65
+ ET_RspQryProductGroup = ET_BASE + 56,
66
+ ET_RspQryMMInstrumentCommissionRate = ET_BASE + 57,
67
+ ET_RspQryMMOptionInstrCommRate = ET_BASE + 58,
68
+ ET_RspQryInstrumentOrderCommRate = ET_BASE + 59,
69
+ ET_RspQrySecAgentTradingAccount = ET_BASE + 60,
70
+ ET_RspQrySecAgentCheckMode = ET_BASE + 61,
71
+ ET_RspQrySecAgentTradeInfo = ET_BASE + 62,
72
+ ET_RspQryOptionInstrTradeCost = ET_BASE + 63,
73
+ ET_RspQryOptionInstrCommRate = ET_BASE + 64,
74
+ ET_RspQryExecOrder = ET_BASE + 65,
75
+ ET_RspQryForQuote = ET_BASE + 66,
76
+ ET_RspQryQuote = ET_BASE + 67,
77
+ ET_RspQryOptionSelfClose = ET_BASE + 68,
78
+ ET_RspQryInvestUnit = ET_BASE + 69,
79
+ ET_RspQryCombInstrumentGuard = ET_BASE + 70,
80
+ ET_RspQryCombAction = ET_BASE + 71,
81
+ ET_RspQryTransferSerial = ET_BASE + 72,
82
+ ET_RspQryAccountregister = ET_BASE + 73,
83
+ ET_RspError = ET_BASE + 74,
84
+ ET_RtnOrder = ET_BASE + 75,
85
+ ET_RtnTrade = ET_BASE + 76,
86
+ ET_ErrRtnOrderInsert = ET_BASE + 77,
87
+ ET_ErrRtnOrderAction = ET_BASE + 78,
88
+ ET_RtnInstrumentStatus = ET_BASE + 79,
89
+ ET_RtnBulletin = ET_BASE + 80,
90
+ ET_RtnTradingNotice = ET_BASE + 81,
91
+ ET_RtnErrorConditionalOrder = ET_BASE + 82,
92
+ ET_RtnExecOrder = ET_BASE + 83,
93
+ ET_ErrRtnExecOrderInsert = ET_BASE + 84,
94
+ ET_ErrRtnExecOrderAction = ET_BASE + 85,
95
+ ET_ErrRtnForQuoteInsert = ET_BASE + 86,
96
+ ET_RtnQuote = ET_BASE + 87,
97
+ ET_ErrRtnQuoteInsert = ET_BASE + 88,
98
+ ET_ErrRtnQuoteAction = ET_BASE + 89,
99
+ ET_RtnForQuoteRsp = ET_BASE + 90,
100
+ ET_RtnCFMMCTradingAccountToken = ET_BASE + 91,
101
+ ET_ErrRtnBatchOrderAction = ET_BASE + 92,
102
+ ET_RtnOptionSelfClose = ET_BASE + 93,
103
+ ET_ErrRtnOptionSelfCloseInsert = ET_BASE + 94,
104
+ ET_ErrRtnOptionSelfCloseAction = ET_BASE + 95,
105
+ ET_RtnCombAction = ET_BASE + 96,
106
+ ET_ErrRtnCombActionInsert = ET_BASE + 97,
107
+ ET_RspQryContractBank = ET_BASE + 98,
108
+ ET_RspQryParkedOrder = ET_BASE + 99,
109
+ ET_RspQryParkedOrderAction = ET_BASE + 100,
110
+ ET_RspQryTradingNotice = ET_BASE + 101,
111
+ ET_RspQryBrokerTradingParams = ET_BASE + 102,
112
+ ET_RspQryBrokerTradingAlgos = ET_BASE + 103,
113
+ ET_RspQueryCFMMCTradingAccountToken = ET_BASE + 104,
114
+ ET_RtnFromBankToFutureByBank = ET_BASE + 105,
115
+ ET_RtnFromFutureToBankByBank = ET_BASE + 106,
116
+ ET_RtnRepealFromBankToFutureByBank = ET_BASE + 107,
117
+ ET_RtnRepealFromFutureToBankByBank = ET_BASE + 108,
118
+ ET_RtnFromBankToFutureByFuture = ET_BASE + 109,
119
+ ET_RtnFromFutureToBankByFuture = ET_BASE + 110,
120
+ ET_RtnRepealFromBankToFutureByFutureManual = ET_BASE + 111,
121
+ ET_RtnRepealFromFutureToBankByFutureManual = ET_BASE + 112,
122
+ ET_RtnQueryBankBalanceByFuture = ET_BASE + 113,
123
+ ET_ErrRtnBankToFutureByFuture = ET_BASE + 114,
124
+ ET_ErrRtnFutureToBankByFuture = ET_BASE + 115,
125
+ ET_ErrRtnRepealBankToFutureByFutureManual = ET_BASE + 116,
126
+ ET_ErrRtnRepealFutureToBankByFutureManual = ET_BASE + 117,
127
+ ET_ErrRtnQueryBankBalanceByFuture = ET_BASE + 118,
128
+ ET_RtnRepealFromBankToFutureByFuture = ET_BASE + 119,
129
+ ET_RtnRepealFromFutureToBankByFuture = ET_BASE + 120,
130
+ ET_RspFromBankToFutureByFuture = ET_BASE + 121,
131
+ ET_RspFromFutureToBankByFuture = ET_BASE + 122,
132
+ ET_RspQueryBankAccountMoneyByFuture = ET_BASE + 123,
133
+ ET_RtnOpenAccountByBank = ET_BASE + 124,
134
+ ET_RtnCancelAccountByBank = ET_BASE + 125,
135
+ ET_RtnChangeAccountByBank = ET_BASE + 126,
136
+ ET_RspQryClassifiedInstrument = ET_BASE + 127,
137
+ ET_RspQryCombPromotionParam = ET_BASE + 128,
138
+ ET_RspQryRiskSettleInvstPosition = ET_BASE + 129,
139
+ ET_RspQryRiskSettleProductStatus = ET_BASE + 130,
140
+ ET_RspQrySPBMFutureParameter = ET_BASE + 131,
141
+ ET_RspQrySPBMOptionParameter = ET_BASE + 132,
142
+ ET_RspQrySPBMIntraParameter = ET_BASE + 133,
143
+ ET_RspQrySPBMInterParameter = ET_BASE + 134,
144
+ ET_RspQrySPBMPortfDefinition = ET_BASE + 135,
145
+ ET_RspQrySPBMInvestorPortfDef = ET_BASE + 136,
146
+ ET_RspQryInvestorPortfMarginRatio = ET_BASE + 137,
147
+ ET_RspQryInvestorProdSPBMDetail = ET_BASE + 138,
148
+ ET_RspQryInvestorCommoditySPMMMargin = ET_BASE + 139,
149
+ ET_RspQryInvestorCommodityGroupSPMMMargin = ET_BASE + 140,
150
+ ET_RspQrySPMMInstParam = ET_BASE + 141,
151
+ ET_RspQrySPMMProductParam = ET_BASE + 142,
152
+ ET_RspQrySPBMAddOnInterParameter = ET_BASE + 143,
153
+ ET_RspQryRCAMSCombProductInfo = ET_BASE + 144,
154
+ ET_RspQryRCAMSInstrParameter = ET_BASE + 145,
155
+ ET_RspQryRCAMSIntraParameter = ET_BASE + 146,
156
+ ET_RspQryRCAMSInterParameter = ET_BASE + 147,
157
+ ET_RspQryRCAMSShortOptAdjustParam = ET_BASE + 148,
158
+ ET_RspQryRCAMSInvestorCombPosition = ET_BASE + 149,
159
+ ET_RspQryInvestorProdRCAMSMargin = ET_BASE + 150,
160
+ ET_RspQryRULEInstrParameter = ET_BASE + 151,
161
+ ET_RspQryRULEIntraParameter = ET_BASE + 152,
162
+ ET_RspQryRULEInterParameter = ET_BASE + 153,
163
+ ET_RspQryInvestorProdRULEMargin = ET_BASE + 154,
164
+ };
165
+
166
+ class TraderSpi : public CThostFtdcTraderSpi {
167
+ public:
168
+ TraderSpi(CThostFtdcTraderApi *api, EventChannel *channel) : api_(api), ch_(channel) {}
169
+ ~TraderSpi() {}
170
+
171
+ void setRisk(RiskEngine *r) { risk_ = r; }
172
+
173
+ void OnFrontConnected() override;
174
+ void OnFrontDisconnected(int arg) override;
175
+ void OnHeartBeatWarning(int arg) override;
176
+ void OnRspAuthenticate(CThostFtdcRspAuthenticateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
177
+ void OnRspUserLogin(CThostFtdcRspUserLoginField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
178
+ void OnRspUserLogout(CThostFtdcUserLogoutField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
179
+ void OnRspUserPasswordUpdate(CThostFtdcUserPasswordUpdateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
180
+ void OnRspTradingAccountPasswordUpdate(CThostFtdcTradingAccountPasswordUpdateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
181
+ void OnRspUserAuthMethod(CThostFtdcRspUserAuthMethodField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
182
+ void OnRspGenUserCaptcha(CThostFtdcRspGenUserCaptchaField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
183
+ void OnRspGenUserText(CThostFtdcRspGenUserTextField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
184
+ void OnRspOrderInsert(CThostFtdcInputOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
185
+ void OnRspParkedOrderInsert(CThostFtdcParkedOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
186
+ void OnRspParkedOrderAction(CThostFtdcParkedOrderActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
187
+ void OnRspOrderAction(CThostFtdcInputOrderActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
188
+ void OnRspQryMaxOrderVolume(CThostFtdcQryMaxOrderVolumeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
189
+ void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
190
+ void OnRspRemoveParkedOrder(CThostFtdcRemoveParkedOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
191
+ void OnRspRemoveParkedOrderAction(CThostFtdcRemoveParkedOrderActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
192
+ void OnRspExecOrderInsert(CThostFtdcInputExecOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
193
+ void OnRspExecOrderAction(CThostFtdcInputExecOrderActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
194
+ void OnRspForQuoteInsert(CThostFtdcInputForQuoteField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
195
+ void OnRspQuoteInsert(CThostFtdcInputQuoteField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
196
+ void OnRspQuoteAction(CThostFtdcInputQuoteActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
197
+ void OnRspBatchOrderAction(CThostFtdcInputBatchOrderActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
198
+ void OnRspOptionSelfCloseInsert(CThostFtdcInputOptionSelfCloseField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
199
+ void OnRspOptionSelfCloseAction(CThostFtdcInputOptionSelfCloseActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
200
+ void OnRspCombActionInsert(CThostFtdcInputCombActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
201
+ void OnRspQryOrder(CThostFtdcOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
202
+ void OnRspQryTrade(CThostFtdcTradeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
203
+ void OnRspQryInvestorPosition(CThostFtdcInvestorPositionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
204
+ void OnRspQryTradingAccount(CThostFtdcTradingAccountField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
205
+ void OnRspQryInvestor(CThostFtdcInvestorField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
206
+ void OnRspQryTradingCode(CThostFtdcTradingCodeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
207
+ void OnRspQryInstrumentMarginRate(CThostFtdcInstrumentMarginRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
208
+ void OnRspQryInstrumentCommissionRate(CThostFtdcInstrumentCommissionRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
209
+ void OnRspQryExchange(CThostFtdcExchangeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
210
+ void OnRspQryProduct(CThostFtdcProductField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
211
+ void OnRspQryInstrument(CThostFtdcInstrumentField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
212
+ void OnRspQryDepthMarketData(CThostFtdcDepthMarketDataField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
213
+ void OnRspQryTraderOffer(CThostFtdcTraderOfferField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
214
+ void OnRspQrySettlementInfo(CThostFtdcSettlementInfoField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
215
+ void OnRspQryTransferBank(CThostFtdcTransferBankField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
216
+ void OnRspQryInvestorPositionDetail(CThostFtdcInvestorPositionDetailField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
217
+ void OnRspQryNotice(CThostFtdcNoticeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
218
+ void OnRspQrySettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
219
+ void OnRspQryInvestorPositionCombineDetail(CThostFtdcInvestorPositionCombineDetailField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
220
+ void OnRspQryCFMMCTradingAccountKey(CThostFtdcCFMMCTradingAccountKeyField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
221
+ void OnRspQryEWarrantOffset(CThostFtdcEWarrantOffsetField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
222
+ void OnRspQryInvestorProductGroupMargin(CThostFtdcInvestorProductGroupMarginField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
223
+ void OnRspQryExchangeMarginRate(CThostFtdcExchangeMarginRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
224
+ void OnRspQryExchangeMarginRateAdjust(CThostFtdcExchangeMarginRateAdjustField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
225
+ void OnRspQryExchangeRate(CThostFtdcExchangeRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
226
+ void OnRspQrySecAgentACIDMap(CThostFtdcSecAgentACIDMapField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
227
+ void OnRspQryProductExchRate(CThostFtdcProductExchRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
228
+ void OnRspQryProductGroup(CThostFtdcProductGroupField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
229
+ void OnRspQryMMInstrumentCommissionRate(CThostFtdcMMInstrumentCommissionRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
230
+ void OnRspQryMMOptionInstrCommRate(CThostFtdcMMOptionInstrCommRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
231
+ void OnRspQryInstrumentOrderCommRate(CThostFtdcInstrumentOrderCommRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
232
+ void OnRspQrySecAgentTradingAccount(CThostFtdcTradingAccountField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
233
+ void OnRspQrySecAgentCheckMode(CThostFtdcSecAgentCheckModeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
234
+ void OnRspQrySecAgentTradeInfo(CThostFtdcSecAgentTradeInfoField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
235
+ void OnRspQryOptionInstrTradeCost(CThostFtdcOptionInstrTradeCostField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
236
+ void OnRspQryOptionInstrCommRate(CThostFtdcOptionInstrCommRateField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
237
+ void OnRspQryExecOrder(CThostFtdcExecOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
238
+ void OnRspQryForQuote(CThostFtdcForQuoteField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
239
+ void OnRspQryQuote(CThostFtdcQuoteField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
240
+ void OnRspQryOptionSelfClose(CThostFtdcOptionSelfCloseField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
241
+ void OnRspQryInvestUnit(CThostFtdcInvestUnitField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
242
+ void OnRspQryCombInstrumentGuard(CThostFtdcCombInstrumentGuardField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
243
+ void OnRspQryCombAction(CThostFtdcCombActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
244
+ void OnRspQryTransferSerial(CThostFtdcTransferSerialField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
245
+ void OnRspQryAccountregister(CThostFtdcAccountregisterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
246
+ void OnRspError(CThostFtdcRspInfoField *e, int id, bool last) override;
247
+ void OnRtnOrder(CThostFtdcOrderField *p) override;
248
+ void OnRtnTrade(CThostFtdcTradeField *p) override;
249
+ void OnErrRtnOrderInsert(CThostFtdcInputOrderField *p, CThostFtdcRspInfoField *e) override;
250
+ void OnErrRtnOrderAction(CThostFtdcOrderActionField *p, CThostFtdcRspInfoField *e) override;
251
+ void OnRtnInstrumentStatus(CThostFtdcInstrumentStatusField *p) override;
252
+ void OnRtnBulletin(CThostFtdcBulletinField *p) override;
253
+ void OnRtnTradingNotice(CThostFtdcTradingNoticeInfoField *p) override;
254
+ void OnRtnErrorConditionalOrder(CThostFtdcErrorConditionalOrderField *p) override;
255
+ void OnRtnExecOrder(CThostFtdcExecOrderField *p) override;
256
+ void OnErrRtnExecOrderInsert(CThostFtdcInputExecOrderField *p, CThostFtdcRspInfoField *e) override;
257
+ void OnErrRtnExecOrderAction(CThostFtdcExecOrderActionField *p, CThostFtdcRspInfoField *e) override;
258
+ void OnErrRtnForQuoteInsert(CThostFtdcInputForQuoteField *p, CThostFtdcRspInfoField *e) override;
259
+ void OnRtnQuote(CThostFtdcQuoteField *p) override;
260
+ void OnErrRtnQuoteInsert(CThostFtdcInputQuoteField *p, CThostFtdcRspInfoField *e) override;
261
+ void OnErrRtnQuoteAction(CThostFtdcQuoteActionField *p, CThostFtdcRspInfoField *e) override;
262
+ void OnRtnForQuoteRsp(CThostFtdcForQuoteRspField *p) override;
263
+ void OnRtnCFMMCTradingAccountToken(CThostFtdcCFMMCTradingAccountTokenField *p) override;
264
+ void OnErrRtnBatchOrderAction(CThostFtdcBatchOrderActionField *p, CThostFtdcRspInfoField *e) override;
265
+ void OnRtnOptionSelfClose(CThostFtdcOptionSelfCloseField *p) override;
266
+ void OnErrRtnOptionSelfCloseInsert(CThostFtdcInputOptionSelfCloseField *p, CThostFtdcRspInfoField *e) override;
267
+ void OnErrRtnOptionSelfCloseAction(CThostFtdcOptionSelfCloseActionField *p, CThostFtdcRspInfoField *e) override;
268
+ void OnRtnCombAction(CThostFtdcCombActionField *p) override;
269
+ void OnErrRtnCombActionInsert(CThostFtdcInputCombActionField *p, CThostFtdcRspInfoField *e) override;
270
+ void OnRspQryContractBank(CThostFtdcContractBankField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
271
+ void OnRspQryParkedOrder(CThostFtdcParkedOrderField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
272
+ void OnRspQryParkedOrderAction(CThostFtdcParkedOrderActionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
273
+ void OnRspQryTradingNotice(CThostFtdcTradingNoticeField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
274
+ void OnRspQryBrokerTradingParams(CThostFtdcBrokerTradingParamsField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
275
+ void OnRspQryBrokerTradingAlgos(CThostFtdcBrokerTradingAlgosField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
276
+ void OnRspQueryCFMMCTradingAccountToken(CThostFtdcQueryCFMMCTradingAccountTokenField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
277
+ void OnRtnFromBankToFutureByBank(CThostFtdcRspTransferField *p) override;
278
+ void OnRtnFromFutureToBankByBank(CThostFtdcRspTransferField *p) override;
279
+ void OnRtnRepealFromBankToFutureByBank(CThostFtdcRspRepealField *p) override;
280
+ void OnRtnRepealFromFutureToBankByBank(CThostFtdcRspRepealField *p) override;
281
+ void OnRtnFromBankToFutureByFuture(CThostFtdcRspTransferField *p) override;
282
+ void OnRtnFromFutureToBankByFuture(CThostFtdcRspTransferField *p) override;
283
+ void OnRtnRepealFromBankToFutureByFutureManual(CThostFtdcRspRepealField *p) override;
284
+ void OnRtnRepealFromFutureToBankByFutureManual(CThostFtdcRspRepealField *p) override;
285
+ void OnRtnQueryBankBalanceByFuture(CThostFtdcNotifyQueryAccountField *p) override;
286
+ void OnErrRtnBankToFutureByFuture(CThostFtdcReqTransferField *p, CThostFtdcRspInfoField *e) override;
287
+ void OnErrRtnFutureToBankByFuture(CThostFtdcReqTransferField *p, CThostFtdcRspInfoField *e) override;
288
+ void OnErrRtnRepealBankToFutureByFutureManual(CThostFtdcReqRepealField *p, CThostFtdcRspInfoField *e) override;
289
+ void OnErrRtnRepealFutureToBankByFutureManual(CThostFtdcReqRepealField *p, CThostFtdcRspInfoField *e) override;
290
+ void OnErrRtnQueryBankBalanceByFuture(CThostFtdcReqQueryAccountField *p, CThostFtdcRspInfoField *e) override;
291
+ void OnRtnRepealFromBankToFutureByFuture(CThostFtdcRspRepealField *p) override;
292
+ void OnRtnRepealFromFutureToBankByFuture(CThostFtdcRspRepealField *p) override;
293
+ void OnRspFromBankToFutureByFuture(CThostFtdcReqTransferField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
294
+ void OnRspFromFutureToBankByFuture(CThostFtdcReqTransferField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
295
+ void OnRspQueryBankAccountMoneyByFuture(CThostFtdcReqQueryAccountField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
296
+ void OnRtnOpenAccountByBank(CThostFtdcOpenAccountField *p) override;
297
+ void OnRtnCancelAccountByBank(CThostFtdcCancelAccountField *p) override;
298
+ void OnRtnChangeAccountByBank(CThostFtdcChangeAccountField *p) override;
299
+ void OnRspQryClassifiedInstrument(CThostFtdcInstrumentField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
300
+ void OnRspQryCombPromotionParam(CThostFtdcCombPromotionParamField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
301
+ void OnRspQryRiskSettleInvstPosition(CThostFtdcRiskSettleInvstPositionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
302
+ void OnRspQryRiskSettleProductStatus(CThostFtdcRiskSettleProductStatusField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
303
+ void OnRspQrySPBMFutureParameter(CThostFtdcSPBMFutureParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
304
+ void OnRspQrySPBMOptionParameter(CThostFtdcSPBMOptionParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
305
+ void OnRspQrySPBMIntraParameter(CThostFtdcSPBMIntraParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
306
+ void OnRspQrySPBMInterParameter(CThostFtdcSPBMInterParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
307
+ void OnRspQrySPBMPortfDefinition(CThostFtdcSPBMPortfDefinitionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
308
+ void OnRspQrySPBMInvestorPortfDef(CThostFtdcSPBMInvestorPortfDefField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
309
+ void OnRspQryInvestorPortfMarginRatio(CThostFtdcInvestorPortfMarginRatioField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
310
+ void OnRspQryInvestorProdSPBMDetail(CThostFtdcInvestorProdSPBMDetailField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
311
+ void OnRspQryInvestorCommoditySPMMMargin(CThostFtdcInvestorCommoditySPMMMarginField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
312
+ void OnRspQryInvestorCommodityGroupSPMMMargin(CThostFtdcInvestorCommodityGroupSPMMMarginField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
313
+ void OnRspQrySPMMInstParam(CThostFtdcSPMMInstParamField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
314
+ void OnRspQrySPMMProductParam(CThostFtdcSPMMProductParamField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
315
+ void OnRspQrySPBMAddOnInterParameter(CThostFtdcSPBMAddOnInterParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
316
+ void OnRspQryRCAMSCombProductInfo(CThostFtdcRCAMSCombProductInfoField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
317
+ void OnRspQryRCAMSInstrParameter(CThostFtdcRCAMSInstrParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
318
+ void OnRspQryRCAMSIntraParameter(CThostFtdcRCAMSIntraParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
319
+ void OnRspQryRCAMSInterParameter(CThostFtdcRCAMSInterParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
320
+ void OnRspQryRCAMSShortOptAdjustParam(CThostFtdcRCAMSShortOptAdjustParamField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
321
+ void OnRspQryRCAMSInvestorCombPosition(CThostFtdcRCAMSInvestorCombPositionField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
322
+ void OnRspQryInvestorProdRCAMSMargin(CThostFtdcInvestorProdRCAMSMarginField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
323
+ void OnRspQryRULEInstrParameter(CThostFtdcRULEInstrParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
324
+ void OnRspQryRULEIntraParameter(CThostFtdcRULEIntraParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
325
+ void OnRspQryRULEInterParameter(CThostFtdcRULEInterParameterField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
326
+ void OnRspQryInvestorProdRULEMargin(CThostFtdcInvestorProdRULEMarginField *p, CThostFtdcRspInfoField *e, int id, bool last) override;
327
+
328
+ private:
329
+ CThostFtdcTraderApi *api_;
330
+ EventChannel *ch_;
331
+ RiskEngine *risk_ = nullptr;
332
+ };
333
+ } // namespace ctp
334
+ #endif
package/src/index.ts ADDED
@@ -0,0 +1,14 @@
1
+ /*
2
+ * index.ts - public entry point.
3
+ */
4
+
5
+ export { MarketData, MarketDataEvent } from "./market-data.js";
6
+ export type { MdLoginReq } from "./market-data.js";
7
+ export { Trader, TraderEvent } from "./trader.js";
8
+ export type { RiskConfig, LotCap, SessionOptions, ArmSpec, ArmHandle } from "./trader.js";
9
+ export { CtpClient } from "./client.js";
10
+ export type { CallbackOptions, RspInfo } from "./client.js";
11
+
12
+ // Generated CTP enums (values) and struct interfaces (types).
13
+ export * from "./generated/enums.gen.js";
14
+ export type * from "./generated/structs.gen.js";
@@ -0,0 +1,145 @@
1
+ /*
2
+ * market-data.ts - the MarketData client (行情).
3
+ *
4
+ * EventEmitter for streaming pushes (rtn-depth-market-data, ...) + Promise for
5
+ * request/response (login). Event codes mirror src/native/mdspi.h.
6
+ */
7
+
8
+ import { native } from "./native-binding.js";
9
+ import { CtpClient, type CallbackOptions } from "./client.js";
10
+ import type {
11
+ DepthMarketData,
12
+ ForQuoteRsp,
13
+ SpecificInstrument,
14
+ RspUserLogin,
15
+ UserLogout,
16
+ } from "./generated/structs.gen.js";
17
+
18
+ const MD_BASE = 0x1000;
19
+ const MD_EVENTS = new Map<number, string>([
20
+ [MD_BASE + 1, "front-connected"],
21
+ [MD_BASE + 2, "front-disconnected"],
22
+ [MD_BASE + 3, "heart-beat-warning"],
23
+ [MD_BASE + 4, "rsp-user-login"],
24
+ [MD_BASE + 5, "rsp-user-logout"],
25
+ [MD_BASE + 6, "rsp-qry-multicast-instrument"],
26
+ [MD_BASE + 7, "rsp-error"],
27
+ [MD_BASE + 8, "rsp-sub-market-data"],
28
+ [MD_BASE + 9, "rsp-unsub-market-data"],
29
+ [MD_BASE + 10, "rsp-sub-for-quote"],
30
+ [MD_BASE + 11, "rsp-unsub-for-quote"],
31
+ [MD_BASE + 12, "rtn-depth-market-data"],
32
+ [MD_BASE + 13, "rtn-for-quote"],
33
+ ]);
34
+
35
+ export interface MdLoginReq {
36
+ tradingDay?: string;
37
+ brokerId?: string;
38
+ userId?: string;
39
+ password?: string;
40
+ userProductInfo?: string;
41
+ }
42
+
43
+ /** Market-data event names (symbolic; plain strings also work). */
44
+ export enum MarketDataEvent {
45
+ FrontConnected = "front-connected",
46
+ FrontDisconnected = "front-disconnected",
47
+ HeartBeatWarning = "heart-beat-warning",
48
+ RspUserLogin = "rsp-user-login",
49
+ RspUserLogout = "rsp-user-logout",
50
+ RspQryMulticastInstrument = "rsp-qry-multicast-instrument",
51
+ RspError = "rsp-error",
52
+ RspSubMarketData = "rsp-sub-market-data",
53
+ RspUnSubMarketData = "rsp-unsub-market-data",
54
+ RspSubForQuote = "rsp-sub-for-quote",
55
+ RspUnSubForQuote = "rsp-unsub-for-quote",
56
+ RtnDepthMarketData = "rtn-depth-market-data",
57
+ RtnForQuote = "rtn-for-quote",
58
+ }
59
+
60
+ export class MarketData extends CtpClient {
61
+ constructor(flowPath: string, fronts: string | string[]) {
62
+ super(new native.MarketData(flowPath, fronts), native.__layoutData(), MD_EVENTS);
63
+ // SimNow's MD login/logout responses come back with requestId 0. Scope the
64
+ // id-0 fallback to those two events (which are never concurrent) so an id-0
65
+ // sub/unsub/error response can't hijack a pending login()/logout().
66
+ this.zeroIdFallbackEvents.add(MD_BASE + 4); // rsp-user-login
67
+ this.zeroIdFallbackEvents.add(MD_BASE + 5); // rsp-user-logout
68
+ this.start();
69
+ }
70
+
71
+ getApiVersion(): string {
72
+ return (this.native as Record<string, () => string>).getApiVersion();
73
+ }
74
+ getTradingDay(): string {
75
+ return (this.native as Record<string, () => string>).getTradingDay();
76
+ }
77
+
78
+ /** Subscribe to depth market data. Returns the CTP send code (0 = ok). */
79
+ subscribe(instrumentIds: string[]): number {
80
+ return (this.native as { subscribeMarketData(a: string[]): number }).subscribeMarketData(instrumentIds);
81
+ }
82
+ unsubscribe(instrumentIds: string[]): number {
83
+ return (this.native as { unsubscribeMarketData(a: string[]): number }).unsubscribeMarketData(instrumentIds);
84
+ }
85
+ subscribeForQuote(instrumentIds: string[]): number {
86
+ return (this.native as { subscribeForQuoteRsp(a: string[]): number }).subscribeForQuoteRsp(instrumentIds);
87
+ }
88
+ unsubscribeForQuote(instrumentIds: string[]): number {
89
+ return (this.native as { unsubscribeForQuoteRsp(a: string[]): number }).unsubscribeForQuoteRsp(instrumentIds);
90
+ }
91
+
92
+ /** Log in; resolves with the login response (or rejects on CTP error). */
93
+ login(req: MdLoginReq = {}): Promise<RspUserLogin> {
94
+ return this.request<RspUserLogin>(
95
+ (id) => (this.native as { reqUserLogin(r: MdLoginReq, id: number): number }).reqUserLogin(req, id),
96
+ true
97
+ );
98
+ }
99
+ logout(req: { brokerId?: string; userId?: string } = {}): Promise<UserLogout> {
100
+ return this.request<UserLogout>(
101
+ (id) => (this.native as { reqUserLogout(r: object, id: number): number }).reqUserLogout(req, id),
102
+ true
103
+ );
104
+ }
105
+
106
+ private armedTrader?: object;
107
+ /** Route this market data's ticks to a Trader's armed triggers (see Trader.arm).
108
+ * A MarketData feeds exactly one Trader's registry; attaching a second,
109
+ * different Trader throws rather than silently stealing the feed. */
110
+ attachArm(trader: { _armRegistry(): unknown }): void {
111
+ if (this.armedTrader && this.armedTrader !== trader) {
112
+ throw new Error("this MarketData already feeds another Trader's armed triggers");
113
+ }
114
+ this.armedTrader = trader;
115
+ this.native._attachArm(trader._armRegistry());
116
+ }
117
+
118
+ /** @internal test-only: inject a synthetic depth tick into the ring. Optional
119
+ * bid/ask override the defaults (3499/3501) so tests can drive edge prices
120
+ * (e.g. a DBL_MAX no-bid sentinel) through the real arm/onTick path. */
121
+ _injectTestTick(bid?: number, ask?: number): void {
122
+ (this.native as { _injectTestTick(b?: number, a?: number): void })._injectTestTick(bid, ask);
123
+ }
124
+
125
+ /** @internal test-only: spawn a background producer thread that pushes `n`
126
+ * synthetic ticks into the ring (cross-thread doorbell stress). Each tick's
127
+ * requestId carries its sequence number. */
128
+ _burstTicks(n: number, jitterUs = 0): void {
129
+ (this.native as { _burstTicks(n: number, j: number): void })._burstTicks(n, jitterUs);
130
+ }
131
+
132
+ // Typed event signatures.
133
+ on(event: "rtn-depth-market-data", cb: (data: DepthMarketData, opts: CallbackOptions) => void): this;
134
+ on(event: "rtn-for-quote", cb: (data: ForQuoteRsp, opts: CallbackOptions) => void): this;
135
+ on(event: "front-connected", cb: (data: undefined, opts: CallbackOptions) => void): this;
136
+ on(event: "front-disconnected", cb: (reason: number, opts: CallbackOptions) => void): this;
137
+ on(event: "heart-beat-warning", cb: (lapse: number, opts: CallbackOptions) => void): this;
138
+ on(event: "rsp-user-login", cb: (data: RspUserLogin, opts: CallbackOptions) => void): this;
139
+ on(event: "rsp-sub-market-data", cb: (data: SpecificInstrument, opts: CallbackOptions) => void): this;
140
+ on(event: "rsp-error", cb: (data: undefined, opts: CallbackOptions) => void): this;
141
+ on(event: string, cb: (...args: never[]) => void): this;
142
+ on(event: string, cb: (...args: never[]) => void): this {
143
+ return super.on(event, cb as (...args: unknown[]) => void);
144
+ }
145
+ }
@@ -0,0 +1,77 @@
1
+ /*
2
+ * arm.cc - see arm.h
3
+ */
4
+
5
+ #include "arm.h"
6
+
7
+ namespace ctp {
8
+
9
+ void ArmRegistry::setSink(OrderSink *sink) {
10
+ std::lock_guard<std::mutex> lk(m_);
11
+ sink_ = sink;
12
+ }
13
+
14
+ void ArmRegistry::clearSink() {
15
+ std::lock_guard<std::mutex> lk(m_);
16
+ sink_ = nullptr;
17
+ }
18
+
19
+ uint64_t ArmRegistry::arm(ArmSpec spec) {
20
+ std::lock_guard<std::mutex> lk(m_);
21
+ spec.id = nextId_++;
22
+ spec.fired = false;
23
+ armed_.push_back(std::move(spec));
24
+ count_.store(armed_.size(), std::memory_order_relaxed);
25
+ return armed_.back().id;
26
+ }
27
+
28
+ bool ArmRegistry::disarm(uint64_t id) {
29
+ std::lock_guard<std::mutex> lk(m_);
30
+ for (auto it = armed_.begin(); it != armed_.end(); ++it) {
31
+ if (it->id == id) {
32
+ armed_.erase(it);
33
+ count_.store(armed_.size(), std::memory_order_relaxed);
34
+ return true;
35
+ }
36
+ }
37
+ return false;
38
+ }
39
+
40
+ size_t ArmRegistry::size() const {
41
+ std::lock_guard<std::mutex> lk(m_);
42
+ return armed_.size();
43
+ }
44
+
45
+ void ArmRegistry::onTick(const char *instrumentId, double bid, double ask) {
46
+ if (count_.load(std::memory_order_relaxed) == 0)
47
+ return; // no arms: zero overhead on the tick hot path
48
+
49
+ std::lock_guard<std::mutex> lk(m_);
50
+ if (!sink_)
51
+ return;
52
+
53
+ for (auto &a : armed_) {
54
+ if (a.fired || a.instrumentId != instrumentId)
55
+ continue;
56
+ // The bid/ask must be a real, finite, sub-sentinel price. CTP writes DBL_MAX
57
+ // (~1.8e308) into BidPrice1/AskPrice1 when a side is empty - e.g. at
58
+ // limit-down there are no bids, so BidPrice1 == DBL_MAX. Without the
59
+ // `< 1e300` guard a SELL trigger (bid >= triggerPrice) would fire on that
60
+ // sentinel: a spurious market sell at the worst possible moment. Mirrors the
61
+ // sentinel band rejected everywhere in risk.cc (sanePositive).
62
+ const bool hit = (a.side == '0')
63
+ ? (ask > 0.0 && ask < 1e300 && ask <= a.triggerPrice)
64
+ : (bid > 0.0 && bid < 1e300 && bid >= a.triggerPrice);
65
+ if (!hit)
66
+ continue;
67
+ a.fired = true; // one-shot: consumed on the first trigger, sent or blocked
68
+ // build order + risk + ReqOrderInsert on this thread; record the outcome so
69
+ // a risk-blocked/failed armed order isn't silent (fireCount would mislead).
70
+ if (sink_->fireArmed(a) == 0)
71
+ fireCount_.fetch_add(1, std::memory_order_relaxed);
72
+ else
73
+ blockedCount_.fetch_add(1, std::memory_order_relaxed);
74
+ }
75
+ }
76
+
77
+ } // namespace ctp
@@ -0,0 +1,71 @@
1
+ /*
2
+ * arm.h - latency-critical "armed" triggers.
3
+ *
4
+ * An armed order is evaluated on the MarketData callback thread on every depth
5
+ * tick and, the instant its trigger hits, fired through the Trader (pre-trade
6
+ * risk + ReqOrderInsert) WITHOUT crossing into JS. The order acknowledgement
7
+ * flows back through the normal trader events (rtn-order / rsp-order-insert);
8
+ * correlate via the order's OrderRef.
9
+ *
10
+ * Lifetime: the registry is shared (shared_ptr) between the Trader that owns it
11
+ * and any MarketData feeding it ticks, so neither side can dangle. The Trader
12
+ * clears the sink (under the registry lock) before releasing its API, so no
13
+ * fire can race a teardown.
14
+ */
15
+
16
+ #ifndef CTP_NATIVE_ARM_H
17
+ #define CTP_NATIVE_ARM_H
18
+
19
+ #include <atomic>
20
+ #include <cstdint>
21
+ #include <mutex>
22
+ #include <string>
23
+ #include <vector>
24
+
25
+ namespace ctp {
26
+
27
+ struct ArmSpec {
28
+ uint64_t id = 0;
29
+ std::string instrumentId;
30
+ char side = '0'; // '0' buy: fire when ask<=trigger; '1' sell: bid>=trigger
31
+ double triggerPrice = 0.0;
32
+ std::vector<uint8_t> orderTemplate; // pre-encoded CThostFtdcInputOrderField
33
+ bool fired = false;
34
+ };
35
+
36
+ // Implemented by the Trader. Called on the MD callback thread; must be
37
+ // thread-safe (CTP request methods are).
38
+ struct OrderSink {
39
+ virtual ~OrderSink() {}
40
+ virtual int fireArmed(const ArmSpec &spec) = 0;
41
+ };
42
+
43
+ class ArmRegistry {
44
+ public:
45
+ void setSink(OrderSink *sink); // link (Trader ctor)
46
+ void clearSink(); // unlink under lock (Trader teardown)
47
+
48
+ uint64_t arm(ArmSpec spec); // JS thread
49
+ bool disarm(uint64_t id); // JS thread
50
+ size_t size() const;
51
+ // Armed triggers that fired and were SENT (fireArmed returned 0).
52
+ uint64_t fireCount() const { return fireCount_.load(std::memory_order_relaxed); }
53
+ // Armed triggers that fired but were REFUSED by the risk gate / send (rc!=0).
54
+ uint64_t blockedCount() const { return blockedCount_.load(std::memory_order_relaxed); }
55
+
56
+ // Hot path: MD callback thread, once per depth tick.
57
+ void onTick(const char *instrumentId, double bid, double ask);
58
+
59
+ private:
60
+ mutable std::mutex m_;
61
+ std::vector<ArmSpec> armed_;
62
+ uint64_t nextId_ = 1;
63
+ std::atomic<size_t> count_{0}; // lock-free "any arms?" gate
64
+ std::atomic<uint64_t> fireCount_{0};
65
+ std::atomic<uint64_t> blockedCount_{0};
66
+ OrderSink *sink_ = nullptr; // guarded by m_
67
+ };
68
+
69
+ } // namespace ctp
70
+
71
+ #endif /* CTP_NATIVE_ARM_H */