Exemplo n.º 1
0
        /// <summary>
        /// 计算期现正套的保本基差的方法是:
        /// (已点价批次以及期货头寸的总盈亏+未点价采购批次的升贴水×数量-未点价销售批次的升贴水×数量)/现货批次的敞口数量
        /// </summary>
        /// <param name="hg"></param>
        /// <param name="ctx"></param>
        public void Handle(HedgeGroup hg, SenLan2Entities ctx)
        {
            hg.StopLossSpread = null;

            //LME PnL
            var lmeLines =
                ctx.HedgeLineLMEPositions.Include("LMEPosition").Include("LMEPosition.Commodity")
                   .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted)
                   .ToList();
            decimal lmePnL = LMEPositionHelper.CalcPositionPnL(lmeLines)*(hg.Rate ?? 1) -
                             lmeLines.Sum(o => o.AssignedCommission) * (hg.Rate ?? 1);

            //SHFE PnL
            var shfeLines =
                ctx.HedgeLineSHFEPositions.Include("SHFEPosition").Include("SHFEPosition.Commodity")
                   .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted)
                   .ToList();
            decimal shfePnL = SHFEPositionHelper.CalcPositionPnL(shfeLines) - shfeLines.Sum(o => o.AssignedCommission);

            decimal totalPnL = (lmePnL + shfePnL); //用于算基差,所以要加负号

            //Quota PnL
            var quotaLines =
                ctx.HedgeLineQuotas.Include("Quota")
                   .Include("Quota.Contract")
                   .Include("Quota.Pricings")
                   .Include("Quota.Currency")
                   .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted).ToList()
                   .Select(o => o.Quota).ToList();
            var exposure = (decimal)quotaLines.Sum(o => o.Quantity * o.Contract.ContractTypeValue);

            if (exposure != 0)
            {
                foreach (var q in quotaLines)
                {
                    decimal rate = 1;
                    if (q.Currency.Code != "CNY")
                        rate = hg.Rate ?? 1;

                    if (q.PricingStatus == (int)PricingStatus.Complete)
                    {
                        totalPnL += (q.FinalPrice * q.Quantity * q.Contract.ContractTypeValue ?? 0) * rate;
                    }
                    else
                    {
                        var pricings = q.Pricings.Where(o => !o.IsDeleted).ToList();
                        var unpricedQty = (decimal)(q.Quantity - pricings.Sum(o => o.PricingQuantity));
                        totalPnL += (decimal)pricings.Sum(o => o.PricingQuantity * o.FinalPrice) * q.Contract.ContractTypeValue * rate;
                        totalPnL += (unpricedQty * q.Premium * q.Contract.ContractTypeValue * rate) ?? 0;
                    }
                }

                hg.StopLossSpread = totalPnL / exposure;
            }
        }
Exemplo n.º 2
0
        public static IBreakEvenSpreadBaseHandlor GetHandlor(HedgeGroup hg)
        {
            switch (hg.ArbitrageType)
            {
                case (int)ArbitrageType.FPArbitrage:
                    return new FPHandlor();
                
                case (int)ArbitrageType.FPRevArbitrage:
                    return new FPRevHandlor();
                
                case (int)ArbitrageType.CarryArbitrage:
                    return new CarryHandlor();

                case (int)ArbitrageType.CarryRevArbitrage:
                    return new CarryRevHandlor();

                default:
                    return null;
            }
        }
