예제 #1
0
        public void AddTrade_openTwoLongsAndSell_closedOneLongAndSplit()
        {
            var      ins   = instrumBL.GetInstrumByID(1);
            PosTable table = new PosTable(instrumBL);

            DateTime dt1 = new DateTime(2010, 1, 1, 10, 30, 0);
            Position p1  = table.NewPosition(ins.InsID, 10 * ins.LotSize, dt1, 100, PosTypes.Long); // 10

            DateTime dt2 = new DateTime(2011, 1, 1, 10, 30, 0);
            Position p2  = table.NewPosition(ins.InsID, 20 * ins.LotSize, dt2, 120, PosTypes.Long); // 20

            table.Initialize(1, new List <Position>()
            {
                p2, p1
            }, null);                                                   // две позиции, добавляем специально не по порядку дат

            Trade t = new Trade()
            {
                InsID = ins.InsID, OrderID = 2, Time = new DateTime(2012, 1, 1, 10, 30, 35), Price = 110m, LotCount = 25, BuySell = BuySell.Sell
            };                                                                                                                                                          // sell 25

            table.AddTrade(t);

            var positions = table.GetPositions();

            Assert.True(positions.Count() == 3);                                           // стало 3 позиции

            var close_pos_20 = positions.FirstOrDefault(r => r.Count == 20 * ins.LotSize); // одна закрытая на 20

            Assert.Equal(dt2, close_pos_20.OpenTime);
            Assert.Equal(120, close_pos_20.OpenPrice);
            Assert.Equal(PosTypes.Long, close_pos_20.PosType);
            Assert.Equal(t.Time, close_pos_20.CloseTime);
            Assert.Equal(t.Price, close_pos_20.ClosePrice);
            Assert.True(close_pos_20.IsChanged);

            var open_pos_5 = positions.FirstOrDefault(r => r.Count == 5 * ins.LotSize && r.CloseTime == null); // одна открытая на 5

            Assert.Equal(dt1, open_pos_5.OpenTime);
            Assert.Equal(100, open_pos_5.OpenPrice);
            Assert.Equal(PosTypes.Long, open_pos_5.PosType);
            Assert.Null(open_pos_5.CloseTime);
            Assert.Null(open_pos_5.ClosePrice);
            Assert.False(open_pos_5.IsChanged);                                                                 // это новая позиция, она не помечена как измененная

            var close_pos_5 = positions.FirstOrDefault(r => r.Count == 5 * ins.LotSize && r.CloseTime != null); // одна закрытая на 5

            Assert.Equal(dt1, close_pos_5.OpenTime);
            Assert.Equal(100, close_pos_5.OpenPrice);
            Assert.Equal(PosTypes.Long, close_pos_5.PosType);
            Assert.Equal(t.Time, close_pos_5.CloseTime);
            Assert.Equal(t.Price, close_pos_5.ClosePrice);
            Assert.True(close_pos_5.IsChanged);
        }
