public async Task <ManagerResult <EquityGroup> > Save(EquityGroup equityGroup)
        {
            ManagerResult <EquityGroup> mgrResult = new ManagerResult <EquityGroup>();

            try
            {
                if (equityGroup.Id == Guid.Empty)
                {
                    using NpgsqlConnection db = new NpgsqlConnection(connString);
                    {
                        await db.InsertAsync(equityGroup);
                    }
                }
                else
                {
                    using NpgsqlConnection db = new NpgsqlConnection(connString);
                    {
                        await db.UpdateAsync(equityGroup);
                    }
                }
                mgrResult.Entity = equityGroup;
            }
            catch (Exception ex)
            {
                mgrResult.Exception = ex;
            }

            return(mgrResult);
        }
        public static async Task<string[]> GetEquityGroupAsync(EquityGroup group)
        {
            if (group == EquityGroup.SP500)
            {
                HttpClient hc = new HttpClient();
                HttpResponseMessage hrm = await hc.GetAsync("https://en.wikipedia.org/wiki/List_of_S%26P_500_companies");
                string content = await hrm.Content.ReadAsStringAsync();

                int loc1 = 0;
                int loc2 = 0;
                List<string> Splitter = new List<string>();

                loc1 = content.IndexOf("<table class=");
                loc1 = content.IndexOf("<tbody>", loc1 + 1);
                loc2 = content.IndexOf("</tbody>", loc1 + 1);
                string tablecontent = content.Substring(loc1, loc2 - loc1 - 1);

                //Split into rows
                Splitter.Clear();
                Splitter.Add("<tr>");
                string[] rows = tablecontent.Split(Splitter.ToArray(), StringSplitOptions.None);
                Splitter.Clear();

                //Extract the symbol from each row
                List<string> StockSymbols = new List<string>();
                int t = 0;
                for (t = 2; t < rows.Length; t++)
                {
                    Splitter.Add("<td>");
                    string[] cols = rows[t].Split(Splitter.ToArray(), StringSplitOptions.None);
                    loc1 = cols[1].IndexOf(">");
                    loc2 = cols[1].IndexOf("<", loc1 + 1);
                    string symbol = cols[1].Substring(loc1 + 1, loc2 - loc1 - 1);
                    StockSymbols.Add(symbol);
                }
                return StockSymbols.ToArray();
            }
            else
            {
                throw new Exception("Method for collecting stock grouping '" + group.ToString() + "' does not exist.");
            }
        }
        public async Task <ManagerResult <bool> > RemoveEquity(EquityGroup sysmbolGroup, Guid equitylId)
        {
            ManagerResult <bool> mgrResult = new ManagerResult <bool>();

            try
            {
                var equityGroupItem = new EquityGroupItem();
                equityGroupItem.GroupId  = sysmbolGroup.Id;
                equityGroupItem.EquityId = equitylId;

                using NpgsqlConnection db = new NpgsqlConnection(connString);
                {
                    mgrResult.Entity = await db.DeleteAsync(equityGroupItem);
                }
            }
            catch (Exception ex)
            {
                mgrResult.Exception = ex;
            }

            return(mgrResult);
        }
        public async Task <ManagerResult <EquityGroupItem> > AddEquity(EquityGroup equityGroup, Guid equityId)
        {
            ManagerResult <EquityGroupItem> mgrResult = new ManagerResult <EquityGroupItem>();
            EquityGroupItem equityGroupItem           = new EquityGroupItem();

            try
            {
                equityGroupItem.GroupId  = equityGroup.Id;
                equityGroupItem.EquityId = equityId;

                using (NpgsqlConnection db = new NpgsqlConnection(connString))
                {
                    await db.InsertAsync(equityGroupItem);
                }

                mgrResult.Entity = equityGroupItem;
            }
            catch (Exception ex)
            {
                mgrResult.Exception = ex;
            }

            return(mgrResult);
        }
