Example #1
0
        /// <summary>
        /// Local processing of the order event
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            orderId = orderEvent.OrderId;
            var tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId);

            nEntryPrice = 0;
            nExitPrice  = 0;

            if (tickets.Any())
            {
                foreach (OrderTicket ticket in tickets)
                {
                    var status = ticket.Status;
                    if (ticket.Status == OrderStatus.Canceled)
                    {
                        iTrendStrategy.orderFilled = false;
                    }
                    if (ticket.Status == OrderStatus.Filled)
                    {
                        iTrendStrategy.orderFilled = true;

                        #region logging

                        OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        var t = transactionFactory.Create(orderEvent, ticket, false);
                        _transactions.Add(t);
                        _tradecount++;
                        #endregion


                        if (Portfolio[orderEvent.Symbol].Invested)
                        {
                            nEntryPrice = orderEvent.FillPrice;
                            #region logging
                            tradefees   = Securities[symbol].Holdings.TotalFees - lasttradefees;
                            nEntryPrice = orderEvent.FillPrice;

                            #endregion
                        }
                        #region logging
                        else
                        {
                            tradefees += Securities[symbol].Holdings.TotalFees - lasttradefees;
                            nExitPrice = orderEvent.FillPrice;
                            CalculateTradeProfit(ticket);
                        }
                        #endregion
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            //add to the list of order events which is saved to a file when running locally 
            //  I will use this file to test Stefano Raggi's code

            var currentSignalInfo = signalInfos.FirstOrDefault(s => s.Symbol == orderEvent.Symbol);
            orderId = orderEvent.OrderId;

            if (currentSignalInfo != null)
                currentSignalInfo.Status = orderEvent.Status;


            IEnumerable<OrderTicket> tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId);

            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                case OrderStatus.Invalid:
                    break;
                case OrderStatus.PartiallyFilled:
                    if (currentSignalInfo != null)
                    {

                        nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;
                        currentSignalInfo.TradeAttempts++;
                        //Log(string.Format("Trade Attempts: {0} OrderId {1}", currentSignalInfo.TradeAttempts, orderEvent.OrderId));
                    }

                    break;
                case OrderStatus.Canceled:
                    if (currentSignalInfo != null)
                    {
                        //Log(string.Format("Order {0} cancelled.", orderEvent.OrderId));
                        currentSignalInfo.IsActive = true;
                        currentSignalInfo.TradeAttempts = 0;
                    }
                    break;
                case OrderStatus.Filled:

                    if (currentSignalInfo != null)
                    {
                        currentSignalInfo.IsActive = true;
                        //if (currentSignalInfo.TradeAttempts > 0)
                        //    Log(string.Format("Order Filled OrderId {0} on attempt {1}", orderEvent.OrderId, currentSignalInfo.TradeAttempts));
                        currentSignalInfo.TradeAttempts = 0;

                    }
                    nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;

                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            //int infoId = Convert.ToInt32(ticket.Tag);

                            #region "save the ticket as a OrderTransacton"
                            OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            OrderTransaction t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            _orderTransactionProcessor.ProcessTransaction(t);
                            _tradecount++;
                            if (_orderTransactionProcessor.TotalProfit != totalProfit)
                            {
                                tradenet = CalculateTradeProfit(t.Symbol);
                            }
                            totalProfit = _orderTransactionProcessor.TotalProfit;

                            #endregion

                        }
                    }
                    break;
            }
        }
