Libannhoo_md_sdk  1.6.1
for index, tick(entrust and trade), and snapshot with solar flare
 All Classes Namespaces Files Functions Variables Pages
subscribe.cpp
Go to the documentation of this file.
1 
10 #include "cmdline.h"
11 
12 #include <iostream>
13 #include <unistd.h>
14 
15 using namespace std;
16 
21 namespace annhoo {
28 class SubscribeAndPrint : public IAHMdSpi {
29 public:
34  {}
35 
39  virtual ~SubscribeAndPrint(){};
40 
44  SubscribeAndPrint(const SubscribeAndPrint& r) = delete;
45 
50 
54  SubscribeAndPrint& operator=(const SubscribeAndPrint& r) = delete;
55 
59  SubscribeAndPrint& operator=(SubscribeAndPrint&& r) = delete;
60 
61 public:
67  virtual void OnIndexCB(AHIndex* index) override
68  {
69  PrintIndex(index);
70  }
71 
77  virtual void OnTickCB(AHTick* tick, uint16_t type) override
78  {
79  if (type == AHType::ENTRUST) {
80  PrintEntrus(tick);
81  } else {
82  PrintTrade(tick);
83  }
84  }
85 
91  virtual void OnSnapshotCB(AHSnapshot* snapshot) override
92  {
93  PrintSnapshot(snapshot);
94  }
95 
96 public:
102  void PrintIndex(AHIndex* index)
103  {
104  if (!flag_) {
105  return;
106  }
107 
108  if (index->header.ExchangeID[1] == 'Z') {
109  std::cout << index->header.MsgType << '\n'
110  << std::string_view(index->header.ExchangeID, 2) << '\n'
111  << std::string_view(index->MDStreamID, 4) << '\n'
112  << index->OrigTime << '\n'
113  << index->ChannelNo << '\n'
114  << std::string_view(index->TradingPhaseCode, 2) << '\n'
115  << std::string_view(index->SecurityID, 8) << '\n'
116  << index->LastIndex << '\n'
117  << index->PreCloseIndex << '\n'
118  << index->OpenIndex << '\n'
119  << index->HighIndex << '\n'
120  << index->LowIndex << '\n'
121  << index->CloseIndex << '\n'
122  << index->TotalVolumeTrade << '\n'
123  << index->TotalValueTrade << '\n'
124  << index->NumTrades << '\n'
125  << '\n';
126  } else {
127  std::cout << index->header.MsgType << '\n'
128  << std::string_view(index->header.ExchangeID, 2) << '\n'
129  << index->TradeTime << '\n'
130  << std::string_view(index->SecurityID, 8) << '\n'
131  << index->LastIndex << '\n'
132  << index->PreCloseIndex << '\n'
133  << index->OpenIndex << '\n'
134  << index->HighIndex << '\n'
135  << index->LowIndex << '\n'
136  << index->CloseIndex << '\n'
137  << index->TotalVolumeTrade << '\n'
138  << index->TotalValueTrade << '\n'
139  << index->NumTrades << '\n'
140  << '\n';
141  }
142 
143  index_.fetch_add(1, std::memory_order_relaxed);
144  }
145 
151  void PrintEntrus(AHTick* entrust)
152  {
153  if (!flag_) {
154  return;
155  }
156 
157  if (entrust->header.ExchangeID[1] == 'Z') {
158  std::cout << entrust->header.MsgType << '\n'
159  << std::string_view(entrust->header.ExchangeID, 2) << '\n'
160  << std::string_view(entrust->MDStreamID, 4) << '\n'
161  << entrust->ChannelNo << '\n'
162  << entrust->Side << '\n'
163  << entrust->OrdType << '\n'
164  << entrust->ApplSeqNum << '\n'
165  << std::string_view(entrust->SecurityID, 8) << '\n'
166  << entrust->Price << '\n'
167  << entrust->OrderQty << '\n'
168  << entrust->TransactTime << '\n'
169  << entrust->OrdType << '\n'
170  << '\n';
171  } else {
172  std::cout << entrust->header.MsgType << '\n'
173  << std::string_view(entrust->header.ExchangeID, 2) << '\n'
174  << entrust->OrderIndex << '\n'
175  << entrust->Channel << '\n'
176  << entrust->OrderBSFlag << '\n'
177  << entrust->OrderType << '\n'
178  << entrust->BizIndex << '\n'
179  << std::string_view(entrust->SecurityID, 8) << '\n'
180  << entrust->OrderPrice << '\n'
181  << entrust->Balance << '\n'
182  << entrust->OrderTime << '\n'
183  << entrust->OrderNO << '\n'
184  << '\n';
185  }
186  entrust_.fetch_add(1, std::memory_order_relaxed);
187  }
188 
194  void PrintTrade(AHTick* trade)
195  {
196  if (!flag_) {
197  return;
198  }
199 
200  if (trade->header.ExchangeID[1] == 'Z') {
201  std::cout << trade->header.MsgType << '\n'
202  << std::string_view(trade->header.ExchangeID, 2) << '\n'
203  << std::string_view(trade->MDStreamID, 4) << '\n'
204  << trade->ChannelNo << '\n'
205  << trade->ExecType << '\n'
206  << trade->ApplSeqNum << '\n'
207  << std::string_view(trade->SecurityID, 8) << '\n'
208  << trade->LastPx << '\n'
209  << trade->LastQty << '\n'
210  << trade->TransactTime << '\n'
211  << trade->BidApplSeqNum << '\n'
212  << trade->OfferApplSeqNum << '\n'
213  << '\n';
214  } else {
215  std::cout << trade->header.MsgType << '\n'
216  << std::string_view(trade->header.ExchangeID, 2) << '\n'
217  << trade->TradeIndex << '\n'
218  << trade->TradeChannel << '\n'
219  << trade->TradeBSFlag << '\n'
220  << trade->BizIndex << '\n'
221  << std::string_view(trade->SecurityID, 8) << '\n'
222  << trade->TradePrice << '\n'
223  << trade->TradeQty << '\n'
224  << trade->TradeTime << '\n'
225  << trade->TradeSellNo << '\n'
226  << trade->TradeMony << '\n'
227  << '\n';
228  }
229 
230  trade_.fetch_add(1, std::memory_order_relaxed);
231  }
232 
238  void PrintSnapshot(AHSnapshot* snapshot)
239  {
240  if (!flag_) {
241  return;
242  }
243 
244  if (snapshot->header.ExchangeID[1] == 'Z') {
245  std::cout << snapshot->header.MsgType << '\n'
246  << std::string_view(snapshot->header.ExchangeID, 2) << '\n'
247  << std::string_view(snapshot->MDStreamID, 4) << '\n'
248  << snapshot->OrigTime << '\n'
249  << snapshot->ChannelNo << '\n'
250  << std::string_view(snapshot->TradingPhaseCode, 4) << '\n'
251  << std::string_view(snapshot->SecurityID, 8) << '\n'
252  << snapshot->BidPrice[0] << '\n'
253  << snapshot->BidPrice[1] << '\n'
254  << snapshot->BidPrice[2] << '\n'
255  << snapshot->BidPrice[3] << '\n'
256  << snapshot->BidPrice[4] << '\n'
257  << snapshot->BidPrice[5] << '\n'
258  << snapshot->BidPrice[6] << '\n'
259  << snapshot->BidPrice[7] << '\n'
260  << snapshot->BidPrice[8] << '\n'
261  << snapshot->BidPrice[9] << '\n'
262  << snapshot->AskPrice[0] << '\n'
263  << snapshot->AskPrice[1] << '\n'
264  << snapshot->AskPrice[2] << '\n'
265  << snapshot->AskPrice[3] << '\n'
266  << snapshot->AskPrice[4] << '\n'
267  << snapshot->AskPrice[5] << '\n'
268  << snapshot->AskPrice[6] << '\n'
269  << snapshot->AskPrice[7] << '\n'
270  << snapshot->AskPrice[8] << '\n'
271  << snapshot->AskPrice[9] << '\n'
272  << snapshot->BidVolume[0] << '\n'
273  << snapshot->BidVolume[1] << '\n'
274  << snapshot->BidVolume[2] << '\n'
275  << snapshot->BidVolume[3] << '\n'
276  << snapshot->BidVolume[4] << '\n'
277  << snapshot->BidVolume[5] << '\n'
278  << snapshot->BidVolume[6] << '\n'
279  << snapshot->BidVolume[7] << '\n'
280  << snapshot->BidVolume[8] << '\n'
281  << snapshot->BidVolume[9] << '\n'
282  << snapshot->AskVolume[0] << '\n'
283  << snapshot->AskVolume[1] << '\n'
284  << snapshot->AskVolume[2] << '\n'
285  << snapshot->AskVolume[3] << '\n'
286  << snapshot->AskVolume[4] << '\n'
287  << snapshot->AskVolume[5] << '\n'
288  << snapshot->AskVolume[6] << '\n'
289  << snapshot->AskVolume[7] << '\n'
290  << snapshot->AskVolume[8] << '\n'
291  << snapshot->AskVolume[9] << '\n'
292  << snapshot->PrevClosePx << '\n'
293  << snapshot->OpenPx << '\n'
294  << snapshot->HighPx << '\n'
295  << snapshot->LowPx << '\n'
296  << snapshot->LastPx << '\n'
297  << snapshot->ClosePx << '\n'
298  << snapshot->NumTrades << '\n'
299  << snapshot->TotalVolumeTrade << '\n'
300  << snapshot->TotalValueTrade << '\n'
301  << snapshot->TotalBidQty << '\n'
302  << snapshot->WeightedAvgBidPx << '\n'
303  << snapshot->TotalOfferQty << '\n'
304  << snapshot->WeightedAvgOfferPx << '\n'
305  << snapshot->IOPV << '\n'
306  << snapshot->LPV << '\n'
307  << snapshot->UpperLimitPrice << '\n'
308  << snapshot->LowerLimitPrice << '\n'
309  << snapshot->OpenInterest << '\n'
310  << snapshot->LastSubPrevClosePrice << '\n'
311  << snapshot->LastSubPrevLastPrice << '\n'
312  << '\n';
313  } else {
314  std::cout << snapshot->header.MsgType << '\n'
315  << std::string_view(snapshot->header.ExchangeID, 2) << '\n'
316  << snapshot->OrigTime << '\n'
317  << std::string_view(snapshot->InstrumentStatus, 2) << '\n'
318  << std::string_view(snapshot->TradingPhaseCode, 4) << '\n'
319  << std::string_view(snapshot->SecurityID, 8) << '\n'
320  << snapshot->BidPrice[0] << '\n'
321  << snapshot->BidPrice[1] << '\n'
322  << snapshot->BidPrice[2] << '\n'
323  << snapshot->BidPrice[3] << '\n'
324  << snapshot->BidPrice[4] << '\n'
325  << snapshot->BidPrice[5] << '\n'
326  << snapshot->BidPrice[6] << '\n'
327  << snapshot->BidPrice[7] << '\n'
328  << snapshot->BidPrice[8] << '\n'
329  << snapshot->BidPrice[9] << '\n'
330  << snapshot->AskPrice[0] << '\n'
331  << snapshot->AskPrice[1] << '\n'
332  << snapshot->AskPrice[2] << '\n'
333  << snapshot->AskPrice[3] << '\n'
334  << snapshot->AskPrice[4] << '\n'
335  << snapshot->AskPrice[5] << '\n'
336  << snapshot->AskPrice[6] << '\n'
337  << snapshot->AskPrice[7] << '\n'
338  << snapshot->AskPrice[8] << '\n'
339  << snapshot->AskPrice[9] << '\n'
340  << snapshot->BidVolume[0] << '\n'
341  << snapshot->BidVolume[1] << '\n'
342  << snapshot->BidVolume[2] << '\n'
343  << snapshot->BidVolume[3] << '\n'
344  << snapshot->BidVolume[4] << '\n'
345  << snapshot->BidVolume[5] << '\n'
346  << snapshot->BidVolume[6] << '\n'
347  << snapshot->BidVolume[7] << '\n'
348  << snapshot->BidVolume[8] << '\n'
349  << snapshot->BidVolume[9] << '\n'
350  << snapshot->AskVolume[0] << '\n'
351  << snapshot->AskVolume[1] << '\n'
352  << snapshot->AskVolume[2] << '\n'
353  << snapshot->AskVolume[3] << '\n'
354  << snapshot->AskVolume[4] << '\n'
355  << snapshot->AskVolume[5] << '\n'
356  << snapshot->AskVolume[6] << '\n'
357  << snapshot->AskVolume[7] << '\n'
358  << snapshot->AskVolume[8] << '\n'
359  << snapshot->AskVolume[9] << '\n'
360  << snapshot->PrevClosePx << '\n'
361  << snapshot->OpenPx << '\n'
362  << snapshot->HighPx << '\n'
363  << snapshot->LowPx << '\n'
364  << snapshot->LastPx << '\n'
365  << snapshot->ClosePx << '\n'
366  << snapshot->NumTrades << '\n'
367  << snapshot->TotalVolumeTrade << '\n'
368  << snapshot->TotalValueTrade << '\n'
369  << snapshot->TotalBidQty << '\n'
370  << snapshot->WeightedAvgBidPx << '\n'
371  << snapshot->TotalOfferQty << '\n'
372  << snapshot->WeightedAvgOfferPx << '\n'
373  << snapshot->IOPV << '\n'
374  << '\n';
375  }
376  snapshot_.fetch_add(1, std::memory_order_relaxed);
377  }
378 
379 public:
383  void Reset(void)
384  {
385  index_ = 0;
386  entrust_ = 0;
387  trade_ = 0;
388  snapshot_ = 0;
389  }
390 
394  void Pause(void)
395  {
396  flag_ = false;
397  }
398 
402  void Resume(void)
403  {
404  flag_ = true;
405  Reset();
406  }
407 
408 private:
409  std::atomic<int> index_{0};
410  std::atomic<int> entrust_{0};
411  std::atomic<int> trade_{0};
412  std::atomic<int> snapshot_{0};
413  std::atomic<bool> flag_{true};
414 };
415 } // namespace annhoo
416 
417 int main(int argc, char* argv[])
418 {
419  cmdline::parser a;
420  a.add<std::string>("config", 'c', "config file", false, "../conf/config_annhoo_md_sdk.yaml");
421  a.add<std::string>("path", 'p', "data path", false, "./data");
422  a.parse_check(argc, argv);
423 
424  // const char*dd config_file = "../conf/config_quotes.yaml";
425  std::string config_file = a.get<std::string>("config");
426  std::string data_path = a.get<std::string>("path");
427  auto end = data_path.rend();
428  for (auto it = data_path.rbegin(); it != end; ++it) {
429  if (*it == '/') {
430  data_path.pop_back();
431  } else {
432  break;
433  }
434  }
435 
436  annhoo::IAHMdApi* ahMdApi = NewMdApi(config_file.c_str());
438  ahMdApi->RegisterSpi(ahMdSpi);
439  ahMdApi->Connect(true);
440 
441  // subscribe "000001 ", "000002 "
442  std::vector<std::string> codes{"000010 ", "300002 ", "600010 ", "688002"};
443  ahMdApi->SubscribeTick(codes);
444  ahMdApi->Start();
445 
446  sleep(120);
447  if (annhoo::SubscribeAndPrint* print = dynamic_cast<annhoo::SubscribeAndPrint*>(ahMdSpi); print) {
448  print->Pause();
449  sleep(5);
450  print->Resume();
451  }
452  // ahMdApi->UnSubscribeTick(codes);
453 
454  ahMdApi->Join();
455  // never to here
456  ahMdApi->UnRegisterSpi(ahMdSpi);
457  delete ahMdSpi;
458  delete ahMdApi;
459 
460  return 0;
461 }
This is the main interface to use library libAHQuotes.
Definition: ah_md_spi.h:26
This is the main interface to use library libAHQuotes.
Definition: ah_md_api.h:25
int64_t PreCloseIndex
Definition: ah_md_common.h:53
virtual int SubscribeTick(const std::vector< std::string > &securityIDs)=0
Subscribe tick lists.
int64_t AskPrice[10]
Definition: ah_md_common.h:81
int64_t TotalVolumeTrade
Definition: ah_md_common.h:91
int64_t BidApplSeqNum
Definition: ah_md_common.h:169
int64_t TradeSellNo
Definition: ah_md_common.h:175
virtual void OnTickCB(AHTick *tick, uint16_t type) override
Callback for AHTick.
Definition: subscribe.cpp:77
int main(int argc, char *argv[])
Definition: get_version.cpp:11
int64_t TransactTime
Definition: ah_md_common.h:162
virtual void Join(void)=0
Join annhoo marketdata quotes end.
int64_t WeightedAvgBidPx
Definition: ah_md_common.h:95
int64_t LowIndex
Definition: ah_md_common.h:56
uint16_t TradeIndex
Definition: ah_md_common.h:119
int64_t TradeMony
Definition: ah_md_common.h:178
AHDataHeader header
Definition: ah_md_common.h:68
int64_t TotalVolumeTrade
Definition: ah_md_common.h:58
int64_t ApplSeqNum
Definition: ah_md_common.h:141
void PrintSnapshot(AHSnapshot *snapshot)
Callback for AHSnapshot print.
Definition: subscribe.cpp:238
AHDataHeader header
Definition: ah_md_common.h:41
char MDStreamID[4]
Definition: ah_md_common.h:42
SubscribeAndPrint()
Constucts a SubscribeAndPrint.
Definition: subscribe.cpp:33
virtual int Connect(bool master)=0
Connect udp server type.
virtual void UnRegisterSpi(IAHMdSpi *imp)=0
Unregister spi.
void Pause(void)
Pause callback(empty callback).
Definition: subscribe.cpp:394
int64_t OrderPrice
Definition: ah_md_common.h:149
int64_t CloseIndex
Definition: ah_md_common.h:57
void PrintEntrus(AHTick *entrust)
Callback for AHTick SZ print.
Definition: subscribe.cpp:151
uint16_t ChannelNo
Definition: ah_md_common.h:48
void Reset(void)
Reset all count to zero.
Definition: subscribe.cpp:383
int64_t NumTrades
Definition: ah_md_common.h:60
int64_t TradePrice
Definition: ah_md_common.h:151
int64_t BizIndex
Definition: ah_md_common.h:142
int64_t TradeTime
Definition: ah_md_common.h:45
int64_t OrderTime
Definition: ah_md_common.h:163
int64_t BidPrice[10]
Definition: ah_md_common.h:80
int64_t OrderQty
Definition: ah_md_common.h:155
char SecurityID[8]
Definition: ah_md_common.h:145
int64_t AskVolume[10]
Definition: ah_md_common.h:83
virtual void OnSnapshotCB(AHSnapshot *snapshot) override
Callback for AHSnapshot.
Definition: subscribe.cpp:91
int64_t LastIndex
Definition: ah_md_common.h:52
virtual void RegisterSpi(IAHMdSpi *imp)=0
Register spi.
int64_t TotalValueTrade
Definition: ah_md_common.h:92
int64_t OfferApplSeqNum
Definition: ah_md_common.h:174
uint16_t ChannelNo
Definition: ah_md_common.h:123
int64_t HighIndex
Definition: ah_md_common.h:55
annhoo market data quotes api.
int64_t TotalValueTrade
Definition: ah_md_common.h:59
char SecurityID[8]
Definition: ah_md_common.h:51
int64_t TradeQty
Definition: ah_md_common.h:158
uint16_t OrderIndex
Definition: ah_md_common.h:118
int64_t LastSubPrevLastPrice
Definition: ah_md_common.h:106
AHDataHeader header
Definition: ah_md_common.h:115
int64_t BidVolume[10]
Definition: ah_md_common.h:82
uint16_t Channel
Definition: ah_md_common.h:124
int64_t TradeTime
Definition: ah_md_common.h:164
annhoo::IAHMdApi * NewMdApi(const char *config_path)
Create a market data api.
virtual ~SubscribeAndPrint()
Frees the SubscribeAndPrint object.
Definition: subscribe.cpp:39
Contains data defined for index.
Definition: ah_md_common.h:40
char TradingPhaseCode[4]
Definition: ah_md_common.h:77
void PrintIndex(AHIndex *index)
Callback for AHIndex print.
Definition: subscribe.cpp:102
int64_t OrigTime
Definition: ah_md_common.h:44
virtual void Start(void)=0
Start annhoo marketdata quotes.
void PrintTrade(AHTick *trade)
Callback for AHTrade print.
Definition: subscribe.cpp:194
Contains data defined for snapshot.
Definition: ah_md_common.h:67
char InstrumentStatus[2]
Definition: ah_md_common.h:76
int64_t LastSubPrevClosePrice
Definition: ah_md_common.h:105
uint16_t TradeChannel
Definition: ah_md_common.h:125
int64_t WeightedAvgOfferPx
Definition: ah_md_common.h:97
annhoo markdata quotes api.
virtual void OnIndexCB(AHIndex *index) override
Callback for AHIndex.
Definition: subscribe.cpp:67
char MDStreamID[4]
Definition: ah_md_common.h:117
int64_t OpenIndex
Definition: ah_md_common.h:54
char TradingPhaseCode[2]
Definition: ah_md_common.h:49
Inheritance from IAHMdSpi.
Definition: subscribe.cpp:28
void Resume(void)
Continue callback.
Definition: subscribe.cpp:402