예제 #2
0
        public void AddTrade_openTwoLongsAndSell_closedTwoLongsAndNewShort()
        {
            var      ins   = instrumBL.GetInstrumByID(1);
            PosTable table = new PosTable(instrumBL);

            DateTime dt1 = new DateTime(2010, 1, 1, 10, 30, 0);
            Position p1  = table.NewPosition(ins.InsID, 10 * ins.LotSize, dt1, 100, PosTypes.Long);
            DateTime dt2 = new DateTime(2011, 1, 1, 10, 30, 0);
            Position p2  = table.NewPosition(ins.InsID, 20 * ins.LotSize, dt2, 120, PosTypes.Long);

            table.Initialize(1, new List <Position>()
            {
                p2, p1
            }, null);                                                   // две позиции, добавляем специально не по порядку дат

            Trade t = new Trade()
            {
                InsID = ins.InsID, OrderID = 2, Time = new DateTime(2012, 1, 1, 10, 30, 35), Price = 110m, LotCount = 35, BuySell = BuySell.Sell
            };                                                                                                                                                          // sell 35

            table.AddTrade(t);

            var positions = table.GetPositions();

            Assert.True(positions.Count() == 3); // стало 3 позиции
            var pos1 = positions.ElementAt(0);
            var pos2 = positions.ElementAt(1);
            var pos3 = positions.ElementAt(2);

            Assert.Equal(dt1, pos1.OpenTime); // первая закрыта (по порядку дат)
            Assert.Equal(100, pos1.OpenPrice);
            Assert.Equal(10 * ins.LotSize, pos1.Count);
            Assert.Equal(PosTypes.Long, pos1.PosType);
            Assert.Equal(t.Time, pos1.CloseTime);
            Assert.Equal(t.Price, pos1.ClosePrice);
            Assert.True(pos1.IsChanged);

            Assert.Equal(dt2, pos2.OpenTime); // вторая закрыта (по порядку дат)
            Assert.Equal(120, pos2.OpenPrice);
            Assert.Equal(20 * ins.LotSize, pos2.Count);
            Assert.Equal(PosTypes.Long, pos2.PosType);
            Assert.Equal(t.Time, pos2.CloseTime);
            Assert.Equal(t.Price, pos2.ClosePrice);
            Assert.True(pos2.IsChanged);

            Assert.Equal(t.Time, pos3.OpenTime); // третья открыта
            Assert.Equal(t.Price, pos3.OpenPrice);
            Assert.Equal(5 * ins.LotSize, pos3.Count);
            Assert.Equal(PosTypes.Short, pos3.PosType);
            Assert.Null(pos3.CloseTime);
            Assert.Null(pos3.ClosePrice);
            Assert.True(pos3.PosID < 0);
        }
예제 #3
0
        public void AddTrade_openShortPosAndBuy_closedShortPos()
        {
            var      ins   = instrumBL.GetInstrumByID(1);
            PosTable table = new PosTable(instrumBL);

            DateTime dt = new DateTime(2010, 1, 1, 10, 30, 0);
            Position p  = table.NewPosition(ins.InsID, 10 * ins.LotSize, dt, 100, PosTypes.Short);

            table.Initialize(1, new List <Position>()
            {
                p
            }, null);

            Assert.False(p.IsChanged); // не изменена

            Trade t = new Trade()
            {
                InsID = ins.InsID, OrderID = 2, Time = new DateTime(2010, 1, 1, 10, 30, 35), Price = 110m, LotCount = 10, BuySell = BuySell.Buy
            };

            table.AddTrade(t);

            var positions = table.GetPositions();

            Assert.Single(positions);
            var pos = positions.ElementAt(0);

            Assert.Equal(dt, pos.OpenTime);
            Assert.Equal(100, pos.OpenPrice);
            Assert.Equal(10 * ins.LotSize, pos.Count);
            Assert.Equal(PosTypes.Short, pos.PosType);
            Assert.Equal(t.Time, pos.CloseTime);
            Assert.Equal(t.Price, pos.ClosePrice);
            Assert.True(pos.IsChanged); // изменена
        }
예제 #4
0
        public void SetCount_pos_change()
        {
            PosTable table = new PosTable(instrumBL);
            var      ins   = instrumBL.GetInstrumByID(1);
            Position p     = table.NewPosition(ins.InsID, 10, DateTime.Today, 100, PosTypes.Long);

            Assert.Equal(10, p.Count);

            Assert.False(p.IsChanged);

            p.SetCount(20);

            Assert.Equal(20, p.Count);
            Assert.True(p.IsChanged);
        }
예제 #5
0
        public void ClosePosition_pos_changedPos()
        {
            PosTable table = new PosTable(instrumBL);
            var      ins   = instrumBL.GetInstrumByID(1);
            Position p     = table.NewPosition(ins.InsID, 10, DateTime.Today, 100, PosTypes.Long);

            Assert.False(p.IsChanged);
            Assert.Null(p.ClosePrice);
            Assert.Null(p.CloseTime);

            DateTime ct = new DateTime(2010, 1, 1, 1, 1, 1);
            decimal  cp = 200m;

            p.ClosePosition(ct, cp);

            Assert.True(p.IsChanged);
            Assert.Equal(ct, p.CloseTime);
            Assert.Equal(cp, p.ClosePrice);
        }
