Metadata-Version: 2.1
Name: vnpy-extra
Version: 0.7.20210706.0
Summary: 基于VNPY进行功能拓展
Home-page: https://github.com/IBATS/vnpy_extra
Author: MG
Author-email: mmmaaaggg@163.com
License: UNKNOWN
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: Unix
Classifier: Operating System :: POSIX
Classifier: License :: OSI Approved :: MIT License
Classifier: Development Status :: 5 - Production/Stable
Classifier: Environment :: No Input/Output (Daemon)
Classifier: Intended Audience :: Developers
Classifier: Natural Language :: Chinese (Simplified)
Classifier: Topic :: Software Development
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Requires-Dist: IBATS-Utils (>=1.20210621.0)
Requires-Dist: IBATS-Common (>=0.20.8)
Requires-Dist: psutil (>=5.7.2)
Requires-Dist: kaleido (>=0.0.3)
Requires-Dist: openpyxl (>=3.0.5)
Requires-Dist: xlwt (>=1.3.0)
Requires-Dist: tqdm (>=4.51.0)
Requires-Dist: peewee (>=3.13.3)
Requires-Dist: mysql-connector-python (>=8.0.22)

# vnpy_extra

## 一、 简介
### 1. 功能简介
本工具包是基于vnpy的回测库的一些增强函数

### 2. 版本历史（按周汇总）

* 2021-07-09 v0.7.20210706.0
  > feat: record_file_name = os.path.join(output_folder, f"{zip_file_name}.txt").\
  > fix: 发现部分情况下 engine_kwargs 中 symbol 与 stats.symbols_info.symbols 不一致，以后者为准.\
  > fix: remove AccountStrategyMapping.import_by_settings(settings) from import_from_json_file.\
  > feat: add AccountStrategyMapping.insert on import_by_settings.\
  > feat: add TradeDateModel.transfer_from_wind().\
  > feat: raise error if setting not in parameters on template.py.\
  > refactor: upload records of uploading tick data of options. \
  > refactor: add nbconvert==5.6.1.

* 2021-07-02 v0.7.20210702.1
  > feat: Scripts for handling 国泰君安 Option Tick data.\
  > feat: add generate_df, generate_factors, generate_y_s, generate_x_df, load_and_plot_bars.\
  > feat: add logic of generate_x_d.\
  > fix: # noinspection PyUnresolvedReferences import ffn  # NOQA\
  > fix: remove cancel_all in on_trade.\
  > fix: sum_df\['汇总'] -> sum_df\[('区间盈亏', '汇总')].\
  > fix: 隔夜建仓平仓导致一些情况下平仓异常引发warning："当前持仓 * 手，当前持仓全部订单不足以处理当前平仓，这种情况发生，说明当前持仓数据与交易数据的累加数字不一致，请检查数据是否缺失"。\
  > fix: 1 if trade_data.direction == Direction.SHORT.value else -1.\
  > feat: add output_path_vt_symbol to shorten file_name.\
  > fix: "CY": \["9:00:00", "23:00:00"] on constant.\
  > fix: vt_symbol not in bars check on TargetPosAndPriceTemplate.\
  > fix: order.vt_symbol not in self.targets check.\
  > fix: add strategy_cls=strategy_cls into bulk_backtest_kwargs.\
  > fix: self.set_target_pos(target_pos * self.base_position) on MultiSignalsTemplate.\
  > refactor: lower some log levels.

* 2021-06-25 v0.7.20210625.1
  > feat: add output_path_has_strategy_class_name, output_separate_by_backtest_status.\
  > feat: add MultiSignalsTemplate.\
  > feat: supporting night md available check.\
  > feat: add return to output_report_between, output_monthly_report. \
  > feat: 在output_report_between中增加sum_df行，列的和. \
  > feat: add get_vt_symbol_target_pos_str for logging.\
  > feat: ignore empty columns when generating markdown files.\
  > feat: self.targets\[vt_symbol] KeyError check and ignore duplicate recent logs.\
  > fix: db = connect("mysql+pool://{user}:{password}@{host}:{port}/{database}".format(**settings)) if linux.\
  > fix: AttributeError on write_log.\
  > fix: drop(columns=\['策略类名']) on report.\
  > fix: RuntimeWarning: divide by zero encountered in double_scalars.