Example #3
0
        /// <summary>
        /// Local processing of the order event
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            orderId = orderEvent.OrderId;
            var tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId);
            if (tickets.Any())
            {
                foreach (OrderTicket ticket in tickets)
                {
                    var status = ticket.Status;
                    if (ticket.Status == OrderStatus.Canceled)
                    {
                        //iTrendStrategy.orderFilled = false;
                    }
                    if (ticket.Status == OrderStatus.Filled)
                    {
                        //iTrendStrategy.orderFilled = true;

                        #region logging
                        OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        OrderTransaction t = transactionFactory.Create(orderEvent, ticket);
                        tradecount++;
                        #endregion


                        if (Portfolio[orderEvent.Symbol].Invested)
                        {
                            //iTrendStrategy.nEntryPrice = orderEvent.FillPrice;
                            #region logging
                            tradefees = Securities[Symbols[0]].Holdings.TotalFees - lasttradefees;
                            #endregion


                        }
                        #region logging
                        else
                        {
                            tradefees += Securities[Symbols[0]].Holdings.TotalFees - lasttradefees;
                            CalculateTradeProfit(ticket);
                        }
                        #endregion
                    }
                }
            }
        }
        /// <summary>
        /// Local processing of the order event
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            var security = Securities[orderEvent.Symbol];
            IEnumerable <OrderTicket> tickets;
            var tm = this.BrokerageModel.GetTransactionModel(security);

            if (orderEvent.Status == OrderStatus.Filled)
            {
                _orderEvents.Add(orderEvent);
            }
            orderId     = orderEvent.OrderId;
            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
            case OrderStatus.New:
            case OrderStatus.None:
            case OrderStatus.Submitted:
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);

                break;

            case OrderStatus.Canceled:
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                iTrendStrategy.orderFilled = false;
                break;

            case OrderStatus.Filled:
            case OrderStatus.PartiallyFilled:
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                if (tickets != null)
                {
                    foreach (OrderTicket ticket in tickets)
                    {
                        iTrendStrategy.orderFilled = true;
                        if (Portfolio[orderEvent.Symbol].Invested)
                        {
                            nEntryPrice = Portfolio[symbol].IsLong ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                            nExitPrice  = 0;
                        }
                        else
                        {
                            nExitPrice  = nEntryPrice < 0 ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                            nEntryPrice = 0;
                        }

                        #region "log the ticket as a OrderTransacton"

                        var transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        var t = transactionFactory.Create(orderEvent, ticket, false);
                        _transactions.Add(t);
                        _orderTransactionProcessor.ProcessTransaction(t);
                        _tradecount++;
                        if (_orderTransactionProcessor.TotalProfit != totalProfit)
                        {
                            CalculateTradeProfit();
                        }
                        totalProfit = _orderTransactionProcessor.TotalProfit;

                        #endregion
                    }
                }

                break;
            }
        }
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable<OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code
            if (orderEvent.Status == OrderStatus.Filled)
                _orderEvents.Add(orderEvent);

            orderId = orderEvent.OrderId;

            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                    // just checking to make sure they are coming through
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    break;
                case OrderStatus.Canceled:
                    // just checking
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    break;
                case OrderStatus.Filled:
                case OrderStatus.PartiallyFilled:

                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            #region logging
                            if (Portfolio[orderEvent.Symbol].Invested)
                            {
                                nEntryPrice = Portfolio[symbol].IsLong ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                                nExitPrice = 0;
                            }
                            else
                            {
                                nExitPrice = nEntryPrice < 0 ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                                nEntryPrice = 0;
                            }

                            #region "log the ticket as a OrderTransacton"

                            OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            OrderTransaction t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            _orderTransactionProcessor.ProcessTransaction(t);
                            _tradecount++;
                            if (_orderTransactionProcessor.TotalProfit != totalProfit)
                            {
                                CalculateTradeProfit();
                            }
                            totalProfit = _orderTransactionProcessor.TotalProfit;
                            #endregion
                            #endregion "logging"
                        }
                    }
                    break;
            }
        }
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable <OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code
            if (orderEvent.Status == OrderStatus.Filled)
            {
                _orderEvents.Add(orderEvent);
            }

            orderId = orderEvent.OrderId;

            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
            case OrderStatus.New:
            case OrderStatus.None:
            case OrderStatus.Submitted:
                // just checking to make sure they are coming through
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                break;

            case OrderStatus.Canceled:
                // just checking
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                break;

            case OrderStatus.Filled:
            case OrderStatus.PartiallyFilled:

                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                if (tickets != null)
                {
                    foreach (OrderTicket ticket in tickets)
                    {
                        #region logging
                        if (Portfolio[orderEvent.Symbol].Invested)
                        {
                            nEntryPrice = Portfolio[symbol].IsLong ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                            nExitPrice  = 0;
                        }
                        else
                        {
                            nExitPrice  = nEntryPrice < 0 ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                            nEntryPrice = 0;
                        }

                        #region "log the ticket as a OrderTransacton"

                        OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        OrderTransaction        t = transactionFactory.Create(orderEvent, ticket, false);
                        _transactions.Add(t);
                        _orderTransactionProcessor.ProcessTransaction(t);
                        _tradecount++;
                        if (_orderTransactionProcessor.TotalProfit != totalProfit)
                        {
                            CalculateTradeProfit();
                        }
                        totalProfit = _orderTransactionProcessor.TotalProfit;
                        #endregion
                        #endregion "logging"
                    }
                }
                break;
            }
        }
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable<OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code
            if (orderEvent.Status == OrderStatus.Filled)
                _orderEvents.Add(orderEvent);

            orderId = orderEvent.OrderId;
            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                    // just checking to make sure they are coming through
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    break;
                case OrderStatus.Canceled:
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            int infoId = Convert.ToInt32(ticket.Tag);
                            SignalInfo si = signalInfos.FirstOrDefault(f => f.Id == infoId);
                            if (si != null)
                                si.IsActive = true;
                        }
                    }
                    break;
                case OrderStatus.Filled:
                case OrderStatus.PartiallyFilled:
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            int infoId = Convert.ToInt32(ticket.Tag);
                            SignalInfo si = signalInfos.FirstOrDefault(f => f.Id == infoId);
                            if (si != null)
                                si.IsActive = true;

                            #region "log the ticket as a OrderTransacton"
                            OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            OrderTransaction t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            _orderTransactionProcessor.ProcessTransaction(t);
                            _tradecount++;
                            if (_orderTransactionProcessor.TotalProfit != totalProfit)
                            {
                                CalculateTradeProfit(t.Symbol);
                            }
                            totalProfit = _orderTransactionProcessor.TotalProfit;
                            #endregion

                        }
                    }
                    break;
            }
        }
