def check_buy(stocks, args): """Check buy signals.""" symbols = stocks.sample(frac=1).index idx = PERIODS.index(args.period) candidate_buy = set() pbar = tqdm(symbols, disable=args.daemon) for symbol in pbar: pbar.set_description(symbol) # Whether to reserve ST stocks. if not args.reserve_st and is_st(stocks.loc[symbol]): continue # Whether to reserve suspended stocks. if not args.reserve_suspend and is_suspend(stocks.loc[symbol]): continue # Check great-great-grandparent period. if idx + 4 < len(PERIODS) and \ args.check_great_great_grandparent: time.sleep(args.interval) kline = XueQiu.kline(symbol, PERIODS[idx + 4], args.datetime) macd = MACD(kline.close) if not macd.golden or (not args.loose and not macd.expand): continue # Check grandparent period. if idx + 2 < len(PERIODS): time.sleep(args.interval) kline = XueQiu.kline(symbol, PERIODS[idx + 2], args.datetime) macd = MACD(kline.close) if not macd.golden or (not args.loose and not macd.expand): continue # Check current period. time.sleep(args.interval) kline = XueQiu.kline(symbol, args.period, args.datetime) macd = MACD(kline.close) if not macd.negative or not macd.death or \ (not args.loose and not macd.narrow): continue # Check child period. if idx > 0 and args.child: time.sleep(args.interval) kline = XueQiu.kline(symbol, PERIODS[idx - 1], args.datetime) macd = MACD(kline.close) if args.child == CHILD_CHOICE_CROSS: if not macd.death or (not args.loose and not macd.narrow): continue elif not macd.will_bottom_divergence: continue candidate_buy.add(symbol) return stocks[stocks.index.isin(candidate_buy)].sort_index()
def test_golden(self): """Test for golden.""" res = XueQiu.kline('000001.SH', 'day', datetime(2020, 2, 17)) macd = MACD(res.close) self.assertTrue(macd.golden) macd = MACD(res.close[:-1]) self.assertFalse(macd.golden)
def test_death(self): """Test for death.""" res = XueQiu.kline('000001.SH', 'day', datetime(2020, 1, 16)) macd = MACD(res.close) self.assertTrue(macd.death) macd = MACD(res.close[:-1]) self.assertFalse(macd.death)
def test_narrow_expand_when_cross_in_death(self): """Test for narrow and expand when just cross in death cross.""" res = XueQiu.kline('000001.SH', 'day', datetime(2020, 1, 16)) macd = MACD(res.close) self.assertFalse(macd.narrow) self.assertTrue(macd.expand) macd = MACD(res.close[:-1]) self.assertTrue(macd.narrow) self.assertFalse(macd.expand)
def test_narrow_expand_when_narrow_in_golden(self): """Test for narrow and expand when just narrow in golden cross.""" res = XueQiu.kline('000001.SH', 'day', datetime(2020, 2, 25)) macd = MACD(res.close) self.assertTrue(macd.narrow) self.assertFalse(macd.expand) macd = MACD(res.close[:-1]) self.assertFalse(macd.narrow) self.assertTrue(macd.expand)
def test_negative_positive_when_golden(self): """Test for negative and positive when golden cross.""" res = XueQiu.kline('000001.SH', 'day', datetime(2020, 2, 24)) macd = MACD(res.close) self.assertFalse(macd.negative) self.assertTrue(macd.positive) macd = MACD(res.close[:-1]) self.assertTrue(macd.negative) self.assertFalse(macd.positive)
def check_sell(stocks, args): """Check sell signals.""" symbols = stocks.sample(frac=1).index idx = PERIODS.index(args.period) candidate_sell = set() pbar = tqdm(symbols, disable=args.daemon) for symbol in pbar: pbar.set_description(symbol) # Check parent period. if idx + 1 < len(PERIODS) and args.check_parent: time.sleep(args.interval) kline = XueQiu.kline(symbol, PERIODS[idx + 1], args.datetime) macd = MACD(kline.close) if not macd.golden or (not args.loose and not macd.narrow): continue # Check current period. time.sleep(args.interval) kline = XueQiu.kline(symbol, args.period, args.datetime) macd = MACD(kline.close) if not macd.golden or (not args.loose and not macd.narrow): continue # Check child period. if idx > 0 and args.child: time.sleep(args.interval) kline = XueQiu.kline(symbol, PERIODS[idx - 1], args.datetime) macd = MACD(kline.close) if args.child == CHILD_CHOICE_CROSS: if not macd.golden or (not args.loose and not macd.narrow): continue elif not macd.will_top_divergence: continue candidate_sell.add(symbol) return stocks[stocks.index.isin(candidate_sell)].sort_index()
def test_will_top_divergence(self): """Test for will_top_divergence property.""" res = XueQiu.kline('000016.SH', 'day', datetime(2019, 10, 21)) macd = MACD(res.close) self.assertTrue(macd.will_top_divergence)
def test_kline(self): """Positive case for kline.""" stocks = XueQiu.list_ashare(page=random.randint(1, 1000), size=1) res = XueQiu.kline(stocks.index[0]) self.assertIsInstance(res, pandas.DataFrame) self.assertGreater(len(res.index), 0)