7 #include "ah_md_spi_inl.h"
14 #include <sys/types.h>
25 "MsgType,ExchangeID,MDStreamID(SZ),OrigTime/TradeTime,ChannelNo(SZ),TradingPhaseCode(SZ),SecurityID,LastIndex,PreCloseIndex,OpenIndex," \
26 "HighIndex,LowIndex,CloseIndex,TotalVolumeTrade,TotalValueTrade,NumTrades\n") \
28 "MsgType,MsgLength,ExchangeID,MsgID,MDStreamID,ChannelNo,ApplSeqNum,SecurityID,Price,OrderQty,Side," \
29 "TransactTime,OrdType\n") \
31 "MsgType,MsgLength,ExchangeID,MsgID,MDStreamID,ChannelNo,ApplSeqNum,BidApplSeqNum,OfferApplSeqNum,SecurityID," \
32 "LastPx,LastQty,ExecType,TransactTime\n") \
34 "MsgType,ExchangeID,MDStreamID(SZ),OrigTime,ChannelNo,SecurityID,TradingPhaseCode,BidPrice[0]," \
35 "BidPrice[1],BidPrice[2],BidPrice[3],BidPrice[4],BidPrice[5],BidPrice[6],BidPrice[7],BidPrice[8],BidPrice[9]," \
36 "AskPrice[0],AskPrice[1],AskPrice[2],AskPrice[3],AskPrice[4],AskPrice[5],AskPrice[6],AskPrice[7],AskPrice[8]," \
37 "AskPrice[9],BidVolume[0],BidVolume[1],BidVolume[2],BidVolume[3],BidVolume[4],BidVolume[5],BidVolume[6]," \
38 "BidVolume[7],BidVolume[8],BidVolume[9],AskVolume[0],AskVolume[1],AskVolume[2],AskVolume[3],AskVolume[4]," \
39 "AskVolume[5],AskVolume[6],AskVolume[7],AskVolume[8],AskVolume[9],PrevClosePx,OpenPrice,HighPrice,LowPrice," \
40 "LastPrice,NumTrades,TotalVolumeTrade,TotalValueTrade,OpenInterest,LastSubPrevClosePrice,LastSubPrevLastPrice," \
41 "TotalBidVolume,TotalAskVolume,MaBidPrice,MaAskPrice,PreIOPV,IOPV,UpperLimitPrice,LowerLimitPrice\n")
44 enum CSV_HEADER { CSV_TABLE };
47 #define X(a, b) [a] = b,
48 const char* CsvHeaderTableStr[] = {CSV_TABLE};
51 inline static bool FileExists(
const std::string& name,
struct stat* st)
53 return (stat(name.c_str(), st) == 0);
56 static inline int64_t GetTimeStampNano(
void)
59 clock_gettime(CLOCK_REALTIME, &ts);
61 return ts.tv_sec * 1000000000 + ts.tv_nsec;
64 AHMdSpiInl::AHMdSpiInl(
const std::string& index_file,
const std::string& tick_file,
const std::string& snapshot_file,
65 const char* delimiter,
const char* suffix)
67 this->delimiter = delimiter;
68 this->suffix = suffix;
70 if (FileExists(index_file, &st)) {
72 index_fd = open(index_file.c_str(), O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
75 fprintf(stderr,
"open index file: %s, errmsg: %s", index_file.c_str(), strerror(err));
79 index_fd = open(index_file.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
83 if (FileExists(snapshot_file, &st)) {
84 snapshot_fd = open(snapshot_file.c_str(), O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
85 if (snapshot_fd < 0) {
87 fprintf(stderr,
"open snap_shot file: %s, errmsg: %s", snapshot_file.c_str(), strerror(err));
91 snapshot_fd = open(snapshot_file.c_str(), O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
96 AHMdSpiInl::~AHMdSpiInl()
104 static const char* sz_format =
"%d%s%c%c%s"
120 static const char* sh_format =
"%d%s%c%c%s"
132 dprintf(index_fd, sz_format,
151 dprintf(index_fd, sh_format,
170 static const char* sz_entrust_format =
"%d%s%c%c%s"
181 static const char* sh_entrust_format =
"%d%s%c%c%s"
193 static const char* sz_trade_format =
"%d%s%c%c%s"
205 static const char* sh_trade_format =
"%d%s%c%c%s"
217 if (type == AHType::ENTRUST) {
220 fprintf(stderr, sz_entrust_format, tick->
header.
MsgType, delimiter.c_str(),
224 tick->
Side, delimiter.c_str(),
225 tick->
OrdType, delimiter.c_str(),
229 tick->
Price, delimiter.c_str(),
233 fprintf(stderr, sh_entrust_format, tick->
header.
MsgType, delimiter.c_str(),
236 tick->
Channel, delimiter.c_str(),
243 tick->
Balance, delimiter.c_str(),
245 tick->
OrderNO, suffix.c_str());
249 fprintf(stderr, sz_trade_format, tick->
header.
MsgType, delimiter.c_str(),
257 tick->
LastPx, delimiter.c_str(),
258 tick->
LastQty, delimiter.c_str(),
263 fprintf(stderr, sh_trade_format, tick->
header.
MsgType, delimiter.c_str(),
283 static const char* sz_format =
"%d%s%c%c%s"
289 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
290 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
291 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
292 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
314 static const char* sh_format =
"%d%s%c%c%s"
319 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
320 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
321 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
322 "%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s%lld%s"
339 dprintf(snapshot_fd, sz_format,
343 snapshot->
OrigTime, delimiter.c_str(),
349 snapshot->
BidPrice[0], delimiter.c_str(),
350 snapshot->
BidPrice[1], delimiter.c_str(),
351 snapshot->
BidPrice[2], delimiter.c_str(),
352 snapshot->
BidPrice[3], delimiter.c_str(),
353 snapshot->
BidPrice[4], delimiter.c_str(),
354 snapshot->
BidPrice[5], delimiter.c_str(),
355 snapshot->
BidPrice[6], delimiter.c_str(),
356 snapshot->
BidPrice[7], delimiter.c_str(),
357 snapshot->
BidPrice[8], delimiter.c_str(),
358 snapshot->
BidPrice[9], delimiter.c_str(),
359 snapshot->
AskPrice[0], delimiter.c_str(),
360 snapshot->
AskPrice[1], delimiter.c_str(),
361 snapshot->
AskPrice[2], delimiter.c_str(),
362 snapshot->
AskPrice[3], delimiter.c_str(),
363 snapshot->
AskPrice[4], delimiter.c_str(),
364 snapshot->
AskPrice[5], delimiter.c_str(),
365 snapshot->
AskPrice[6], delimiter.c_str(),
366 snapshot->
AskPrice[7], delimiter.c_str(),
367 snapshot->
AskPrice[8], delimiter.c_str(),
368 snapshot->
AskPrice[9], delimiter.c_str(),
369 snapshot->
BidVolume[0], delimiter.c_str(),
370 snapshot->
BidVolume[1], delimiter.c_str(),
371 snapshot->
BidVolume[2], delimiter.c_str(),
372 snapshot->
BidVolume[3], delimiter.c_str(),
373 snapshot->
BidVolume[4], delimiter.c_str(),
374 snapshot->
BidVolume[5], delimiter.c_str(),
375 snapshot->
BidVolume[6], delimiter.c_str(),
376 snapshot->
BidVolume[7], delimiter.c_str(),
377 snapshot->
BidVolume[8], delimiter.c_str(),
378 snapshot->
BidVolume[9], delimiter.c_str(),
379 snapshot->
AskVolume[0], delimiter.c_str(),
380 snapshot->
AskVolume[1], delimiter.c_str(),
381 snapshot->
AskVolume[2], delimiter.c_str(),
382 snapshot->
AskVolume[3], delimiter.c_str(),
383 snapshot->
AskVolume[4], delimiter.c_str(),
384 snapshot->
AskVolume[5], delimiter.c_str(),
385 snapshot->
AskVolume[6], delimiter.c_str(),
386 snapshot->
AskVolume[7], delimiter.c_str(),
387 snapshot->
AskVolume[8], delimiter.c_str(),
388 snapshot->
AskVolume[9], delimiter.c_str(),
390 snapshot->
OpenPx, delimiter.c_str(),
391 snapshot->
HighPx, delimiter.c_str(),
392 snapshot->
LowPx, delimiter.c_str(),
393 snapshot->
LastPx, delimiter.c_str(),
394 snapshot->
ClosePx, delimiter.c_str(),
402 snapshot->
IOPV, delimiter.c_str(),
403 snapshot->
LPV, delimiter.c_str(),
410 dprintf(snapshot_fd, sh_format,
419 snapshot->
BidPrice[0], delimiter.c_str(),
420 snapshot->
BidPrice[1], delimiter.c_str(),
421 snapshot->
BidPrice[2], delimiter.c_str(),
422 snapshot->
BidPrice[3], delimiter.c_str(),
423 snapshot->
BidPrice[4], delimiter.c_str(),
424 snapshot->
BidPrice[5], delimiter.c_str(),
425 snapshot->
BidPrice[6], delimiter.c_str(),
426 snapshot->
BidPrice[7], delimiter.c_str(),
427 snapshot->
BidPrice[8], delimiter.c_str(),
428 snapshot->
BidPrice[9], delimiter.c_str(),
429 snapshot->
AskPrice[0], delimiter.c_str(),
430 snapshot->
AskPrice[1], delimiter.c_str(),
431 snapshot->
AskPrice[2], delimiter.c_str(),
432 snapshot->
AskPrice[3], delimiter.c_str(),
433 snapshot->
AskPrice[4], delimiter.c_str(),
434 snapshot->
AskPrice[5], delimiter.c_str(),
435 snapshot->
AskPrice[6], delimiter.c_str(),
436 snapshot->
AskPrice[7], delimiter.c_str(),
437 snapshot->
AskPrice[8], delimiter.c_str(),
438 snapshot->
AskPrice[9], delimiter.c_str(),
439 snapshot->
BidVolume[0], delimiter.c_str(),
440 snapshot->
BidVolume[1], delimiter.c_str(),
441 snapshot->
BidVolume[2], delimiter.c_str(),
442 snapshot->
BidVolume[3], delimiter.c_str(),
443 snapshot->
BidVolume[4], delimiter.c_str(),
444 snapshot->
BidVolume[5], delimiter.c_str(),
445 snapshot->
BidVolume[6], delimiter.c_str(),
446 snapshot->
BidVolume[7], delimiter.c_str(),
447 snapshot->
BidVolume[8], delimiter.c_str(),
448 snapshot->
BidVolume[9], delimiter.c_str(),
449 snapshot->
AskVolume[0], delimiter.c_str(),
450 snapshot->
AskVolume[1], delimiter.c_str(),
451 snapshot->
AskVolume[2], delimiter.c_str(),
452 snapshot->
AskVolume[3], delimiter.c_str(),
453 snapshot->
AskVolume[4], delimiter.c_str(),
454 snapshot->
AskVolume[5], delimiter.c_str(),
455 snapshot->
AskVolume[6], delimiter.c_str(),
456 snapshot->
AskVolume[7], delimiter.c_str(),
457 snapshot->
AskVolume[8], delimiter.c_str(),
458 snapshot->
AskVolume[9], delimiter.c_str(),
460 snapshot->
OpenPx, delimiter.c_str(),
461 snapshot->
HighPx, delimiter.c_str(),
462 snapshot->
LowPx, delimiter.c_str(),
463 snapshot->
LastPx, delimiter.c_str(),
464 snapshot->
ClosePx, delimiter.c_str(),
472 snapshot->
IOPV, suffix.c_str());
virtual void OnIndexCB(AHIndex *index) override
Callback for AHIndex.
int64_t LastSubPrevLastPrice
Contains data defined for index.
virtual void OnSnapshotCB(AHSnapshot *snap_shot) override
Callback for AHSnapshot.
Contains data defined for snapshot.
int64_t LastSubPrevClosePrice
virtual void OnTickCB(AHTick *tick, uint16_t type) override
Callback for AHEntrust or AHTrade.
int64_t WeightedAvgOfferPx