Beispiel #1
0
        protected void debugMarketSimulationMaofPlaceOrderCallback(IWrite iWrite, string cmdName, object[] cmdArguments)
        {
            if (marketSimulationMaof == default(MarketSimulationMaof))             // check if there active simulation to get data from
            {
                iWrite.WriteLine("No active market simulations.");
                return;
            }

            int  id;
            bool res = FindSecurity(cmdName, out id);

            if (!res)
            {
                iWrite.WriteLine("Unknown security in the command " + cmdName);
                return;
            }

            // i got security ID
            bool buyOrder  = (cmdArguments[1].ToString().ToUpper().CompareTo("BUY") == 0);
            bool sellOrder = (cmdArguments[1].ToString().ToUpper().CompareTo("SELL") == 0);

            if (!buyOrder && !sellOrder)
            {
                iWrite.WriteLine("Use words buy or sell to specify the order type");
                return;
            }
            if (buyOrder && sellOrder)
            {
                iWrite.WriteLine("Internal error: both buy and sell in " + cmdArguments[1]);
                return;
            }


            // are there three and only three numbers in the command line ?
            const string patternNumbers = ".+[0-9]+.+[0-9]+ +[0-9]+";

            System.Text.RegularExpressions.Regex           regexNumbers = new System.Text.RegularExpressions.Regex(patternNumbers);
            System.Text.RegularExpressions.MatchCollection matches      = regexNumbers.Matches(cmdName);
            if (matches.Count != 1)
            {
                iWrite.WriteLine("Three and only three numbers - security ID, limit price and quantiy are allowed. I got '" + cmdName + "'");
                return;
            }

            // last arguments are price and quantity
            string limitPriceStr = cmdArguments[cmdArguments.Length - 2].ToString();
            string quantintyStr  = cmdArguments[cmdArguments.Length - 1].ToString();
            int    limitPrice    = Int32.Parse(limitPriceStr);
            int    quantity      = Int32.Parse(quantintyStr);

            if (limitPrice == 0)
            {
                iWrite.WriteLine("Failed to parse limit price " + limitPriceStr);
                return;
            }
            if (quantity == 0)
            {
                iWrite.WriteLine("Failed to parse quantinty " + quantintyStr);
                return;
            }

            TransactionType transaction;

            if (buyOrder)
            {
                transaction = TransactionType.BUY;
            }
            else
            {
                transaction = TransactionType.SELL;
            }
            MarketSimulation.ReturnCode        errorCode;
            MarketSimulation.ISystemLimitOrder order = marketSimulationMaof.CreateOrder(id, limitPrice, quantity, transaction, placeOrderCallback);
            res = marketSimulationMaof.PlaceOrder(order, out errorCode);
            if (!res)
            {
                iWrite.WriteLine("Failed to place order error=" + errorCode);
            }
        }