Esempio n. 1
0
        public override void ProcessTrades(Trade[] trades)
        {
            if (trades.Length == 0)
            return;

              XDocument document = XDocument.Load(FileName);
              XElement tradesElement = document.Root.Element(TradesElementName);
              XElement ordersElement = document.Root.Element(OrdersElementName);
              XElement stocksElement = document.Root.Element(StocksElementName);

              UpdateStockPrice(trades.First().StockId, trades.First().Price, stocksElement);

              foreach (Trade trade in trades)
              {
            XElement tradeElement = PrepareTradeElement(trade);
            tradesElement.Add(tradeElement);

            XElement buyOrderElement = GetOrderElement(trade.BuyOrderId, ordersElement);
            ProcessOrderElement(buyOrderElement, trade);

            XElement sellOrderElement = GetOrderElement(trade.SellOrderId, ordersElement);
            ProcessOrderElement(sellOrderElement, trade);
              }

              using (XmlWriter writer = new XmlTextWriter(FileName, Encoding.UTF8))
              {
            document.WriteTo(writer);
              }
        }
        public override void ProcessTrade(Trade trade)
        {
            string serializedTrade = Format(trade);
              string query = string.Format("SELECT process_trades((ARRAY[{0}])::t_trade[]);", serializedTrade);

              using (OdbcCommand command = new OdbcCommand())
              {
            command.Connection = connection;
            command.CommandText = query;
            command.ExecuteNonQuery();
              }
        }
        public override void ProcessTrade(Trade trade)
        {
            OdbcCommand command = new OdbcCommand();
              OdbcTransaction transaction = null;
              DateTime now = DateTime.Now;

              try
              {
            transaction = connection.BeginTransaction();
            command.Connection = connection;
            command.Transaction = transaction;

            // stock
            command.CommandText =
            "UPDATE stocks SET price = " + trade.StockId + " WHERE id = " + trade.StockId;
            command.ExecuteNonQuery();

            // trade
            command.CommandText =
            "INSERT INTO trades (boid, soid, sid, buyer, seller, amount, price, executed) VALUES " +
            string.Format("({0}, {1}, {2}, {3}, {4}, {5}, {6}, TIMESTAMP '{7}');",
            trade.BuyOrderId, trade.SellOrderId, trade.StockId, trade.BuyerId, trade.SellerId,
            trade.Amount, trade.Price.ToString(CultureInfo.CreateSpecificCulture("en-US")), now);
            command.ExecuteNonQuery();

            // buy order
            ProcessTradeOrder(trade.Amount, command, now, trade.BuyOrderId);

            // sell order
            ProcessTradeOrder(trade.Amount, command, now, trade.SellOrderId);

            transaction.Commit();
              }
              catch (Exception ex)
              {
            Console.WriteLine(ex.Message);
            try
            {
              transaction.Rollback();
            }
            catch { }
              }
        }
Esempio n. 4
0
        public override void ProcessTrade(Trade trade)
        {
            XDocument document = XDocument.Load(FileName);
              XElement tradesElement = document.Root.Element(TradesElementName);
              XElement ordersElement = document.Root.Element(OrdersElementName);
              XElement stocksElement = document.Root.Element(StocksElementName);

              UpdateStockPrice(trade.StockId, trade.Price, stocksElement);

              XElement tradeElement = PrepareTradeElement(trade);
              tradesElement.Add(tradeElement);

              XElement buyOrderElement = GetOrderElement(trade.BuyOrderId, ordersElement);
              ProcessOrderElement(buyOrderElement, trade);

              XElement sellOrderElement = GetOrderElement(trade.SellOrderId, ordersElement);
              ProcessOrderElement(sellOrderElement, trade);

              using (XmlWriter writer = new XmlTextWriter(FileName, Encoding.UTF8))
              {
            document.WriteTo(writer);
              }
        }
        public override void ProcessTrades(Trade[] trades)
        {
            string[] serializedTrades = new string[trades.Length];
              for (int index = 0; index < trades.Length; index++)
            serializedTrades[index] = Format(trades[index]);

              string query = string.Format("SELECT process_trades((ARRAY[{0}])::t_trade[]);",
              string.Join(", ", serializedTrades));

              using (OdbcCommand command = new OdbcCommand())
              {
            try
            {
              command.Connection = connection;
              command.CommandText = query;
              command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
              Recorder.Instance.WriteLine("Exception occured during processing trades: " + ex.Message +
              ". No ownership changes were made.");
            }
              }
        }
