Skip to main content

基于VNPY进行功能拓展

Project description

vnpy_extra

一、 简介

1. 功能简介

本工具包是基于vnpy的回测库的一些增强函数

2. 版本历史(按周汇总)

2021-05-14 v0.7.20210508.0

feat: add z_score_days param on MACDSignal.
feat: add param vt_symbol on CtaSignal.
feat: support period_enum on StrategyTemplate of portfolio strategy.
feat: add strategy_cls name on progress bar.
feat: add param same_direction on is_cross func: 是否要求金叉时一定要方向相同.即:金叉时 price1 方向向上,死叉是 price1 方向向下.
feat: add param same_direction param on MACrossSignal.
feat: update id_name if it's changed when track backtest.
fix: avg_entry_price wrong.
fix: TradeDateModel no available date check on is_end_week.
fix: self.set_signal_pos(-flag if self.reverse_bs else flag) on MACrossPriceSignal and add reverse_bs on MACrossSignal.
fix: same_direction is not available param on MACrossPriceSignal.

2021-04-30 v0.7.20210430.0

feat: add short_name on sub classes of CtaSignal.
feat: add daily_bars_needed_at_least, get_short_name, get_signal_name_header on CtaSignal.
feat: add unique key of ('stg_info', 'symbols_info', 'short_name',) and ('stg_info', 'symbols_info', 'shown_name',) on StrategyBacktestStats.
feat: auto fill short_name on update_backtest_status.
feat: add update_strategy_stats_by_dir for updating all files in dir.
feat: add score on calculate_statistics.
feat: 追踪回测时,文件名 id_name_[short_name].
feat: merge daily price and profit charts together.
feat: get_available_status_group_by_strategy all filter by symbols list or strategy_class_name list.
feat: lower dependency of FutureAdjFactor table when load_bar_data.
feat: add csv_split_chunk to handle large csv files.
fix: self.current_bars[vt_symbol] is None on send_order of portfolio template.py.
fix: symbols.upper() on get_symbols_id_dict and update_backtest_status.
fix: only warning if pos status < today and tick data missing.
refactor: lru_cache(maxsize=10) lower memory use on load_data. refactor: root_folder_name = f'{strategy_cls.name}{date_2_str(date.today())}' on bulk_backtest.
refactor: root_folder_name=f'{strategy_cls.name}
{vt_symbol}' on bulk_backtest_separated_by_symbol.

2021-04-23 v0.7.20210423.0

feat: 默认 root_folder_name 使用程序创建时的系统日期,为了防止跨日出现多个不同日期的文件夹,该日期初始化后不再改变。
feat: add avg/max/lw/avg_square/most of drawdown on cta/portfolio backtest engine.
feat: enable strategy_only filter and root_folder_name on backtest_all_strategies.
feat: DEFAULT_STATIC_ITEM -> DEFAULT_STATIC_ITEM_DIC. and include items: "return_most_drawdown_ratio", "return_loss_ratio".
feat: change default columns orders of bulk backtest output csv/excel files.
feat: add default params 'stop_opening_pos' 'base_position' on settings of settings files.
feat: support output orders/trades with output_orders/output_trades params on backtest.
feat: add capital param on bulk_backtest_separated_by_symbol.
feat: capital=1000000 by default of run_backtest.
feat: add StrategyBacktestStats.expire_stats for disabling old stats by symbols on both StrategyBacktestStats and AccountStrategyMapping.
feat: add trailing stop and stop loss.
feat: day end at 15:00 of BarGenerator.
fix: df is None check when is_available check.
fix: return_loss_ratio wrong.
fix: vt_symbols wrong of settings files.
fix: self.daily_df = None on clear_data. \ refactor: lru_cache() for TradeDateModel.get_trade_date_dic.

2021-04-16 v0.7.20210416.1

feat: add INSTRUMENT_TYPE_SUBSCRIPTION_DIC.
feat: add validation rule "最大回撤到最后一个交易日需要出现新高".
feat: add shown "annual_return", "sortino_ratio", "return_risk_ratio" by default.
feat: class_name + param_str by default of id_name of strategy.
fix: generate right vt_symbol for subscription.
fix: enhance change_curr_contract of FutureAdjFactor.
fix: main contract switch wrong on apply_2_symbol.
fix: bar.vt_symbol wrong on mock_load_bar_data. \ fix: is_main check on mock_load_bar_data.
fix: secondary contract reversion rights.
fix: return_loss_ratio error on calculate_statistics.
fix: INSTRUMENT_TYPE_SUBSCRIPTION_DIC wrong.
refactor: make 'id_name', 'backtest_status', 'short_name', 'shown_name' at the end columns of csv/xlsx files.