示例#5
0
        public List <EquityAttibution> Attribute()
        {
            if (this.Benchmark == null || this.Benchmark.Components == null || this.Benchmark.Components.Count == 0)
            {
                MessageManager.GetInstance().AddMessage(MessageType.Information, "未设置比较基准", "");
                return(null);
            }

            ASecurityGroup benchmarkcomponents = this.Benchmark.GetLatestComponents();

            if (benchmarkcomponents.SecurityList == null || benchmarkcomponents.SecurityList.Count == 0 || benchmarkcomponents.SecurityList[0].Type != SecurityType.Equity)
            {
                throw new Exception("比较基准设置错误,仅股票型可用");
            }

            List <EquityGroup> bmindustrylist = ((EquityGroup)benchmarkcomponents).IndustryList;
            EquityGroup        portfolio      = (EquityGroup)this.PortfGroup.GetLatestPortfolio().EquityHoldings;
            List <EquityGroup> pindustrylist  = portfolio.IndustryList;

            //基准总回报:
            //  1) benchmarkcomponents.Position.AccumulatedYieldIndex - 1;  //指数成份的区间加权平均收益率,这个不准确,因为一个月内成分权重会随着涨跌变化
            //  2) this.Benchmark.Position.CurrentYield;                    //指数的区间收益率,这个准确
            double totalbmreturn = this.Benchmark.Position.CurrentYield;

            this.EquityAttributions.Clear();
            foreach (EquityGroup bmindustry in bmindustrylist)
            {
                EquityGroup pindustry = pindustrylist.Find(delegate(EquityGroup eg) { return(eg.Name == bmindustry.Name); });
                if (pindustry == null)
                {
                    pindustry      = new EquityGroup();
                    pindustry.Name = bmindustry.Name;
                }
                //•行业配置回报 = (组合权重 - 基准权重) * (基准回报 - 基准总回报),反应投资组合的行业配置效应;
                //•行业内选股回报 = 基准权重 * (组合回报 - 基准回报),反应投资组合行业内的选股效应;
                //•交叉回报 = (组合权重 - 基准权重) * (组合回报 - 基准回报),反应行业配置和行业内选股的联合效应;
                //•增加值 = 组合权重 * 组合回报 - 基准权重 * 基准回报,反应主动性管理给投资组合带来的超额收益效应;
                EquityAttibution ea = new EquityAttibution();
                ea.IndustryName    = bmindustry.Name;
                ea.MarketValue     = pindustry.Position.MarketValue;
                ea.PortfolioWeight = pindustry.Position.MarketValuePct;
                ea.BenchmarkWeight = bmindustry.Position.MarketValuePct;
                ea.PortfolioReturn = pindustry.Position.AccumulatedYieldIndex - 1;
                ea.BenchmarkReturn = bmindustry.Position.AccumulatedYieldIndex - 1;


                ea.IndustryAttribution = (ea.PortfolioWeight - ea.BenchmarkWeight) * (ea.BenchmarkReturn - totalbmreturn);
                ea.SecurityAttribution = ea.BenchmarkWeight * (ea.PortfolioReturn - ea.BenchmarkReturn);
                ea.CrossAttribution    = (ea.PortfolioWeight - ea.BenchmarkWeight) * (ea.PortfolioReturn - ea.BenchmarkReturn);
                ea.ValueAdded          = ea.PortfolioWeight * ea.PortfolioReturn - ea.BenchmarkWeight * ea.BenchmarkReturn;

                this.EquityAttributions.Add(ea);
            }

            //按市值排序
            this.EquityAttributions.Sort();

            //整个股票组合
            EquityAttibution equityattr = new EquityAttibution();

            equityattr.IndustryName    = "股票组合";
            equityattr.MarketValue     = portfolio.Position.MarketValue;
            equityattr.PortfolioWeight = portfolio.Position.MarketValuePct;
            equityattr.PortfolioReturn = portfolio.Position.AccumulatedYieldIndex - 1;
            equityattr.BenchmarkReturn = totalbmreturn;
            equityattr.ValueAdded      = equityattr.PortfolioReturn - equityattr.BenchmarkReturn;
            this.EquityAttributions.Add(equityattr);

            return(this.EquityAttributions);
        }