{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "library path: /Users/xuanyidong/Desktop/XAutoDL/lib\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[61704:MainThread](2021-03-22 13:56:38,104) INFO - qlib.Initialization - [config.py:276] - default_conf: client.\n",
      "[61704:MainThread](2021-03-22 13:56:38,106) WARNING - qlib.Initialization - [config.py:291] - redis connection failed(host=127.0.0.1 port=6379), cache will not be used!\n",
      "[61704:MainThread](2021-03-22 13:56:38,680) INFO - qlib.Initialization - [__init__.py:46] - qlib successfully initialized based on client settings.\n",
      "[61704:MainThread](2021-03-22 13:56:38,681) INFO - qlib.Initialization - [__init__.py:47] - data_path=/Users/xuanyidong/.qlib/qlib_data/cn_data\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'class': 'DatasetH',\n",
      " 'kwargs': {'handler': {'class': 'Alpha158',\n",
      "                        'kwargs': {'end_time': '2020-08-01',\n",
      "                                   'fit_end_time': '2014-12-31',\n",
      "                                   'fit_start_time': '2008-01-01',\n",
      "                                   'instruments': 'csi100',\n",
      "                                   'start_time': '2008-01-01'},\n",
      "                        'module_path': 'qlib.contrib.data.handler'},\n",
      "            'segments': {'test': ('2017-01-01', '2020-08-01'),\n",
      "                         'train': ('2008-01-01', '2014-12-31'),\n",
      "                         'valid': ('2015-01-01', '2016-12-31')}},\n",
      " 'module_path': 'qlib.data.dataset'}\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "import qlib\n",
    "import pprint\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "from pathlib import Path\n",
    "\n",
    "__file__ = os.path.dirname(os.path.realpath(\"__file__\"))\n",
    "\n",
    "lib_dir = (Path(__file__).parent / \"..\" / \"lib\").resolve()\n",
    "print(\"library path: {:}\".format(lib_dir))\n",
    "assert lib_dir.exists(), \"{:} does not exist\".format(lib_dir)\n",
    "if str(lib_dir) not in sys.path:\n",
    "    sys.path.insert(0, str(lib_dir))\n",
    "\n",
    "from qlib import config as qconfig\n",
    "from qlib.utils import init_instance_by_config\n",
    "\n",
    "qlib.init(provider_uri='~/.qlib/qlib_data/cn_data', region=qconfig.REG_CN)\n",
    "\n",
    "dataset_config = {\n",
    "            \"class\": \"DatasetH\",\n",
    "            \"module_path\": \"qlib.data.dataset\",\n",
    "            \"kwargs\": {\n",
    "                \"handler\": {\n",
    "                    \"class\": \"Alpha158\",\n",
    "                    \"module_path\": \"qlib.contrib.data.handler\",\n",
    "                    \"kwargs\": {\n",
    "                        \"start_time\": \"2008-01-01\",\n",
    "                        \"end_time\": \"2020-08-01\",\n",
    "                        \"fit_start_time\": \"2008-01-01\",\n",
    "                        \"fit_end_time\": \"2014-12-31\",\n",
    "                        \"instruments\": \"csi100\",\n",
    "                    },\n",
    "                },\n",
    "                \"segments\": {\n",
    "                    \"train\": (\"2008-01-01\", \"2014-12-31\"),\n",
    "                    \"valid\": (\"2015-01-01\", \"2016-12-31\"),\n",
    "                    \"test\": (\"2017-01-01\", \"2020-08-01\"),\n",
    "                },\n",
    "            },\n",
    "        }\n",
    "pprint.pprint(dataset_config)\n",
    "dataset = init_instance_by_config(dataset_config)\n",
    "\n",
    "df_train, df_valid, df_test = dataset.prepare(\n",
    "            [\"train\", \"valid\", \"test\"],\n",
    "            col_set=[\"feature\", \"label\"],\n",
    "            data_key=DataHandlerLP.DK_L,\n",
    "        )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'class': 'DatasetH',\n",
      " 'kwargs': {'handler': {'class': 'Alpha158',\n",
      "                        'kwargs': {'end_time': '2020-08-01',\n",
      "                                   'fit_end_time': '2014-12-31',\n",
      "                                   'fit_start_time': '2008-01-01',\n",
      "                                   'instruments': 'csi300',\n",
      "                                   'start_time': '2008-01-01'},\n",
      "                        'module_path': 'qlib.contrib.data.handler'},\n",
      "            'segments': {'test': ('2017-01-01', '2020-08-01'),\n",
      "                         'train': ('2008-01-01', '2014-12-31'),\n",
      "                         'valid': ('2015-01-01', '2016-12-31')}},\n",
      " 'module_path': 'qlib.data.dataset'}\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[95290:MainThread](2021-03-03 12:18:43,481) INFO - qlib.timer - [log.py:81] - Time cost: 237.911s | Loading data Done\n",
      "[95290:MainThread](2021-03-03 12:18:45,080) INFO - qlib.timer - [log.py:81] - Time cost: 0.465s | DropnaLabel Done\n",
      "[95290:MainThread](2021-03-03 12:18:51,572) INFO - qlib.timer - [log.py:81] - Time cost: 6.491s | CSZScoreNorm Done\n",
      "[95290:MainThread](2021-03-03 12:18:51,573) INFO - qlib.timer - [log.py:81] - Time cost: 8.090s | fit & process data Done\n",
      "[95290:MainThread](2021-03-03 12:18:51,573) INFO - qlib.timer - [log.py:81] - Time cost: 246.003s | Init data Done\n"
     ]
    }
   ],
   "source": [
    "from trade_models.transformations import get_transformer\n",
    "\n",
    "model = get_transformer(None)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}