Beispiel #1
0
 public void Calculate(HedgeGroupDetailVM vm)
 {
     if (CheckForSettlement(vm))
     {
         //CalculatePL(vm);
         CalculateTotalFixedPL(vm);
     }
 }
Beispiel #2
0
 private void GetRate(HedgeGroupDetailVM vm)
 {
     if (vm.Rate == null || vm.Rate == 0)
     {
         using (var rateService = SvcClientManager.GetSvcClient<RateServiceClient>(SvcType.RateSvc))
         {
             _rate = rateService.GetExchangeRateByCode("USD", "CNY") ?? 0;
         }
     }
     else
     {
         _rate = (decimal)vm.Rate;
     }
 }
Beispiel #3
0
        private void CalculateTotalFixedPL(HedgeGroupDetailVM vm)
        {
            GetRate(vm);
            var quotaPL = CalculatePhysicFixedPL(vm);
            var lmePL = CalculateLMEFixedPL(vm);
            var shfePL = CalculateSHFEFixedPL(vm);

            decimal totalPL = quotaPL + lmePL * _rate + shfePL;
            vm.TotalPLStr = totalPL.ToString("N2") + " CNY";

            if (_cnyCurrency == null)
            {
                using (var currencyService = SvcClientManager.GetSvcClient<CurrencyServiceClient>(SvcType.CurrencySvc))
                {
                    _cnyCurrency = currencyService.GetCurrencyByCode("CNY");
                }
            }

            using (var hgService = SvcClientManager.GetSvcClient<HedgeGroupServiceClient>(SvcType.HedgeGroupSvc))
            {
                var hg = hgService.GetById(vm.ObjectId);
                hg.PLAmount = totalPL;
                hg.PLCurrencyId = _cnyCurrency.Id;
                hg.PhyFixedPL = quotaPL;
                hg.SHFEFixedPL = shfePL;
                hg.LMEFixedPL = lmePL;
                hg.Status = (int)HedgeGroupStatus.Settled;
                hgService.UpdatehedgeGroupPL(hg, vm.CurrentUser.Id);
            }
        }
Beispiel #4
0
        private decimal CalculateLMEFixedPL(HedgeGroupDetailVM vm)
        {
            ////∑(空头数量 * 价格 ) - ∑(多头数量 * 价格 )
            decimal lmePL = (decimal)vm.AddedLMEPositions.Sum(
                o =>
                o.AssignedLotAmount * o.LMEPosition.TradeDirectionValue * o.LMEPosition.AgentPrice *
                o.LMEPosition.Commodity.LMEQtyPerHand) * -1 -
                            vm.AddedLMEPositions.Sum(o => o.AssignedCommission);
            vm.LMEPLStr = lmePL.ToString("N2") + " " + vm.LMEPLCurrency;

            return lmePL;
        }
Beispiel #5
0
        private decimal CalculatePhysicFixedPL(HedgeGroupDetailVM vm)
        {
            //现货锁定盈亏 = ∑(销售合同实际数量 * 点价价格 ) - ∑(采购合同实际数量 * 点价价格 )
            decimal pl = 0;

            foreach (var q in vm.AddedQuotas)
            {
                decimal averagePrice = GetAveragePrice(q.Quota, vm.UserId);
                decimal sum = q.Quota.VerifiedQuantity * averagePrice;

                if (q.Quota.Contract.ContractType == (int)ContractType.Purchase)
                {
                    pl -= sum;
                }
                else
                {
                    pl += sum;
                }
            }
            vm.QuotaFixedPL = pl;
            vm.QuotaFixedPLStr = pl.ToString("N2") + " CNY";

            return pl;
        }