2021-04-09 v0.7.20210409.0

feat: add some instrument type on EXCHANGE_INSTRUMENTS_DIC.
feat: add vt_symbol_price_tick on CtaTemplate.
feat: get shown_name and short_name from get_name_by_pattern.
feat: add is_cross on func module feat: add ma on ArrayManager.
feat: add MACrossPriceSignal.
fix: wrong error_count on import results.
fix: on creating instance of StrategyTemplate, CtaTemplate of apply_2_symbol function.
fix: add exception handler on add_2_account_strategy_mapping.

2021-04-02 v0.7.20210402.0

feat: add output_report_between on report function.
feat: stats ['区间盈亏', '交易次数', '盈利次数', '亏损次数'] on reports.
feat: add rr wh fb cy trade datetime range.
feat: add get_instrument_type_and_exchange.
feat: warning '【非交易时段】' only on realtime mode.
feat: add base_price_type param for default kind of price when calculating indexes.
feat: add add_mapping_all, add_2_account_strategy_mapping, apply_2_symbol on StrategyBacktestStats.
fix: key available check on backtest.
fix: broker_id: str = IntegerField(help_text="经纪商代码") of AccountStrategyMapping.
fix: user_name, broker_id = int(user_name), int(broker_id) on AccountStrategyMapping.add_mapping.
fix: logging track test error.
fix: ignore updating ['id_name', 'backtest_status', 'short_name', 'shown_name'] if insert conflict on backtest.
fix: stats wrong on report.
fix: unique index on ('user_name', 'broker_id', 'short_name',) and ('user_name', 'broker_id', 'shown_name',) of AccountStrategyMapping.

2021-03-25 v0.7.20210325.0

feat: update_strategy_stats_by_df with AccountStrategyMapping.
feat: new logic of import/export settings with AccountStrategyMapping.
feat: support custom is_available func for strategy template.
feat: support start and end date range for bulk_backtest_separated_by_symbol.
feat: add strict logic of window bar on CtaSignal and BarGenerator.
feat: add GeneralPeriodEnum.
feat: init_load_days = 0 by default.
feat: add enable_entry_exit_price feature.
feat: backtest end at 3 month ago for in-sample test by default.

2021-03-17 v0.7.20210317.0

feat: update hour bar logic of BarGenerator to vnpy same logic.
feat: add {strategy_cls.name}_ on file name header of backtest.
feat: add user_id on file names on daily reports.
feat: speed up load history bar.
fix: reset error_count on each loop.
refactor: add log on bulk backtest.

2021-03-11 v0.7.20210311.1

feat: 支持加载主连连续合约作为合约历史行情数据(默认为False).
feat: move TBCtaSignal to vnpy_extra_tb.
feat: reset_index on on f'{key}_{date_2_str(date.today())}.xlsx' files.

2021-03-09 v0.7.20210309.0

feat: self.load_bar(self.init_load_days) on on_init() for cta/portfolio template.
feat: add remove_old_symbol_until_nth_latest on generate_md_with_adj_factor 删除旧的“带 symbol 标记”的主连行情数据,截止到第N个最新的主力合约.
feat: EXCHANGE_INSTRUMENTS_DIC, INSTRUMENT_EXCHANGE_DIC for constants.
feat: add generate md with adj_factor module.

2021-03-05 v0.6.20210305.0

