public void GetPossibleNotionalByIndicatorTest()
        {
            string strategyName = "unittest_position";
            long maxNotional = 1000;

            StrategyManager.Ins().UnRegister(strategyName);
            AgentManager.Ins().Clear();

            STR_KtbSkel factory = new STR_KtbSkel();
            Input_KtbSkel input = new Input_KtbSkel();
            input.MaxNotional = maxNotional;
            input.Spot10yrCode = "KR1035027161";
            input.Future10yrCode = "167FC000";

            Serializer serializer = new Serializer(typeof(Input_KtbSkel));
            String jsonText = serializer.Serialize(input);

            STR_KtbSkel strategy = StrategyBuilder_KtbSkel.CreateStrategy(jsonText, strategyName);
            PrivateObject po = new PrivateObject(strategy);
            STR_KtbSkel_Accessor acc = new STR_KtbSkel_Accessor(po);
            acc.SetAccountOrderLimit();

            ProcessController_KtbSkel controller = acc.Controller;
            ProcessController_KtbSkel_Accessor accController = new ProcessController_KtbSkel_Accessor(
                new PrivateObject(controller));

            ProcessState_Default ps = accController._processState as ProcessState_Default;
            ProcessState_Default_Accessor psAcc = new ProcessState_Default_Accessor(
                new PrivateObject(ps));

            long notional = psAcc.GetPossibleNotionalByIndicator_Out(10F);
            Assert.AreEqual((-500), notional);

            Assert.AreEqual(false, accController.SortedPositionStack.IsFull(10F));

            notional = psAcc.GetPossibleNotionalByIndicator_Out(6F);
            Assert.AreEqual((-100), notional);
            Assert.AreEqual(false, accController.SortedPositionStack.IsFull(6F));

            notional = psAcc.GetPossibleNotionalByIndicator_Out(5F);
            Assert.AreEqual(0, notional);
            Assert.AreEqual(true, accController.SortedPositionStack.IsFull(5F));

            notional = psAcc.GetPossibleNotionalByIndicator_Out(3.0F);
            Assert.AreEqual(200, notional);
            Assert.AreEqual(true, accController.SortedPositionStack.IsFull(3F));

            notional = psAcc.GetPossibleNotionalByIndicator_Out(0F);
            Assert.AreEqual(500, notional);
            Assert.AreEqual(true, accController.SortedPositionStack.IsFull(0F));

            notional = psAcc.GetPossibleNotionalByIndicator_Out(-1F);
            Assert.AreEqual(500, notional);
            Assert.AreEqual(true, accController.SortedPositionStack.IsFull(-1F));
        }
        public void TestKtbSkelCreateStrategy()
        {
            STR_KtbSkel factory = new STR_KtbSkel();
            Input_KtbSkel input = new Input_KtbSkel();
            input.Spot10yrCode = "KR1035027161";
            input.Future10yrCode = "167FC000";
            Serializer serializer = new Serializer(typeof(Input_KtbSkel));
            String jsonText = serializer.Serialize(input);

            STR_KtbSkel strategy = StrategyBuilder_KtbSkel.CreateStrategy(jsonText, "test ktb skel");
            PrivateObject po = new PrivateObject(strategy);
            STR_KtbSkel_Accessor acc = new STR_KtbSkel_Accessor(po);
            acc.SetAccountOrderLimit();

            TestSTR_KtbSkel_Raw(acc);
            TestController_Raw(acc);
        }
        void TestTakeOverPosition()
        {
            string strategyName = "unittest_position";
            long maxNotional = 100;
            double downBoundary = 0F;
            double upBoundary = 10F;

            StrategyManager.Ins().UnRegister(strategyName);
            AgentManager.Ins().Clear();

            STR_KtbSkel factory = new STR_KtbSkel();
            Input_KtbSkel input = new Input_KtbSkel();
            input.MaxNotional = maxNotional;
            input.Spot10yrCode = "KR1035027161";
            input.Future10yrCode = "167FC000";

            Serializer serializer = new Serializer(typeof(Input_KtbSkel));
            String jsonText = serializer.Serialize(input);

            STR_KtbSkel strategy = StrategyBuilder_KtbSkel.CreateStrategy(jsonText, strategyName);
            PrivateObject po = new PrivateObject(strategy);
            STR_KtbSkel_Accessor strategyAcc = new STR_KtbSkel_Accessor(po);
            strategyAcc.SetAccountOrderLimit();

            ProcessController_KtbSkel controller = strategyAcc.Controller;

            String spotCode = KtbSpotUtil.Ins().KtbSpot_10yr.Code;
            String futureCode = KtbFutureUtil.Ins().KtbFuture_10yr_1.Code;

            SortedPositionStack_KtbSkel target =
                new SortedPositionStack_KtbSkel(controller, strategyName, spotCode, futureCode,
                    maxNotional, downBoundary, upBoundary);

            SortedPositionStack_KtbSkel_Accessor acc =
                new SortedPositionStack_KtbSkel_Accessor(
                    new PrivateObject(target));

            acc._stack.Clear();
            acc.DeleteTable();

            int i = 1;
            Position_KtbSkel pos1 = new Position_KtbSkel(
                spotCode, 50 * i, 8888,
                futureCode, (-50) * i, 107.00);
            ++i;
            Position_KtbSkel pos2 = new Position_KtbSkel(
                spotCode, 50 * i, 8888,
                futureCode, (-50) * i, 107.04);
            ++i;
            Position_KtbSkel pos3 = new Position_KtbSkel(
                spotCode, 50 * i, 8888,
                futureCode, (-50) * i, 107.02);
            ++i;
            Position_KtbSkel pos4 = new Position_KtbSkel(
                spotCode, 50 * i, 8888,
                futureCode, (-50) * i, 107.03);

            acc.Push(pos1);
            acc.ToDB();
            acc._stack.Clear();
            acc.TakeOverPositionFromDB();

            Assert.AreEqual(1, acc._stack.Count);

            acc.DeleteTable();
            acc._stack.Clear();

            acc.Push(pos1);
            acc.Push(pos2);
            acc.Push(pos3);
            acc.Push(pos4);
            acc.ToDB();
            acc._stack.Clear();
            Assert.AreEqual(0, acc._stack.Count);

            acc.TakeOverPositionFromDB();
            Assert.AreEqual(4, acc._stack.Count);
            Assert.AreEqual(acc.Peek().SignedSpotCount, 100);
        }
        void TestInvalidPositionFromDB()
        {
            // 100억이 현재 DB에 있는데 10억이 MaxNotional이다. 에러가 난다.
            string strategyName = "unittest_position";

            StrategyManager.Ins().UnRegister(strategyName);
            AgentManager.Ins().Clear();

            STR_KtbSkel factory = new STR_KtbSkel();
            Input_KtbSkel input = new Input_KtbSkel();
            input.Spot10yrCode = "KR1035027161";
            input.Future10yrCode = "167FC000";

            Serializer serializer = new Serializer(typeof(Input_KtbSkel));
            String jsonText = serializer.Serialize(input);

            STR_KtbSkel strategy = StrategyBuilder_KtbSkel.CreateStrategy(jsonText, strategyName);
            PrivateObject po = new PrivateObject(strategy);
            STR_KtbSkel_Accessor strategyAcc = new STR_KtbSkel_Accessor(po);
            strategyAcc.SetAccountOrderLimit();

            Util.LogOutCriticalError("\t\t# TakeOverPosition 1 permitted.");
            Assert.AreEqual(strategyAcc.Controller.SortedPositionStack.TakeOverPositionFromDB(), false);

            StrategyManager.Ins().UnRegister(strategyName);
            AgentManager.Ins().Clear();
        }
        public void TestGetTargetNotional()
        {
            string strategyName = "unittest_position";
            long maxNotional = 1000;

            StrategyManager.Ins().UnRegister(strategyName);
            AgentManager.Ins().Clear();

            STR_KtbSkel factory = new STR_KtbSkel();
            Input_KtbSkel input = new Input_KtbSkel();
            input.MaxNotional = maxNotional;
            input.Spot10yrCode = "KR1035027161";
            input.Future10yrCode = "167FC000";

            Serializer serializer = new Serializer(typeof(Input_KtbSkel));
            String jsonText = serializer.Serialize(input);

            STR_KtbSkel strategy = StrategyBuilder_KtbSkel.CreateStrategy(jsonText, strategyName);
            PrivateObject po = new PrivateObject(strategy);
            STR_KtbSkel_Accessor acc = new STR_KtbSkel_Accessor(po);
            acc.SetAccountOrderLimit();

            ProcessController_KtbSkel controller = acc.Controller;
            ProcessController_KtbSkel_Accessor accController = new ProcessController_KtbSkel_Accessor(
                new PrivateObject(controller));

            ProcessState_Default ps = accController._processState as ProcessState_Default;
            ProcessState_Default_Accessor psAcc = new ProcessState_Default_Accessor(
                new PrivateObject(ps));

            acc._monitorEnter.ReadyBeforeGetIndicator();
            RawMarketData rmdSpot = acc._monitorEnter.SpotRmdThisTurn;
            RawMarketData rmdFuture = acc._monitorEnter.FutureRmdThisTurn;

            {
                rmdSpot.AskCount1 = 20;
                rmdSpot.BidCount1 = 50;
                rmdFuture.AskCount1 = 30;
                rmdFuture.BidCount1 = 40;

                long notional = psAcc.GetTargetNotional_Enter(0, rmdSpot, rmdFuture);
                Assert.AreEqual(0, notional);

                notional = psAcc.GetTargetNotional_Enter(-1, rmdSpot, rmdFuture);
                Assert.AreEqual(0, notional);

                notional = psAcc.GetTargetNotional_Enter(5.11F, rmdSpot, rmdFuture);
                Assert.AreEqual(10, notional);

                notional = psAcc.GetTargetNotional_Enter(10F, rmdSpot, rmdFuture);
                Assert.AreEqual(20, notional);

                rmdSpot.AskCount1 = 100;
                rmdSpot.BidCount1 = 100;

                notional = psAcc.GetTargetNotional_Enter(10F, rmdSpot, rmdFuture);
                Assert.AreEqual(40, notional);
            }

            {
                rmdSpot.AskCount1 = 10;
                rmdSpot.BidCount1 = 20;
                rmdFuture.AskCount1 = 30;
                rmdFuture.BidCount1 = 40;

                double indicator = 0F;

                long notional = psAcc.GetTargetNotional_Out(indicator, 0, rmdSpot, rmdFuture);
                Assert.AreEqual(0, notional);

                notional = psAcc.GetTargetNotional_Out(indicator, -1, rmdSpot, rmdFuture);
                Assert.AreEqual(0, notional);

                notional = psAcc.GetTargetNotional_Out(indicator, 10, rmdSpot, rmdFuture);
                Assert.AreEqual(10, notional);

                notional = psAcc.GetTargetNotional_Out(indicator, 100, rmdSpot, rmdFuture);
                Assert.AreEqual(20, notional);

                rmdSpot.AskCount1 = 100;
                rmdSpot.BidCount1 = 100;

                notional = psAcc.GetTargetNotional_Out(indicator, 100, rmdSpot, rmdFuture);

                // future 수량이 가장 적다.
                Assert.AreEqual(30, notional);

                indicator = 4.91; // 500억이 차 있으므로 10억이 된다.

                notional = psAcc.GetTargetNotional_Out(indicator, 100, rmdSpot, rmdFuture);

                // indicator 수량이 가장 적다.
                Assert.AreEqual(10, notional);

                indicator = 5F;
                notional = psAcc.GetTargetNotional_Out(indicator, 100, rmdSpot, rmdFuture);

                // indicator 수량이 0
                Assert.AreEqual(0, notional);

                indicator = 5.11;

                notional = psAcc.GetTargetNotional_Out(indicator, 100, rmdSpot, rmdFuture);
                // indicator 수량이 0
                Assert.AreEqual(0, notional);
            }
        }