{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 建立 Pipeline\n", "\n", "Pipeline 幫助使用者於每個交易日整理數據,並計算各種交易所需的指標。在編寫交易策略時,會在 `initialize` 函式中使用 `attach_pipeline` 函數將 Pipeline 導入策略當中,協助我們產出交易所需的指標。\n", "\n", "### zipline.pipeline.Pipeline\n", "\n", "#### Parameters:\n", "* columns: _dict_, optional\n", " columns 為一個將欄位名稱映射到 factors、 filters 或 classifiers 的字典,讓 pipeline 知道要輸出哪些資料\n", "* screen: _zipline.pipeline.Filter_, optional\n", " 用來設定過濾標的條件。\n", "\n", "於此例中,我們首先 ingest 台積電與旺宏股價。" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import tejapi\n", "import pandas as pd\n", "import numpy as np\n", "\n", "import os\n", "os.environ['TEJAPI_BASE'] = 'https://api.tej.com.tw'\n", "os.environ['TEJAPI_KEY'] = 'your key'\n", "\n", "os.environ['ticker'] = '2330 2337'\n", "os.environ['mdate'] = '20170101 20230701'\n", "\n", "from zipline.pipeline import Pipeline\n", "from zipline.pipeline.data import TWEquityPricing" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Merging daily equity files:\n", "Currently used TEJ API key call quota 971/9223372036854775807 (0.0%)\n", "Currently used TEJ API key data quota 55470214/9223372036854775807 (0.0%)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[2024-08-06 09:25:26.772739] INFO: zipline.data.bundles.core: Ingesting tquant.\n", "[2024-08-06 09:25:32.110433] INFO: zipline.data.bundles.core: Ingest tquant successfully.\n" ] } ], "source": [ "!zipline ingest -b tquant" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "接著宣告一個 `make_pipeline`,並設定 Pipeline 的 columns 參數。\n", "\n", "在這邊,我們設定 `columns` 為 `TWEquityPricing` 中的最新收盤價格。`TWEquityPricing` 為價量資料的資料集" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def make_pipeline():\n", " return Pipeline(columns={'close':TWEquityPricing.close.latest})" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "my_pipe = make_pipeline()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 運行 Pipeline\n", "\n", "接著我們使用 `run_pipeline`,`run_pipeline` 僅用於研究,並不用於編寫交易策略。\n", "\n", "觀察 Pipeline 輸出資料,可以發現 Pipeline 會提供每間公司每日的收盤價。由於我們這邊沒有設定 `screen` 與 `mask`,所以總共就是我們前面 ingest 的兩家公司(2330 與 2327)。\n", "\n", "### zipline.TQresearch.tej_pipeline.run_pipeline\n", "\n", "#### Parameters:\n", "* pipeline: _zipline.pipeline.Pipeline_\n", " 已經定義的 pipeline。\n", "* start_date: _str_ or _pd.Timestamp_\n", " 起始日期。\n", "* end_date: _str_ or _pd.Timestamp_\n", " 結束日期。" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "from zipline.TQresearch.tej_pipeline import run_pipeline" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "result = run_pipeline(my_pipe, '2018-01-03', '2022-12-30')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | \n", " | close | \n", "
---|---|---|
2018-01-03 00:00:00+00:00 | \n", "Equity(0 [2330]) | \n", "232.50 | \n", "
Equity(1 [2337]) | \n", "43.60 | \n", "|
2018-01-04 00:00:00+00:00 | \n", "Equity(0 [2330]) | \n", "237.00 | \n", "
Equity(1 [2337]) | \n", "44.35 | \n", "|
2018-01-05 00:00:00+00:00 | \n", "Equity(0 [2330]) | \n", "239.50 | \n", "