Exemple #1
0
        void AddPosition(UserPosition up, decimal curPrice)
        {
            this.Count += up.Count;

            //总金额加上本次成交的金额
            var thisTotal = up.Order.DonePrice * up.Order.DoneCount;

            this.BuyTotal += thisTotal;
            this.BuyPrice  = this.Count == 0?0: this.BuyTotal / this.Count;
            Calc(curPrice, up.Count);
        }
Exemple #2
0
        void SubPosition(UserPosition up, decimal curPrice)
        {
            try
            {
                this.Count -= up.Count;
                //仓位转换:数量为0清空持仓,数量>0正常计算,小于0转换仓位类型以剩余数量计算
                if (Count == 0)
                {
                    ClearIndex();
                }
                else if (Count < 0)
                {
                    ClearIndex();
                    //只有持仓类型不同才翻转
                    if (up.Order.PositionType != this.ptype)
                    {
                        if (this.ptype == Core.PositionType.义务仓)
                        {
                            this.ptype = Core.PositionType.权利仓;
                        }
                        else
                        {
                            this.ptype = Core.PositionType.义务仓;
                        }
                        this.PositionType = ptype.ToString();
                        this.Count        = this.Count * (-1);

                        this.BuyPrice = up.Order.DonePrice;

                        var thisTotal = up.Order.DonePrice * this.Count;
                        this.BuyTotal = thisTotal;
                        Calc(curPrice, this.Count);
                    }
                }
                else
                if (Count > 0)
                {
                    //如果10买一个,20买一个,再100卖一个,则成本价为负数
                    //var thisTotal = up.Count * up.Order.DonePrice;
                    //this.BuyTotal -= thisTotal;
                    //this.BuyPrice =this.BuyTotal / this.Count;
                    //修改为当量减少时,只减少成本,单价不变
                    this.BuyTotal -= up.Count * this.BuyPrice;

                    Calc(curPrice, up.Count);
                    CalcClosableProfit(up, curPrice);
                }
            }
            catch (Exception ex)
            {
                Singleton <TextLog> .Instance.Error(ex, "subposition");
            }
        }
Exemple #3
0
        /// <summary>
        /// 计算平仓盈亏:计算此持仓的盈亏,然后累加到已有值
        /// </summary>
        /// <param name="up"></param>
        void CalcClosableProfit(UserPosition up, decimal curPrice)
        {
            if (up.Order.PositionType == this.ptype)
            {
                var dp    = this.BuyPrice - curPrice;
                var count = up.Count;

                var dt = dp * count;
                if (this.ptype == Core.PositionType.义务仓)
                {
                    this.CloseProfit += dt;
                }
                else
                {
                    this.CloseProfit += dt * (-1);
                }
            }
        }
Exemple #4
0
        public PositionSummary(UserPosition up, decimal curPrice)
        {
            this.Contract     = up.Order.Contract;
            this.OrderType    = up.Order.OrderType;
            this.CCode        = up.Order.Contract.Code;
            this.CName        = up.Order.Contract.Name;
            this.Coin         = up.Order.Contract.Coin;
            this.PositionType = up.Order.PositionType.ToString();
            this.ptype        = up.Order.PositionType;
            Id            = CCode;
            this.Count    = up.Count;
            this.BuyPrice = up.Order.DonePrice;

            var thisTotal = up.Order.DonePrice * up.Count;

            this.BuyTotal = thisTotal;
            Calc(curPrice, up.Count);
        }
Exemple #5
0
 public void Update(UserPosition up, bool isAdd, decimal curPrice)
 {
     if (this.Count == 0)
     {
         ClearIndex();
         this.ptype        = up.Order.PositionType;
         this.PositionType = ptype.ToString();
         //因为持仓数为0,所以只计算开仓,不再计算平仓
         if (isAdd)
         {
             AddPosition(up, curPrice);
         }
     }
     else
     {
         if (up.Order.PositionType == ptype)
         {
             if (isAdd)
             {
                 AddPosition(up, curPrice);
             }
             else
             {
                 SubPosition(up, curPrice);
             }
         }
         else
         {
             if (isAdd)//只有开仓时才计算:开权利仓=平=义务仓
             {
                 if (!isAdd)
                 {
                     AddPosition(up, curPrice);
                 }
                 else
                 {
                     SubPosition(up, curPrice);
                 }
             }
         }
     }
 }