Example #8
0
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable <OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code

            var currentSignalInfo = signalInfos.FirstOrDefault(s => s.Symbol == orderEvent.Symbol);

            orderId = orderEvent.OrderId;

            if (currentSignalInfo != null)
            {
                currentSignalInfo.Status = orderEvent.Status;
            }


            tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId);

            switch (orderEvent.Status)
            {
            case OrderStatus.New:
            case OrderStatus.None:
            case OrderStatus.Submitted:
            case OrderStatus.Invalid:
                break;

            case OrderStatus.PartiallyFilled:
                if (currentSignalInfo != null)
                {
                    nEntryPrice = Portfolio[symbol].HoldStock ? Portfolio[symbol].AveragePrice : 0;
                }

                break;

            case OrderStatus.Canceled:
                if (currentSignalInfo != null)
                {
                    currentSignalInfo.IsActive = true;
                }

                break;

            case OrderStatus.Filled:

                if (currentSignalInfo != null)
                {
                    currentSignalInfo.IsActive = true;
                }

                nEntryPrice = Portfolio[symbol].HoldStock ? Portfolio[symbol].AveragePrice : 0;

                if (tickets != null)
                {
                    foreach (OrderTicket ticket in tickets)
                    {
                        //int infoId = Convert.ToInt32(ticket.Tag);

                        #region "save the ticket as a OrderTransacton"
                        OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        OrderTransaction        t = transactionFactory.Create(orderEvent, ticket, false);
                        _transactions.Add(t);
                        _orderTransactionProcessor.ProcessTransaction(t);
                        _tradecount++;
                        if (_orderTransactionProcessor.TotalProfit != totalProfit)
                        {
                            tradenet = CalculateTradeProfit(t.Symbol);
                        }
                        totalProfit = _orderTransactionProcessor.TotalProfit;

                        #endregion
                    }
                }
                break;
            }
        }
