Beispiel #1
0
        public void recvOrderbookNotification(IOrderbook_Matcher ob, IOrderbookEvent evt)
        {
            String line = "";

            if (evt is IOrderbookEvent_AddOrder)
            {
                // log an Add row
                IOrderbookEvent_AddOrder addEvt = (IOrderbookEvent_AddOrder)evt;
                IOrder order = addEvt.getOrder();

                string status = "Add";
                string time   = "" + String.Format("{0:.###}", Scheduler.GetTime());

                string price = "" + (ob.getNumBids() > 0 ? String.Format("{0:.###}", ob.getHighestBid()) : "?")
                               + "," + (ob.getNumAsks() > 0 ? String.Format("{0:.###}", ob.getLowestAsk()) : "?");
                string direction  = "" + (order.isBid() ? "BID" : "ASK");
                string orderprice = "" + String.Format("{0:.###}", order.getPrice());
                string ordersize  = "" + order.getVolume();
                string owner      = "\"" + order.getOwner().ToString() + "\"";
                line = status + "," + time + "," + price + "," + direction + "," + orderprice + "," + ordersize + "," + owner;
                Console.WriteLine(line);
            }
            else if (evt is IOrderbookEvent_CancelOrder)
            {
                // log a Cancel
                IOrderbookEvent_CancelOrder cancelEvt = (IOrderbookEvent_CancelOrder)evt;
                IOrder order = cancelEvt.getOrder();

                string status = "Cancel";
                string time   = "" + String.Format("{0:.###}", Scheduler.GetTime());
                string price  = "" + (ob.getNumBids() > 0 ? String.Format("{0:.###}", ob.getHighestBid()) : "?")
                                + "," + (ob.getNumAsks() > 0 ? String.Format("{0:.###}", ob.getLowestAsk()) : "?"); string direction = "" + (order.isBid() ? "BID" : "ASK");
                string orderprice = "" + String.Format("{0:.###}", order.getPrice());
                string ordersize  = "" + order.getVolume();
                string owner      = "\"" + order.getOwner().ToString() + "\"";
                line = status + "," + time + "," + price + "," + direction + "," + orderprice + "," + ordersize + "," + owner;
                Console.WriteLine(line);
            }
            else if (evt is IOrderbookEvent_FillOrder)
            {
                // no op
                IOrderbookEvent_FillOrder fillEvt = (IOrderbookEvent_FillOrder)evt;
                IOrder order = fillEvt.getOrder();

                string status = "Fill";
                string time   = "" + String.Format("{0:.###}", Scheduler.GetTime());
                string price  = "" + (ob.getNumBids() > 0 ? String.Format("{0:.###}", ob.getHighestBid()) : "?")
                                + "," + (ob.getNumAsks() > 0 ? String.Format("{0:.###}", ob.getLowestAsk()) : "?"); string direction = "" + (order.isBid() ? "BID" : "ASK");
                string orderprice = "" + String.Format("{0:.###}", order.getPrice());
                string ordersize  = "" + order.getVolume();
                string owner      = "\"" + order.getOwner().ToString() + "\"";
                line = status + "," + time + "," + price + "," + direction + "," + orderprice + "," + ordersize + "," + owner;
                Console.WriteLine(line);
            }

            OrderbookBoundaries newbdy = new OrderbookBoundaries();

            newbdy.process(ob);
            if (_bdy.differentFrom(newbdy))
            {
                Console.WriteLine("*** OB boundaries changed!! " + newbdy.ToString());
            }
            _bdy = newbdy;

            using (FileStream fs = new FileStream("frames/OB-" + _tag + ".txt", FileMode.Append, FileAccess.Write))
                using (StreamWriter sw = new StreamWriter(fs)) {
                    sw.WriteLine(line);
                }

            if (Scheduler.GetTime() - _LastDumpTime > DUMP_INTERVAL)
            {
                _LastDumpTime = Scheduler.GetTime();

                using (FileStream fs = new FileStream("frames/BIDS-" + _tag + "-" + _DumpNumber + ".dat", FileMode.Append, FileAccess.Write))
                    using (StreamWriter sw = new StreamWriter(fs)) {
                        if (_Virgin)
                        {
                            sw.WriteLine("#===================================");
                            _Virgin = true;
                        }

                        IDictionary <double, IList <IOrder_Mutable> > bids = ob.getBids_Mutable();
                        sw.WriteLine("# num bid: " + bids.Keys.Count);
                        sw.WriteLine("# low bid: " + (ob.getNumBids() > 0 ? "" + ob.getLowestBid():"?"));
                        sw.WriteLine("# high bid: " + (ob.getNumBids() > 0 ? "" + ob.getHighestBid():"?"));
                        foreach (double price in bids.Keys)
                        {
                            IList <IOrder_Mutable> orders = bids[price];
                            double vol = 0;
                            foreach (IOrder_Mutable o in orders)
                            {
                                vol += o.getVolume();
                            }
                            sw.WriteLine("" + price + "," + vol + "");
                        }
                    }


                using (FileStream fs = new FileStream("frames/ASKS-" + _tag + "-" + _DumpNumber + ".dat", FileMode.Append, FileAccess.Write))
                    using (StreamWriter sw = new StreamWriter(fs)) {
                        if (_Virgin)
                        {
                            sw.WriteLine("#===================================");
                            _Virgin = true;
                        }

                        IDictionary <double, IList <IOrder_Mutable> > asks = ob.getAsks_Mutable();
                        sw.WriteLine("# num ask: " + asks.Keys.Count);
                        sw.WriteLine("# low ask: " + (ob.getNumAsks() > 0 ? "" + ob.getLowestAsk():"?"));
                        sw.WriteLine("# high ask: " + (ob.getNumAsks() > 0 ? "" + ob.getHighestAsk():"?"));
                        foreach (double price in asks.Keys)
                        {
                            IList <IOrder_Mutable> orders = asks[price];
                            double vol = 0;
                            foreach (IOrder_Mutable o in orders)
                            {
                                vol += o.getVolume();
                            }
                            sw.WriteLine("" + price + "," + vol);
                        }
                    }

                using (FileStream fs = new FileStream("frames/GP-" + _tag + "-" + _DumpNumber + ".gp", FileMode.Append, FileAccess.Write))
                    using (StreamWriter sw = new StreamWriter(fs)) {
                        sw.WriteLine("set terminal postscript eps color");
                        sw.WriteLine("set output \"EPS-" + _tag + "-" + String.Format("{0:00000000}", _DumpNumber) + ".eps\"");
                        sw.WriteLine("set title \"Orderbook at time=" + Scheduler.GetTime() + " in " + _tag + "\"");
                        sw.WriteLine("set ylabel \"Orders (count)\"");
                        sw.WriteLine("set xlabel \"Price\"");
                        sw.WriteLine("binwidth=0.125");
                        sw.WriteLine("set boxwidth binwidth");
                        sw.WriteLine("bin(x,width)=width*floor(x/width) + binwidth/2.0");
                        sw.WriteLine("plot 'Asks-" + _tag + "-" + _DumpNumber + ".dat' using (bin($1,binwidth)):(1.0) smooth freq with boxes t \"ASKS " + _tag + "-" + _DumpNumber + "\" fs solid 0.50, 'Bids-" + _tag + "-" + _DumpNumber + ".dat' using (bin($1,binwidth)):(1.0) smooth freq with boxes t \"BIDS " + _tag + "-" + _DumpNumber + "\" fill empty");
                    }

                _DumpNumber++;
            }
        }
