Skip to content

Commit

Permalink
V0.9.21 更新一批代码 (waditu#151)
Browse files Browse the repository at this point in the history
* 0.9.21 first commit

* 0.9.21 first commit

* 0.9.21 update

* 0.9.21 新增信号分析

* 0.9.21 新增 CTAResearch 模块

* 0.9.21 update

* 0.9.21 update

* 0.9.21 update

* add vscode settings

* 0.9.21 update

* 0.9.21 update

* 0.9.21 update cmd

* 0.9.21 优化代码格式

* 0.9.21 update

* 0.9.21 fix test

* 0.9.21 update

* 0.9.21 update

* 0.9.21 update

* 0.9.21 更新琅盎共享的信号函数

* 0.9.21 update

* 0.9.21 update coo_td_V221111

* 0.9.21 新增CCI基础策略

* update

* update

* 0.9.21 update

* 0.9.21 fix bug: 合并K线后,会出现 high_ubi 大于 bi.high,这是正常的

* 0.9.21 update

* 0.9.21 update

* 0.9.21 优化价值发现算法

* fix divide by 0

* 0.9.21 新增阿里云OSS操作类

* update

* update

* update

* update
  • Loading branch information
zengbin93 authored Jun 16, 2023
1 parent d7e2584 commit ce390fa
Show file tree
Hide file tree
Showing 41 changed files with 2,368 additions and 570 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pythonpackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ name: Python package

on:
push:
branches: [ master, V0.9.20 ]
branches: [ master, V0.9.21 ]
pull_request:
branches: [ master ]

Expand All @@ -15,7 +15,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9, 3.10.11, 3.11.3]
python-version: [3.7, 3.8, 3.9, '3.10', '3.11']

steps:
- uses: actions/checkout@v2
Expand Down
20 changes: 20 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"python.testing.pytestArgs": [
"test"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.formatting.provider": "none",
"python.formatting.autopep8Args": [
"--max-line-length",
"120"
],
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.blackArgs": [
"--line-length",
"120"
],
"typescript.locale": "zh-CN",
}
7 changes: 4 additions & 3 deletions czsc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
from czsc.utils import get_sub_elements, get_py_namespace, freqs_sorted, x_round, import_by_name, create_grid_params
from czsc.utils import cal_trade_price, cross_sectional_ic, update_bbars, update_tbars, update_nbars
from czsc.utils import CrossSectionalPerformance
from czsc.sensors import holds_concepts_effect, SignalsPerformance, CTAResearch
from czsc.sensors import holds_concepts_effect, CTAResearch
from czsc.utils.signal_analyzer import SignalAnalyzer, SignalPerformance


__version__ = "0.9.20"
__version__ = "0.9.21"
__author__ = "zengbin93"
__email__ = "[email protected]"
__date__ = "20230524"
__date__ = "20230601"


def welcome():
Expand Down
33 changes: 14 additions & 19 deletions czsc/analyze.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def remove_include(k1: NewBar, k2: NewBar, k3: RawBar):
direction = Direction.Down
else:
k4 = NewBar(symbol=k3.symbol, id=k3.id, freq=k3.freq, dt=k3.dt, open=k3.open,
close=k3.close, high=k3.high, low=k3.low, vol=k3.vol, elements=[k3])
close=k3.close, high=k3.high, low=k3.low, vol=k3.vol, amount=k3.amount, elements=[k3])
return False, k4

# 判断 k2 和 k3 之间是否存在包含关系,有则处理
Expand All @@ -45,15 +45,16 @@ def remove_include(k1: NewBar, k2: NewBar, k3: RawBar):

open_, close = (high, low) if k3.open > k3.close else (low, high)
vol = k2.vol + k3.vol
amount = k2.amount + k3.amount
# 这里有一个隐藏Bug,len(k2.elements) 在一些及其特殊的场景下会有超大的数量,具体问题还没找到;
# 临时解决方案是直接限定len(k2.elements)<=100
elements = [x for x in k2.elements[:100] if x.dt != k3.dt] + [k3]
k4 = NewBar(symbol=k3.symbol, id=k2.id, freq=k2.freq, dt=dt, open=open_,
close=close, high=high, low=low, vol=vol, elements=elements)
close=close, high=high, low=low, vol=vol, amount=amount, elements=elements)
return True, k4
else:
k4 = NewBar(symbol=k3.symbol, id=k3.id, freq=k3.freq, dt=k3.dt, open=k3.open,
close=k3.close, high=k3.high, low=k3.low, vol=k3.vol, elements=[k3])
close=k3.close, high=k3.high, low=k3.low, vol=k3.vol, amount=k3.amount, elements=[k3])
return False, k4


Expand All @@ -75,7 +76,7 @@ def check_fxs(bars: List[NewBar]) -> List[FX]:
"""输入一串无包含关系K线,查找其中所有分型"""
fxs = []
for i in range(1, len(bars)-1):
fx: FX = check_fx(bars[i-1], bars[i], bars[i+1])
fx = check_fx(bars[i-1], bars[i], bars[i+1])
if isinstance(fx, FX):
# 这里可能隐含Bug,默认情况下,fxs本身是顶底交替的,但是对于一些特殊情况下不是这样,这是不对的。
# 临时处理方案,强制要求fxs序列顶底交替
Expand All @@ -94,7 +95,7 @@ def check_fxs(bars: List[NewBar]) -> List[FX]:
return fxs


