Esempio n. 1
0
        public void TestAccountTradeSendOrder()
        {
            List <AccountFeeInfo> feeInfo = new List <AccountFeeInfo>();

            feeInfo.Add(new AccountFeeInfo("m1705", 10, 10, 3, CommissionChargeCalcType.Fix));
            AccountFee   fee          = new AccountFee(feeInfo);
            AccountTrade accountTrade = new AccountTrade(100000, fee);

            MockPriceGetter priceGetter = new MockPriceGetter();
            string          code        = "m1705";
            OrderInfo       order       = new OrderInfo(code, 20170601.093101, OpenCloseType.Open, 3110, 15, OrderSide.Buy);
            OrderInfo       order2      = new OrderInfo(code, 20170601.093201, OpenCloseType.Open, 3120, 15, OrderSide.Buy);

            priceGetter.ChangePattern(0);
            accountTrade.SendOrder(order);
            accountTrade.SendOrder(order2);
            accountTrade.ChangeTime(20170601.093300, priceGetter);
            //accountTrade.ChangeTime(20170103.093001,)
            Console.WriteLine(accountTrade);

            priceGetter.ChangePattern(1);
            OrderInfo order_Sell  = new OrderInfo(code, 20170601.093501, OpenCloseType.Close, 3110, 5, OrderSide.Buy);
            OrderInfo order2_Sell = new OrderInfo(code, 20170601.093505, OpenCloseType.Close, 3120, 10, OrderSide.Buy);

            accountTrade.SendOrder(order_Sell);
            accountTrade.SendOrder(order2_Sell);
            accountTrade.ChangeTime(20170601.093300, priceGetter);

            Assert.AreEqual(25, accountTrade.AccountPosition.GetAllPositions()[0].Position);
            Console.WriteLine(accountTrade);
        }
Esempio n. 2
0
        public void TestSendPosition()
        {
            AccountPosition accountPosition = new AccountPosition(100000);

            Assert.AreEqual(100000, accountPosition.Money);
            Trade(accountPosition);
            Assert.AreEqual(100000, accountPosition.Money);

            List <AccountFeeInfo> feeInfo = new List <AccountFeeInfo>();

            feeInfo.Add(new AccountFeeInfo("m1705", 10, 10, 3, CommissionChargeCalcType.Fix));
            AccountFee fee = new AccountFee(feeInfo);

            accountPosition = new AccountPosition(100000, fee);
            Trade(accountPosition);
            Assert.AreEqual(99910, accountPosition.Money);
        }
Esempio n. 3
0
        private void ClosePosition(OrderInfo order, int mount)
        {
            string code = order.Instrumentid;

            if (order.Direction == OrderSide.Buy)
            {
                if (position_Code_Long.ContainsKey(code))
                {
                    PositionInfo currentPosition = position_Code_Long[code];
                    int          totalPosition   = currentPosition.Position - mount;
                    if (totalPosition == 0)
                    {
                        RemovePosition(currentPosition);
                    }
                    else
                    {
                        double avgPrice = (currentPosition.Position * currentPosition.PositionCost - order.Price * mount) / totalPosition;
                        currentPosition.Position     = totalPosition;
                        currentPosition.PositionCost = avgPrice;
                    }
                }
            }
            else
            {
                if (position_Code_Short.ContainsKey(code))
                {
                    PositionInfo currentPosition = position_Code_Short[code];
                    int          totalPosition   = currentPosition.Position - mount;
                    if (totalPosition == 0)
                    {
                        RemovePosition(currentPosition);
                    }
                    else
                    {
                        double avgPrice = (currentPosition.Position * currentPosition.PositionCost - order.Price * mount) / totalPosition;
                        currentPosition.Position     = totalPosition;
                        currentPosition.PositionCost = avgPrice;
                    }
                }
            }
            AccountFeeInfo feeInfo = accountFee.GetAccountFee(code);

            this.money += AccountFee.CalcMoney_Close(order, feeInfo, mount);
        }
Esempio n. 4
0
        private void OpenPosition(OrderInfo order, int mount)
        {
            string code = order.Instrumentid;

            if (order.Direction == OrderSide.Buy)
            {
                if (position_Code_Long.ContainsKey(code))
                {
                    PositionInfo currentPosition = position_Code_Long[code];
                    int          totalPosition   = currentPosition.Position + mount;
                    double       avgPrice        = (currentPosition.Position * currentPosition.PositionCost + order.Price * mount) / totalPosition;
                    currentPosition.Position     = totalPosition;
                    currentPosition.PositionCost = avgPrice;
                }
                else
                {
                    PositionInfo position = new PositionInfo(code, PositionSide.Long, mount, order.Price);
                    AddPosition(position);
                }
            }
            else
            {
                if (position_Code_Short.ContainsKey(code))
                {
                    PositionInfo currentPosition = position_Code_Short[code];
                    int          totalPosition   = currentPosition.Position + mount;
                    double       avgPrice        = (currentPosition.Position * currentPosition.PositionCost + order.Price * mount) / totalPosition;
                    currentPosition.Position     = totalPosition;
                    currentPosition.PositionCost = avgPrice;
                }
                else
                {
                    PositionInfo position = new PositionInfo(code, PositionSide.Short, mount, order.Price);
                    AddPosition(position);
                }
            }
            AccountFeeInfo feeInfo = accountFee.GetAccountFee(code);

            this.money -= AccountFee.CalcMoney_Open(order, feeInfo, mount);
        }
Esempio n. 5
0
 public AccountTrade(double money, AccountFee accountFee)
 {
     this.accountOrder    = new AccountTodayOrder();
     this.accountPosition = new AccountPosition(money, accountFee);
 }
Esempio n. 6
0
 public AccountPosition(double money, AccountFee fee)
 {
     this.money      = money;
     this.accountFee = fee;
 }
Esempio n. 7
0
 public AccountPosition(double money)
 {
     this.money      = money;
     this.accountFee = new AccountFee();
 }