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 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 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, name="日线") plot_ka(ka, file_image="test.jpg") # 线段背驰 zs1 = { "start_dt": '2018-07-26 15:00:00', "end_dt": '2018-10-19 15:00:00', "direction": "down" } zs2 = { "start_dt": '2018-01-29 15:00:00', "end_dt": '2018-07-06 15:00:00', "direction": "down" } assert is_bei_chi(ka, zs1, zs2, mode='xd', adjust=0.9) zs1 = { "start_dt": '2013-12-10 15:00:00', "end_dt": '2014-01-20 15:00:00', "direction": "down" } zs2 = { "start_dt": '2013-09-12 15:00:00', "end_dt": '2013-11-14 15:00:00', "direction": "down" } assert not is_bei_chi(ka, zs1, zs2, mode='xd', adjust=0.9) # 笔背驰 zs1 = {"start_dt": '2019-05-17 15:00:00', "end_dt": '2019-06-10 15:00:00'} zs2 = {"start_dt": '2019-04-08 15:00:00', "end_dt": '2019-05-10 15:00:00'} assert is_bei_chi(ka, zs1, zs2, mode='bi', adjust=0.9) zs1 = {"start_dt": '2018-09-28 15:00:00', "end_dt": '2018-10-19 15:00:00'} zs2 = {"start_dt": '2018-08-28 15:00:00', "end_dt": '2018-09-12 15:00:00'} assert not is_bei_chi(ka, zs1, zs2, mode='bi', adjust=0.9)
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)
# coding: utf-8 import sys import warnings from cobra.data.kline import get_kline sys.path.insert(0, '.') sys.path.insert(0, '..') import czsc from czsc import KlineAnalyze from czsc.analyze import is_bei_chi, find_zs warnings.warn(f"czsc version is {czsc.__version__}") df = get_kline(ts_code="000001.SH", end_dt="2020-04-28 15:00:00", freq='D', asset='I') ka = KlineAnalyze(df, name="日线", bi_mode='old') print(ka) def test_kline_analyze(): assert ka.bi[-1]['fx_mark'] == 'g' assert ka.xd[-1]['fx_mark'] == 'd' # 测试背驰识别 assert ka.bi_bei_chi() assert ka.xd_bei_chi() print(ka.zs[-2]) ka.to_html("kline.html") ka.to_image("kline.png")