feat: add StrategyBacktestStatsArchive.restore.
refactor: LatestTickPriceModel.replace -> LatestTickPriceModel.insert.
feat: add pool_size for multi thread.
feat: add TBCtaSignal.
feat: add export_tables_2_csv.
fix: engine_kwargs["vt_symbols"] or engine_kwargs["vt_symbol"] mistake on update_backtest_status.
fix: available_df.shape[0]>0 on backtest.
fix: set_index(multi_valued_param_name_list) on backtest.
fix: statistics available check on StrategyBacktestStats.update_stats.
feat: send_email_qq support msg list.
feat: order_by(StrategyBacktestStats.short_name) on StrategyBacktestStats.get_by_status.
feat: variables = ["target_pos", "target_price", "bar_count"] on template.
feat: add block_size=1024 * 1024 * 10 param on build_emails.
feat: add ignore_error param on csv_2_tables and import_data_2_tables.
fix: is_same_as_order for template.
fix: pd.isna(short_name) check.
fix: IntegrityError handler on import_data_2_tables.
feat: replace insert -> on duplicate key update.
feat: add trade count summary on monthly report.
feat: update update_backtest_status logic.
fix: reduce reject order.
fix: StrategyBacktestStats.update_backtest_status.
feat: add last_tick and fill default price if price <= 0.
feat: drop_duplicates and create f'{key}_{date_2_str(date.today())}.xlsx' file.

2021-02-28 v0.6.20210228.0

fix: order timeout on submit status. and clean tmp data when on_start called.
fix: output f'{key}_{date_2_str(date.today())}.csv' file event if len(df_list) == 1.
fix: ignore save_stats exceptions.
feat: remove duplicate msg.
feat: add check datetime trade available.
refactor: fit INSTRUMENT_RATE_DIC.
feat: email_attachment_2_tables, add csv_2_tables.
refactor: logger = logging.getLogger(f'strategies.*.{strategy_name}').
refactor: write_log(f"最新持仓目标 {target_pos} {self.target_price}", 'debug').
fix: annual return, calmar, sharp.
feat: export_tables_and_send_email.
feat: add password param for email login password. feat: export_tables_and_send_email(password).
fix: Writing to Excel with MultiIndex columns and no index ('index'=False) is not yet implemented.
fix: price <= 0.0 check.
fix: output_file_dic = {}.
fix: strategy.strategy_name is None in some cases.
fix: log and ignore lock error when cleaning TradeDataModel.
refactor: create a unique on strategy_class_name.

2021-02-19 v0.6.20210219.3

feat: feat: add StopOpeningPos.
feat: return output_file_dic on refresh_position_and_report.
feat: use CalVer.
feat: now all signal classes are sub classes of vnpy_extra.utils.enhancement.CtaSignal.
feat: add filter_n_available=1 param for each sub class.
feat: add check_datetime_available.

2021-02-12 v0.6.21

fix: some dirty data has to be clean.
fix: charts_data null=True, symbols cannot be unique.
refactor: bigger=1.0
feat: add backtest_status_path on output path.
fix: enable_join_collector.
refactor: add description on progress bar.
feat: add charts_data for showing echarts of strategy.
feat: progress bar shows file_name_header.
feat: support import strategy setting from ["cta_strategy_setting.json", "portfolio_strategy_setting.json"].
feat: auto add main_contract testcase for import_strategy_setting module.
fix: case-insensitive for portfolio portfolio_strategy.engine.BacktestingEngine.

2021-02-03 v0.6.7

feat: add algo trading on TargetPosAndPriceTemplate.
feat: remove tzinfo from last_tick_time on cta's CtaTemplate and portfolio's StrategyTemplate.
feat: add get_id_name function.
feat: add import_strategy_setting module.
fix: export/import tables and send email by io.
fix: ctaTemplate algo trading logic.

2021-01-26 v0.6.2

feat: add short_name, shown_name by default null and unique into db.
feat: export/import tables and send email by io.
fix: TargetPosAndPriceTemplate order missing.

2021-01-22 v0.6.0

feat: add track performance feature.
refactor: quant_vnpy -> vnpy_extra.

2021-01-19 v0.5.12

fix: TargetPosAndPriceTemplate current_pos -> target_pos by one step.
fix: strategy_class_name wrong.

2021-01-15 v0.5.5

fix: add exception handle and logger on TradeDataCollector.
fix: TargetPosAndPriceTemplate current_pos -> target_pos by one step.
feat: record backtest stats.
feat: add default rate for backtest.
fix: DCE夜盘交易日期为下一交易日,将会被重写为当前系统日期.
fix: 修复跨日报表统计错误.
fix: cross_limit_method param missing for file_name_func function.
fix: 主力合约、次主力合约数据复权整理 bug.

