def test_kline_analyze(): df = get_kline(ts_code="300008.SZ", end_dt="2020-03-23 15:00:00", freq='30min', asset='E') ka = KlineAnalyze(df) # 测试识别结果 assert ka.bi[-1]['fx_mark'] == 'g' assert ka.xd[-1]['fx_mark'] == 'g' # 测试背驰识别 assert not ka.bi_bei_chi() assert not ka.xd_bei_chi()
def use_kline_analyze(): print('=' * 100, '\n') print("KlineAnalyze 的使用方法:\n") kline = get_kline(symbol='SHSE.000300', end_date="2020-02-02") ka = KlineAnalyze(kline) print("线段:", ka.xd, "\n") print("中枢:", ka.zs, "\n")
def use_kline_analyze(): print('=' * 100, '\n') print("KlineAnalyze 的使用方法:\n") kline = get_kline(ts_code="000009.SZ", end_date="20200210", freq='30min', asset="I") ka = KlineAnalyze(kline) print("线段:", ka.xd, "\n") print("中枢:", ka.zs, "\n")
def check_trade(ts_code, freq, file_bs, file_png="bs.png"): """在图上画出买卖点""" bs = pd.read_excel(file_bs) bs['f'] = bs['操作提示'].apply(lambda x: 1 if freq_map[freq] in x and "同级别" not in x else 0) bs = bs[bs.f == 1] df = get_kline(ts_code, freq=freq, end_date="20200314", asset='E') ka = KlineAnalyze(df) df = pd.DataFrame(ka.kline) df = macd(df) df = df.merge(bs[['操作提示', '出现时间', '基准价格']], left_on='dt', right_on='出现时间', how='left') draw(df, file_png)
def make_one_day(ts_code, trade_date, asset="E"): if "-" in trade_date: end_date = datetime.strptime(trade_date, '%Y-%m-%d') else: end_date = datetime.strptime(trade_date, '%Y-%m-%d') start_date = end_date - timedelta(days=1) end_dt = end_date + timedelta(days=30) start_date = start_date.strftime("%Y-%m-%d") end_date = end_date.strftime("%Y-%m-%d") if not is_trade_day(start_date): return print(f"start trade simulator on {start_date}") trade_simulator(ts_code=ts_code, start_date=start_date, end_date=end_date, asset=asset, watch_interval=1) for freq in ['1min', '5min', '30min']: file_signals = os.path.join( data_path, f"{ts_code}_{start_date}_{end_date}_signals.txt") signals = [ eval(x) for x in open(file_signals, encoding='utf-8').readlines() ] df = pd.DataFrame(signals) kline = get_kline(ts_code, end_dt=end_dt.strftime("%Y-%m-%d %H:%M:%S"), freq=freq, asset=asset) ka = KlineAnalyze(kline) print(kline.head(), "\n\n") xd = deepcopy(ka.xd) xd = sorted(xd, key=lambda row: row['dt'], reverse=False) print(xd, "\n\n") def ___xd_status(dt): for x in xd: if x['dt'] >= dt: if x['fx_mark'] == 'd': s = "向下段" elif x['fx_mark'] == 'g': s = "向上段" else: raise ValueError return s return "o" col = f'{freq}线段状态' df[col] = df['交易时间'].apply(___xd_status) file_excel = "./data/%s_%s_%s_%s.xlsx" % (ts_code, start_date, end_date, freq) df.to_excel(file_excel, index=False)
def get(self): ts_code = self.get_argument('ts_code') freq = self.get_argument('freq') trade_date = self.get_argument('trade_date') if trade_date == 'null': trade_date = datetime.now().date().__str__().replace("-", "") kline = get_gm_kline(symbol=ts_code, end_date=trade_date, freq=freq, k_count=3000) ka = KlineAnalyze(kline) kline = ka.chan_result kline = kline.fillna("") columns = ["dt", "open", "close", "low", "high", "vol", 'fx_mark', 'fx', 'bi', 'xd'] self.finish({'kdata': kline[columns].values.tolist()})
def check_trade(ts_code, file_bs, freq, end_date="20200314", asset="E", file_html="bs.html"): """在图上画出买卖点""" bs = pd.read_excel(file_bs) bs['f'] = bs['操作提示'].apply(lambda x: 1 if freq_map[freq] in x and "线" not in x else 0) bs = bs[bs.f == 1] bs['操作提示'] = bs['操作提示'].apply(lambda x: x.replace(freq_map[freq], "")) df = get_kline(ts_code, freq=freq, end_date=end_date, asset=asset) ka = KlineAnalyze(df) plot_kline(ka, bs, file_html)
def check_trade(ts_code, file_bs, freq, end_date="20200314", asset="E", file_html="bs.html"): """在图上画出买卖点""" bs = pd.read_excel(file_bs) bs.loc[:, 'f'] = bs.apply(lambda x: 1 if x['交易级别'] == freq_map[freq] else 0, axis=1) bs = bs[bs.f == 1] bs.loc[:, '操作提示'] = bs['操作提示'].apply(lambda x: x.replace(freq_map[freq], "")) bs = bs[["操作提示", "交易时间", "交易价格"]] print(bs) df = get_kline(ts_code, freq=freq, end_date=end_date, asset=asset) ka = KlineAnalyze(df) plot_kline(ka, bs, file_html, width="1400px", height="680px")
def get(self): ts_code = self.get_argument('ts_code') freq = self.get_argument('freq') asset = self.get_argument('asset', "E") trade_date = self.get_argument('trade_date') if trade_date == 'null': trade_date = datetime.now().date().__str__().replace("-", "") kline = get_kline(ts_code=ts_code, end_date=trade_date, freq=freq, asset=asset) ka = KlineAnalyze(kline) kline = pd.DataFrame(ka.kline) kline = kline.fillna("") columns = [ "dt", "open", "close", "low", "high", "vol", 'fx_mark', 'fx', 'bi', 'xd' ] self.finish({'kdata': kline[columns].values.tolist()})
def test_bei_chi(): df = get_kline(ts_code="000001.SH", end_dt="2020-04-28 15:00:00", freq='D', asset='I') ka = KlineAnalyze(df) # 线段背驰 zs1 = ['2018-07-26 15:00:00', '2018-10-19 15:00:00'] zs2 = ['2018-01-29 15:00:00', '2018-07-06 15:00:00'] assert is_bei_chi(ka, zs1, zs2, direction='down', mode='xd', adjust=0.9) zs1 = ['2013-12-10 15:00:00', '2014-01-20 15:00:00'] zs2 = ['2013-09-12 15:00:00', '2013-11-14 15:00:00'] assert not is_bei_chi(ka, zs1, zs2, direction='down', mode='xd', adjust=0.9) # 笔背驰 zs1 = ['2019-05-17 15:00:00', '2019-06-10 15:00:00'] zs2 = ['2019-04-08 15:00:00', '2019-05-10 15:00:00'] assert is_bei_chi(ka, zs1, zs2, mode='bi', adjust=0.9) zs1 = ['2018-09-28 15:00:00', '2018-10-19 15:00:00'] zs2 = ['2018-08-28 15:00:00', '2018-09-12 15:00:00'] assert not is_bei_chi(ka, zs1, zs2, mode='bi', adjust=0.9)
def test_kline_analyze(): df = get_kline(ts_code="300803.SZ", freq='1min', end_date="20200216") ka = KlineAnalyze(df)