Exemple #1
0
        public override universe Pass(IEnumerable <string> stocks)
        {
            var client = new kdatadb();

            log.Info("query k60");
            var k60 = stocks
                      .AsParallel()
                      .Select(code => client.kdata(code, "60"))
                      .Where(p => p != null && p.Any())
                      .ToArray();

            log.InfoFormat("k60 total {0}", k60.Count());

            var codes = k60
                        .Where(p =>
            {
                var macd = (macd) new MACD(p.close());
                return(macd != null &&
                       macd.MACD > 0 && macd.DIF <= 0.01 &&
                       macd.Date.Date == DateTime.Today);
            })
                        .Select(p => p.Code)
                        .Distinct()
                        .ToArray();

            if (codes.Any())
            {
                log.Info("query k15");
                var k15 = codes
                          .AsParallel()
                          .Select(code => client.kdata(code, "15"))
                          .Where(p => p != null && p.Any())
                          .ToArray();
                log.InfoFormat("k15 total {0}", k15.Count());
                codes = k15
                        .Where(p =>
                {
                    var close     = p.close();
                    var macd      = (macd) new MACD(close);
                    var deviation = (deviation) new DEVIATION(close, deviationtype.底背离);
                    return(macd != null &&
                           macd.MACD > 0 &&
                           macd.Date.Date == DateTime.Today &&
                           deviation != null &&
                           deviation.d2.Date == DateTime.Today);
                })
                        .Select(p => p.Code)
                        .Distinct()
                        .ToArray();
            }

            log.InfoFormat("selected {0}", codes.Count());

            return(new universe("macd60", codes));
        }
Exemple #2
0
        public override universe Pass(IEnumerable <string> stocks)
        {
            var client = new kdatadb();

            log.Info("query market data");
            var data = stocks
                       .AsParallel()
                       .Select(code => client.kdata(code, "D"))
                       .Where(p => p != null)
                       .ToArray();

            log.InfoFormat("total {0}", data.Count());

            log.Info("query fundamentals");
            var basics = new db().basics(data.Select(p => p.Code).Distinct().ToArray());

            var stat = data
                       .Select(series => new factorset(series.Code)
            {
                收阳百分比  = new close_up_percent(series, TimeSpan.FromDays(180)).value,
                均线多头   = new jun_xian_dou_tout(series).value,
                低点反弹高度 = new low_to_historical_lowest(series, new DateTime(2015, 5, 1)).value
            })
                       .ToArray();

            if (junxianduotou)
            {
                stat = stat.Where(p => p.均线多头).ToArray();
            }

            stat = stat
                   .Where(p => p.低点反弹高度 < benchmark * beta)
                   .OrderBy(p => p.低点反弹高度)
                   .ToArray();

            var q = from s in stat
                    join b in basics on s.代码 equals b.code
                    join d in data on s.代码 equals d.Code
                    select new Stock(s.代码,
                                     new
            {
                s.代码,
                b.name,
                s.收阳百分比,
                s.低点反弹高度,
                s.均线多头,
                b.pe,
                b.totalAssets,
                b.industry,
                b.liquidAssets
            });

            return(new universe("lowbeta", q.Select(p => p.Code).Distinct().ToArray()));
        }
Exemple #3
0
        public override void Run(Account account)
        {
            var client = new kdatadb();

            log.InfoFormat("total {0}", account.universe.Count);
            foreach (var stock in account.universe.AsParallel())
            {
                log.InfoFormat("run {0}", stock.Code);
                var k         = client.kdata(stock.Code, "15");
                var kdj       = new MACD(k.close());
                var crossup   = kdj.cross_gold();
                var crossdown = kdj.cross_dead();

                if (crossup.Any())
                {
                    var s = crossup.Last();
                    if (s.Date == k.Last().date&& s.Date.Date == DateTime.Today)
                    {
                        Buy(account, stock.Code, s.Date);
                        continue;
                    }
                }

                if (crossdown.Any())
                {
                    var s = crossdown.Last();
                    if (s.Date == k.Last().date&& s.Date.Date == DateTime.Today)
                    {
                        Sell(account, stock.Code, s.Date);
                        continue;
                    }
                }

                log.InfoFormat("no signal {0}", stock.Code);
            }
        }
Exemple #4
0
        public override universe Pass(IEnumerable <string> stocks)
        {
            var client = new kdatadb();

            log.Info("query D");
            var D = stocks
                    .AsParallel()
                    .Select(code => client.kdata(code, "D"))
                    .Where(p => p != null && p.Any())
                    .ToArray();

            log.InfoFormat("D total {0}", D.Count());

            var codes = D
                        .Where(p =>
            {
                if (!p.Any())
                {
                    return(false);
                }

                var close = p.close();
                var cross = new MACD(p.volume()).cross();

                return(cross.Any() &&
                       cross.Last().type == Interface.Data.crosstype.gold &&
                       cross.Last().value.Date.Date == DateTime.Today);
            })
                        .Select(p => p.Code)
                        .Distinct()
                        .ToArray();

            log.InfoFormat("selected {0}", codes.Count());

            return(new universe("volume", codes));
        }
Exemple #5
0
        public override void exec()
        {
            //var a = analytic.hitkeyprices();
            //log.Info(a.ToCsv());
            //return;

            log.Info("run strategy " + param.name.ToLower());

            log.Info("get codes");
            universe universe;

            if (param.astock)
            {
                var codes =
                    param.universe.Split(new[] { ' ', ',' }, StringSplitOptions.RemoveEmptyEntries);
                universe = new universe(param.universe, codes);
            }
            else if (param.asector)
            {
                universe = new universe(param.universe, new Trade.Db.db().codes(param.universe).ToArray());
            }
            else
            {
                universe = getUniverse(param.universe);
            }

            log.Info("total " + universe.codes.Length);

            log.Info("get run");
            var pool   = new StockPool(universe.codes);
            var orders = !param.backtest
                ? new IOrder[] { new dbOrder(), new smsOrder() }
                : new IOrder[] { new dbOrder() };
            var portflio = (param.portflio ?? param.name.ToLower()) + (param.backtest ? "-backtest" : "");
            var account  = new Account(portflio, pool, orders, param.backtest);

            switch (param.name.ToLower())
            {
            case "macd15min":
                new strategies.macd15minstrategy().Run(account);
                break;
            }

            if (param.backtest)
            {
                log.Info("run back test");
                var client = new kdatadb();
                log.InfoFormat("total {0}", account.universe.Count);
                var pnls = new List <pnl>();
                foreach (var stock in account.universe.AsParallel())
                {
                    log.InfoFormat("run {0}", stock.Code);
                    var k = client.kdata(stock.Code, "D");
                    if (k == null && !k.Any())
                    {
                        log.WarnFormat("empty data set for {0}", stock.Code);
                        continue;
                    }

                    var trades = account.Trades
                                 .Where(p => p.code == stock.Code)
                                 //.Where(p=>p.Date >= new DateTime(2016,9,1))
                                 .OrderBy(p => p.date)
                                 .ToArray();

                    var backtest = new backtesting(stock.Code, k.close(), trades);

                    if (backtest.pnl != null)
                    {
                        pnls.Add(backtest.pnl);
                    }
                }

                var format = "{0,-15}{1,-20}{2,10:N0}{3,10:N0}{4,10:N1}";
                log.InfoFormat(format, "code", "date", "value", "capital", "ratio%");
                foreach (var pnl in pnls)
                {
                    log.InfoFormat(format, pnl.code, pnl.date, pnl.value, pnl.capital, pnl.ratio);
                }
            }

            log.Info("**********DONE**********");
        }