Esempio n. 1
0
        /// <summary>
        /// Liquidate all holdings. Called at the end of day for tick-strategies.
        /// </summary>
        /// <returns>Array of order ids for liquidated symbols</returns>
        public List <int> Liquidate(string symbolToLiquidate = "")
        {
            int        quantity    = 0;
            List <int> orderIdList = new List <int>();

            symbolToLiquidate = symbolToLiquidate.ToUpper();

            foreach (string symbol in Securities.Keys)
            {
                //Send market order to liquidate if 1, we have stock, 2, symbol matches.
                if (Portfolio[symbol].HoldStock && (symbol == symbolToLiquidate || symbolToLiquidate == ""))
                {
                    if (Portfolio[symbol].IsLong)
                    {
                        quantity = -Portfolio[symbol].Quantity;
                    }
                    else
                    {
                        quantity = Math.Abs(Portfolio[symbol].Quantity);
                    }
                    //Liquidate at market price.
                    orderIdList.Add(Transacions.AddOrder(new Order(symbol, quantity, OrderType.Market, Time, Securities[symbol].Price), Portfolio));
                }
            }
            return(orderIdList);
        }
Esempio n. 2
0
        /// <summary>
        /// Submit a new order for quantity of symbol using type order.
        /// </summary>
        /// <param name="type">Buy/Sell Limit or Market Order Type.</param>
        /// <param name="symbol">Symbol of the MarketType Required.</param>
        /// <param name="quantity">Number of shares to request.</param>
        public int Order(string symbol, int quantity, OrderType type = OrderType.Market)
        {
            //Add an order to the transacion manager class:
            int     orderId       = -1;
            decimal price         = 0;
            string  orderRejected = "Order Rejected at " + Time.ToShortDateString() + " " + Time.ToShortTimeString() + ": ";

            //Internals use upper case symbols.
            symbol = symbol.ToUpper();

            //Ordering 0 is useless.
            if (quantity == 0)
            {
                return(orderId);
            }

            if (type != OrderType.Market)
            {
                Debug(orderRejected + "Currently only market orders supported.");
            }

            //If we're not tracking this symbol: throw error:
            if (!Securities.ContainsKey(symbol))
            {
                Debug(orderRejected + "You haven't requested " + symbol + " data. Add this with AddSecurity() in the Initialize() Method.");
            }

            //Set a temporary price for validating order for market orders:
            if (type == OrderType.Market)
            {
                price = Securities[symbol].Price;
            }

            try
            {
                orderId = Transacions.AddOrder(new Order(symbol, quantity, type, Time, price), Portfolio);

                if (orderId < 0)
                {
                    //Order failed validaity checks and was rejected:
                    Debug(orderRejected + OrderErrors.ErrorTypes[orderId]);
                }
            }
            catch (Exception err) {
                Error("Algorithm.Order(): Error sending order. " + err.Message);
            }
            return(orderId);
        }
        /// <summary>
        /// Liquidate all holdings. Called at the end of day for tick-strategies.
        /// </summary>
        /// <returns>Array of order ids for liquidated symbols</returns>
        public List <int> Liquidate(string symbolToLiquidate = "")
        {
            List <int> orderIdList = new List <int>();

            foreach (string symbol in Equities.Keys)
            {
                //Send market order to liquidate if 1, we have stock, 2, symbol matches.
                if (Portfolio[symbol].HoldStock && (symbol == symbolToLiquidate || symbolToLiquidate == ""))
                {
                    int quantity = Portfolio[symbol].Quantity;
                    if (Portfolio[symbol].IsLong)
                    {
                        quantity = -Portfolio[symbol].Quantity;
                    }
                    orderIdList.Add(Transacions.AddOrder(new Order(symbol, quantity, OrderType.Market, Time)));
                }
            }
            return(orderIdList);
        }