* 2021-06-18 v0.7.20210618.1
  > feat: add get_daily_bar_count on StrategyTemplate.\
  > fix: trade_data.volume == 0 filter.\
  > fix: active_order_waiting_since_dt_dict is None.\
  > fix: vt_symbol key error on.\
  > fix: do not build am if array_size is None.\
  > fix: stg_info， symbols_info is ForeignKeyField.\
  ```
  stg_info 与 StrategyInfo 外键关系在实际数据库中已经解除，但在逻辑关系中保留。
    get_available_status_group_by_strategy 函数强烈依赖于该外键关系
    ALTER TABLE `vnpy`.`strategy_backtest_stats`
      DROP FOREIGN KEY `strategy_backtest_stats_ibfk_2`,
      DROP FOREIGN KEY `strategy_backtest_stats_ibfk_1`;
  ```
  > fix: BarGenerator.is_end_day.\
  > fix: db = MySQLDatabase(**settings) in linux.\
  > fix: with lock on db connections.\
  > refactor: ALTER TABLE `vnpy`.`dbbardata` ADD PARTITION PARTITIONS 400;\
  > refactor: spread_type check.

* 2021-06-11 v0.7.20210611.0
  > feat: add symbol_is_main_dic for judge the main contract.\
  feat: add generate_md_by_xls_files.\
  feat: add signal strategy demo. \
  fix: engine_kwargs\['interval'] = Interval(interval) if interval is not None else Interval.MINUTE.\
  fix: cover/sell then buy/short on TargetPosAndPriceTemplate.\
  fix: keep trying bulk insert and backup data if exception.\
  fix: INSTRUMENT_TYPE_SUBSCRIPTION_DIC 能源所：小写+4个数字.\
  fix: get_instrument_type_and_exchange.\
  fix: return_loss_ratio is nan if abs_loss_pnl == 0, return_risk_ratio is nan if return_loss_ratio is nan or 0.\
  fix: charts_data error when len(vt_symbols)>2.\
  fix: add output_separate_by_backtest_status for the bug of output folder with backtest_status always.\
  fix: backtest_status missing. \
  fix: stg_info -> stg_info_id, symbols_info -> symbols_info_id after remove Foreign Key relations.\
  refactor: json.dump -obj_2_json.\
  refactor: change columns order and fix short_name, shown_name order.\
  refactor: return_loss_ratio with decimals 2.
  ```SQL
  ALTER TABLE `vnpy`.`strategy_backtest_stats` 
  DROP FOREIGN KEY `strategy_backtest_stats_ibfk_2`,
  DROP FOREIGN KEY `strategy_backtest_stats_ibfk_1`;
  ALTER TABLE `vnpy`.`strategy_backtest_stats` PARTITION BY KEY(stg_info_id) PARTITIONS 50;
  ```
  ```SQL
  ALTER TABLE `vnpy`.`strategy_backtest_stats_archive` 
  DROP FOREIGN KEY `strategy_backtest_stats_archive_ibfk_2`,
  DROP FOREIGN KEY `strategy_backtest_stats_archive_ibfk_1`;
  ALTER TABLE `vnpy`.`strategy_backtest_stats_archive` PARTITION BY KEY(stg_info_id) PARTITIONS 50;
  ```
  ```SQL
  ALTER TABLE `vnpy`.`dbbardata`  ADD PARTITION PARTITIONS 400;
  ```

* 2021-06-04 v0.7.20210604.1
  > feat: 增加按策略类统计方法.\
  feat: StrategyBacktestStats.set_strategy_unavailable.\
  feat: add StrategyBacktestStats.get_by_keys.\
  feat: try getting old strategy_stats if strategy_stats_original is None, and reuse backtest_status, short_name, shown_name if they are not None.\
  feat: add StrategyInfo.get_strategy_class_by_name.\
  feat: add backtest_by_warning_log.\
  fix: save to json file if save stats failed.\
  fix: output_report_between on reports.\
  fix: z_score = get_daily_min_bar_count() * z_score_days.\
  fix: daily_bars_needed_at_least on MACDSignal and add short_name on MACD,KDJ,RSI,BOLL.\
  fix: curr_closing_trade_data is None.\
  fix: statistics = dict_2_jsonable(statistics).\
  refactor: beauty log of StrategyBacktestStats.expire_stats.\
  refactor: add warning '没有找到对应的记录' on update_backtest_status.\
  refactor: avoid loop import.

