Ejemplo n.º 1
0
OrderList PgsqlDataProvider::getOrders(OrderType type, string methodName, uint32_t stockId)
{
  OrderList orderList;

  nontransaction command(*conn);  
  string query = "SELECT id, broker_id, stock_id, amount, price FROM " + methodName + "(" + to_string(stockId) + ");";
  result queryResult(command.exec(query));
  
  if (verbose)
    cout << "Result of " << query << ": " << endl;

  ResultIterator endIterator = queryResult.end();
  for (ResultIterator iterator = queryResult.begin(); iterator != endIterator; ++iterator) 
  {
    Order order;
    order.setId(iterator[0].as<uint64_t>());
    order.setType(type); 
    order.setBrokerId(iterator[1].as<uint32_t>());
    order.setStockId(iterator[2].as<uint32_t>());
    order.setAmount(iterator[3].as<uint32_t>());
    order.setPrice(Decimal(iterator[4].as<string>()));
    orderList.push_back(order);
  }

  if (verbose)
    cout << orderList.size() << " orders. " << endl;

  return orderList;
}
Ejemplo n.º 2
0
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;
    }
  }
}
Ejemplo n.º 3
0
UBool OrderList::matchesAt(int32_t offset, const OrderList &other) const
{
    // NOTE: sizes include the NULLORDER, which we don't want to compare.
    int32_t otherSize = other.size() - 1;

    if (listSize - 1 - offset < otherSize) {
        return FALSE;
    }

    for (int32_t i = offset, j = 0; j < otherSize; i += 1, j += 1) {
        if (getOrder(i) != other.getOrder(j)) {
            return FALSE;
        }
    }

    return TRUE;
}