Example #9
0
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            int orderId;
            var strategy = Strategies.FirstOrDefault(s => s.GetSymbol() == orderEvent.Symbol);
            if (strategy != null)
            {
                orderId = orderEvent.OrderId;

                strategy.Status = orderEvent.Status;
            }

            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                case OrderStatus.Invalid:
                    break;
                case OrderStatus.PartiallyFilled:
                    if (strategy != null)
                    {
                        //nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;
                        strategy.TradeAttempts++;
                        //Log(string.Format("Trade Attempts: {0} OrderId {1}", currentSignalInfo.TradeAttempts, orderEvent.OrderId));
                    }

                    break;
                case OrderStatus.Canceled:
                    if (strategy != null)
                    {
                        //Log(string.Format("Order {0} cancelled.", orderEvent.OrderId));
                        strategy.IsActive = true;
                        strategy.TradeAttempts = 0;
                        strategy.ActualSignal = OrderSignal.doNothing;
                    }
                    break;
                case OrderStatus.Filled:

                    if (strategy != null)
                    {
                        strategy.Entryprice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;
                        strategy.Exitprice = Portfolio[orderEvent.Symbol].HoldStock ? 0 : orderEvent.FillPrice;
                        strategy.IsActive = true;
                        //if (currentSignalInfo.TradeAttempts > 0)
                        //    Log(string.Format("Order Filled OrderId {0} on attempt {1}", orderEvent.OrderId, currentSignalInfo.TradeAttempts));
                        strategy.TradeAttempts = 0;
                        strategy.ActualSignal = OrderSignal.doNothing;


                    }
                    //nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;
                    IEnumerable<OrderTicket> tickets = Transactions.GetOrderTickets(t => t.OrderId == orderEvent.OrderId);
                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            #region "save the ticket as a OrderTransacton"
                            OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            OrderTransaction t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            _orderTransactionProcessor.ProcessTransaction(t);
                            #endregion

                        }
                    }
                    break;
            }
        }
        /// <summary>
        /// Local processing of the order event
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            var security = Securities[orderEvent.Symbol];
            IEnumerable<OrderTicket> tickets;
            var tm = this.BrokerageModel.GetTransactionModel(security);
            if (orderEvent.Status == OrderStatus.Filled)
                _orderEvents.Add(orderEvent);
            orderId = orderEvent.OrderId;
            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);

                    break;
                case OrderStatus.Canceled:
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    iTrendStrategy.orderFilled = false;
                    break;
                case OrderStatus.Filled:
                case OrderStatus.PartiallyFilled:
                    tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            iTrendStrategy.orderFilled = true;
                            if (Portfolio[orderEvent.Symbol].Invested)
                            {
                                nEntryPrice = Portfolio[symbol].IsLong ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                                nExitPrice = 0;
                            }
                            else
                            {
                                nExitPrice = nEntryPrice < 0 ? orderEvent.FillPrice : orderEvent.FillPrice * -1;
                                nEntryPrice = 0;
                            }

                            #region "log the ticket as a OrderTransacton"

                            var transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            var t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            _orderTransactionProcessor.ProcessTransaction(t);
                            _tradecount++;
                            if (_orderTransactionProcessor.TotalProfit != totalProfit)
                            {
                                CalculateTradeProfit();
                            }
                            totalProfit = _orderTransactionProcessor.TotalProfit;

                            #endregion
                        }
                    }

                    break;

            }
        }