* 2021-05-28 v0.7.20210528.0
  > feat: show fire_stop_order in variables if self.stop_loss_rate 0 or self.trailing_stop_rate 0. \
  feat: add TargetPosAndPriceTemplate on portfolio. \
  feat: beauty log and add TargetPosAndPriceNoStopOrderTemplate. \
  feat: enhance ArrayManager with datetime_array. \
  feat: add spread_type, curr_spread, curr_win_spread, win_spreads, win_am_spread on TargetPosAndPriceSpreadTemplate. \
  feat: change indicator_dic structure on two engine. \
  feat: add indicator by trade in portfolio_strategy engine. \
  fix: show "最新持仓目标 ***" only if target_pos != self.pos and not self.fire_stop_order. \
  fix: send_and_on_order_dt_dic pop ALLTRADED order. \
  fix: cancel all stop orders if fire_stop_order and pos -0 done.\
  fix: cancel_all_stop_orders() -cancel_all(). \
  fix: base_price_type -self.base_price_type. \
  fix: change way of np.int64 -int. \
  fix: cancel_all_stop_orders() -cancel_all().\
  fix: StrategyBacktestStats.update(update_dic).\
  fix: fire_stop_order_dt is None check. \
  fix: set 0 when calculate by trade if trades data is None. \
  fix: (order.volume - order.traded) <= vol <= order.volume on is_same_as_order func and beauty log.\
  fix: None check on calc_spread_bar.\
  fix: _update_spread.\
  refactor: beauty order log.

* 2021-05-21 v0.7.20210521.0
  > feat: add TargetPosAndPriceSpreadTemplate. \
  feat: add author_list on backtest_all_strategies. \
  feat: add TargetPosAndPrice2SymbolTemplate. \
  feat: add get_daily_min_bar_count on enhancement. \
  feat: add calc_init_load_days on StrategyTemplate. \
  feat: period_enum become optional. \ 
  feat: 添加calculate_by_trade，通过每笔交易计算指标. \
  feat: add get_id_name on MACrossPriceSignal. \
  fix: handle DoesNotExist on apply_2_symbol.\
  fix: update short_name, shown_name update to None on update_backtest_status. \
  fix: int64 cannot be json on indicator_dic.\
  fix: update backtest_status on update_stats. \
  fix: Duplicate entry error. remove fn.ABS(StrategyBacktestStats.backtest_status) >= StrategyBacktestStatusEnum.QuasiOnline.value condition.

* 2021-05-14 v0.7.20210514.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. \
  feat: add check_account_mapping on StrategyBacktestStats.update_backtest_status. \
  feat: add ams of ArrayManager Dict on StrategyTemplate. \
  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. \
  fix: score +/- wrong. \
  fix: file extension check ('.csv', '.xls', '.xlsx'). \
  fix: old_id_name = None and add exception log. \
  fix: only warning if pos status < today and tick data missing.\
  fix: duplicate key error in some cases of update_stats. \
  fix: cancel stop_vt_orderids when target position direction changed. \
  fix: key will not be added in send_and_on_order_dt_dic if stop == True in send_order. \
  fix: checking bar is None. \
  fix: handle InterfaceError on StrategyStatus.query_status. \
  fix: '主连合约' '次主连合约' shown wrong. \
  fix: do not calc avg_entry_price if self.pos == 0. \
  fix: load continue data. \
  refactor: add hint self.window_bar: Optional\[BarData] = None.\
  feat: add new parameter indicator_dic to return of BacktestingEngine.calculate_statistics(), which contain all indicator data.\
  feat: add new model field indicator_dic to StrategyBacktestStats in orm.py\
  feat: 数据库变更脚本
  ```sql
  ALTER TABLE `vnpy`.`strategy_backtest_stats`
  ADD COLUMN `indicator_dic` JSON NULL DEFAULT NULL AFTER `charts_data`;
  ALTER TABLE `vnpy`.`strategy_backtest_stats_archive`
  ADD COLUMN `indicator_dic` JSON NULL DEFAULT NULL AFTER `charts_data`;
  ALTER TABLE `vnpy`.`strategy_backtest_stats_test`
  ADD COLUMN `indicator_dic` JSON NULL DEFAULT NULL AFTER `charts_data`;
  ```

