Metadata-Version: 2.1
Name: vnpy-extra
Version: 0.8.20211215.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: 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.20211214.0)
Requires-Dist: IBATS-Common (>=0.20.11)
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)
Requires-Dist: cvxpy (>=1.1.13)
Requires-Dist: cvxopt (>=1.2.6)
Requires-Dist: opencv-python (>=4.5.3)
Requires-Dist: pyportfolioopt (>=1.4.2)
Requires-Dist: numba (>=0.52.0)
Requires-Dist: scikit-learn (>=0.24.2)

# vnpy_extra

[TOC]

## 一、 简介

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

### 2. 版本历史（按周汇总）
* 2021-12-17 v0.8.20211215.0
  > feat: add backtest_from_tb_xml.
  > 
  > feat: ignore_existed_backtest if file param_file_name existed.
  > 
  > fix: json.dump(settings, fp, indent=4).
  > 
  > refactor: show plot_by_account before generate_strategy_setting_file.
  > 
  > refactor: upgrade IBATS-Utils>=1.20211214.0

* 2021-12-10 v0.8.20211210.0
  > feat: set_target_pos only if win_bar_count changed.
  > 
  > feat: add patch_missing_settings_by_dir.
  > 
  > feat: f"[{'_'.join([make_param_2_str(_) for _ in param])}]" if isinstance(param, list) of make_param_2_str.
  > 
  > fix: _id_name error if len(param_value_dic) >= 2.
  > 
  > fix: KeyError on group_name = key_group_dic[key].
  > 
  > refactor: value = json.loads(value) if isinstance(value, str) for signal param on MultiSignalsTemplate.

* 2021-12-03 v0.8.20211203.2
  > feat: add bulk_backtest_multi_process_by_symbols for both cta and portfolio.
  > 
  > feat: add ret_mid=False param for am.boll.
  > 
  > feat: add grace_bar_count param for BOLLSignal.
  > 
  > feat: add pre_call_before_backtest on refresh_vt_symbol_2_curr_contract_by_account, backtest_all_strategies. 
  > 
  > refactor: add am.logger.
  > 
  > refactor: 为防止出现类公共属性数据混淆，在 __init__ 里面重新进行初始化相关变量 

* 2021-11-26 v0.8.20211126.0
  > feat: 允许子类在 super.__init__ 之前 覆盖父类的对应属性 on PublicSignal.
  > 
  > feat: add rr, volatility, stochf, stoch, keltner for am.
  >
  > feat: add pre_call_before_backtest for dynamic loading modules.
  > 
  > fix: sum result on psy if array==True.
  > 
  > fix: er(self, period, has_plus_or_minus=False, array: bool = False).
  > 
  > fix: MultiSignalsTemplate.get_id_name.

* 2021-11-20 v0.8.20211119.0
  > feat: add NATRSignal. 
  >
  > feat: filter by pct_change_s.quantile(\[0.02, 0.98]) for net_pnl_gross_s.
  > 
  > feat: add Signal2PositionMethodEnum.viscosity for MultiSignalsTemplate.
  > 
  > feat: replace atr, remove atr_no_hl, natr_no_hl for ArrayManager.
  > 
  > feat: output _all stats only.
  > 
  > fix: BASE_POSITION, STOP_OPENING_POS_PARAM missing on account mapping table.
  > 
  > refactor: self.logger = logging.getLogger(f"{strategy_obj.strategy_name}.{self.__class__.__name__}").
  > 
  > refactor: performance tuning for multiprocess of backtest and tracking backtest.
  > 
  > fix: no data check on report.

