File size: 1,405 Bytes
44cddac
8f4d2d0
 
 
44cddac
8f4d2d0
 
 
 
44cddac
8f4d2d0
 
 
 
 
 
 
 
 
 
 
 
 
 
44cddac
8f4d2d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44cddac
8f4d2d0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include "actors/MDGActor.hpp"

namespace eunex {

MDGActor::MDGActor() {
    registerEventHandler<TradeEvent>(*this);
    registerEventHandler<BookUpdateEvent>(*this);
}

void MDGActor::onEvent(const TradeEvent& event) {
    const auto& t = event.trade;
    auto& snap = snapshots_[t.symbolIdx];
    snap.symbolIdx      = t.symbolIdx;
    snap.lastTradePrice  = t.price;
    snap.lastTradeQty    = t.quantity;
    snap.tradeCount++;
    snap.updateTime      = nowNs();

    recentTrades_.push_back(t);
    if (recentTrades_.size() > MAX_RECENT_TRADES) {
        recentTrades_.erase(recentTrades_.begin());
    }
}

void MDGActor::onEvent(const BookUpdateEvent& event) {
    auto& snap = snapshots_[event.symbolIdx];
    snap.symbolIdx = event.symbolIdx;
    snap.updateTime = nowNs();

    if (event.bidDepth > 0) {
        snap.bestBid = event.bids[0].price;
        snap.totalBidQty = 0;
        for (int i = 0; i < event.bidDepth; ++i)
            snap.totalBidQty += event.bids[i].qty;
    }

    if (event.askDepth > 0) {
        snap.bestAsk = event.asks[0].price;
        snap.totalAskQty = 0;
        for (int i = 0; i < event.askDepth; ++i)
            snap.totalAskQty += event.asks[i].qty;
    }
}

const MarketDataSnapshot* MDGActor::getSnapshot(SymbolIndex_t sym) const {
    auto it = snapshots_.find(sym);
    return it != snapshots_.end() ? &it->second : nullptr;
}

} // namespace eunex