Example #11
0
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable <OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code
            if (orderEvent.Status == OrderStatus.Filled)
            {
                _orderEvents.Add(orderEvent);
            }

            orderId = orderEvent.OrderId;
            if (orderId == 92)
            {
                System.Diagnostics.Debug.WriteLine("92");
            }
            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
            case OrderStatus.New:
            case OrderStatus.None:
            case OrderStatus.Submitted:
                // just checking to make sure they are coming through
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                break;

            case OrderStatus.Canceled:
                // just checking
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                break;

            case OrderStatus.Invalid:
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                break;

            case OrderStatus.Filled:
            case OrderStatus.PartiallyFilled:

                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                if (tickets != null)
                {
                    foreach (OrderTicket ticket in tickets)
                    {
                        #region logging
                        #region "log the ticket as a OrderTransacton"

                        var transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        var t = transactionFactory.Create(orderEvent, ticket, false);
                        _transactions.Add(t);
                        _orderTransactionProcessor.ProcessTransaction(t);
                        _tradecount++;
                        if (_orderTransactionProcessor.TotalProfit != totalProfit)
                        {
                            CalculateTradeProfit();
                        }
                        totalProfit = _orderTransactionProcessor.TotalProfit;
                        #endregion
                        #endregion "logging"
                    }
                }
                break;
            }
        }
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable <OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code
            if (orderEvent.Status == OrderStatus.Filled)
            {
                _orderEvents.Add(orderEvent);
            }

            orderId     = orderEvent.OrderId;
            tradeResult = orderEvent.Status;
            switch (orderEvent.Status)
            {
            case OrderStatus.New:
            case OrderStatus.None:
            case OrderStatus.Submitted:
                // just checking to make sure they are coming through
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                break;

            case OrderStatus.Canceled:
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                if (tickets != null)
                {
                    foreach (OrderTicket ticket in tickets)
                    {
                        int        infoId = Convert.ToInt32(ticket.Tag);
                        SignalInfo si     = signalInfos.FirstOrDefault(f => f.Id == infoId);
                        if (si != null)
                        {
                            si.IsActive = true;
                        }
                    }
                }
                break;

            case OrderStatus.Filled:
            case OrderStatus.PartiallyFilled:
                tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId && t.Status == orderEvent.Status);
                if (tickets != null)
                {
                    foreach (OrderTicket ticket in tickets)
                    {
                        int        infoId = Convert.ToInt32(ticket.Tag);
                        SignalInfo si     = signalInfos.FirstOrDefault(f => f.Id == infoId);
                        if (si != null)
                        {
                            si.IsActive = true;
                        }

                        #region "log the ticket as a OrderTransacton"
                        OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                        OrderTransaction        t = transactionFactory.Create(orderEvent, ticket, false);
                        _transactions.Add(t);
                        _orderTransactionProcessor.ProcessTransaction(t);
                        _tradecount++;
                        if (_orderTransactionProcessor.TotalProfit != totalProfit)
                        {
                            CalculateTradeProfit(t.Symbol);
                        }
                        totalProfit = _orderTransactionProcessor.TotalProfit;
                        #endregion
                    }
                }
                break;
            }
        }
Example #13
0
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            //add to the list of order events which is saved to a file when running locally 
            //  I will use this file to test Stefano Raggi's code
            if (orderEvent.OrderId == 82)
                System.Diagnostics.Debug.WriteLine("ORder 82");
            var currentSignalInfo = signalInfos.FirstOrDefault(s => s.Symbol == orderEvent.Symbol);
            orderId = orderEvent.OrderId;

            if (currentSignalInfo != null) currentSignalInfo.Status = orderEvent.Status;
            IEnumerable<OrderTicket> tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId);
            string msg = string.Empty;
            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                    break;
                case OrderStatus.Invalid:
                    msg = string.Format("Order {0} invalidated attempt {1}. {2} shares at {3}, {4}",
                        orderEvent.OrderId,
                        currentSignalInfo.TradeAttempts,
                        orderEvent.FillQuantity,
                        orderEvent.FillPrice,
                        orderEvent.Message);
                    Log(msg);
                    EmailTradeBarMessage(msg);
                    break;
                case OrderStatus.PartiallyFilled:
                    if (currentSignalInfo != null)
                    {

                        nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;
                        currentSignalInfo.TradeAttempts++;
                        msg = string.Format("Order {0} Partial Fill confirmed on attempt {1}. {2} shares at {3}",
                            orderEvent.OrderId,
                            currentSignalInfo.TradeAttempts,
                            orderEvent.FillQuantity,
                            orderEvent.FillPrice);
                        Log(msg);
                        EmailTradeBarMessage(msg);
                    }

                    break;
                case OrderStatus.Canceled:
                    if (currentSignalInfo != null)
                    {
                        Log(string.Format("Order {0} cancellation confirmed.", orderEvent.OrderId));
                        currentSignalInfo.IsActive = true;
                        currentSignalInfo.TradeAttempts = 0;
                        msg = string.Format("Order {0} Holdings for {1}: {2} shares at {3}",
                            orderEvent.OrderId,
                            Portfolio[orderEvent.Symbol].Symbol.Value,
                            Portfolio[orderEvent.Symbol].Quantity,
                            Portfolio[orderEvent.Symbol].AveragePrice);
                        Log(msg);
                        EmailTradeBarMessage(msg);

                    }
                    break;
                case OrderStatus.Filled:

                    if (currentSignalInfo != null)
                    {
                        currentSignalInfo.IsActive = true;
                        msg = string.Format("Order {0} Fill {1} confirmed on attempt {2}. {3} shares at {4}",
                            orderEvent.OrderId,
                            currentSignalInfo.Value,
                            currentSignalInfo.TradeAttempts,
                            orderEvent.FillQuantity,
                            orderEvent.FillPrice);
                        Log(msg);
                        EmailTradeBarMessage(msg);
                        currentSignalInfo.TradeAttempts = 0;
                    }
                    nEntryPrice = Portfolio[orderEvent.Symbol].HoldStock ? Portfolio[orderEvent.Symbol].AveragePrice : 0;

                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            #region "save the ticket as a OrderTransacton"
                            OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            OrderTransaction t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            string json = JsonConvert.SerializeObject(t);
                            EmailTransactionMessage(json);
                            #endregion
                        }
                        msg = string.Format("Order {0} Holdings for {1}: {2} shares at {3}",
                            orderEvent.OrderId,
                            Portfolio[orderEvent.Symbol].Symbol.Value,
                            Portfolio[orderEvent.Symbol].Quantity,
                            Portfolio[orderEvent.Symbol].AveragePrice);
                        Log(msg);
                        EmailTradeBarMessage(msg);
                    }
                    break;
            }
        }
