{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "hide_input": false,
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'11 / 306'"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-7bb67c4915eb>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m    189\u001b[0m     \u001b[0mstock_pool\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstock_pool\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    190\u001b[0m     \u001b[0mcommission\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcommission\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 191\u001b[1;33m     \u001b[0mplot\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mFalse\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    192\u001b[0m )\n\u001b[0;32m    193\u001b[0m run(\n",
      "\u001b[1;32m<ipython-input-1-7bb67c4915eb>\u001b[0m in \u001b[0;36mrun\u001b[1;34m(**kwargs)\u001b[0m\n\u001b[0;32m    113\u001b[0m         \u001b[0mcerebro\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maddanalyzer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0manalyzers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mPyFolio\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'pyfolio'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    114\u001b[0m         \u001b[0mcerebro\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maddanalyzer\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mbt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0manalyzers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDrawDown\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'drawdown'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 115\u001b[1;33m         \u001b[0mstrategy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcerebro\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    116\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    117\u001b[0m         \u001b[0mpyfolio\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mstrategy\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0manalyzers\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgetbyname\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'pyfolio'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda2\\lib\\site-packages\\backtrader\\cerebro.pyc\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, **kwargs)\u001b[0m\n\u001b[0;32m   1071\u001b[0m             \u001b[1;31m# let's skip process \"spawning\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1072\u001b[0m             \u001b[1;32mfor\u001b[0m \u001b[0miterstrat\u001b[0m \u001b[1;32min\u001b[0m \u001b[0miterstrats\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1073\u001b[1;33m                 \u001b[0mrunstrat\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrunstrategies\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0miterstrat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1074\u001b[0m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrunstrats\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrunstrat\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1075\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda2\\lib\\site-packages\\backtrader\\cerebro.pyc\u001b[0m in \u001b[0;36mrunstrategies\u001b[1;34m(self, iterstrat, predata)\u001b[0m\n\u001b[0;32m   1228\u001b[0m                     \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_runonce_old\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrunstrats\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1229\u001b[0m                 \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1230\u001b[1;33m                     \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_runonce\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrunstrats\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1231\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1232\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mp\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0moldsync\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda2\\lib\\site-packages\\backtrader\\cerebro.pyc\u001b[0m in \u001b[0;36m_runonce\u001b[1;34m(self, runstrats)\u001b[0m\n\u001b[0;32m   1623\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1624\u001b[0m             \u001b[1;32mfor\u001b[0m \u001b[0mstrat\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrunstrats\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1625\u001b[1;33m                 \u001b[0mstrat\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_oncepost\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdt0\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1626\u001b[0m                 \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_event_stop\u001b[0m\u001b[1;33m:\u001b[0m  \u001b[1;31m# stop if requested\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1627\u001b[0m                     \u001b[1;32mreturn\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda2\\lib\\site-packages\\backtrader\\strategy.pyc\u001b[0m in \u001b[0;36m_oncepost\u001b[1;34m(self, dt)\u001b[0m\n\u001b[0;32m    282\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_notify\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    283\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 284\u001b[1;33m         \u001b[0mminperstatus\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_getminperstatus\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    285\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mminperstatus\u001b[0m \u001b[1;33m<\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    286\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mnext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32mC:\\ProgramData\\Anaconda2\\lib\\site-packages\\backtrader\\strategy.pyc\u001b[0m in \u001b[0;36m_getminperstatus\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m    245\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_getminperstatus\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    246\u001b[0m         \u001b[1;31m# check the min period status connected to datas\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 247\u001b[1;33m         \u001b[0mdlens\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0moperator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msub\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_minperiods\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdatas\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    248\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_minperstatus\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mminperstatus\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mmax\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdlens\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    249\u001b[0m         \u001b[1;32mreturn\u001b[0m \u001b[0mminperstatus\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "%cd \"E:\\Project\\backtrader_strategy\"\n",
    "\n",
    "import sys\n",
    "import utils\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import talib\n",
    "import plotly\n",
    "import plotly.offline\n",
    "from plotly.offline import iplot\n",
    "import cufflinks as cf\n",
    "plotly.offline.init_notebook_mode(connected=True)\n",
    "cf.set_config_file(theme=None, sharing='private', offline=True)\n",
    "import plotly.plotly as py\n",
    "import plotly.tools as tls\n",
    "import plotly.graph_objs as go\n",
    "import backtrader as bt\n",
    "import talib\n",
    "import math\n",
    "import qrisk\n",
    "\n",
    "from IPython.display import display, clear_output\n",
    "\n",
    "\n",
    "def get_ts_stock_period_hq_df(stock_id, begin_date_str, end_date_str, cycle='日'):\n",
    "    begin_date = utils.str_to_ts_date(begin_date_str)\n",
    "    end_date = utils.str_to_ts_date(end_date_str)\n",
    "    # 交易所算法，前复权\n",
    "    stock_daily_data = utils.ts_remote_call_func('Stock_PeriodHq', [begin_date, end_date, cycle, 2, 0], {\n",
    "        'StockID': stock_id\n",
    "    })\n",
    "    row_size = len(stock_daily_data)\n",
    "    if row_size == 0:\n",
    "        return None\n",
    "    stock_daily_data.reverse()  # 天软查询接口是倒序，转换成按照时间先后排序，日期小的排前面\n",
    "    return utils.ts_list_to_dataframe(\n",
    "        ts_list=stock_daily_data,\n",
    "        columns=[('datetime', '日期'), ('open', '开盘'), ('close', '收盘'), ('high', '最高'), ('low', '最低'),\n",
    "                 ('p_change', '涨幅(%)'), ('volume', '成交量(手)'), ('turnover', '换手率(%)')],\n",
    "        index_type=pd.DatetimeIndex\n",
    "    )\n",
    "\n",
    "\n",
    "class PeriodHQData(bt.feeds.PandasData):\n",
    "    lines = ('open', 'close', 'high', 'low', 'p_change', 'volume',  'vol_sma5')\n",
    "    params = (\n",
    "        ('datetime', None),\n",
    "        ('open', -1),\n",
    "        ('high', -1),\n",
    "        ('close', -1),\n",
    "        ('low', -1),\n",
    "        ('p_change', -1),\n",
    "        ('volume', -1),\n",
    "        ('vol_sma5', -1),\n",
    "    )\n",
    "    datafields = [x[0] for x in params]\n",
    "\n",
    "    def __init__(self):\n",
    "        super(PeriodHQData, self).__init__()\n",
    "        \n",
    "        \n",
    "class ShortV2(bt.Strategy):\n",
    "    \n",
    "    def __init__(self):\n",
    "        super(ShortV2, self).__init__()\n",
    "        self.last_buy_date = None\n",
    "        self.size = None\n",
    "        \n",
    "    def next_open(self):\n",
    "        # 收盘卖出，隔日买入策略，卖出条件：\n",
    "        # 1、昨日量比 > 1.5，且昨日涨幅> 5%\n",
    "        # 2、今日成交量>昨日成交量 * 90%\n",
    "        # 3、今日涨幅 < 昨日 * 50%，且 没有跌停\n",
    "        \n",
    "        if not self.position:\n",
    "            if len(self.data0) == self.data0.buflen():\n",
    "                return\n",
    "            if math.isnan(self.data0.vol_sma5[-2]):\n",
    "                return\n",
    "\n",
    "            if (self.data0.volume[-1] / self.data0.vol_sma5[-2]) > 1.5 and \\\n",
    "                self.data0.p_change[-1] > 5 and \\\n",
    "                self.data0.volume[0] > self.data0.volume[-1] * 0.9 and \\\n",
    "                -10 < self.data0.p_change[0] < self.data0.p_change[-1] * 0.5:\n",
    "                if self.size is None:\n",
    "                    self.size = int(self.broker.cash / self.data0.close[0])\n",
    "                self.sell(data=self.data0,\n",
    "                          size=self.size,\n",
    "                          price=self.data0.close[0],\n",
    "                          exectype=bt.Order.StopLimit)\n",
    "                \n",
    "        else:\n",
    "            self.close(price=self.data0.open[0], exectype=bt.Order.Limit)\n",
    "\n",
    "total_stat_df = pd.DataFrame()\n",
    "\n",
    "def run(**kwargs):\n",
    "    stock_pool = kwargs['stock_pool']\n",
    "    trade_date_list = utils.get_trade_date_str_list(start_date=kwargs['begin_date'], end_date=kwargs['end_date'])\n",
    "    value_df = pd.DataFrame(index=pd.DatetimeIndex(trade_date_list), columns=stock_pool)\n",
    "    transactions_df = None\n",
    "    global total_stat_df\n",
    "\n",
    "    for cnt, stock in enumerate(stock_pool, start=1):\n",
    "        clear_output(wait=True)\n",
    "        display('%s / %s' % (cnt, len(stock_pool)))\n",
    "        df = get_ts_stock_period_hq_df(stock, kwargs['begin_date'], kwargs['end_date'])\n",
    "        if df is None:\n",
    "            continue\n",
    "        df['vol_sma5'] = talib.SMA(df['volume'].values, timeperiod = 5)\n",
    "        cerebro = bt.Cerebro(cheat_on_open=True, preload=True)\n",
    "        cerebro.adddata(PeriodHQData(dataname=df), name=stock)\n",
    "        cerebro.addstrategy(ShortV2)\n",
    "        cerebro.broker.setcash(kwargs.get('cash', 1000000))\n",
    "        cerebro.broker.setcommission(kwargs.get('commission', 0.0016))\n",
    "        cerebro.addanalyzer(bt.analyzers.PyFolio, _name='pyfolio')\n",
    "        cerebro.addanalyzer(bt.analyzers.DrawDown, _name='drawdown')\n",
    "        strategy = cerebro.run()[0]\n",
    "        \n",
    "        pyfolio = strategy.analyzers.getbyname('pyfolio')\n",
    "        drawdown = strategy.analyzers.getbyname('drawdown')\n",
    "        returns, positions, transactions, gross_lev = pyfolio.get_pf_items()\n",
    "        profit = transactions['value'].sum()\n",
    "        if profit == 0:\n",
    "            continue\n",
    "        value_series = positions.sum(axis=1)\n",
    "        if transactions_df is None:\n",
    "            transactions_df = transactions.copy()\n",
    "        else:\n",
    "            transactions_df = transactions_df.append(transactions)\n",
    "        \n",
    "        for dt in value_series.index:\n",
    "            value_df.loc[dt.date()][stock] = value_series[dt]\n",
    "\n",
    "    # 先往后填充nan，再往前填充nan（补齐开头的nan到初始值）\n",
    "    value_df = value_df.fillna(method='ffill').fillna(method='bfill').dropna(axis=1)\n",
    "    clear_output(wait=True)\n",
    "    cumulative_return_series = value_df.sum(axis=1) / (value_df.columns.size * kwargs.get('cash', 1000000))\n",
    "    non_cumulative_returns_series = cumulative_return_series.diff().dropna()\n",
    "    \n",
    "    stat_df = pd.DataFrame(np.zeros((value_df.columns.size, 2)), \n",
    "                           columns=['succ', 'fail'], \n",
    "                           index=pd.Index(value_df.columns, name='stock'))\n",
    "    \n",
    "    display()\n",
    "\n",
    "    for stock, group in transactions_df.groupby('symbol'):\n",
    "        for idx in range(group.index.size / 2):\n",
    "            if group.iloc[idx * 2]['price'] - group.iloc[idx * 2 + 1]['price'] > 0:\n",
    "                stat_df.loc[stock]['succ'] += 1\n",
    "            else:\n",
    "                stat_df.loc[stock]['fail'] += 1\n",
    "    # 交易次数直方图\n",
    "    cnt_hist_df = stat_df.sum(axis=1)\n",
    "    montly_return_series = qrisk.aggregate_returns(non_cumulative_returns_series, 'monthly')\n",
    "    stat_df = pd.DataFrame(\n",
    "        data={\n",
    "            '最大回撤': ['%.2f%%' % (qrisk.max_drawdown(non_cumulative_returns_series) * 100)],\n",
    "            '股票数目': [value_df.columns.size],\n",
    "            '总交易次数': [stat_df.sum(axis=1).sum()],\n",
    "            '成功次数': [stat_df['succ'].sum()],\n",
    "            '胜率': ['%.2f%%' % (stat_df['succ'].sum() * 100 / stat_df.sum(axis=1).sum())],\n",
    "            '正收益月个数': [montly_return_series[montly_return_series > 0].size],\n",
    "            '正收益日概率': ['%.2f%%' % \n",
    "                       (float((non_cumulative_returns_series[non_cumulative_returns_series > 0].size)) * 100\\\n",
    "                        / float((non_cumulative_returns_series[non_cumulative_returns_series != 0].size)))],\n",
    "            '累计收益': ['%.2f%%' % ((cumulative_return_series[-1] - 1) * 100)],\n",
    "            '夏普比': [qrisk.sharpe_ratio(non_cumulative_returns_series)]\n",
    "        },\n",
    "        index=pd.Index([kwargs['begin_date'] + ' 至 ' + kwargs['end_date']]),\n",
    "        columns=['股票数目', '总交易次数', '成功次数', '胜率', \\\n",
    "                 '正收益日概率', '正收益月个数', '累计收益', \\\n",
    "                 '最大回撤', '夏普比']\n",
    "    )\n",
    "    total_stat_df = total_stat_df.append(stat_df)\n",
    "    if kwargs.get('plot', True):\n",
    "        dimensions = (None, 400)\n",
    "        color = 'rgba(90, 168, 250, 100)'\n",
    "        cumulative_return_series.iplot(dimensions=dimensions, color=color)\n",
    "        non_cumulative_returns_series.iplot(kind='bar', dimensions=dimensions, color=color)\n",
    "        cnt_hist_df.iplot(kind='histogram', dimensions=dimensions, color=color)\n",
    "\n",
    "# stock_pool = utils.get_stock_pool()[:10]\n",
    "stock_pool = utils.get_stock_pool()\n",
    "commission = 0.0008\n",
    "\n",
    "#　夏普比率、月胜率、手续费比\n",
    "\n",
    "run(\n",
    "    begin_date = '2012-01-01',\n",
    "    end_date = '2013-01-01',\n",
    "    stock_pool = stock_pool,\n",
    "    commission = commission,\n",
    "    plot = False\n",
    ")\n",
    "run(\n",
    "    begin_date = '2013-01-01',\n",
    "    end_date = '2014-01-01',\n",
    "    stock_pool = stock_pool,\n",
    "    commission = commission,\n",
    "    plot = False\n",
    ")\n",
    "run(\n",
    "    begin_date = '2014-01-01',\n",
    "    end_date = '2015-01-01',\n",
    "    stock_pool = stock_pool,\n",
    "    commission = commission,\n",
    "    plot = False\n",
    ")\n",
    "run(\n",
    "    begin_date = '2015-01-01',\n",
    "    end_date = '2016-01-01',\n",
    "    stock_pool = stock_pool,\n",
    "    commission = commission,\n",
    "    plot = False\n",
    ")\n",
    "run(\n",
    "    begin_date = '2016-01-01',\n",
    "    end_date = '2017-01-01',\n",
    "    stock_pool = stock_pool,\n",
    "    commission = commission,\n",
    "    plot = True\n",
    ")\n",
    "\n",
    "display(total_stat_df)"
   ]
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