Beispiel #6
0
        private bool CheckForSettlement(HedgeGroupDetailVM vm)
        {
            //Check if it is ready to calculate PL

            #region 原始代码


            //if (vm.AddedQuotas.Any(o => o.Quota.PricingStatus != (int)PricingStatus.Complete))
            //{
            //    throw new Exception(ResHedgeGroup.NotAllPriced);
            //}

            //if (vm.AddedQuotas.Any(o => !o.Quota.DeliveryStatus))
            //{
            //    throw new Exception(ResHedgeGroup.NotAllDeliveryComplete);
            //}

            //var quotaGroups = vm.AddedQuotas.GroupBy(o => o.Quota.CommodityId);
            //foreach (var quotaGroup in quotaGroups)
            //{
            //    var quotaGroupList = quotaGroup.ToList();
            //    var comSum = (decimal)quotaGroupList.Sum(o => o.Quota.Contract.ContractTypeValue * o.Quota.Quantity);
            //    if (comSum != 0)
            //    {
            //        throw new Exception(ResHedgeGroup.QuotaNotEven);
            //    }
            //}

            //var lmeGroupByComms = vm.AddedLMEPositions.GroupBy(o => o.LMEPosition.CommodityId);
            //foreach (var lmeGroupByComm in lmeGroupByComms)
            //{
            //    var lmeListByComm = lmeGroupByComm.ToList();
            //    var lmeGroupByPrompts = lmeListByComm.GroupBy(o => o.LMEPosition.PromptDate);
            //    foreach (var lmeGroupByPrompt in lmeGroupByPrompts)
            //    {
            //        var lmeListByPrompt = lmeGroupByPrompt.ToList();
            //        if (Math.Round(lmeListByPrompt.Sum(o => o.AssignedLotAmount * o.LMEPosition.TradeDirectionValue), 2) != 0)
            //        {
            //            throw new Exception(ResHedgeGroup.LMENotEven);
            //        }
            //    }
            //}

            //var shfeGroupByComms = vm.AddedSHFEPositions.GroupBy(o => o.SHFEPosition.CommodityId);
            //foreach (var shfeGroupByComm in shfeGroupByComms)
            //{
            //    var shfeListByComm = shfeGroupByComm.ToList();
            //    var shfeGroupBySHFEs = shfeListByComm.GroupBy(o => o.SHFEPosition.SHFEId);
            //    foreach (var shfeGroupBySHFE in shfeGroupBySHFEs)
            //    {
            //        var shfeListBySHFE = shfeGroupBySHFE.ToList();
            //        var openLongSum =
            //            shfeListBySHFE.Where(
            //                o =>
            //                o.SHFEPosition.PositionDirection == (int)PositionDirection.Long &&
            //                o.SHFEPosition.OpenClose == (int)PositionOpenClose.Open).Sum(o => o.AssignedLotAmount);
            //        var closeLongSum =
            //            shfeListBySHFE.Where(
            //                o =>
            //                o.SHFEPosition.PositionDirection == (int)PositionDirection.Long &&
            //                o.SHFEPosition.OpenClose == (int)PositionOpenClose.Close).Sum(o => o.AssignedLotAmount);
            //        var openShortSum =
            //            shfeListBySHFE.Where(
            //                o =>
            //                o.SHFEPosition.PositionDirection == (int)PositionDirection.Short &&
            //                o.SHFEPosition.OpenClose == (int)PositionOpenClose.Open).Sum(o => o.AssignedLotAmount);
            //        var closeShortSum =
            //            shfeListBySHFE.Where(
            //                o =>
            //                o.SHFEPosition.PositionDirection == (int)PositionDirection.Short &&
            //                o.SHFEPosition.OpenClose == (int)PositionOpenClose.Close).Sum(o => o.AssignedLotAmount);
            //        if (Math.Round(openLongSum - closeShortSum, 2) != 0 || Math.Round(openShortSum - closeLongSum) != 0)
            //        {
            //            throw new Exception(ResHedgeGroup.SHFENotEven);
            //        }
            //    }
            //}

            #endregion

            switch (vm.SelectedArbitrageTypeId)
            {
                case (int)ArbitrageType.Common:
                    //普通
                    CheckCommon(vm.AddedQuotas,vm.AddedSHFEPositions,vm.AddedLMEPositions);
                    break;
                case (int)ArbitrageType.FPArbitrage:
                //期现正套
                case (int)ArbitrageType.FPRevArbitrage:
                    //期现反套
                    CheckFPArbitrage(vm.AddedQuotas, vm.AddedSHFEPositions, vm.AddedLMEPositions);
                    break;
                case (int)ArbitrageType.CarryArbitrage:
                //跨期正套
                case (int)ArbitrageType.CarryRevArbitrage:
                    //跨期反套
                    CheckCarryArbitrage(vm.AddedQuotas, vm.AddedSHFEPositions, vm.AddedLMEPositions);
                    break;
                case (int)ArbitrageType.MarketArbitrage:
                //跨市正套
                case (int)ArbitrageType.MarketRevArbitrage:
                    //跨市反套
                    CheckMarketArbitrage(vm.AddedQuotas, vm.AddedSHFEPositions, vm.AddedLMEPositions);
                    break;
            }

            return true;
        }