public void MatchedAndUnmatchedTransactions()
        {
            string path = @"C:\Users\Nick\Documents\Visual Studio 2013\Projects\LeanITrend\Engine\bin\Debug\";
            string pathname = path + "transactions.csv";
            // This part of the test is just to look at the JsonConvert
            //string txt;
            //using (StreamReader sr = new StreamReader(pathname))
            //{
            //    txt = sr.ReadToEnd();
            //    sr.Close();
            //}
            //int index = txt.IndexOf("\r\n", System.StringComparison.Ordinal);
            //string titlesremoved = txt.Substring(index + 2);

            int counter = 0;
            List<OrderTransaction> list = new List<OrderTransaction>();

            OrderTransactionProcessor processor = new OrderTransactionProcessor();

            using (StreamReader sr = new StreamReader(pathname))
            {
                string line = sr.ReadLine();    // read the header but do not count it.

                while (!sr.EndOfStream)
                {
                    line = sr.ReadLine();
                    if (line != null && line.Contains("Symbol")) continue;
                    Assert.IsNotNull(line);
                    counter++;
                    OrderTransaction t = new OrderTransaction();
                    CsvSerializer.Deserialize(",",line,ref t,false);
                    list.Add(t);
                    processor.ProcessTransaction(t);
                }
                sr.Close();
            }
            var csv = CsvSerializer.Serialize(",", processor.Trades, true);
            using (StreamWriter sw = new StreamWriter(path + "Trades.csv"))
            {
                foreach (var s in csv)
                {
                    sw.WriteLine(s);
                }
                sw.Flush();
                sw.Close();
            }
            var x = JsonConvert.SerializeObject(list);
            Assert.IsTrue(counter == list.Count);
            Assert.IsTrue(processor.TotalProfit == 52.75m);
            Assert.IsTrue(processor.TotalCommission == -26m);
        }
        /// <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;
            }
        }
Exemplo n.º 4
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;
            }
        }
Exemplo n.º 5
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;
            }
        }