Exemple #1
0
        public override void exec()
        {
            log.Info("**********START**********");

            var cache  = new Dictionary <string, kdata>();
            var db     = new Db.db();
            var codes  = db.codes();
            var i      = 0;
            var count  = codes.Count();
            var ktypes = new[] { "5", "15", "30", "60", "D", "W", "M" };

            foreach (var code in codes.AsParallel())
            {
                Interlocked.Increment(ref i);
                log.InfoFormat("{0}/{1} calc {2}", i, count, code);

                foreach (var ktype in ktypes)
                {
                    try
                    {
                        var kdata = new kdatadb().kdata(code, ktype);
                        if (kdata != null && kdata.Any())
                        {
                            var date  = kdata.Last().date;
                            var close = kdata.close();

                            var macd    = new MACD(close);
                            var macdvol = new MACD(kdata.volume(100));
                            var ma5     = new MA(close, 5);
                            var ma10    = new MA(close, 10);
                            var ma20    = new MA(close, 20);
                            var ma30    = new MA(close, 30);
                            var ma60    = new MA(close, 60);
                            var ma120   = new MA(close, 120);
                            var chg     = new CHG(close);

                            var ka = new kanalytic()
                            {
                                code    = code,
                                date    = date,
                                ktype   = ktype,
                                close   = kdata.Last().close.ZeroNaN(),
                                open    = kdata.Last().open.ZeroNaN(),
                                high    = kdata.Last().high.ZeroNaN(),
                                low     = kdata.Last().low.ZeroNaN(),
                                volume  = kdata.Last().volume.ZeroNaN(),
                                chg     = chg.Last().Value.ZeroNaN(),
                                ma5     = ma5.Last().Value.ZeroNaN(),
                                ma10    = ma10.Last().Value.ZeroNaN(),
                                ma20    = ma20.Last().Value.ZeroNaN(),
                                ma30    = ma30.Last().Value.ZeroNaN(),
                                ma60    = ma60.Last().Value.ZeroNaN(),
                                ma120   = ma120.Last().Value.ZeroNaN(),
                                dea     = macd.Last().DEA.ZeroNaN(),
                                macd    = macd.Last().MACD.ZeroNaN(),
                                dif     = macd.Last().DIF.ZeroNaN(),
                                deavol  = macdvol.Last().DEA.ZeroNaN(),
                                macdvol = macdvol.Last().MACD.ZeroNaN(),
                                difvol  = macdvol.Last().DIF.ZeroNaN()
                            };

                            db.save(new[] { ka });
                        }
                    }
                    catch (Exception e)
                    {
                        log.Warn("ex @ calc " + code + " for " + ktype, e);
                    }
                }
            }

            log.Info("**********DONE**********");
        }
        public analytic Get(string id, string ktype)
        {
            var result = new analytic();

            var k     = new kdatadb().kdata(id, ktype);
            var basic = new db().basics(id);

            result.istock  = basic.assettype == assettypes.stock;
            result.sectors = basic.getsectors();
            result.indexes = new db().basicnames(basic.getindexes()).ToArray();
            result.stocks  = basic.assettype == assettypes.sector
                            ? new db().basicnamesinsector(id).ToArray() : new basicname[0];
            result.code = basic.code;
            result.name = basic.name;
            result.PE   = basic.pe;

            if (k != null && k.Any())
            {
                var cur  = k.Last();
                var prev = k.Count > 1 ? k[k.Count - 2] : null;

                result.date   = cur.date.ToString("yyyy-MM-dd");
                result.high   = cur.high;
                result.low    = cur.close;
                result.open   = cur.open;
                result.close  = cur.close;
                result.change = prev == null ? (double?)null : ((cur.close - prev.close) / prev.close) * 100;

                var q = (quotation) new QUOTATION(k);
                if (q != null)
                {
                    result.state    = q.state.ToString();
                    result.position = q.position;
                    result.strategy = q.strategy;
                }

                var devs_up = deviations(id, cur.date.Date, deviationtype.底背离);
                if (devs_up.Any())
                {
                    result.deviation_up = string.Join(",", devs_up);
                }
                var devs_down = deviations(id, cur.date.Date, deviationtype.顶背离);
                if (devs_down.Any())
                {
                    result.deviation_down = string.Join(",", devs_down);
                }

                var ma    = new List <string>();
                var close = k.close();
                var ma5   = (double?)new MA(close, 5);
                var ma30  = (double?)new MA(close, 30);
                var ma55  = (double?)new MA(close, 55);
                var ma120 = (double?)new MA(close, 120);
                if (ma5 >= ma30 && ma30 >= ma55 && ma55 >= ma120)
                {
                    ma.Add("多头");
                }
                if (cur.close < ma5)
                {
                    ma.Add("↓↓5日线");
                }
                //if (cur.close < ma30)
                //    ma.Add("破30日生命线");
                //if (cur.close < ma55)
                //    ma.Add("破55日生命线");
                if (cur.close < ma120)
                {
                    ma.Add("↓↓半年线");
                }
                else
                {
                    ma.Add("半年线↑↑");
                }
                if (ma.Any())
                {
                    result.ma = string.Join(",", ma);
                }

                var cross = new MACD(k.volume()).cross();
                if (cross.Any())
                {
                    if (cross.Last().type == Interface.Data.crosstype.gold)
                    {
                        result.buyorsell = cross.Last().value.Date.Date == DateTime.Today ? "买入" : "持有";
                    }
                    else if (cross.Last().type == Interface.Data.crosstype.dead)
                    {
                        result.buyorsell = "卖出";
                    }
                }
            }

            var mainindex = basic.mainindex();

            if (!string.IsNullOrEmpty(mainindex))
            {
                var dindex = new kdatadb().kdata(mainindex, ktype);
                result.beta = new BETA(
                    new kdata(id, k.Where(p => p.date >= Trade.Cfg.Configuration.data.bearcrossbull).ToArray()),
                    new kdata(id, dindex.Where(p => p.date >= Trade.Cfg.Configuration.data.bearcrossbull).ToArray())).beta;
            }

            return(result);
        }