Exemplo n.º 1
0
        public void TestPositionUpdate()
        {
            var positionCache = new PositionCache();
            var tradeRecord   = MessageUtil.CreateTradeRecord("IBM", "Buy", 100, 110.89, "QF.Test");
            var position      = positionCache.Update(tradeRecord);

            Assert.AreEqual(1, positionCache.Count);

            VerifyPositionRecord(position);

            Assert.AreEqual("IBM", position["BID"]);
            Assert.AreEqual("QF.Test", position["Strategy"]);
            Assert.AreEqual("100", position["SharesBought"]);
            Assert.AreEqual("0", position["SharesSold"]);
            Assert.AreEqual("100", position["Position"]);

            // Lets double up
            var doubledPosition = positionCache.Update(tradeRecord);

            Assert.AreEqual(1, positionCache.Count);
            VerifyPositionRecord(doubledPosition);
            Assert.AreEqual("IBM", doubledPosition["BID"]);
            Assert.AreEqual("QF.Test", doubledPosition["Strategy"]);
            Assert.AreEqual("200", doubledPosition["SharesBought"]);
            Assert.AreEqual("0", doubledPosition["SharesSold"]);
            Assert.AreEqual("200", doubledPosition["Position"]);

            // Add the other side
            var tradeRecord3 = MessageUtil.CreateTradeRecord("IBM", "Sell", 50, 111.64, "QF.Test");
            var position3    = positionCache.Update(tradeRecord3);

            Assert.AreEqual(1, positionCache.Count);

            VerifyPositionRecord(position3);
            Assert.AreEqual("IBM", position3["BID"]);
            Assert.AreEqual("QF.Test", position3["Strategy"]);
            Assert.AreEqual("200", position3["SharesBought"]);
            Assert.AreEqual("50", position3["SharesSold"]);
            Assert.AreEqual("150", position3["Position"]);

            // Add a different position
            var tradeRecord4 = MessageUtil.CreateTradeRecord("MSFT", "Sell", 50, 55.32, "QF.Test");
            var position4    = positionCache.Update(tradeRecord4);

            Assert.AreEqual(2, positionCache.Count);

            VerifyPositionRecord(position3);
            Assert.AreEqual("MSFT", position4["BID"]);
            Assert.AreEqual("QF.Test", position4["Strategy"]);
            Assert.AreEqual("0", position4["SharesBought"]);
            Assert.AreEqual("50", position4["SharesSold"]);
            Assert.AreEqual("-50", position4["Position"]);
        }
Exemplo n.º 2
0
        public void TestOrderXmlToOrders()
        {
            var fullFileContents = LoadTestOrders();

            // do the test
            var orders = MessageUtil.ParseXmlToDictionary(fullFileContents, 1);

            Assert.IsNotNull(orders);
            Assert.AreEqual(5, orders.Count);

            var equityOrder = orders[0];

            Assert.IsTrue(MessageUtil.IsEquityOrder(equityOrder));

            VerifyAllFields(orders);
            equityOrder["BID"] = EquityTickerUtil.ConvertRicToTicker(equityOrder["RIC"]);

            var tradeRecord = MessageUtil.CreateTradeRecord(equityOrder);

            Assert.IsNotNull(tradeRecord);
            Assert.AreEqual("MSFT", tradeRecord["BID"]);
        }
        /// <summary>
        /// Event handler for order creation and/or update.
        /// </summary>
        /// <param name="orderId">the order id or multiple if more than one order is received.</param>
        /// <param name="xmlOrders"></param>
        /// <param name="uniqueId"></param>
        /// <param name="confirmed"></param>
        public void ReceiveOrders(string orderId, string xmlOrders, int uniqueId, ref bool confirmed)
        {
            // extract the orders, translate symbols, and update the cache
            var positionsToPublish = new List <IDictionary <string, string> >();

            lock (_orderEventIds) {
                try {
                    if (_orderEventIds.Contains(uniqueId))
                    {
                        return;
                    }
                    var orders = MessageUtil.ParseXmlToDictionary(xmlOrders, uniqueId);
                    foreach (var order in orders)
                    {
                        // convert order into a trade record
                        if (MessageUtil.IsEquityOrder(order))
                        {
                            var ticker = EquityTickerUtil.ConvertRicToTicker(order["RIC"]);
                            order["BID"] = ticker;
                        }
                        else if (MessageUtil.IsFuturesOrder(order))
                        {
                            var failedRicLookup = false;
                            var ric             = order["RIC"];
                            var ricRoot         = FuturesSymbolUtil.ExtractSymbolRoot(ric);
                            var bbRoot          = _fsm.ConvertRicRoot(ricRoot);
                            if (string.IsNullOrEmpty(bbRoot))
                            {
                                _log.Warn("No mapping for RIC root: " + ricRoot);
                                bbRoot           = ricRoot;
                                order["WARNING"] = "No mapping for RIC root";
                                failedRicLookup  = true;
                            }
                            var monthYear = FuturesSymbolUtil.ExtractMaturityMonthFromSymbol(order["RIC"]);
                            var bbSymbol  = FuturesSymbolUtil.CombineRootMaturityMonthYear(bbRoot, monthYear);
                            if (failedRicLookup)
                            {
                                bbSymbol = bbSymbol + " - using RIC";
                            }
                            order["BID"] = bbSymbol;
                        }
                        else
                        {
                            continue;
                        }

                        var tradeRecord     = MessageUtil.CreateTradeRecord(order);
                        var updatedPosition = _positionCache.Update(tradeRecord);
                        positionsToPublish.Add(updatedPosition);
                    }
                    _orderEventIds.Add(uniqueId);
                    confirmed = true;
                } catch (Exception e) {
                    _log.Error("Processing order events from TradingScreen", e);
                }
            }
            try {
                foreach (var position in positionsToPublish)
                {
                    position.Add("Source", "TradingScreen");
                    var strategy = position["Strategy"];
                    if (string.IsNullOrEmpty(strategy))
                    {
                        strategy = "MISSING";
                    }
                    var sb = new StringBuilder(512);
                    sb.Append("TradingScreen.Positions.").Append(strategy).Append(".").Append(position["BID"]);

                    _broker.Publish(sb.ToString(), position);
                }
            } catch (Exception e) {
                _log.Error("Publishing positions from TradingScreen", e);
            }
        }