def check_bi(bars: List[NewBar], benchmark: float = None):
def check_bi(bars: List[NewBar], benchmark=None):
"""输入一串无包含关系K线,查找其中的一笔
:param bars: 无包含关系K线列表
Expand Down Expand Up @@ -151,22 +152,15 @@ def check_bi(bars: List[NewBar], benchmark: float = None):
if (not ab_include) and (len(bars_a) >= min_bi_len or power_enough):
fxs_ = [x for x in fxs if fx_a.elements[0].dt <= x.dt <= fx_b.elements[2].dt]
bi = BI(symbol=fx_a.symbol, fx_a=fx_a, fx_b=fx_b, fxs=fxs_, direction=direction, bars=bars_a)

low_ubi = min([x.low for y in bars_b for x in y.raw_bars])
high_ubi = max([x.high for y in bars_b for x in y.raw_bars])
if (bi.direction == Direction.Up and high_ubi > bi.high) \
or (bi.direction == Direction.Down and low_ubi < bi.low):
return None, bars
else:
return bi, bars_b
return bi, bars_b
else:
return None, bars


class CZSC:
def __init__(self,
bars: List[RawBar],
get_signals: Callable = None,
get_signals = None,
max_bi_num=envs.get_max_bi_num(),
):
"""
Expand Down Expand Up @@ -223,7 +217,8 @@ def __update_bi(self):
logger.info(f"{self.symbol} - {self.freq} - {bars_ubi[-1].dt} 未完成笔延伸数量: {len(bars_ubi)}")

if envs.get_bi_change_th() > 0.5 and len(self.bi_list) >= 5:
benchmark = min(self.bi_list[-1].power_price, np.mean([x.power_price for x in self.bi_list[-5:]]))
price_seq = [x.power_price for x in self.bi_list[-5:]]
benchmark = min(self.bi_list[-1].power_price, sum(price_seq) / len(price_seq))
else:
benchmark = None

Expand Down Expand Up @@ -261,7 +256,7 @@ def update(self, bar: RawBar):
for bar in last_bars:
if len(bars_ubi) < 2:
bars_ubi.append(NewBar(symbol=bar.symbol, id=bar.id, freq=bar.freq, dt=bar.dt,
open=bar.open, close=bar.close,
open=bar.open, close=bar.close, amount=bar.amount,
high=bar.high, low=bar.low, vol=bar.vol, elements=[bar]))
else:
k1, k2 = bars_ubi[-2:]
Expand Down Expand Up @@ -292,7 +287,7 @@ def update(self, bar: RawBar):
else:
self.signals = OrderedDict()

def to_echarts(self, width: str = "1400px", height: str = '580px', bs=None):
def to_echarts(self, width: str = "1400px", height: str = '580px', bs=[]):
"""绘制K线分析图
:param width: 宽
Expand All @@ -306,8 +301,8 @@ def to_echarts(self, width: str = "1400px", height: str = '580px', bs=None):
[{'dt': self.bi_list[-1].fx_b.dt, "bi": self.bi_list[-1].fx_b.fx}]
fx = [{'dt': x.dt, "fx": x.fx} for x in self.fx_list]
else:
bi = None
fx = None
bi = []
fx = []
chart = kline_pro(kline, bi=bi, fx=fx, width=width, height=height, bs=bs,
title="{}-{}".format(self.symbol, self.freq.value))
return chart
Expand Down
39 changes: 0 additions & 39 deletions czsc/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,3 @@ def aphorism():
from czsc.aphorism import print_one

print_one()


@czsc.command()
@click.option('-f', '--file_strategy', type=str, required=True, help="Python择时策略文件路径")
def dummy(file_strategy):
"""使用 CzscDummyTrader 进行快速的择时策略研究"""
pass


@czsc.command()
@click.option('-f', '--file_strategy', type=str, required=True, help="Python择时策略文件路径")
def replay(file_strategy):
"""执行择时策略在某个品种上的交易回放"""
pass


@czsc.command()
@click.option('-f', '--file_strategy', type=str, required=True, help="Python信号检查文件路径")
@click.option('-d', '--delta_days', type=int, required=False, default=1, help="两次相同信号之间的间隔天数")
def check(file_strategy, delta_days):
"""执行择时策略中使用的信号在某个品种上的校验"""
from czsc.traders import check_signals_acc
from czsc.utils import get_py_namespace

py = get_py_namespace(file_strategy)
get_signals = py['get_signals']
check_params = py.get('check_params', None)

if not check_params:
logger.warning(f"{file_strategy} 中没有设置策略回放参数,将使用默认参数执行")

# 获取单个品种的基础周期K线
symbol = check_params.get('symbol', "000001.SZ#E")
sdt = check_params.get('sdt', '20200101')
edt = check_params.get('edt', '20220101')
bars = py['read_bars'](symbol, sdt, edt)
logger.info(f"信号检查参数 | {symbol} - sdt: {sdt} - edt: {edt}")

check_signals_acc(bars, get_signals=get_signals, delta_days=delta_days)
3 changes: 3 additions & 0 deletions czsc/connectors/qmt_connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs):

kline = get_kline(symbol, period, sdt, edt, dividend_type=dividend_type,
download_hist=kwargs.get("download_hist", True), df=True)
if kline.empty:
return []

kline['dt'] = pd.to_datetime(kline['time'])
kline['vol'] = kline['volume']
bars = resample_bars(kline, freq, raw_bars=True)
Expand Down
2 changes: 2 additions & 0 deletions czsc/connectors/research.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def get_raw_bars(symbol, freq, sdt, edt, fq='前复权', **kwargs):
if 'dt' not in kline.columns:
kline['dt'] = pd.to_datetime(kline['datetime'])
kline = kline[(kline['dt'] >= pd.to_datetime(sdt)) & (kline['dt'] <= pd.to_datetime(edt))]
if kline.empty:
return []
_bars = czsc.resample_bars(kline, freq, raw_bars=True)
return _bars

Expand Down
Loading

0 comments on commit ce390fa

Please sign in to comment.