* 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 subclasses of vnpy_extra.utils.enhancement.CtaSignal. \
  feat: add filter_n_available=1 param for each subclass.  \
  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](https://repo.anaconda.com/archive/Anaconda3-2019.10-Windows-x86_64.exe) 或 Miniconda（python 3.7 版本）
### 2. 安装 [vnpy 2.1.6或以上版本](https://download.vnpy.com/vnstudio-2.1.7.exe) \
### 3. 运行安装相关组件 
```cmd
pip isntall -r requirement.txt
conda install -c plotly plotly-orca
conda install -c plotly python-kaleido
```

如果执行遇到问题可分别执行如下：
1. 通用组件
    ```cmd
    pip install -r requirement.txt
    ```

2. orca 组件
    orca 组件为回测功能中保存回测视图结果的组件，windows系统性需要单独安装，才可保证功能正常使用 \
    安装步骤如下： 
    1. 安装组件包

        ```cmd
        conda install -c plotly plotly-orca
        ```

    2. 下载并安装 orca 应用

    组件下载地址：[orca组件](https://github-production-release-asset-2e65be.s3.amazonaws.com/99037241/8ebe1c00-6f72-11ea-8c70-1021f91c27bc?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20201028%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20201028T052839Z&X-Amz-Expires=300&X-Amz-Signature=2a9e44c0ac740c2335a130a8c1cb9734994f4bd01a7a55c6c12fe5330a487dbf&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=99037241&response-content-disposition=attachment%3B%20filename%3Dwindows-release.zip&response-content-type=application%2Foctet-stream)

    安装后设置话就环境变了 Path 加入相应路径，默认情况下window10操作系统 orca 组件将被安装在如下路径：
    C:\Users\mmmaaaggg\AppData\Local\Programs\orca

    3. 批量关闭 orca 进程方法
    ```shell script
    ps -ef | grep orca | grep -v grep | awk '{print $2}' | xargs kill -9
    ```

3. MD文件转word文档工具 \
    到[pandoc官网](https://pandoc.org/installing.html)下载对应的软件并安装后即可运行 Scripts\md_2_docx.bat 脚本

4. pycharm MemoryError 解决方案
    打开pycharm安装目录 d:\IDE\JetBrains\PyCharm Community Edition 2020.2.1\bin\ ，找到 pycharm.exe.vmoptions 文件，
    ```text
    -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

    ```text
    -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
    ```

### 4. 不错的 Jupyter 组建

* nbextension
  ```shell
  pip install jupyter_contrib_nbextensions
  jupyter contrib nbextension install
  ```

* jupyter-kite
  ```shell
  pip install jupyter-kite
  jupyter labextension install @kiteco/jupyterlab-kite
  ```

* jupyter-matplotlib
  ```shell
  pip install ipympl
  jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyter-matplotlib
  ```

* jupyterlab-system-monitor
  ```shell
  pip install nbresuse
  jupyter labextension install jupyterlab-topbar-extension jupyterlab-system-monitor
  ```

* jupyterlab-variableInspector
  ```shell
  jupyter labextension install @lckr/jupyterlab_variableinspector
  ```

* debugger

  ```shell
  conda install xeus-python=0.8.0 -c conda-forge
  jupyter labextension install @jupyterlab/debugger
  ```

* 其他
  ```shell
  pip install jupyterlab_widgets
  jupyter labextension install @jupyter-widgets/jupyterlab-manager
  jupyter labextension install @jupyterlab/toc
  jupyter labextension install jupyterlab-execute-time
  jupyter labextension install @jupyter-widgets/jupyterlab-manager plotlywidget@4.9.0
  ```

## 三、 常用命令

### 1. 切换远程仓库地址方法
```cmd
>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. 数据库命令

#### 1) dump 数据

备份主力合约

 ```cmd
"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
```

备份主力，次主力合约

```cmd
"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
```

#### 2) 数据库分区

```SQL
ALTER TABLE `vnpy`.`dbbardata` 
 PARTITION BY KEY (symbol) PARTITIONS 100;
```

```SQL
ALTER TABLE `vnpy`.`strategy_backtest_stats` 
 PARTITION BY KEY(stg_info_id) PARTITIONS 50;
```

#### 3) 错误解决方案
```shell script
peewee.OperationalError: (-1, 'MySQL Connection not available.', None)
```
pip install peewee==3.13.3

### 3. 压缩命令

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


### 4. 独立启动交易界面（可以多开）
```cmd
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'}"
```

```cmd
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. 错误提示框太长，无法看到错误信息
	```cmd
	python.exe -m vnstation
	```

2. 错误提示：
	ValueError: numpy.ufunc size changed, may indicate binary incompatibility. Expected 216 from C header, got 192 from PyObject
	解决方法：
	```cmd
	Scripts\pip.exe install numpy==1.16.1 --user
	```

3. 穿透式测试通不过，采集不到CPU、硬盘、BIOS信息
    修改环境变量
    增加目录
    C:\Windows\SysWOW64\wbem


