string PgsqlDataProvider::prepareProcessTradesQuery(const TradeList& tradeList) { ostringstream query; query << "SELECT process_trades((ARRAY["; if (tradeList.size() != 0) { ConstTradeIterator iterator = tradeList.begin(); ConstTradeIterator endIterator = tradeList.end(); query << format(*iterator); iterator++; while (iterator != endIterator) { query << ", " << format(*iterator); iterator++; } } query << "])::t_trade[]);"; if (verbose) cout << "Query: " << query.str() << endl; return query.str(); }
void populateList (TradeList &BuyListAll, TradeList &SellListAll, string req){ vector<string> tokenized_string = tokenize (req,'@'); TradeList::iterator it; Scrip s; s.ID = tokenized_string[0]; s.type = tokenized_string[1]; s.name = tokenized_string[2]; s.rate = ConvertStringToNumber(tokenized_string[3]); s.num = ConvertStringToNumber(tokenized_string[4]); vector<string> expiryTime = tokenize(tokenized_string[5],'$'); s.expiry = expiryTime[1]; s.date = expiryTime[0]; if ( (s.type).compare("B") == 0 ){ cout << "entering this part B" <<endl; //PrintScrip(s); it = BuyListAll.find(s.name); if ( it != BuyListAll.end() ) // key exists already and needs to be updated (it -> second).push_back(s); else{ ScripList SL; SL.push_back(s); BuyListAll[s.name] = SL; } //BuyListAll[s.name] = s; } else{ cout << "entering this part S" <<endl; //PrintScrip(s); it = SellListAll.find(s.name); if ( it != SellListAll.end() ) // key exists already and needs to be updated (it -> second).push_back(s); else{ ScripList SL; SL.push_back(s); SellListAll[s.name] = SL; } //SellListAll[s.name] = s; } }
void doTransaction ( TradeList &BuyListAll, TradeList &SellListAll ){ TradeList::iterator buy,sell; bool tradeflag = true; for ( sell = SellListAll.begin() ; sell != SellListAll.end() ; sell++ ){ buy = BuyListAll.find(sell->first); if ( buy != BuyListAll.end() ){ // there is a buyer ScripList blist = buy->second; ScripList slist = sell->second; ScripList::iterator b1,s1; s1 = slist.begin();//check the first sale item for ( b1 = blist.begin(); b1 != blist.end() && tradeflag ; b1++ ){//check the buyer list entirely if ( (*b1).rate > (*s1).rate ){ if ( (*b1).num >= (*s1).num ){//all sold // *************print to file*************** // PrintScrip(*s1); PrintScrip(*b1); (*b1).num -= (*s1).num; (sell->second).erase(s1); if ( (*b1).num == 0 ) (buy->second).erase(b1); tradeflag = false; } if ( (*b1).num < (*s1).num ){//partially sold // *************print to file*************** // PrintScrip(*s1); PrintScrip(*b1); (*s1).num -= (*b1).num; (buy->second).erase(b1); } } } } } }
void checkExpiry ( TradeList &BuyListAll, TradeList &SellListAll ){ cout << "Checking expiry once " << endl; TradeList::iterator buy,sell; ScripList::iterator b1,s1; ScripList blist, slist; time_t CT; time(&CT); string CurrentTime = Current_Time(&CT); for ( sell = SellListAll.begin() ; sell != SellListAll.end() ; sell++ ){ slist = sell->second; for ( s1 = slist.begin(); s1 != slist.end() ; s1++ ){ if ( !isAlive ( (*s1).expiry, CurrentTime ) ){ //print to file PrintScrip(*s1); (sell->second).erase(s1); cout << "Erasing a record once since it is consumed" << endl; } } } for ( buy = BuyListAll.begin() ; buy != BuyListAll.end() ; buy++ ){ blist = buy->second; for ( b1 = blist.begin(); b1 != blist.end() ; b1++ ){ if ( !isAlive ( (*b1).expiry, CurrentTime ) ){ //print to file PrintScrip(*b1); (buy->second).erase(b1); cout << "Erasing a record once since it is consumed" << endl; } } } }
void OrderMatcher::matchOrders(TradeList& tradeList, OrderList& buyList, OrderList& sellList, Decimal price) { if (verbose) cout << endl << "Matching orders..." << endl; if (buyList.size() == 0 || sellList.size() == 0) return; Order buyOrder = buyList.front(); Order sellOrder = sellList.front(); bool finished = false; while (!finished && buyOrder.getPrice() >= price && sellOrder.getPrice() <= price) { uint32_t amount = std::min(buyOrder.getAmount(), sellOrder.getAmount()); Trade trade(amount, price, buyOrder, sellOrder); tradeList.push_back(trade); buyOrder.decreaseAmount(amount); sellOrder.decreaseAmount(amount); if (buyOrder.getAmount() == 0) { buyList.pop_front(); if (buyList.size() > 0) buyOrder = buyList.front(); else finished = true; } if (sellOrder.getAmount() == 0) { sellList.pop_front(); if (sellList.size() > 0) sellOrder = sellList.front(); else finished = true; } } }