2021-01-08 v0.5.0

refactor: merge portfolio run_backtest and cta run_backtest.
feat: set_strategy_status(StrategyStatusEnum.Stopped).
feat: monitor add setting.
refactor: longer interval of plotly.io._orca.cleanup.
feat: backtest: output param file if it's available.
fix: MACDSignal.
fix: check not strategy_status_monitor.is_alive().

2020-12-27 v0.4.8

feat: backtest: reset_index on result_df.
fix: portfolio template, last_order_dt -> dict.
feat: add new TargetPosAndPriceTemplate, MACrossSignal.
feat: backtest: default rate.
feat: backtest: available filter for return_drawdown_ratio < 2 and np.round for some stats items.

2020-12-25 v0.4.6

feat: backtest: auto search symbol size.
fix: report gl calc wrong in some cases. fix: report holding pos status calc wrong.
fix: stop_opening_pos on templates.

2020-12-22 v0.4.2

fix: order_data_collector error on portfolio_strategy.template.
fix: open_price -> last_price.

2020-12-18 v0.4.0

feat: support user_name, broker_id.
feat: add last_order_dt on template. fix: orm close connection.
feat: position daily stat.
feat: add CrossLimitMethod.fix_price for backtest.
feat: add quant_vnpy.backtest.cta_strategy.template.CtaTemplate.
feat: backtest cross price method.
add position monitor

2020-12-11 v0.2.16

feat: on_tick active on_bar by bg on template.py.
feat: add OrderDataCollector, TradeDataCollector class.
feat: add stop_if_pos_2_0 on cta template.
fix: bug fix of on_tick and report error.

2020-12-04 v0.2.8

feat: more readable log.

2020-11-30 v0.2.7

feat: orm add symbols.
fix: bug fix on log format.
fix: bug fix on on_stop of portfolio template.
feat: add strategy status monitor.
feat: add bar_count.

2020-11-20 v0.1.8

bug fix on portfolio's template.
feat: 对 cta 及 portfolio 增加 template 模板类.
feat: signal 增加 0 判断 当 0 时,默认为 default 值.
feat: add current_bar for cta, portfolio's template classes.
fix: template bugs.
feat: 最新依赖版本 IBATS_Common>=0.20.8,最新支持道 vnpy 2.1.7 版本.
feat: 调整 INSTRUMENT_TRADE_TIME_PAIR_DIC 道 constants.py.

2020-11-10 v0.1.0

feat: 基于vnpy 2.1.6进行的功能增强。此前版本不支持。

二、 环境设置及组件安装(首次运行前需要)

1. 系统环境包含 Anaconda 或 Miniconda(python 3.7 版本)

2. 安装 vnpy 2.1.6或以上版本 \

3. 运行安装相关组件

pip isntall -r requirement.txt
conda install -c plotly plotly-orca
conda install -c plotly python-kaleido

如果执行遇到问题可分别执行如下:

  1. 通用组件

    pip install -r requirement.txt
    
  2. orca 组件 orca 组件为回测功能中保存回测视图结果的组件,windows系统性需要单独安装,才可保证功能正常使用
    安装步骤如下:

    1. 安装组件包

      conda install -c plotly plotly-orca
      
    2. 下载并安装 orca 应用

    组件下载地址:orca组件

    安装后设置话就环境变了 Path 加入相应路径,默认情况下window10操作系统 orca 组件将被安装在如下路径:

    C:\Users\mmmaaaggg\AppData\Local\Programs\orca

    1. 批量关闭 orca 进程方法
    ps -ef | grep orca | grep -v grep | awk '{print $2}' | xargs kill -9
    
  3. MD文件转word文档工具
    pandoc官网下载对应的软件并安装后即可运行 Scripts\md_2_docx.bat 脚本

  4. pycharm MemoryError 解决方案 打开pycharm安装目录 d:\IDE\JetBrains\PyCharm Community Edition 2020.2.1\bin\ ,找到 pycharm.exe.vmoptions 文件,

    -Xms128m
    -Xmx768m
    -XX:ReservedCodeCacheSize=240m
    -XX:+UseConcMarkSweepGC
    -XX:SoftRefLRUPolicyMSPerMB=50
    -ea
    -XX:CICompilerCount=2
    -Dsun.io.useCanonPrefixCache=false
    -Djava.net.preferIPv4Stack=true
    -Djdk.http.auth.tunneling.disabledSchemes=""
    -XX:+HeapDumpOnOutOfMemoryError
    -XX:-OmitStackTraceInFastThrow
    -Djdk.attach.allowAttachSelf
    

    修改 -Xms为 256m -Xmx为 2048m

    -Xms256m
    -Xmx2048m
    -XX:ReservedCodeCacheSize=240m
    -XX:+UseConcMarkSweepGC
    -XX:SoftRefLRUPolicyMSPerMB=50
    -ea
    -XX:CICompilerCount=2
    -Dsun.io.useCanonPrefixCache=false
    -Djava.net.preferIPv4Stack=true
    -Djdk.http.auth.tunneling.disabledSchemes=""
    -XX:+HeapDumpOnOutOfMemoryError
    -XX:-OmitStackTraceInFastThrow
    -Djdk.attach.allowAttachSelf
    