Exemplo n.º 3
0
        public void Handle(HedgeGroup hg, SenLan2Entities ctx)
        {
            hg.StopLossSpread = null;

            var lmeLines =
                ctx.HedgeLineLMEPositions.Include("LMEPosition").Include("LMEPosition.Commodity")
                   .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted)
                   .ToList();

            var shfeLines =
                ctx.HedgeLineSHFEPositions.Include("SHFEPosition").Include("SHFEPosition.Commodity")
                   .Where(o => o.HedgeGroupId == hg.Id && !o.IsDeleted)
                   .ToList();

            if (lmeLines.Count > 0)
            {
                var groups = lmeLines.GroupBy(o => o.LMEPosition.PromptDate);
                decimal exposure = 0;
                foreach (var @group in groups)
                {
                    var list = @group.ToList();
                    var tmp =
                        Math.Round(
                            (decimal)
                            list.Sum(
                                o =>
                                o.AssignedLotAmount*o.LMEPosition.TradeDirectionValue*
                                o.LMEPosition.Commodity.LMEQtyPerHand), RoundRules.QUANTITY, MidpointRounding.AwayFromZero);

                    if (tmp != 0)
                    {
                        exposure = Math.Abs(tmp);
                        break;
                    }
                }

                if (exposure != 0)
                {
                    var pnl =
                        -lmeLines.Sum(o => o.AssignedLotAmount * o.LMEPosition.AgentPrice * o.LMEPosition.TradeDirectionValue * o.LMEPosition.Commodity.LMEQtyPerHand) - lmeLines.Sum(o => o.AssignedCommission);
                    hg.StopLossSpread = pnl / exposure;
                }
            }
            else if (shfeLines.Count > 0)
            {
                var groups = shfeLines.GroupBy(o => o.SHFEPosition.PromptDate);
                decimal exposure = 0;
                foreach (var @group in groups)
                {
                    var list = @group.ToList();
                    var tmp =
                        Math.Round(
                            (decimal)list.Sum(
                                o =>
                                o.AssignedLotAmount * o.SHFEPosition.OpenCloseValue * o.SHFEPosition.Commodity.SHFEQtyPerHand), RoundRules.QUANTITY, MidpointRounding.AwayFromZero);
                    if (tmp != 0)
                    {
                        exposure = Math.Abs(tmp);
                        break;
                    }
                }

                if (exposure != 0)
                {
                    var pnl =
                        -shfeLines.Sum(
                            o =>
                            o.AssignedLotAmount * o.SHFEPosition.Price * o.SHFEPosition.TradeDirectionValue * o.SHFEPosition.Commodity.SHFEQtyPerHand) - shfeLines.Sum(o => o.AssignedCommission);
                    hg.StopLossSpread = pnl / exposure;
                }
            }
        }
Exemplo n.º 4
0
     private void FixupHedgeGroup(HedgeGroup previousValue)
     {
         if (IsDeserializing)
         {
             return;
         }
 
         if (previousValue != null && previousValue.HedgeLineLMEPositions.Contains(this))
         {
             previousValue.HedgeLineLMEPositions.Remove(this);
         }
 
         if (HedgeGroup != null)
         {
             if (!HedgeGroup.HedgeLineLMEPositions.Contains(this))
             {
                 HedgeGroup.HedgeLineLMEPositions.Add(this);
             }
 
             HedgeGroupId = HedgeGroup.Id;
         }
         if (ChangeTracker.ChangeTrackingEnabled)
         {
             if (ChangeTracker.OriginalValues.ContainsKey("HedgeGroup")
                 && (ChangeTracker.OriginalValues["HedgeGroup"] == HedgeGroup))
             {
                 ChangeTracker.OriginalValues.Remove("HedgeGroup");
             }
             else
             {
                 ChangeTracker.RecordOriginalValue("HedgeGroup", previousValue);
             }
             if (HedgeGroup != null && !HedgeGroup.ChangeTracker.ChangeTrackingEnabled)
             {
                 HedgeGroup.StartTracking();
             }
         }
     }
Exemplo n.º 5
0
        /// <summary>
        /// Override the update function in basevm
        /// </summary>
        protected override void Update()
        {
            CheckSaveLogic();
            var hg = new HedgeGroup { Id = ObjectId, Name = HedgeGroupName, HedgeDate = HedgeGroupDate, Rate = Rate, Status = (int)HedgeGroupStatus.NotSettled, PLAmount = null, PLCurrencyId = null, ArbitrageType = SelectedArbitrageTypeId };

            List<int> newLmeIds = NewLMEPositions.Select(o => o.LMEPositionId).ToList();
            var updatedLmes = AddedLMEPositions.Where(o => !newLmeIds.Contains(o.LMEPositionId)).ToList();

            List<int> newShfeIds = NewSHFEPositions.Select(o => o.SHFEPositionId).ToList();
            var updatedShfes = AddedSHFEPositions.Where(o => !newShfeIds.Contains(o.SHFEPositionId)).ToList();

            using (var hgService = SvcClientManager.GetSvcClient<HedgeGroupServiceClient>(SvcType.HedgeGroupSvc))
            {
                hgService.UpdateHedgeGroup(hg, NewQuotas, DeletedQuotas, updatedLmes, NewLMEPositions,
                                           DeletedLMEPositions, updatedShfes, NewSHFEPositions, DeletedSHFEPositions,
                                           CurrentUser.Id);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// Override the create function in basevm
 /// </summary>
 protected override void Create()
 {
     CheckSaveLogic();
     var hg = new HedgeGroup { Name = HedgeGroupName, HedgeDate = HedgeGroupDate, Rate = Rate, Status = (int)HedgeGroupStatus.NotSettled, ArbitrageType = SelectedArbitrageTypeId };
     using (var hgService = SvcClientManager.GetSvcClient<HedgeGroupServiceClient>(SvcType.HedgeGroupSvc))
     {
         hgService.CreateHedgeGroup(hg, AddedQuotas, AddedLMEPositions, AddedSHFEPositions, CurrentUser.Id);
     }
 }