Example #14
0
        /// <summary>
        /// Local processing of the order event.  It only logs the transaction and orderEvent
        /// </summary>
        /// <param name="orderEvent">OrderEvent - the order event</param>
        private void ProcessOrderEvent(OrderEvent orderEvent)
        {
            IEnumerable<OrderTicket> tickets;

            //add to the list of order events which is saved to a file when running locally
            //  I will use this file to test Stefano Raggi's code

            _orderEvents.Add(orderEvent);
            var currentSignalInfo = signalInfos.FirstOrDefault(s => s.Symbol == orderEvent.Symbol);
            orderId = orderEvent.OrderId;
            tradeResult = orderEvent.Status;

            if (currentSignalInfo != null)
                currentSignalInfo.Status = orderEvent.Status;

            tickets = Transactions.GetOrderTickets(t => t.OrderId == orderId);

            switch (orderEvent.Status)
            {
                case OrderStatus.New:
                case OrderStatus.None:
                case OrderStatus.Submitted:
                case OrderStatus.Invalid:

                    // just checking to make sure they are coming through

                    break;
                case OrderStatus.PartiallyFilled:
                    if (currentSignalInfo != null)
                    {

                        if (Portfolio[symbol].HoldStock)
                        {
                            nEntryPrice = Portfolio[symbol].AveragePrice;
                        }
                        else
                        {
                            nEntryPrice = 0;
                        }

                    }

                    break;
                case OrderStatus.Canceled:
                    //if (tickets != null)
                    //{
                    //    foreach (OrderTicket ticket in tickets)
                    //    {
                    //        int infoId = Convert.ToInt32(ticket.Tag);
                    //        SignalInfo si = signalInfos.FirstOrDefault(f => f.Id == infoId);
                    //        if (si != null)
                    //            si.IsActive = true;
                    //    }
                    //}
                    if (currentSignalInfo != null)
                        currentSignalInfo.IsActive = true;

                    break;
                case OrderStatus.Filled:

                    if (currentSignalInfo != null)
                        currentSignalInfo.IsActive = true;

                    if (Portfolio[symbol].HoldStock)
                    {
                        nEntryPrice = Portfolio[symbol].AveragePrice;
                        //nExitPrice = 0;
                    }
                    else
                    {
                        nEntryPrice = 0;
                        //nExitPrice = orderEvent.FillPrice;
                    }

                    if (tickets != null)
                    {
                        foreach (OrderTicket ticket in tickets)
                        {
                            //int infoId = Convert.ToInt32(ticket.Tag);

                            #region "log the ticket as a OrderTransacton"
                            OrderTransactionFactory transactionFactory = new OrderTransactionFactory((QCAlgorithm)this);
                            OrderTransaction t = transactionFactory.Create(orderEvent, ticket, false);
                            _transactions.Add(t);
                            _orderTransactionProcessor.ProcessTransaction(t);
                            _tradecount++;
                            if (WebSiteAvailable)
                                SendTransaction(t);
                            if (_orderTransactionProcessor.TotalProfit != totalProfit)
                            {
                                tradenet = CalculateTradeProfit(t.Symbol);
                            }
                            totalProfit = _orderTransactionProcessor.TotalProfit;
                            #endregion

                        }
                    }
                    break;
            }
        }