* 2021-11-12 v0.8.20211112.3

  > feat: add ** kwargs for bulk_backtest_with_backtest_params_iter, backtest_all_strategies, run_backtest_all_strategies_after_refresh.
  >
  > feat: add min_high_after_entry_by_signal, max_low_after_entry_by_signal, highest_after_entry_by_signal, lowest_after_entry_by_signal, min_high_after_entry_by_signal_last, max_low_after_entry_by_signal_last, highest_after_entry_by_signal_last, lowest_after_entry_by_signal_last, bars_since_entry_by_signal, bars_since_exit_by_signal, entry_price_by_signal, exit_price_by_signal on CtaSignal.
  >
  > feat: vt_symbol_weight_multiplier_dic, strategy_weight_multiplier_dic on fit_and_merge_key_stats_dic.
  >
  > feat: add ArrayManager.natr_no_hl ArrayManager.atr_no_hl.
  >
  >  feat: trade_end_time on BarGenerator, PublicSignal.
  >
  > feat: add window_bar_last for bg.
  >
  > fix: error message too much when trade direction conflicting.
  >
  > fix: MultiSignalsTemplate.get_name_by_pattern.
  >
  > fix: backtest_status convert to int.
  >
  > fix: except (MissMatchParametersError, ValueError) for stats_obj.apply_2_symbol.
  >
  > fix: log of cta -> portfolio wrong on generate_strategy_setting_file.
  >
  > fix: volatility = np.sum(np.abs(close_rolling\[:, 1:] - close_rolling\[:, :-1]), axis=1) on calc_er.
  >
  > fix: np.zeros_like(**, dtype=float).
  >
  > fix: fix_db_bar_datetime on Template.
  >
  > fix: add duplicate data checking on bg.
  >
  > fix: duplicate data load when fix_db_bar_datetime.
  >
  > fix: current_bar_daily if self.bg.window_bar is None.
  >
  > fix: stats_main is None check.
  >
  > refactor: pnl_std2 = net_pnl_gross_s\[net_pnl_gross_s != 0\].std() * 3.
  >
  > refactor: np.seterr(divide='ignore') and pass calculate if no trade.
  >
  > refactor: remove duplicate msg.
  >
  > refactor: add warning log when ignore stg on backtest_all_strategies.
  >
  > refactor: pool_size = cpu_count() if pool_size <= -1 on generate_adj_md.
  >
  > refactor: add OpenDailyTest demo.

* 2021-10-29 v0.8.20211028.1
  > feat: change to new version engine.
  > feat: add bias, kd index on am.
  > feat: add is_same_trade_date.
  > feat: add vt_symbol_multipliers, vt_symbol_rates, vt_symbol_price_ticks for StrategyTemplate.
  > feat: add is_cross_threshold.
  > feat: add set_other_stg_pos on AccountStrategyMapping.bulk_update_position.
  > fix: StrategyBacktestStats.charts_data.
  > fix: self.window > 1 的情况下不允许单根bar形成 window_bar.
  > fix: instrument_id_secondary is None check on FutureAdjFactor.is_main.
  > fix: _target_price = (tick.bid_price_1 if activate else tick.ask_price_1) is zero check.
  > fix: ArrayManager.kd.
  > fix: key_weight_dic is None check.

* 2021-10-22 v0.7.20211022.0

  > feat: add max_strategy_weight on PortfolioOptimization.
  > feat: add StopOpeningPos.stop_opening_long_and_log, StopOpeningPos.stop_opening_short_and_log etc.
  > feat: 禁止跌停做多、禁止涨停做空.
  > feat: write_log(self, msg: str, logger_method='info', *, enable=True, callback=None).
  > fix: backtest_all_strategies pool_size = multiprocessing.cpu_count() if pool_size < 0.
  > fix: vt_symbol_market_value_dic\[vt_symbol] += on PortfolioOptimization.get_vt_symbol_market_value_dic.

* 2021-10-15 v0.7.20211015.1
  > feat: add clear_mapping_first on clone_mapping.
  > feat: add import_save_failed_by_dir.
  > feat: add win_bar_log on PublicSignal.
  > feat: add 边际贡献(%), drawback plot.
  > feat: add matplotlib_use='Qt5Agg' on plot_with_weights.
  > feat: add min max data comments on plot_with_weights.
  > fix: self.interval: str, bg(..., interval=Interval(self.interval)).
  > fix: output trades if _.volume != 0.
  > fix: InstrumentClassificationEnum.All = set(\[get_instrument_type(_) for _ in VT_SYMBOL_LIST_ALL\]).
  > fix: rr_df shape 0 check on fit_and_merge_key_stats_dic.
  > fix: highest_after_entry, lowest_after_entry 0 check on CtaTemplate.
  > fix: df is not None on is_available.
  > fix: super().on_window(bar) on CtaSignal.
  > fix: self.bgs_daily None check.
  > fix: stg_obj is not None and hasattr(stg_obj, 'get_name_by_pattern').
  > fix: ret_key_weight_dic = {k: v for k, v in key_weight_dic.items() if not np.isnan(v)} on PortfolioOptimizationGroupFitting._fit_by_group_keys.
  > fix: check_backtest_status on plot_by_account.
  > fix: win_ratio = profit_days / total_days.
  > fix: vt_symbol_market_value_dic\[vt_symbol] += mv on plot_with_base_position.
  > fix: available_weight_dic on plot_with_weights.
  > refactor: windows->period.

