Пример #1
0
        public static bool AddFundPosition(string fundCode, decimal avgCost, decimal share)
        {
            if (string.IsNullOrWhiteSpace(fundCode))
            {
                Log.Warning("Asset.AddFundPosition 指令 fundcode 不能为空");
                return(false);
            }
            if (avgCost <= 0)
            {
                Log.Warning("Asset.AddFundPosition 指令 avgCost 必须为正数");
                return(false);
            }

            var position = AssetDao.GetFundPosition(fundCode);

            if (position == null)
            {
                if (share <= 0)
                {
                    Log.Warning("Asset.AddFundPosition 指令 share 必须为正数");
                    return(false);
                }

                if (AssetDao.AddFundPosition(new Entities.FundPosition
                {
                    FundCode = fundCode,
                    AvgCost = avgCost,
                    Share = share
                }) > 0)
                {
                    Log.Information($"添加持仓成功,fundcode: {fundCode}, avgCost: {avgCost}, share: {share}");
                    return(true);
                }
                else
                {
                    Log.Warning($"添加持仓失败,fundcode: {fundCode}, avgCost: {avgCost}, share: {share}");
                    return(false);
                }
            }
            else
            {
                if (share > 0)
                {
                    var amount = position.AvgCost * position.Share + avgCost * share;
                    position.Share  += share;
                    position.AvgCost = amount / position.Share;
                }
                else
                {
                    if (position.Share + share < 0)
                    {
                        Log.Warning($"添加持仓失败,fundcode: {fundCode}, avgCost: {avgCost}, share: {share}, 减仓份额大于现有份额 {position.Share}");
                        return(false);
                    }
                    var amount = position.AvgCost * position.Share + avgCost * share;
                    position.Share  += share;
                    position.AvgCost = amount / position.Share;
                }
                if (AssetDao.UpdateFundPosition(position) > 0)
                {
                    Log.Information($"添加持仓成功,fundcode: {fundCode}, avgCost: {avgCost}, share: {share}, 现有份额: {position.Share}, 摊薄成本: {position.AvgCost}");
                    return(true);
                }
                else
                {
                    Log.Warning($"添加持仓失败,fundcode: {fundCode}, avgCost: {avgCost}, share: {share}");
                    return(false);
                }
            }
        }