Esempio n. 6
0
        public Trade[] MatchOrders(Order[] buyList, Order[] sellList, decimal price)
        {
            // TODO: razeni by se dalo delat jen jednou, vyuziva to i IosPriceCalculator... - nejaky preprocesor
              // Tady se akorat navic rovna podle druhyho kriteria: casu
              // TODO: a vubec, stacilo by seznam zkratit podle spocitane price (je serazeny, tak useknout)
              Order[] sortedBuyList = buyList.Where(o => o.ProposedPrice >= price)
            .OrderByDescending(o => o.ProposedPrice).ThenBy(o => o.Received).ToArray();
              Order[] sortedSellList = sellList.Where(o => o.ProposedPrice <= price)
            .OrderBy(o => o.ProposedPrice).ThenBy(o => o.Received).ToArray();

              if (sortedBuyList.Length == 0) // pak je i sortedSellList.Length == 0
            return new Trade[0];

              IList<Trade> result = new List<Trade>();
              int buyListIndex = 0;
              int sellListIndex = 0;

              do
              {
            Order buyOrder = sortedBuyList[buyListIndex];
            Order sellOrder = sortedSellList[sellListIndex];

            if (buyOrder.Amount > sellOrder.Amount)
            {
              int amount = sellOrder.Amount;
              Trade trade = new Trade(amount, price, buyOrder, sellOrder);
              result.Add(trade);

              buyOrder.Amount -= amount;
              sellOrder.Amount = 0;
              sellListIndex++;
            }
            else if (buyOrder.Amount < sellOrder.Amount)
            {
              int amount = buyOrder.Amount;
              Trade trade = new Trade(amount, price, buyOrder, sellOrder);
              result.Add(trade);

              sellOrder.Amount -= buyOrder.Amount;
              buyOrder.Amount = 0;
              buyListIndex++;
            }
            else // ==
            {
              int amount = buyOrder.Amount;
              Trade trade = new Trade(amount, price, buyOrder, sellOrder);
              result.Add(trade);

              buyOrder.Amount = 0;
              buyListIndex++;
              sellOrder.Amount = 0;
              sellListIndex++;
            }
              }
              while (buyListIndex < sortedBuyList.Length && sellListIndex < sortedSellList.Length);

              // NAPAD: zapis do DB, co se prodalo komu. Tam, kde zbylo Q = 0, to je jasny,
              // k tomu zapsat zvlast posledni pouzity indexy, pokud Q != 0. ... To ale nepouzivam.

              return result.ToArray();
        }
        public override Trade[] SelectTrades(int stockId)
        {
            IList<Trade> trades = new List<Trade>();

              string query = "SELECT id, boid, soid, sid, buyer, seller, amount, price FROM trades WHERE sid = " + stockId;
              OdbcCommand command = new OdbcCommand(query, connection);
              OdbcDataReader reader = command.ExecuteReader();

              while (reader.Read())
              {
            Trade trade = new Trade();
            trade.Id = reader.GetInt32(0);
            trade.BuyOrderId = reader.GetInt32(1);
            trade.SellOrderId = reader.GetInt32(2);
            trade.StockId = reader.GetInt32(3);
            trade.BuyerId = reader.GetInt32(4);
            trade.SellerId = reader.GetInt32(5);
            trade.Amount = reader.GetInt32(6);
            trade.Price = reader.GetDecimal(7);
            trades.Add(trade);
              }

              reader.Close();
              return trades.ToArray();
        }
Esempio n. 8
0
 private void ProcessOrderElement(XElement orderElement, Trade trade)
 {
     int orderAmount = int.Parse(orderElement.Element("amount").Value);
       if (orderAmount == trade.Amount)
     orderElement.Remove();
       else
     orderElement.Element("amount").Value = (orderAmount - trade.Amount).ToString();
 }