* 2021-09-30 v0.7.20210928.1
  > feat: add check_backtest_status on AccountStrategyMapping.get_stats_by_account, PortfolioOptimization.build_by_account, portfolio_optimization_by_account.
  > feat: add max_low_after_entry_last, min_high_after_entry_last on CtaSignal.
  > feat: ignore_backtest_status_0=True.
  > feat: add ignore_set, only_set, name param on FittingInstrumentGroup.create_by_instrument_classification_enum.
  > feat: add calc_vidya and am.vidya() func.
  > feat: add PortfolioSignal.
  > feat: divide trade into three daily df by direction.
  > feat: add a new calculate_statistics function.
  > feat: add calc_vidya, calc_er, calc_c_factor, calc_ama.
  > feat: add er, c_factor, ama for ArrayManager.
  > feat: add daily_array_size, bar_count_since_entry_dic, bar_count_since_exit_dic, win_bar_count_since_entry_dic, win_bar_count_since_exit_dic, bgs_daily, ams_daily on StrategyTemplate.
  > feat: add daily_spread_array_size, daily_am_spread, on_daily_spread_bar, get_latest_available_bar on TargetPosAndPriceSpreadTemplate.
  > fix: "signal_type" in self._signal_name_kwargs_dic\[signal_name].
  > refactor: add schema_table_names_dic param on backup_db_2_zip_multiprocess.
  > refactor: abstract PublicSignal class.

* 2021-09-24 v0.7.20210924.1
  > feat: add sub_class_name_tail property for f"{''.join(\[_.get_short_name().capitalize() for _ in signal_cls_list\])}{tail_name}".
  > feat: add entry_prices and entry_price for SpreadTemplate.
  > feat: add register_vt_symbol_9999_8888.
  > feat: reorg params_kwargs by make_param_2_str, get_name_by_param.
  > feat: fix_db_bar_datetime = True on MultiSignalsTemplate by default.
  > feat: strategy_class_name available check on get_backtest_status_info_by_images_in_output_stg_dir.
  > fix: attach_file_paths = {k: v for k, v in attach_file_paths.items() if v is not None}.
  > fix: from vnpy_extra.utils.symbol import get_instrument_type.
  > fix: last_bar.high, low, open -> close.
  > fix: self.price_scale = getattr(strategy_obj, "vt_symbol_price_tick", 1).
  > fix: high_price = max(high_price_temp, close_price, open_price, low_price_temp).
  > fix: attr_name = f'{signal_name}_period_enum' on create_cls_by_signals.
  > fix: SymbolsInfo.get_or_create_curr_symbols(vt_symbol, create_if_not_exist=True).
  > fix: market_position 赋值钱判断类型.
  > fix: id_name.endswith(symbol_str) check for get_backtest_status_info_by_images_in_output_stg_dir.
  > refactor: f"{param_name} = '{value}' 该参数用法已经废弃，建议使用 '{signal_param_header}period_enum' 的参数名称".