三、 常用命令

1. 切换远程仓库地址方法

>git remote
origin
>git remote get-url --all origin
https://github.com/IBATS/vnpy_extra.git
>git remote set-url origin git@gitee.com:ibats/vnpy_extra.git
>git push
Enumerating objects: 82, done.
Counting objects: 100% (82/82), done.
Delta compression using up to 8 threads
Compressing objects: 100% (65/65), done.
Writing objects: 100% (65/65), 14.57 KiB | 710.00 KiB/s, done.
Total 65 (delta 50), reused 0 (delta 0), pack-reused 0
remote: Powered by GITEE.COM [GNK-5.0]
To gitee.com:ibats/vnpy_extra.git
   4f85956..fe47acc  master -> master

2. 数据库 dump 数据

备份主力合约

"c:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe" -u mg -p --databases vnpy dbbardata --where="symbol in ('rb9999', 'hc9999', 'i9999') and `interval`='1m'" > dbbardata_dump.sql

备份主力,次主力合约

"c:\Program Files\MySQL\MySQL Server 8.0\bin\mysqldump.exe" -u mg -p vnpy dbbardata --where="(symbol like'%9999' or symbol like'%8888') and `interval`='1m'" > dbbardata_dump.sql

3. 压缩命令

zip -r0q /media/mg/Data/output_20201220.zip output

4. 独立启动交易界面(可以多开)

cd d:
cd d:\ide\vnstudio
python.exe -m vnstation runtrader "{'gateway': {'CTP': True}, 'app': {'CtaStrategy': True, 'PortfolioStrategy': True, 'PortfolioManager': True}, 'path': 'D:\\TradeTools\\vnpy\\jianxin_11859077'}"
cd c:\ide\vnstudio
python.exe -m vnstation runtrader "{'gateway': {'CTP': True}, 'app': {'CtaStrategy': True, 'PortfolioStrategy': True, 'PortfolioManager': True}, 'path': r'C:\TradeTools\vnpy_work_root\simnow'}"

四、 各个版本常见错误

2.0.3版本

  1. 错误提示框太长,无法看到错误信息

    python.exe -m vnstation
    
  2. 错误提示:

    ValueError: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216 from C header, got 192 from PyObject 解决方法:

    Scripts\pip.exe install numpy==1.16.1 --user
    
  3. 穿透式测试通不过,采集不到CPU、硬盘、BIOS信息 修改环境变量 增加目录

    C:\Windows\SysWOW64\wbem

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

vnpy_extra-0.7.20210508.0-py3-none-any.whl (130.1 kB view details)

Uploaded Python 3

File details

Details for the file vnpy_extra-0.7.20210508.0-py3-none-any.whl.

File metadata

  • Download URL: vnpy_extra-0.7.20210508.0-py3-none-any.whl
  • Upload date:
  • Size: 130.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.1

File hashes

Hashes for vnpy_extra-0.7.20210508.0-py3-none-any.whl
Algorithm Hash digest
SHA256 304e89aeae1a74a8254a0cb85c4badc6973242dc77777984eb708bb6bb0007d7
MD5 efdea9112b1fa19c05fa381d6598372f
BLAKE2b-256 1344f579d6d2a527e1691a38ea0b0cf2b49f701627d60ee704c8675e6e6f5058

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page