예제 #1
0
        static void AddPendingOrder(LiveOpenPositionsEditor openPositionData, Symbol symbol, string orderId, long size, DateTime submittedTime,
            OrderType orderType, TransactionType transactionType, double price, string customString)
        {
            if (openPositionData.PortfolioXml.PendingOrders.Any(o => o.OrderId == orderId))
            {
                //  Order already tracked
                return;
            }

            PositionType positionType = (transactionType == TransactionType.Buy || transactionType == TransactionType.Sell) ? PositionType.Long : PositionType.Short;

            //  This assumes there is just one position per symbol.  If this isn't the case then you will need to find a way of figuring out which
            //  position a pending order corresponds to.
            PositionDataXml position = openPositionData.PortfolioXml.Positions.FirstOrDefault(pos => pos.Symbol.Equals(symbol) && pos.PositionType == positionType);

            if (position == null)
            {
                //  No existing position, so create a new one
                position = openPositionData.AddPosition(symbol, positionType);
                position.CustomString = customString;
            }

            BrokerOrder brokerOrder = new BrokerOrder();
            if (orderType == OrderType.Limit || orderType == OrderType.LimitOnClose)
            {
                brokerOrder.LimitPrice = price;
            }
            else if (orderType == OrderType.Stop || orderType == OrderType.TrailingStop)
            {
                brokerOrder.StopPrice = price;
            }
            brokerOrder.CustomString = customString;

            TradeOrderXml tradeOrder = openPositionData.AddPendingOrder(position, brokerOrder, orderId, size, submittedTime, orderType, transactionType);
        }
예제 #2
-1
        //  Order ID
        //  Transaction type
        static void AddFilledOrder(LiveOpenPositionsEditor openPositionData, string orderID, Symbol symbol, PositionType direction, TransactionType transactionType, double fillPrice, long fillSize, DateTime fillTime, string customString)
        {
            if (openPositionData.PortfolioXml.Positions.SelectMany(pos => pos.Trades).Any(trade => trade.OrderID == orderID))
            {
                //  Trade already recorded
                return;
            }

            PositionDataXml position;

            BrokerOrder existingOrder = openPositionData.PortfolioXml.PendingOrders.FirstOrDefault(order => order.OrderId == orderID);
            if (existingOrder != null)
            {
                //  Order was pending, remove it
                PositionDataXml existingPosition = openPositionData.PortfolioXml.Positions.Single(p => p.PosID == existingOrder.PositionID);
                TradeOrderXml existingTradeOrder = existingPosition.PendingOrders.Single(to => to.OrderID == existingOrder.OrderId);
                existingPosition.PendingOrders.Remove(existingTradeOrder);
                openPositionData.PortfolioXml.PendingOrders.Remove(existingOrder);

                position = existingPosition;
            }
            else
            {
                position = openPositionData.AddPosition(symbol, direction);
                position.CustomString = customString;
            }

            openPositionData.AddCompletedTradeToPosition(position, fillTime, transactionType, new Price(fillPrice, fillPrice), fillSize, orderID);
        }