* 2021-09-17 v0.7.20210918.1
  > feat: get_instance_by_strategy_class_name cal with lock.
  > feat: @lru_cache() get_instance_by_strategy_class_name.
  > feat: add fire_exit_signal, reset_exit_signal on CtaExitSignal.
  > feat: add _close_from_position for reset exit signal status.
  > feat: add get_backtest_status_info_by_images_in_output_stg_dir.
  > feat: add GeneralPeriodEnum.parse_2_values.
  > feat: add ArrayManager.psy.
  > feat: add MultiSignalsTemplate.reset_close_signal_status.
  > feat: add db_bar_datetime_2_real_datetime(db_bar.to_bar()) 修正数据库bar时间为 end_time 导致整体时间错后1分钟的问题，该功能可以通过策略的 fix_db_bar_datetime 参数处罚。考虑到历史兼容性，该参数默认为 False。
  > feat: add exit cta signals.
  > feat: add output_csv on po.
  > feat: add daily info.
  > feat: add max_low_after_entry, min_high_after_entry.
  > feat: add on_spread_bar, on_win_spread_bar, am_spread on TargetPosAndPriceSpreadTemplate template.py.
  > feat: add calculation of close_price, open_price, high_price, low_price, volume, open_interest.
  > feat: group settings on backtest_by_json.
  > feat: add er_ratio on am.
  > fix: IBATS-Utils>=1.20210912.0.
  > fix: getattr(stats, 'freeze_of_account', 0).
  > fix: stats_obj.apply_2_symbol(ignore_no_s_pattern_record=ignore_no_s_pattern_record).
  > fix: None if TypeError in export_table_2_df_io. 
  > fix: on_conflict_ignore on add_2_account.
  > fix: image path and beauty log on update_strategy_stats_by_images_in_output_stg_dir and update_backtest_status_bulk.
  > fix: get_interval_str interval = Interval.MINUTE if interval is None else interval.
  > fix: GBK -> utf-8 for backtest_portfolio.py.
  > fix: change targetPnlSignal.
  > fix: update target_price of MultiSignalsTemplate.
  > fix: self.set_target_pos(target_pos * self.base_position, target_price) on MultiSignalsTemplate.
  > refactor: pos -> market_position.
  > refactor: abstract am.py, bg.py, cta_signal.py.

* 2021-09-10 v0.7.20210910.1
  > feat: add get_base_position_of_account get_stop_opening_pos_of_account get_freeze_of_account of StrategyBacktestStats.
  > feat: ignore not _.get_freeze_of_account() on po.
  > feat: auto calc vt_symbol_market_value_dic if plot_by_account.
  > feat: use fit_and_merge_key_stats_dic on po.
  > feat: add curr_symbol_only on po.
  > feat: copy_freeze on refresh_vt_symbol_2_curr_contract_by_account.
  > feat: add default_backtest_status on backtest funcs.
  > fix: AccountStrategyMapping.freeze == 0 on set_base_position.
  > fix: active_order_close is None error in log.
  > fix: ignore AttributeError error on backtest_all_strategies.
  > fix: cancel_all_stop_orders and cancel_all_close_orders if pos==0 in on_trade callback.
  > fix: short_name = getattr(self, 'short_name_of_account', self.short_name).
  > fix: change lowest_after_entry and highest_after_entry to property.
  > refactor: self.write_log(f"目标仓位方向改变，取消停止单 {self.stop_vt_orderids}", 'debug').
  > refactor: sleep_seconds = 30 by default of AccountStrategyStatusMonitor.

* 2021-09-03 v0.7.20210903.0
  > feat: add param stg_info_duplicate_name_options 默认 DuplicateNameOptions.raise_error。策略重名应对措施。该参数仅在 save_stats == True 的情况下启用.
  > feat: reorg log of po.
  > feat: add plot_by_account.
  > feat: add freeze on AccountStrategyMapping.
  > feat: remove StrategyBacktestStatsKey and add new logic of StrategyBacktestStats.get_stats_key_str.
  > feat: add subplots on plot_with_weights.
  > feat: add StrategyBacktestStats.get_profit_s.
  > feat: .ffill().dropna() for profit_df and balance_df.
  > feat: current_month param on output_monthly_report.
  > feat: add user_name=None, broker_id=None, description=None param on output_report_between.
  > fix: count=0 error.
  > fix: symbol_price_dic None check.
  > fix: to_markdown(index=False).
  > fix: weights too big on equal_mdd.
  > fix: self.send_order_dic.pop(order.vt_orderid) if order.status in (Status.REJECTED, Status.CANCELLED).
  > fix: self.last_tick_time by time_diff_seconds_local_2_server.
  > fix: self.vt_symbol.upper() on indicator_by_trade_dic.
  > refactor: target_pos = self.target_pos.

  ```SQL
  ALTER TABLE `vnpy`.`account_strategy_mapping` ADD COLUMN `freeze` tinyint NOT NULL DEFAULT 0 AFTER `position`;
  ```