Beispiel #2
0
        private void fill(IOrder_Mutable order, double p, IOrderbook_Matcher ob)
        {
            IDictionary <double, IList <IOrder_Mutable> > asksDict = ob.getAsks_Mutable();
            IDictionary <double, IList <IOrder_Mutable> > bidsDict = ob.getBids_Mutable();

            IList <IOrder_Mutable> orderlist;

            if (!order.isBid())
            {
                orderlist = bidsDict[p];
            }
            else
            {
                orderlist = asksDict[p];
            }

            // use the dual orders at p to fill the pending order
            while (orderlist != null && orderlist.Count > 0)
            {
                IOrder_Mutable o2 = orderlist[0];

                int tradeVolume = Math.Min(o2.getVolume(), order.getVolume());


                // collide o2 against the order
                double executionPrice;

                // symmetrized logic
                if (order.isAsk())
                {
                    // an Ask arrived
                    // the execution price is the Bid price (per conversation with KD)
                    executionPrice = o2.getPrice();
                }
                else
                {
                    // a Bid arrived
                    // the execution price is the Ask price (per conversation with KD)
                    executionPrice = o2.getPrice();
                }

                _logger.DebugLog(SOURCE, "Matcher pairs: (A) " + order + " and (B) " + o2 + " -- tradeVol=" + tradeVolume + " price=" + executionPrice);

                o2.setFilled(tradeVolume, executionPrice);
                order.setFilled(tradeVolume, executionPrice);

                _logger.DebugLog(SOURCE, "Matcher post trade: (A) " + order + " and (B) " + o2);

                // if o2 gets filled, keep going through other orders at p
                if (o2.isFilled())
                {
                    ob.flushOrder(o2);

                    if (!order.isBid())
                    {
                        if (bidsDict.ContainsKey(p))
                        {
                            orderlist = bidsDict[p];
                        }
                        else
                        {
                            orderlist = null;
                        }
                    }
                    else
                    {
                        if (asksDict.ContainsKey(p))
                        {
                            orderlist = asksDict[p];
                        }
                        else
                        {
                            orderlist = null;
                        }
                    }

                    // notify o2's owner of fill
                    _NumFillsSent++;
                    _logger.DebugLog(SOURCE, "Matcher notify o2's owner of fill");
                    ob.notifyObserverSynchronous(new OrderbookEvent_FillOrder(o2, executionPrice, tradeVolume, true));
                }
                else
                {
                    // notify o2's owener of partial fill
                    _NumPartialFillsSent++;
                    _logger.DebugLog(SOURCE, "Matcher notify o2's owner of partial fill");
                    ob.notifyObserverSynchronous(new OrderbookEvent_FillOrder(o2, executionPrice, tradeVolume, false));
                }

                // if order get filled
                if (order.isFilled())
                {
                    ob.flushOrder(order);
                    // notify order's owner of fill
                    _NumFillsSent++;
                    _logger.DebugLog(SOURCE, "Matcher notify o2's owner of fill");
                    ob.notifyObserverSynchronous(new OrderbookEvent_FillOrder(order, executionPrice, tradeVolume, true));
                    break;
                }
                else
                {
                    // notify order's owner of partial fill
                    _NumPartialFillsSent++;
                    _logger.DebugLog(SOURCE, "Matcher notify o2's owner of partial fill");
                    ob.notifyObserverSynchronous(new OrderbookEvent_FillOrder(order, executionPrice, tradeVolume, false));
                }
            }
        }