Exemple #1
0
        /// <summary>
        /// Processing order sitting in the queue when a new trade bar comes in (i.e. MOO orders)
        /// and updates the current value of stocks on our portfolio.
        /// Called from base algo.
        /// </summary>
        public void ProcessNewTradebar(TradeBar tradeBar)
        {
            //Pick up pending MOO orders placed yesterday
            for (int i = 0; i < PendingOrderQueue.Count; i++)
            {
                Order order = PendingOrderQueue[i];

                if (order.OrderType == OrderType.MOO && order.Status == OrderStatus.Pending)
                {
                    fillOrder(tradeBar, order);
                    PendingOrderQueue.RemoveAt(i);
                }
            }

            //Update current stock price of stocks in the portfolio
            if (IsHoldingStock(tradeBar.Symbol))
            {
                StockPortfolio.Find(p => p.Symbol == tradeBar.Symbol).CurrentPrice = tradeBar.Close;
            }

            //Update equity over time
            EquityOverTime.Add(tradeBar.Day, TotalEquity);
        }
Exemple #2
0
        /// <summary>
        /// Updates holdings for filled orders.
        /// </summary>
        private void updateHoldings(Order order)
        {
            //Only process filled orders
            if (order.Status != OrderStatus.Filled)
            {
                return;
            }

            //OrderHistory.Add(order);
            TotalTrades++;

            //Transaction fees
            TotalTransactionFees += TransactionFee;
            AvailableCash        -= TransactionFee;

            decimal orderTotal = order.Quantity * order.FillPrice;

            ///////////////////////////////
            //Buying
            //////////////////////////////
            switch (order.Action)
            {
            //Buying
            case Action.Buy:
                AvailableCash -= orderTotal;

                //Find existing stock in holdings
                if (IsHoldingStock(order.Symbol))
                {
                    Stock stock = StockPortfolio.Find(p => p.Symbol == order.Symbol);
                    stock.Quantity      += order.Quantity;
                    stock.TotalInvested += orderTotal;
                    stock.CurrentPrice   = order.FillPrice;
                }
                //Create a new stock object and add to holdings
                else
                {
                    Stock newStock = new Stock()
                    {
                        Symbol        = order.Symbol,
                        Quantity      = order.Quantity,
                        TotalInvested = orderTotal,
                        CurrentPrice  = order.FillPrice
                    };

                    StockPortfolio.Add(newStock);
                }
                break;

            ///////////////////////
            //Selling
            ///////////////////////
            case Action.Sell:
                AvailableCash += orderTotal;
                _totalSellTrades++;

                if (IsHoldingStock(order.Symbol))
                {
                    Stock stock = StockPortfolio.Find(p => p.Symbol == order.Symbol);

                    //Is this a win or loss?
                    if (orderTotal > (stock.AverageFillPrice * order.Quantity))
                    {
                        _totalWins++;
                    }
                    else
                    {
                        _totalLosses++;
                    }
                    stock.TotalInvested -= stock.AverageFillPrice * order.Quantity;
                    stock.Quantity      -= order.Quantity;

                    //remove the stock from the portfolio if no longer holding any.
                    if (stock.Quantity <= 0)
                    {
                        StockPortfolio.Remove(stock);
                    }
                }
                break;
            }
        }