* 2021-08-27 v0.7.20210827.1
  > feat: add get_price_tick, get_vt_symbol_rate.
  > feat: add 'pl', 'gross_pl', 'rr', 'gross_rr' on TradeWinLossStatsTuple.
  > feat: add key_base_position_dic on log_contributes, plot_with_base_position.
  > feat: add lp_portfolio_param param on PortfolioOptimization.set_goal, cache get_base_positions.
  > feat: add latest_cancel_response_order on CtaTemplate.
  > feat: new logic of import_cta_strategy_data_json.
  > feat: add portfolio_optimization_by_account.
  > feat: query_latest_position_status(recent_n_days=32).
  > feat: add ignore_old_symbol on generate_strategy_setting_file.
  > feat: add AccountStrategyMapping.clone_mapping.
  > feat: add track_backtest=False, track_backtest_pool=3 on refresh_vt_symbol_2_curr_contract_by_account.
  > feat: add PortfolioOptimizationGroupFitting.
  > feat: back_up database by multiprocess.
  > fix: fix BASE_POSITION 1 on backtest_all_strategies.
  > fix: AccountStrategyMapping.position == 0 on AccountStrategyMapping.delete_if_no_position.
  > fix: target_pos != self.pos and not self.fire_stop_order and not self.stop_opening_pos on TargetPosAndPriceNoStopOrderTemplate.

* 2021-08-20 v0.7.20210819.1
  > feat: add include_holding_pos_stg param on generate_strategy_setting_file.
  > feat: support black_litterman on PortfolioOptimization.
  > feat: add INSTRUMENT_TYPE_MAIN_CONTRACT_DIC.
  > feat: add position on AccountStrategyMapping.
  > feat: add AccountStrategyMapping.bulk_update_position.
  > feat: add AccountStrategyMapping.delete_if_no_position.
  > feat: only optimize curr symbols.
  > feat: add CtaExitSignal.
  > feat: add exit_signal_name_list on MultiSignalsTemplate.
  > feat: add incrementally param on generate_adj_md.
  > feat: add get_by_vt_symbol_short_name.
  > feat: add LatestTickPriceModel.insert_price_by_dic.
  > feat: add import_cta_strategy_data_json.
  > feat: 已经支持每日复权，此前只能按照周级别复权因此需要做跨周处理，此后不再需要.
  > fix: shown_name wrong on refresh_vt_symbol_2_curr_contract_by_account.
  > fix: ignore FileNotFoundError error and log.
  > fix: shown_name is None check on apply_2_symbol.
  > fix: error handler on FileNotFoundError of run_backtest, limit file_name_header.
  > fix: variables.append("trailing_stop_hl_price") if self.trailing_stop_rate != 0.
  > fix: change symbols to curr symbols.
  > fix: StrategyBacktestStats.charts_data.is_null(False) check.
  > fix: error on np.concatenate if no trade.
  > refactor: account_stg.strategy_settings is None check.

  ```sql
  ALTER TABLE `vnpy`.`account_strategy_mapping` ADD COLUMN `position` INT NOT NULL DEFAULT 0 AFTER `shown_name`;
  ```

* 2021-08-13 v0.7.20210813.1
  > feat: add acc_drawback_rate, drawback_rate on CtaTemplate.
  > feat: add SymbolsInfo.get_by_symbol_str.
  > feat: add get_merged_profit_by_json.
  > feat: add AccountStrategyMapping.add_2_account, StrategyBacktestStats.update_backtest_status, StrategyBacktestStats.generate_f_short_name.
  > feat: add add_stg_2_account_by_json.
  > feat: add PortfolioOptimizeGoalEnum.equal_mdd.
  > feat: support optimized with group_weight_dic.
  > feat: plot_with_weights(stats_list, weight_dic: dict, capital=None).
  > feat: add fit_by_group, max_vt_symbol_weight, start_date constrains on PortfolioOptimization.set_goal.
  > feat: add refresh_vt_symbol_2_main_contract.
  > fix: id_name ignore (STOP_OPENING_POS_PARAM, BASE_POSITION, ENABLE_COLLECT_DATA_PARAM).
  > fix: TradeDataModel.query_latest_n_trade_date_list -> TradeDateModel.query_latest_n_trade_date_list.
  > fix: contract_result None check.
  > fix: ALL_VT_SYMBOL_LIST -> VT_SYMBOL_LIST_ALL.
  > refactor: self.enable_collect_data = setting.get(ENABLE_COLLECT_DATA_PARAM, False).
  > refactor: portfolio management -> pm for short.
  > refactor: update_backtest_status -> update_backtest_status_bulk.