예제 #6
0
        public void AddTrade_openShortPosAndBuy_closedShortAndNewLong()
        {
            var      ins   = instrumBL.GetInstrumByID(1);
            PosTable table = new PosTable(instrumBL);

            DateTime posDt = new DateTime(2010, 1, 1, 10, 30, 0);
            Position p     = table.NewPosition(ins.InsID, 10 * ins.LotSize, posDt, 100, PosTypes.Short);

            table.Initialize(1, new List <Position>()
            {
                p
            }, null);                                              // есть позиция short на 10

            Trade t = new Trade()
            {
                InsID = ins.InsID, OrderID = 2, Time = new DateTime(2010, 1, 1, 10, 30, 35), Price = 110m, LotCount = 12, BuySell = BuySell.Buy
            };                                                                                                                                                         // купили 12

            table.AddTrade(t);

            var positions = table.GetPositions();

            Assert.True(positions.Count() == 2); // стало 2 позиции
            var pos1 = positions.ElementAt(0);
            var pos2 = positions.ElementAt(1);

            Assert.Equal(posDt, pos1.OpenTime); // прежняя позиция полностью закрыта
            Assert.Equal(100, pos1.OpenPrice);
            Assert.Equal(10 * ins.LotSize, pos1.Count);
            Assert.Equal(PosTypes.Short, pos1.PosType);
            Assert.Equal(t.Time, pos1.CloseTime);
            Assert.Equal(t.Price, pos1.ClosePrice);
            Assert.True(pos1.IsChanged);

            Assert.Equal(t.Time, pos2.OpenTime);       // и противоположная новая открыта
            Assert.Equal(110m, pos2.OpenPrice);        // время и цена сделки, а не прежней позиции
            Assert.Equal(2 * ins.LotSize, pos2.Count); // остаток
            Assert.Equal(PosTypes.Long, pos2.PosType); // стала long
            Assert.Null(pos2.CloseTime);               // открыта
            Assert.Null(pos2.ClosePrice);
            Assert.True(pos2.PosID < 0);               // новая
        }
예제 #7
0
        public void AddTrade_openShortPosAndBuy_splitShortPos()
        {
            var      ins   = instrumBL.GetInstrumByID(1);
            PosTable table = new PosTable(instrumBL);

            DateTime posDt = new DateTime(2010, 1, 1, 10, 30, 0);
            Position p     = table.NewPosition(ins.InsID, 10 * ins.LotSize, posDt, 100, PosTypes.Short);

            table.Initialize(1, new List <Position>()
            {
                p
            }, null);                                              // есть позиция short на 10

            Trade t = new Trade()
            {
                InsID = ins.InsID, OrderID = 2, Time = new DateTime(2010, 1, 1, 10, 30, 35), Price = 110m, LotCount = 3, BuySell = BuySell.Buy
            };                                                                                                                                                        // купили всего 3

            table.AddTrade(t);

            var positions = table.GetPositions();

            Assert.True(positions.Count() == 2); // стало 2 позиции
            var pos1 = positions.ElementAt(0);
            var pos2 = positions.ElementAt(1);

            Assert.Equal(posDt, pos1.OpenTime);
            Assert.Equal(100, pos1.OpenPrice);
            Assert.Equal(3 * ins.LotSize, pos1.Count);
            Assert.Equal(PosTypes.Short, pos1.PosType);
            Assert.Equal(t.Time, pos1.CloseTime);
            Assert.Equal(t.Price, pos1.ClosePrice);
            Assert.True(pos1.IsChanged);

            Assert.Equal(posDt, pos2.OpenTime);
            Assert.Equal(100, pos2.OpenPrice);
            Assert.Equal(7 * ins.LotSize, pos2.Count);
            Assert.Equal(PosTypes.Short, pos2.PosType);
            Assert.Null(pos2.CloseTime);
            Assert.Null(pos2.ClosePrice);
            Assert.True(pos2.PosID < 0);
        }