public void test_markExeProcessed()
        {
            IAppStrategyManager stgManager   = getStgManager();
            IAppOrderManager    orderManager = new AppOrderManager(stgManager);
            ExecutionMessage    exeMessage   = getExeMessage_for_Sell3Contract();
            String execId = exeMessage.Execution.ExecId;

            Assert.IsFalse(orderManager.ProcessedExecution.ContainsKey(execId));
            orderManager.markExeProcessed(execId);
            Assert.IsTrue(orderManager.ProcessedExecution.ContainsKey(execId));
        }
        public void test_addAppOrUpdateOrder()
        {
            IAppOrderManager orderManager = new AppOrderManager(getStgManager());
            IAppOrder        appOrder     = createOrder_for_addAppOrUpdateOrder();

            orderManager.addAppOrUpdateOrder(appOrder);
            IAppOrder retAppOrder = orderManager.AppOrderStore[appOrder.OrderId];

            Assert.IsNotNull(retAppOrder);
            Assert.AreEqual(appOrder.OrderId, retAppOrder.OrderId);
        }
        public void test_isExecutionProcessed()
        {
            IAppStrategyManager stgManager   = getStgManager();
            IAppOrderManager    orderManager = new AppOrderManager(stgManager);
            ExecutionMessage    exeMessage   = getExeMessage_for_Sell3Contract();
            String execId = exeMessage.Execution.ExecId;

            Assert.IsFalse(orderManager.isExecutionProcessed(execId));
            orderManager.ProcessedExecution.AddOrUpdate(execId, execId, (key, oldValue) => oldValue);
            Assert.IsTrue(orderManager.isExecutionProcessed(execId));
        }
        public void test_addOpenAppOrder()
        {
            IAppStrategyManager stgManager   = getStgManager();
            IAppOrderManager    orderManager = new AppOrderManager(stgManager);
            IAppOrder           appOrder     = createOrder_for_addAppOrUpdateOrder();
            int stgIndex = 2;

            orderManager.addOpenAppOrder(stgIndex, appOrder);
            String[]         stgNames         = stgManager.getStgNames();
            List <IAppOrder> lstStgOpenOrders = orderManager.StoreStgOpenOrders[stgNames[stgIndex]];
            IAppOrder        retAppOrder      = lstStgOpenOrders[lstStgOpenOrders.Count - 1];

            Assert.IsNotNull(retAppOrder);
            Assert.AreEqual(appOrder.OrderId, retAppOrder.OrderId);
        }
        public void test_updateAppOrderExecution()
        {
            IAppStrategyManager stgManager   = getStgManager();
            IAppOrderManager    orderManager = new AppOrderManager(stgManager);
            IAppOrder           appOrder     = createOrder_Sell3Contract();

            orderManager.AppOrderStore.AddOrUpdate(appOrder.OrderId, appOrder, (key, oldValue) => oldValue);
            ExecutionMessage exeMessage = getExeMessage_for_Sell3Contract();

            orderManager.updateAppOrderExecution(exeMessage);
            IAppOrder retAppOrder = orderManager.AppOrderStore[appOrder.OrderId];

            IAppExecution appExe = retAppOrder.Executions[0];

            Assert.AreEqual(1, retAppOrder.Executions.Count);
            Assert.AreEqual(23000, appExe.AvgPrice);
            Assert.AreEqual("1102", appExe.ExecId);
            Assert.AreEqual(3, appExe.ExeShare);
            Assert.AreEqual("2015-11-11 01:07:01", appExe.LastExecTime);
            Assert.AreEqual(1001, appExe.OrderId);
            Assert.AreEqual(AppConstant.SELL_SIGNAL, appExe.Side);
        }
        public void test_addOrderRecordToRepositry()
        {
            IAppStrategyManager stgManager   = getStgManager();
            IAppOrderManager    orderManager = new AppOrderManager(stgManager);
            IAppOrder           appOrder     = createOrder_Sell3Contract();

            Order       order      = appOrder.IBOrder;
            String      strOrderId = order.OrderId.ToString();
            OrderRecord orderR     = new OrderRecord();

            orderR.orderId   = order.OrderId;
            orderR.sno       = appOrder.StratgeyShortName;
            orderR.orderTime = new DateTime(2015, 12, 11, 03, 03, 10);


            Assert.IsFalse(orderManager.OrderRepositry.ContainsKey(strOrderId));
            orderManager.addOrderRecordToRepositry(strOrderId, orderR);
            Assert.IsTrue(orderManager.OrderRepositry.ContainsKey(strOrderId));
            OrderRecord retOrderR = orderManager.OrderRepositry[strOrderId];

            Assert.AreEqual(order.OrderId, retOrderR.orderId);
            Assert.AreEqual(orderR.orderTime, retOrderR.orderTime);
            Assert.AreEqual(appOrder.StratgeyShortName, retOrderR.sno);
        }
        public void test_updatePosition()
        {
            List <IAppExecution> lstExecution = createExecution_for_updatePosition();
            IAppStrategyManager  stgManager   = getStgManager();
            IAppOrderManager     orderManager = new AppOrderManager(stgManager);
            int stgIndex = 2;
            int numExe   = 5;

            int[]    accBuyQ      = new int[numExe];
            int[]    accSellQ     = new int[numExe];
            int[]    netQ         = new int[numExe];
            double[] accBuyMoney  = new double[numExe];
            double[] accSellMoney = new double[numExe];
            double[] totalPnL     = new double[numExe];

            accBuyQ[0]      = 0;
            accSellQ[0]     = 6;
            netQ[0]         = -6;
            accBuyMoney[0]  = 0;
            accSellMoney[0] = 6 * 22100;
            totalPnL[0]     = 0;

            accBuyQ[1]      = 3;
            accSellQ[1]     = 6;
            netQ[1]         = -3;
            accBuyMoney[1]  = 3 * 22000;
            accSellMoney[1] = 6 * 22100;
            totalPnL[1]     = 0;

            accBuyQ[2]      = 6;
            accSellQ[2]     = accSellQ[1];
            netQ[2]         = 0;
            accBuyMoney[2]  = accBuyMoney[1] + 3 * 21900;
            accSellMoney[2] = accSellMoney[1];
            totalPnL[2]     = 6 * 22100 - (3 * 22000 + 3 * 21900);

            accBuyQ[3]      = 7;
            accSellQ[3]     = accSellQ[2];
            netQ[3]         = 1;
            accBuyMoney[3]  = accBuyMoney[2] + 23000;
            accSellMoney[3] = accSellMoney[2];
            totalPnL[3]     = totalPnL[2];

            accBuyQ[4]      = accBuyQ[3];
            accSellQ[4]     = 7;
            netQ[4]         = 0;
            accBuyMoney[4]  = accBuyMoney[3];
            accSellMoney[4] = accSellMoney[3] + 23500;
            totalPnL[4]     = totalPnL[3] + (23500 - 23000);

            orderManager.updatePosition(stgIndex, lstExecution[0]);
            String[]    stgNames = stgManager.getStgNames();
            String      stgName  = stgNames[stgIndex];
            AppPosition position = orderManager.StoreStgPositions[stgName];

            Assert.AreEqual(accSellQ[0], position.AccSellQ);
            Assert.AreEqual(accSellMoney[0], position.AccSellMoney);
            Assert.AreEqual(netQ[0], position.NetQ);

            orderManager.updatePosition(stgIndex, lstExecution[1]);
            Assert.AreEqual(accBuyQ[1], position.AccBuyQ);
            Assert.AreEqual(accBuyMoney[1], position.AccBuyMoney);
            Assert.AreEqual(netQ[1], position.NetQ);
            Assert.AreEqual(totalPnL[1], position.TotalPnL);

            orderManager.updatePosition(stgIndex, lstExecution[2]);
            Assert.AreEqual(accBuyQ[2], position.AccBuyQ);
            Assert.AreEqual(accBuyMoney[2], position.AccBuyMoney);
            Assert.AreEqual(netQ[2], position.NetQ);
            Assert.AreEqual(totalPnL[2], position.TotalPnL);

            orderManager.updatePosition(stgIndex, lstExecution[3]);
            Assert.AreEqual(accBuyQ[3], position.AccBuyQ);
            Assert.AreEqual(accBuyMoney[3], position.AccBuyMoney);
            Assert.AreEqual(netQ[3], position.NetQ);
            Assert.AreEqual(totalPnL[3], position.TotalPnL);

            orderManager.updatePosition(stgIndex, lstExecution[4]);
            Assert.AreEqual(accSellQ[4], position.AccSellQ);
            Assert.AreEqual(accSellMoney[4], position.AccSellMoney);
            Assert.AreEqual(netQ[4], position.NetQ);
            Assert.AreEqual(totalPnL[4], position.TotalPnL);
        }