* 2021-08-06 v0.7.20210806.1
  > feat: add param output_available_only on backtest_by_json.
  > feat: add plot_with_weights.
  > feat: add AccountStrategyMapping.strategy_settings.alias("strategy_settings_of_account") column.
  > feat: add CtaSignal.pos.
  > feat: add CtaSignal.strategy_parameters.
  > feat: add do_update_base_positions on _test_po.
  > feat: log 最短周期策略 and None check.
  > feat: add check_signal_name_list param at get_signal_params on MultiSignalsTemplate.
  > feat: add exit_price, keep_win_loss_counter, daily_trade_stats_list.
  > feat: add short_name_with_file_name_if_exist param on backtest funcs.
  > feat: add skip_current_week for FutureAdjFactor.is_main. 
  > feat: add until_date for TradeDateModel.get_latest_trade_date.
  > feat: add allow_trade_direction param on TargetPosAndPriceTemplate and TargetPosAndPriceNoStopOrderTemplate.
  > fix: output_available_only=False on backtest_by_json.
  > fix: root_folder_name missing for root_folder_path.
  > fix: ignore duplicate records.
  > fix: return None if self.charts_data is None of get_balance_df.
  > fix: rr_df plot wrong on plot_with_weights.
  > fix: lw_dd_pct, lw_drawdown error if drawdown_s.count() <= 1.
  > fix:[make_param_2_str(_) for key, _ in kwargs.items() if key not in ('module_name', )\] on get_name_by_param.
  > refactor: move STOP_OPENING_POS_PARAM = "stop_opening_pos" ENABLE_COLLECT_DATA_PARAM = "enable_collect_data" to constants.py.
  > refactor: enhance get_interval_str and get_name_by_param.
  > refactor: params_str = '_'.join(\[str(v) for v in signal_name_id_name_dic.values()\]).
  > refactor: ignore_fields='auto'.
  > refactor: move symbol related funcs to utils.symbol.

* 2021-07-30 v0.7.20210730.2
  > feat: add monthly_holding_rr_distributions.
  > feat: add strategy_obj param on CtaSignal.
  > feat: add loss win hist bars on monthly_holding_rr_distributions_dic.
  > feat: backtest_by_json supports bulk_backtest_with_backtest_params_iter.
  > feat: latest_reject_response_order reset to None when on_stop.
  > feat: generate_strategy_setting_file(ignore_stop_opening_pos_stg=True).
  > feat: add PortfolioOptimizeGoalEnum.
  > feat: add param ignore_stop_opening_pos_stg.
  > fix: exchange wrong and name_idx_dic = {v: k for k, v in structure_df\['字段名'\].items()}.
  > fix: msg missing and "【非交易时段】" check if self._is_realtime_mode.
  > fix: handle StrategyInfo.get_strategy_class_by_name(strategy_class_name) error.
  > fix: AttributeError: 'weakref' object has no attribute 'vt_symbol_price_tick'.
  > fix: update AccountStrategyMapping but StrategyBacktestStats.
  > refactor: instrument_type = get_instrument_type(symbol, pattern=PATTERN_INSTRUMENT_TYPE_OPTION, error_log=False).
  > refactor: engine_kwargs\['end'] = end or date.today() of backtest_by_json.
  > refactor: CtaSignal strategy_obj 参数为必输项。

* 2021-07-23 v0.7.20210722.3
  > feat: add replace_vt_symbol_2_curr_contract_of_settings.
  > feat: add file_name_include_symbol.
  > feat: add user_name_broker_id_pair_list on backtest_all_strategies.
  > feat: add get_vt_symbol_multiplier.
  > feat: add AccountStrategyMapping.get_stats_by_account, StrategyBacktestStats.get_stats_key, StrategyBacktestStats.get_stats_key_str, StrategyBacktestStats.get_balance_df.
  > feat: add SymbolsInfo.get_symbol_latest_price_dic StrategyBacktestStats.set_base_position.
  > feat: add stg_list param on generate_strategy_setting_file.
  > feat: add PortfolioOptimize.
  > fix: change shown_name to file_name_header by default.
  > fix: setting file format error.
  > fix: ignore KeyError on pop all traded order.
  > fix: ignore IntegrityError on backup to archive.
  > fix: engine_kwargs\[vt_symbol_key] = engine_kwargs\[vt_symbol_key].split("_") if it's portfolio.
  > fix: .execute() missing on AccountStrategyMapping.insert.
  > fix: set shown_name, short_name if they are not None.
  > fix: z_score_days: typing.Optional\[int\].
  > fix: None check -> unavailable check on cta signal params.
  > fix: add MacpMacmaPrAndFutures2Options demo.
  > fix: add try catch on run_backtesting.
  > refactor: user_name: str = CharField(max_length=50, help_text="用户名") broker_id: str = CharField(max_length=20, help_text="经纪商代码") on AccountStrategyMapping.
  > refactor: numpy>=1.20.3.
  > 重新配置环境:
  ```cmd
  pip uninstall enum34 -y
  pip install "numpy-1.21.0+mkl-cp37-cp37m-win_amd64.whl"
  pip install vnpy_extra-0.7.20210722.1-py3-none-any.whl
  pip install cvxpy==1.1.13
  pip install cvxopt==1.2.6
  opencv_python==4.5.3
  ```

