{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "539cc97c-cfc1-447d-8db3-70f312b634c0",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# 用于将 聚宽平台 下载的数据，导入到本地数据库中\n",
    "\n",
    "通过 `joinquant/数据下载.ipynb` 在 聚宽平台执行，将文件下载到本地，通过以下代码将数据导入到本地数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cbe299f-7a4b-429e-8c5a-4767100f5316",
   "metadata": {
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from chanlun.exchange.exchange_db import ExchangeDB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4578d60a-c2d8-4bb3-9b55-ce1498e9656e",
   "metadata": {
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "ex = ExchangeDB('a')\n",
    "pd_klines = pd.read_csv('./data/stock.csv')\n",
    "len(pd_klines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1f82194c-ce31-4727-8f44-617c64a56587",
   "metadata": {
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "codes = pd_klines['code'].unique()\n",
    "frequencys = pd_klines['frequency'].unique()\n",
    "pd_klines['date'] = pd.to_datetime(pd_klines['date'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7886fff-75a7-4236-ac52-64346ee7f45b",
   "metadata": {
    "tags": [],
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "for code in codes:\n",
    "    for f in frequencys:\n",
    "        klines = pd_klines[pd_klines['code'] == code][pd_klines['frequency'] == f]\n",
    "        print(f'code {code} frequency {f} len {len(klines)}')\n",
    "        ex.insert_klines(code, f, klines)\n",
    "print('Done')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c36d6c3-5042-42cb-8949-b9ea74b476c2",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.7.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}