#pragma once #include "stock.h" #include <map> #include <deque> #include <string> class BidQuoteObserver { public: BidQuoteObserver(); ~BidQuoteObserver(); int BidQuoteReceived( const StockBid::Bid& stBid ); private: std::map<std::string, std::deque< StockBid::Bid > > mapQuoteHistory; };
#include "stdafx.h" #include "BidQuoteObserver.h" #include <iostream> using namespace std; BidQuoteObserver::BidQuoteObserver() { } BidQuoteObserver::~BidQuoteObserver() { } int BidQuoteObserver::BidQuoteReceived( const StockBid::Bid& stBid ) { string sStockCode = stBid.stock_code; mapQuoteHistory[ sStockCode ].push_back( stBid ); cout << "add one bid. time:" << stBid.update_time << ", price:" << stBid.price << ", code:" << stBid.stock_code << ", size is " << mapQuoteHistory[ sStockCode ].size() << endl; map<string, deque< StockBid::Bid > >::iterator itr = mapQuoteHistory.find( sStockCode ); // 保持deque的大小最小 if ( itr->second.size() > 2 ) { // 向前探测一个消息,如果价格不同,则删除之前insert的元素,只保留当前元素 deque< StockBid::Bid >::reverse_iterator itrQ = itr->second.rbegin(); ++itrQ; if ( itrQ->price != stBid.price ) { itr->second.clear(); itr->second.push_back( stBid ); return 0; } } int iMaxSameSeconds = 30; if ( itr->second.size() >= iMaxSameSeconds ) { deque< StockBid::Bid >::reverse_iterator itrQ = itr->second.rbegin(); int iSteadyPrice = 0; int iSameCount = 0; for ( ; itrQ != itr->second.rend(); ++itrQ ) { int iCurrPrice = itrQ->price; if ( iSteadyPrice == 0 ) { iSteadyPrice = iCurrPrice; iSameCount = 1; } else { if ( iSteadyPrice == iCurrPrice ) { ++iSameCount; } else { // 有不一样的,就不要再向前扫描了 break; } } if ( iSameCount >= iMaxSameSeconds ) { cout << "WARNNING:stock : " << itr->first << " " << iSameCount << " seconds not updated.iPrice:" << iSteadyPrice << ", iNow : " << stBid.update_time << endl; break; } } } if ( itr->second.size() > iMaxSameSeconds ) { itr->second.pop_front(); } return 0; }