* 2021-07-16 v0.7.20210716.0
  > feat: add start, end on backtest_all_strategies of track_performance.py。\
  > feat: add column visible on strategy_info.
  ```SQL
  ALTER TABLE `vnpy`.`strategy_info` 
  ADD COLUMN `visible` TINYINT NULL DEFAULT 1 AFTER `backtest_folder_path`;
  ```
  > feat: add function backup_tables_2_zips to backup.
  > feat: add generate_setting_json_file on backtest funcs.
  > feat: add gross profits.
  > feat: close -> open if position conflict on report.
  > fix: z_score_days error on macd signal.
  > fix: add indent=4 on json file.
  > refactor: 5 years for track backtest.
  > refactor: add index on bac.
  ```SQL
  ALTER TABLE `vnpy`.`strategy_backtest_stats` 
  DROP INDEX `query_symbol_status` , 
  ADD INDEX `query_symbol_status` (`backtest_status` ASC, `symbols_info_id` ASC, `stg_info_id` ASC) VISIBLE,
  DROP INDEX `query_symbol_available` ,
  ADD INDEX `query_symbol_available` (`available` ASC, `symbols_info_id` ASC, `stg_info_id` ASC) VISIBLE;
  ```

* 2021-07-09 v0.7.20210709.1
  > feat: record_file_name = os.path.join(output_folder, f"{zip_file_name}.txt").
  > feat: add load_days_span on template.py.
  > 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.
  > feat: add send_order_2_target_pos_close_rejected on TargetPosAndPriceNoStopOrderTemplate.
  > feat: ignore target if  target_price<0 on TargetPosAndPriceTemplate.
  > feat: add TargetPosAndPriceFutures2OptionTemplate for futures position -> options positions.、
  > fix: 发现部分情况下 engine_kwargs 中 symbol 与 stats.symbols_info.symbols 不一致，以后者为准.
  > fix: remove AccountStrategyMapping.import_by_settings(settings) from import_from_json_file.
  > fix: ignore 'class_name' in setting.
  > fix: mismatch symbols' target_price should > 0.
  > fix: "target_pos" not in self.variables check on TargetPosAndPriceTemplate of cta.
  > fix: "targets" not in self.variables check on TargetPosAndPriceTemplate of portfolio.
  > refactor: upload records of uploading tick data of options.
  > refactor: add nbconvert==5.6.1.
  > refactor: add logger_name on all cta/portfolio templates.

* 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_bulk.
  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_bulk.
  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_bulk.
  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_bulk.
  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_bulk.
  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_bulk.
  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_bulk logic.
  fix: reduce reject order.
  fix: StrategyBacktestStats.update_backtest_status_bulk.
  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 jupyterlab jupyter_contrib_nbextensions yapf autopep8 nbresuse -i https://pypi.mirrors.ustc.edu.cn/simple
  jupyter contrib nbextension install
  pip install --user jupyter_nbextensions_configurator -i https://pypi.mirrors.ustc.edu.cn/simple
  jupyter nbextensions_configurator enable --user
  ```

* 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 @jupyterlab/toc jupyterlab-execute-time 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

### 2.1.8版本
#### 1. Windows Server 2012 R2 错误提示：
```cmd
This Windows version (6.3.9600) does not support the required bluetooth API. Consider updating to a more recent Windows (10.0.10685 or above).
```
解决方法：
* 第一步：找到vnpy的QT的bin路径，例如我的是：C:\vnstudio\Lib\site-packages\PyQt5\Qt\bin
* 第二步：找到Qt5Bluetooth.dll，并重命名为其它名，例如：Qt5Bluetooth.dll1