Esempio n. 9
0
 //public override Order[] SelectCrossedBuyOrders(int stockId)
 //{
 //  Order[] sellList = SelectSellOrders(stockId)./*Where(o => o.StockId == stockId).*/ToArray();
 //  if (sellList.Length == 0)
 //    return null;
 //  decimal minPrice = sellList.Min(o => o.ProposedPrice);
 //  return SelectBuyOrders(stockId).Where(o => /*o.StockId == stockId &&*/ o.ProposedPrice >= minPrice).ToArray();
 //}
 //public override Order[] SelectCrossedSellOrders(int stockId)
 //{
 //  Order[] buyList = SelectBuyOrders(stockId)./*Where(o => o.StockId == stockId).*/ToArray();
 //  if (buyList.Length == 0)
 //    return null;
 //  decimal maxPrice = buyList.Max(o => o.ProposedPrice);
 //  return SelectSellOrders(stockId).Where(o => /*o.StockId == stockId &&*/ o.ProposedPrice <= maxPrice).ToArray();
 //}
 private XElement PrepareTradeElement(Trade trade)
 {
     XElement tradeElement = new XElement("trade");
       tradeElement.Add(new XElement("amount", trade.Amount));
       tradeElement.Add(new XElement("price", trade.Price));
       tradeElement.Add(new XElement("stockId", trade.StockId));
       tradeElement.Add(new XElement("buyerId", trade.BuyerId));
       tradeElement.Add(new XElement("sellerId", trade.SellerId));
       tradeElement.Add(new XElement("buyOrderId", trade.BuyOrderId));
       tradeElement.Add(new XElement("sellOrderId", trade.SellOrderId));
       tradeElement.Add(new XElement("executed", trade.Executed));
       return tradeElement;
 }
Esempio n. 10
0
        public override Trade[] SelectTrades(int stockId)
        {
            XDocument document = XDocument.Load(FileName);
              XElement tradesElement = document.Root.Element(TradesElementName);
              IList<Trade> tradeList = new List<Trade>();

              foreach (XElement tradeElement in tradesElement.Elements())
              {
            Trade trade = new Trade();
            trade.Amount = int.Parse(tradeElement.Element("amount").Value);
            trade.Price = decimal.Parse(tradeElement.Element("price").Value, new CultureInfo("en-US"));
            trade.StockId = int.Parse(tradeElement.Element("stockId").Value);
            trade.BuyerId = int.Parse(tradeElement.Element("buyerId").Value);
            trade.SellerId = int.Parse(tradeElement.Element("sellerId").Value);
            trade.BuyOrderId = int.Parse(tradeElement.Element("buyOrderId").Value);
            trade.SellOrderId = int.Parse(tradeElement.Element("sellOrderId").Value);
            trade.Executed = DateTime.Parse(tradeElement.Element("executed").Value);
            if (trade.StockId == stockId)
              tradeList.Add(trade);
              }

              return tradeList.ToArray();
        }
Esempio n. 11
0
 private string Format(Trade trade)
 {
     // example: $$(1, 3, 1, 1, 1, 20, 11.5)$$
       return string.Format("$$({0}, {1}, {2}, {3}, {4}, {5}, {6})$$",
       trade.BuyOrderId, trade.SellOrderId, trade.StockId, trade.BuyerId, trade.SellerId,
       trade.Amount, trade.Price.ToString(CultureInfo.CreateSpecificCulture("en-US")));
 }
Esempio n. 12
0
        public override Trade[] SelectTrades(int stockId)
        {
            IList<Trade> trades = new List<Trade>();

              using (OdbcCommand command = new OdbcCommand())
              {
            command.Connection = connection;
            command.CommandText = string.Format(
            "SELECT id, buy_order_id, sell_order_id, stock_id, buyer_id, seller_id, amount, price, executed " +
            "FROM get_trades({0});", stockId);

            using (OdbcDataReader reader = command.ExecuteReader())
            {
              while (reader.Read())
              {
            Trade trade = new Trade();
            trade.Id = reader.GetInt32(0);
            trade.BuyOrderId = reader.GetInt32(1);
            trade.SellOrderId = reader.GetInt32(2);
            trade.StockId = reader.GetInt32(3);
            trade.BuyerId = reader.GetInt32(4);
            trade.SellerId = reader.GetInt32(5);
            trade.Amount = reader.GetInt32(6);
            trade.Price = reader.GetDecimal(7);
            trade.Executed = reader.GetDateTime(8);
            trades.Add(trade);
              }

              reader.Close();
            }
              }

              return trades.ToArray();
        }
Esempio n. 13
0
 /// <summary>
 /// Zapíše uzavřené obchody.
 /// </summary>
 /// <param name="trades">Uzavřené obchody.</param>
 public virtual void ProcessTrades(Trade[] trades)
 {
     foreach (Trade trade in trades)
     ProcessTrade(trade);
 }
Esempio n. 14
0
 /// <summary>
 /// Zapíše uzavřený obchod.
 /// </summary>
 /// <param name="trade">Uzavřený obchod.</param>
 public abstract void ProcessTrade(Trade trade);