Пример #1
0
        private IEnumerable <string> deviations(string code, DateTime date, deviationtype type)
        {
            var ktypes = new[] { "D", "60", "30", "15", "5" };

            foreach (var ktype in ktypes)
            {
                var k = new kdatadb().kdata(code, ktype);
                if (k == null || !k.Any())
                {
                    continue;
                }

                var deviation = (deviation) new DEVIATION(k.close(), type);
                if (deviation != null && deviation.d2.Date == date.Date)
                {
                    yield return(ktype);
                }
            }
        }
Пример #2
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**********");
        }
Пример #3
0
        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);
        }
Пример #4
0
        public static dynamic[] macd60()
        {
            var kas = new db().kanalytics("60")
                      .Where(p => { return(p.macd > 0 && p.dif <= 0.01); })
                      .ToArray();

            if (!kas.Any())
            {
                return(new dynamic[0]);
            }
            kas = new db().kanalytic(kas.Select(p => p.code).Distinct().ToArray(), "15")
                  .Where(p => { return(p.macd > 0); })
                  .ToArray();
            if (!kas.Any())
            {
                return(new dynamic[0]);
            }

            kas = kas
                  .AsParallel()
                  .Where(p =>
            {
                var k = new kdatadb().kdata(p.code, "15");
                if (k == null || !k.Any())
                {
                    return(false);
                }

                var deviation = (deviation) new DEVIATION(k.close(), deviationtype.底背离);
                return(deviation != null && deviation.d2.Date.Date >= p.date.Date);
            })
                  .ToArray();

            if (!kas.Any())
            {
                return(new dynamic[0]);
            }

            var codes  = kas.Select(p => p.code).Distinct().ToArray();
            var basics = new db().basics(codes);
            var ka     = new db().kanalytic(codes, "D");

            var q = (from k in ka
                     join t in basics on k.code equals t.code
                     where t.assettype == assettypes.stock && !t.terminated && !t.suspended && !t.st
                     select new { k, t }
                     )
                    .Select(_ =>
            {
                var k = _.k;
                var t = _.t;

                dynamic d = new ExpandoObject();
                d.date    = k.date;
                d.pe      = t.pe;
                d.code    = t.code;
                d.name    = t.name;
                d.high    = k.high;
                d.low     = k.low;
                d.open    = k.open;
                d.close   = k.close;
                d.volume  = k.volume;
                d.chg     = k.chg;
                d.pb      = t.pb;
                d.pe      = t.pe;
                return(d);
            });

            var r = q
                    .OrderBy(p => p.code)
                    .ThenByDescending(p => p.date)
                    .GroupBy(p => new { p.code })
                    .Select(p => p.First())
                    .ToArray();

            return(r);
        }