{
"cells": [
{
"cell_type": "markdown",
"id": "42795695",
"metadata": {},
"source": [
"\n",
"# Pipeline built-in filters\n",
"\n",
"本文介紹常用的內建 `Filters`。"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "44e37ddc",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Merging daily equity files:\n",
"Currently used TEJ API key call quota 126/100000 (0.13%)\n",
"Currently used TEJ API key data quota 872686/10000000 (8.73%)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"[2024-07-09 05:27:50.154669] INFO: zipline.data.bundles.core: Ingesting tquant.\n",
"[2024-07-09 05:27:56.108611] INFO: zipline.data.bundles.core: Ingest tquant successfully.\n"
]
}
],
"source": [
"import os \n",
"import pandas as pd\n",
"import numpy as np \n",
"\n",
"os.environ['TEJAPI_BASE'] = \"https://api.tej.com.tw\"\n",
"os.environ['TEJAPI_KEY'] = \"your key\"\n",
"\n",
"os.environ['ticker'] = \"1101 1301 1303 1802 2002 2101 2303 2317 2330 2337 2382 2388 2451 2454 2603 2881 2885 2890 2903 3711 IR0001\"\n",
"os.environ['mdate'] = '20180101 20220330'\n",
"\n",
"!zipline ingest -b tquant"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "c11a9d52",
"metadata": {},
"outputs": [],
"source": [
"from zipline.data import bundles\n",
"from zipline.pipeline import Pipeline\n",
"from zipline.TQresearch.tej_pipeline import run_pipeline\n",
"from zipline.pipeline.data import TWEquityPricing, TQAltDataSet, TQDataSet\n",
"from zipline.pipeline.factors import *\n",
"from zipline.pipeline.filters import *\n",
"\n",
"start = pd.Timestamp(\"2018-02-06\", tz='utc')\n",
"end = pd.Timestamp(\"2022-02-06\", tz='utc')\n",
"\n",
"bundle = bundles.load('tquant')\n",
"sids = bundle.asset_finder.equities_sids\n",
"assets = bundle.asset_finder.retrieve_all(sids)"
]
},
{
"cell_type": "markdown",
"id": "e155ae12",
"metadata": {},
"source": [
"\n",
" \n",
"### Menu\n",
"* [All](#All)\n",
"* [Any](#Any)\n",
"* [AtLeastN](#AtLeastN)\n",
"* [AllPresent](#AllPresent)\n",
"* [StaticAssets](#StaticAssets)\n",
"* [StaticSids](#StaticSids)\n",
"* [SingleAsset](#SingleAsset)\n",
"* [top/bottom](#top/bottom)\n",
"* [percentile_between](#percentile_between)\n",
"* [if_else](#if_else)"
]
},
{
"cell_type": "markdown",
"id": "bf86a688",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.All\n",
"\n",
"在 n 日內,若一資產每日皆符合條件值,該資產為 True。\n",
"\n",
">### Parameters:\n",
">* inputs _( zipline.pipeline.data.Dataset.Boundcolumn_ or _boolean )_ - 資產價量資訊與條件值。\n",
">* window_length _( int )_ - 決定 n 日。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f4783970",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" ALL | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-02-06 00:00:00+00:00 | \n",
" Equity(0 [1101]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(1 [1301]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(3 [1802]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" False | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2022-01-26 00:00:00+00:00 | \n",
" Equity(16 [2885]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(17 [2890]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(18 [2903]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(19 [3711]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(20 [IR0001]) | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
20340 rows × 1 columns
\n",
"
"
],
"text/plain": [
" ALL\n",
"2018-02-06 00:00:00+00:00 Equity(0 [1101]) False\n",
" Equity(1 [1301]) True\n",
" Equity(2 [1303]) True\n",
" Equity(3 [1802]) False\n",
" Equity(4 [2002]) False\n",
"... ...\n",
"2022-01-26 00:00:00+00:00 Equity(16 [2885]) False\n",
" Equity(17 [2890]) False\n",
" Equity(18 [2903]) False\n",
" Equity(19 [3711]) True\n",
" Equity(20 [IR0001]) True\n",
"\n",
"[20340 rows x 1 columns]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from zipline.pipeline.filters import All\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"ALL\": All(\n",
" inputs = [TWEquityPricing.close.latest > 40], # 設定條件為前一日收盤價 > 40 時為 True\n",
" window_length = 1\n",
" )\n",
" }\n",
" )\n",
"\n",
"run_pipeline(make_pipeline(), start, end)"
]
},
{
"cell_type": "markdown",
"id": "a41f05bb",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.Any\n",
"\n",
"在 n 日內,若一資產任一日符合條件值,該資產為 True。\n",
"\n",
">### Parameters:\n",
">* inputs _( zipline.pipeline.data.Dataset.Boundcolumn_ or _boolean )_ - 資產價量資訊與條件值。\n",
">* window_length _( int )_ - 決定 n 日。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "22d3c62f",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" Any | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-02-06 00:00:00+00:00 | \n",
" Equity(0 [1101]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(1 [1301]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(3 [1802]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" False | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2022-01-26 00:00:00+00:00 | \n",
" Equity(16 [2885]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(17 [2890]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(18 [2903]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(19 [3711]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(20 [IR0001]) | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
20340 rows × 1 columns
\n",
"
"
],
"text/plain": [
" Any\n",
"2018-02-06 00:00:00+00:00 Equity(0 [1101]) False\n",
" Equity(1 [1301]) True\n",
" Equity(2 [1303]) True\n",
" Equity(3 [1802]) False\n",
" Equity(4 [2002]) False\n",
"... ...\n",
"2022-01-26 00:00:00+00:00 Equity(16 [2885]) False\n",
" Equity(17 [2890]) False\n",
" Equity(18 [2903]) False\n",
" Equity(19 [3711]) True\n",
" Equity(20 [IR0001]) True\n",
"\n",
"[20340 rows x 1 columns]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from zipline.pipeline.filters import Any\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"Any\": Any(\n",
" inputs = [TWEquityPricing.close.latest > 40], \n",
" window_length = 10\n",
" )\n",
" }\n",
" )\n",
"\n",
"run_pipeline(make_pipeline(), start, end)"
]
},
{
"cell_type": "markdown",
"id": "f3828abb",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.AtLeastN\n",
"\n",
"在 m 日內,若一資產至少有 n 日符合條件值,該資產為 True。\n",
"\n",
">### Parameters:\n",
">* inputs _( zipline.pipeline.data.Dataset.Boundcolumn_ or _boolean )_ - 資產價量資訊與條件值。\n",
">* window_length _( int )_ - 決定 m 日。\n",
">* N _( int )_ - 決定 n 日。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "36164da0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" AtLeastN | \n",
"
\n",
" \n",
" \n",
" \n",
" 2018-02-06 00:00:00+00:00 | \n",
" Equity(0 [1101]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(1 [1301]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(3 [1802]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" False | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 2022-01-26 00:00:00+00:00 | \n",
" Equity(16 [2885]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(17 [2890]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(18 [2903]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(19 [3711]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(20 [IR0001]) | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
20340 rows × 1 columns
\n",
"
"
],
"text/plain": [
" AtLeastN\n",
"2018-02-06 00:00:00+00:00 Equity(0 [1101]) False\n",
" Equity(1 [1301]) True\n",
" Equity(2 [1303]) True\n",
" Equity(3 [1802]) False\n",
" Equity(4 [2002]) False\n",
"... ...\n",
"2022-01-26 00:00:00+00:00 Equity(16 [2885]) False\n",
" Equity(17 [2890]) False\n",
" Equity(18 [2903]) False\n",
" Equity(19 [3711]) True\n",
" Equity(20 [IR0001]) True\n",
"\n",
"[20340 rows x 1 columns]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from zipline.pipeline.filters import AtLeastN\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"AtLeastN\": AtLeastN(\n",
" inputs = [TWEquityPricing.close.latest > 40],\n",
" window_length = 10,\n",
" N = 2\n",
" )\n",
" }\n",
" )\n",
"\n",
"run_pipeline(make_pipeline(), start, end)"
]
},
{
"cell_type": "markdown",
"id": "dffc1dbc",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.AllPresent\n",
"\n",
"在 n 日內,若每日皆有指定資料,該資產為 True。\n",
"\n",
">### Parameters:\n",
">* inputs _( zipline.pipeline.data.Dataset.Boundcolumn_ or _boolean )_ - 資產價量資訊。\n",
">* window_length _( int )_ - 決定 n 日。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "95c01104",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" AllPresent | \n",
"
\n",
" \n",
" \n",
" \n",
" Equity(0 [1101]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(1 [1301]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(3 [1802]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(5 [2101]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(6 [2303]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(7 [2317]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(8 [2330]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(9 [2337]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(10 [2382]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(11 [2388]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(12 [2451]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(13 [2454]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(14 [2603]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(15 [2881]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(16 [2885]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(17 [2890]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(18 [2903]) | \n",
" True | \n",
"
\n",
" \n",
" Equity(19 [3711]) | \n",
" False | \n",
"
\n",
" \n",
" Equity(20 [IR0001]) | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" AllPresent\n",
"Equity(0 [1101]) True\n",
"Equity(1 [1301]) True\n",
"Equity(2 [1303]) True\n",
"Equity(3 [1802]) True\n",
"Equity(4 [2002]) True\n",
"Equity(5 [2101]) True\n",
"Equity(6 [2303]) True\n",
"Equity(7 [2317]) True\n",
"Equity(8 [2330]) True\n",
"Equity(9 [2337]) True\n",
"Equity(10 [2382]) True\n",
"Equity(11 [2388]) True\n",
"Equity(12 [2451]) True\n",
"Equity(13 [2454]) True\n",
"Equity(14 [2603]) True\n",
"Equity(15 [2881]) True\n",
"Equity(16 [2885]) True\n",
"Equity(17 [2890]) True\n",
"Equity(18 [2903]) True\n",
"Equity(19 [3711]) False\n",
"Equity(20 [IR0001]) True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from zipline.pipeline.filters import AllPresent\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"AllPresent\": AllPresent(\n",
" inputs = [TWEquityPricing.close], \n",
" window_length = 10\n",
" )\n",
" }\n",
" )\n",
"\n",
"run_pipeline(make_pipeline(), start, end).loc[\"2018-05-04\"]\n",
"# 可注意到 3711 在 2018-04-30 才上市,因此 2018-05-04 為 False"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b590e42c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[Equity(0 [1101]),\n",
" Equity(1 [1301]),\n",
" Equity(2 [1303]),\n",
" Equity(3 [1802]),\n",
" Equity(4 [2002]),\n",
" Equity(5 [2101]),\n",
" Equity(6 [2303]),\n",
" Equity(7 [2317]),\n",
" Equity(8 [2330]),\n",
" Equity(9 [2337]),\n",
" Equity(10 [2382]),\n",
" Equity(11 [2388]),\n",
" Equity(12 [2451]),\n",
" Equity(13 [2454]),\n",
" Equity(14 [2603]),\n",
" Equity(15 [2881]),\n",
" Equity(16 [2885]),\n",
" Equity(17 [2890]),\n",
" Equity(18 [2903]),\n",
" Equity(19 [3711]),\n",
" Equity(20 [IR0001])]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 首先抓出所有 bundle 中的股價\n",
"from zipline.data import bundles\n",
"\n",
"bundle = bundles.load('tquant')\n",
"sids = bundle.asset_finder.equities_sids\n",
"assets = bundle.asset_finder.retrieve_all(sids)\n",
"assets"
]
},
{
"cell_type": "markdown",
"id": "17bcacc3",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.StaticAssets\n",
"\n",
"指定特定資產為 True。\n",
"\n",
">### Parameters:\n",
">* assets _( zipline.assets.Asset, iterable )_ - 指定資產。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "894c82f2",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"====================================================================================================\n",
" StaticAssets\n",
"Equity(0 [1101]) False\n",
"Equity(1 [1301]) False\n",
"Equity(2 [1303]) False\n",
"Equity(3 [1802]) False\n",
"Equity(4 [2002]) True\n",
"Equity(5 [2101]) True\n",
"Equity(6 [2303]) True\n",
"Equity(7 [2317]) True\n",
"Equity(8 [2330]) False\n",
"Equity(9 [2337]) False\n",
"Equity(10 [2382]) False\n",
"Equity(11 [2388]) False\n",
"Equity(12 [2451]) False\n",
"Equity(13 [2454]) False\n",
"Equity(14 [2603]) False\n",
"Equity(15 [2881]) False\n",
"Equity(16 [2885]) False\n",
"Equity(17 [2890]) False\n",
"Equity(18 [2903]) False\n",
"Equity(19 [3711]) False\n",
"Equity(20 [IR0001]) False\n"
]
}
],
"source": [
"from zipline.pipeline.filters import StaticAssets\n",
"from zipline import run_algorithm\n",
"from zipline.api import symbol, attach_pipeline, pipeline_output\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"StaticAssets\": StaticAssets(\n",
" assets = assets[4:8]\n",
" )\n",
" }\n",
" )\n",
"\n",
"def initialize(context):\n",
" my_pipe = attach_pipeline(make_pipeline(), 'my_pipe')\n",
" \n",
"def handle_data(context, data):\n",
" pipe = pipeline_output('my_pipe')\n",
" print(\"=\" * 100)\n",
" print(pipe)\n",
"\n",
"def analyze(context, perf):\n",
" pass\n",
"\n",
"results = run_algorithm(\n",
" start = pd.Timestamp('2019-01-02', tz='utc'),\n",
" end = pd.Timestamp('2019-01-02', tz='utc'),\n",
" initialize = initialize,\n",
" capital_base = 1e6,\n",
" handle_data = handle_data,\n",
" analyze = analyze, \n",
" bundle = 'tquant'\n",
")"
]
},
{
"cell_type": "markdown",
"id": "42d04544",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.StaticSids\n",
"\n",
"指定特定資產為 True。\n",
"\n",
">### Parameters:\n",
">* sids _( int, iterable )_ - 指定資產的 sid。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "262ae3f7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"====================================================================================================\n",
" StaticSids\n",
"Equity(0 [1101]) False\n",
"Equity(1 [1301]) False\n",
"Equity(2 [1303]) False\n",
"Equity(3 [1802]) False\n",
"Equity(4 [2002]) True\n",
"Equity(5 [2101]) True\n",
"Equity(6 [2303]) True\n",
"Equity(7 [2317]) True\n",
"Equity(8 [2330]) False\n",
"Equity(9 [2337]) False\n",
"Equity(10 [2382]) False\n",
"Equity(11 [2388]) False\n",
"Equity(12 [2451]) False\n",
"Equity(13 [2454]) False\n",
"Equity(14 [2603]) False\n",
"Equity(15 [2881]) False\n",
"Equity(16 [2885]) False\n",
"Equity(17 [2890]) False\n",
"Equity(18 [2903]) False\n",
"Equity(19 [3711]) False\n",
"Equity(20 [IR0001]) False\n"
]
}
],
"source": [
"from zipline.pipeline.filters import StaticSids\n",
"from zipline import run_algorithm\n",
"from zipline.api import symbol, attach_pipeline, pipeline_output\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"StaticSids\": StaticSids(\n",
" sids = range(4,8)\n",
" )\n",
" }\n",
" )\n",
"\n",
"def initialize(context):\n",
" my_pipe = attach_pipeline(make_pipeline(), 'my_pipe')\n",
" \n",
"def handle_data(context, data):\n",
" pipe = pipeline_output('my_pipe')\n",
" print(\"=\" * 100)\n",
" print(pipe)\n",
"\n",
"def analyze(context, perf):\n",
" pass\n",
"\n",
"results = run_algorithm(\n",
" start = pd.Timestamp('2019-01-01', tz='utc'),\n",
" end = pd.Timestamp('2019-01-02', tz='utc'),\n",
" initialize = initialize,\n",
" capital_base = 1e6,\n",
" handle_data = handle_data,\n",
" analyze = analyze, \n",
" bundle = 'tquant'\n",
")\n"
]
},
{
"cell_type": "markdown",
"id": "ae06f54f",
"metadata": {},
"source": [
"\n",
"\n",
"## zipline.pipeline.filters.SingleAsset\n",
"\n",
"指定單一特定資產為 True。\n",
"\n",
">### Parameters:\n",
">* assets _( zipline.assets.Asset )_ - 指定資產。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "4c4aebe7",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"====================================================================================================\n",
" SingleAsset\n",
"Equity(0 [1101]) False\n",
"Equity(1 [1301]) False\n",
"Equity(2 [1303]) False\n",
"Equity(3 [1802]) False\n",
"Equity(4 [2002]) True\n",
"Equity(5 [2101]) False\n",
"Equity(6 [2303]) False\n",
"Equity(7 [2317]) False\n",
"Equity(8 [2330]) False\n",
"Equity(9 [2337]) False\n",
"Equity(10 [2382]) False\n",
"Equity(11 [2388]) False\n",
"Equity(12 [2451]) False\n",
"Equity(13 [2454]) False\n",
"Equity(14 [2603]) False\n",
"Equity(15 [2881]) False\n",
"Equity(16 [2885]) False\n",
"Equity(17 [2890]) False\n",
"Equity(18 [2903]) False\n",
"Equity(19 [3711]) False\n",
"Equity(20 [IR0001]) False\n"
]
}
],
"source": [
"from zipline.pipeline.filters import SingleAsset\n",
"from zipline import run_algorithm\n",
"from zipline.api import symbol, attach_pipeline, pipeline_output\n",
"\n",
"def make_pipeline():\n",
" return Pipeline(\n",
" columns = {\n",
" \"SingleAsset\": SingleAsset(\n",
" asset = assets[4]\n",
" )\n",
" }\n",
" )\n",
"\n",
"def initialize(context):\n",
" my_pipe = attach_pipeline(make_pipeline(), 'my_pipe')\n",
" \n",
"def handle_data(context, data):\n",
" pipe = pipeline_output('my_pipe')\n",
" print(\"=\" * 100)\n",
" print(pipe)\n",
"\n",
"def analyze(context, perf):\n",
" pass\n",
"\n",
"results = run_algorithm(\n",
" start = pd.Timestamp('2019-01-02', tz='utc'),\n",
" end = pd.Timestamp('2019-01-02', tz='utc'),\n",
" initialize = initialize,\n",
" capital_base = 1e6,\n",
" handle_data = handle_data,\n",
" analyze = analyze, \n",
" bundle = 'tquant'\n",
")"
]
},
{
"cell_type": "markdown",
"id": "02dcc34f-17de-4783-99fb-054b1894b9c6",
"metadata": {},
"source": [
"\n",
"\n",
"## top/bottom\n",
"\n",
"將最大 / 最小的 N 項標為 True,其餘為 False。\n",
"\n",
">### Parameters:\n",
">* N _( int )_ - 數量。\n",
">* mask _( zipline.pipeline.Filter, optional )_ - 預設為無,若加上 mask,僅排名 mask = True 的項目。\n",
">* groupby _( zipline.pipeline.Classifier, optional )_ -\n",
"> * 預設為無。\n",
"> * 必須為 `Classifier`,若給定 `Classifier` 則是每個分類取最大/最小的 N 項。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "markdown",
"id": "b4e8aa6e-6e63-4bb2-8175-1d20e51ea308",
"metadata": {},
"source": [
"### Examples-top\n",
"\n",
"在以下範例中: \n",
"* *sma_quartiles* 將股票依據 SMA 由低至高分成四個級距 (0, 1, 2, 3)\n",
"* *top_beta* 會先篩出平均成交額超過 5 億的股票,再從 4 個 SMA 等級中,各挑 beta 最高的 2 支股票。"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "ca2f4c30-0700-4dc5-9035-005d2fc677d1",
"metadata": {},
"outputs": [],
"source": [
"assets_ex_IR0001 = [i for i in assets if i!= bundle.asset_finder.lookup_symbol('IR0001', as_of_date=None)]\n",
"\n",
"def make_pipeline():\n",
"\n",
"# quartiles\n",
" sma = SimpleMovingAverage(inputs = [TWEquityPricing.close], window_length = 30)\n",
" sma_quartiles = sma.quartiles(mask = StaticAssets(assets_ex_IR0001))\n",
" \n",
"# top \n",
" sbeta = SimpleBeta(target = bundle.asset_finder.lookup_symbol('IR0001', as_of_date=None),\n",
" regression_length = 300,\n",
" allowed_missing_percentage = 0.25)\n",
" \n",
" adv = AverageDollarVolume(window_length = 10)\n",
" top_dollar = adv > 500000000\n",
" top_beta = sbeta.top(N = 2, mask = top_dollar & StaticAssets(assets_ex_IR0001), groupby = sma_quartiles)\n",
" \n",
" return Pipeline(\n",
" columns={\n",
" 'SMA': sma,\n",
" 'SMA Quartile': sma_quartiles,\n",
" 'Average Dollar Volume':adv,\n",
" 'Simple Beta': sbeta,\n",
" 'top_beta': top_beta\n",
" }\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "315f0a96-3be6-463b-aad3-d482b86ba0ac",
"metadata": {},
"source": [
"可以看到在 *top_beta* 欄位中,4 個 SMA 級距各有兩檔股票被標為 True,且平均成交額皆大於 5 億 ( 5e+08 )。"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "19e75435-b7a1-4fc4-a8a4-df2a1d47e0b3",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" Average Dollar Volume | \n",
" SMA Quartile | \n",
" Simple Beta | \n",
" top_beta | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-02-07 00:00:00+00:00 | \n",
" Equity(14 [2603]) | \n",
" 1.386923e+10 | \n",
" 3 | \n",
" 1.920292 | \n",
" True | \n",
"
\n",
" \n",
" Equity(13 [2454]) | \n",
" 5.716348e+09 | \n",
" 3 | \n",
" 1.453462 | \n",
" True | \n",
"
\n",
" \n",
" Equity(7 [2317]) | \n",
" 2.912853e+09 | \n",
" 2 | \n",
" 1.142091 | \n",
" True | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" 6.476843e+08 | \n",
" 2 | \n",
" 0.881953 | \n",
" True | \n",
"
\n",
" \n",
" Equity(6 [2303]) | \n",
" 8.088602e+09 | \n",
" 1 | \n",
" 1.774655 | \n",
" True | \n",
"
\n",
" \n",
" Equity(9 [2337]) | \n",
" 7.098468e+08 | \n",
" 1 | \n",
" 1.380687 | \n",
" True | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" 1.103390e+09 | \n",
" 0 | \n",
" 1.134731 | \n",
" True | \n",
"
\n",
" \n",
" Equity(16 [2885]) | \n",
" 6.144226e+08 | \n",
" 0 | \n",
" 0.843807 | \n",
" True | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Average Dollar Volume \\\n",
"2022-02-07 00:00:00+00:00 Equity(14 [2603]) 1.386923e+10 \n",
" Equity(13 [2454]) 5.716348e+09 \n",
" Equity(7 [2317]) 2.912853e+09 \n",
" Equity(2 [1303]) 6.476843e+08 \n",
" Equity(6 [2303]) 8.088602e+09 \n",
" Equity(9 [2337]) 7.098468e+08 \n",
" Equity(4 [2002]) 1.103390e+09 \n",
" Equity(16 [2885]) 6.144226e+08 \n",
"\n",
" SMA Quartile Simple Beta \\\n",
"2022-02-07 00:00:00+00:00 Equity(14 [2603]) 3 1.920292 \n",
" Equity(13 [2454]) 3 1.453462 \n",
" Equity(7 [2317]) 2 1.142091 \n",
" Equity(2 [1303]) 2 0.881953 \n",
" Equity(6 [2303]) 1 1.774655 \n",
" Equity(9 [2337]) 1 1.380687 \n",
" Equity(4 [2002]) 0 1.134731 \n",
" Equity(16 [2885]) 0 0.843807 \n",
"\n",
" top_beta \n",
"2022-02-07 00:00:00+00:00 Equity(14 [2603]) True \n",
" Equity(13 [2454]) True \n",
" Equity(7 [2317]) True \n",
" Equity(2 [1303]) True \n",
" Equity(6 [2303]) True \n",
" Equity(9 [2337]) True \n",
" Equity(4 [2002]) True \n",
" Equity(16 [2885]) True "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = run_pipeline(make_pipeline(), end, end)\n",
"result.loc[:,['Average Dollar Volume', 'SMA Quartile', 'Simple Beta', 'top_beta']]\\\n",
" [result.top_beta == True].sort_values(['SMA Quartile', 'Simple Beta'], ascending=[False, False])"
]
},
{
"cell_type": "markdown",
"id": "71c1c9fe-662c-4c8d-8d53-32b04f416a90",
"metadata": {},
"source": [
"\n",
"\n",
"## percentile_between\n",
"\n",
"將數值大小介於兩個百分位數(含)之間的資料標為 True,其餘為 False。\n",
"\n",
">### Parameters:\n",
">* min_percentile _( float )_ - 下限,介於 [0.0, 100.0]。\n",
">* max_percentile _( float )_ - 上限,介於 [0.0, 100.0]。\n",
">* mask _( zipline.pipeline.Filter, optional )_ - 預設為無,若加上 mask,僅排名 mask = True 的項目。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "markdown",
"id": "733ce9e5-c767-4b7e-878a-3a740cfccf51",
"metadata": {},
"source": [
"### Examples-percentile_between\n",
"\n",
"在以下範例中:\n",
"```python\n",
"daily_r = DailyReturns()\n",
"top_r = daily_r.percentile_between(min_percentile = 80, max_percentile = 100, mask=StaticAssets(assets_ex_IR0001))\n",
"```\n",
"篩選出日報酬率前 20% 的股票。"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "d8c21c24-905e-48a3-bc78-4a9b37041620",
"metadata": {},
"outputs": [],
"source": [
"def make_pipeline():\n",
"\n",
"# percentile_between \n",
" daily_r = DailyReturns(inputs = [TWEquityPricing.close])\n",
" top_r = daily_r.percentile_between(min_percentile = 80, max_percentile = 100, mask=StaticAssets(assets_ex_IR0001))\n",
" \n",
" return Pipeline(\n",
" columns={\n",
" 'Daily Return': daily_r,\n",
" 'top_r': top_r\n",
" }\n",
" )"
]
},
{
"cell_type": "markdown",
"id": "65941ae6-91da-471d-9565-d29273dc48ae",
"metadata": {},
"source": [
"共有 20 x ( 100% - 80% ) = 4 檔股票被標為 True。"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "e0434a41-471e-4670-8790-3ac473685a86",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" Daily Return | \n",
" top_r | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-02-07 00:00:00+00:00 | \n",
" Equity(9 [2337]) | \n",
" 0.062176 | \n",
" True | \n",
"
\n",
" \n",
" Equity(14 [2603]) | \n",
" 0.027273 | \n",
" True | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" 0.010526 | \n",
" True | \n",
"
\n",
" \n",
" Equity(18 [2903]) | \n",
" 0.009615 | \n",
" True | \n",
"
\n",
" \n",
" Equity(11 [2388]) | \n",
" 0.007519 | \n",
" False | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" 0.005979 | \n",
" False | \n",
"
\n",
" \n",
" Equity(1 [1301]) | \n",
" 0.004785 | \n",
" False | \n",
"
\n",
" \n",
" Equity(12 [2451]) | \n",
" 0.004310 | \n",
" False | \n",
"
\n",
" \n",
" Equity(16 [2885]) | \n",
" 0.003976 | \n",
" False | \n",
"
\n",
" \n",
" Equity(17 [2890]) | \n",
" 0.003049 | \n",
" False | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Daily Return top_r\n",
"2022-02-07 00:00:00+00:00 Equity(9 [2337]) 0.062176 True\n",
" Equity(14 [2603]) 0.027273 True\n",
" Equity(2 [1303]) 0.010526 True\n",
" Equity(18 [2903]) 0.009615 True\n",
" Equity(11 [2388]) 0.007519 False\n",
" Equity(4 [2002]) 0.005979 False\n",
" Equity(1 [1301]) 0.004785 False\n",
" Equity(12 [2451]) 0.004310 False\n",
" Equity(16 [2885]) 0.003976 False\n",
" Equity(17 [2890]) 0.003049 False"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"result = run_pipeline(make_pipeline(), end, end)\n",
"result.loc[:,['Daily Return','top_r']].sort_values(by = 'Daily Return', ascending = False).head(10)"
]
},
{
"cell_type": "markdown",
"id": "2be733d8-d9fd-4e91-ad26-5f8898166277",
"metadata": {},
"source": [
"\n",
"\n",
"## if_else(if_true, if_false)\n",
"\n",
"在 `if_else` 函數前會先給定一個條件,若符合條件則回傳 *if_true* 的值,不符合條件則回傳 *if_false* 的值。\n",
"\n",
">### Parameters:\n",
">* if_true _( zipline.pipeline.term.ComputableTerm )_ - 符合條件回傳的值。\n",
">* if_false _( zipline.pipeline.term.ComputableTerm )_ - 不符合條件回傳的值。\n",
"\n",
"[Go to Menu](#menu)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b43deb8b-1d36-4403-9f22-8c0ea8e8edc1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Currently used TEJ API key call quota 138/100000 (0.14%)\n",
"Currently used TEJ API key data quota 992682/10000000 (9.93%)\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"[2024-07-09 05:28:16.975164] INFO: zipline.data.bundles.core: Ingesting fundamentals.\n",
"[2024-07-09 05:28:34.900705] INFO: zipline.data.bundles.core: Ingest fundamentals successfully.\n"
]
}
],
"source": [
"columns = ['Industry', 'Sub_Industry']\n",
"\n",
"fields = ' '.join(columns)\n",
"os.environ['fields'] = fields\n",
"\n",
"!zipline ingest -b fundamentals"
]
},
{
"cell_type": "markdown",
"id": "35156211-5bb4-42a8-aa71-9b152ae730cb",
"metadata": {},
"source": [
"### Examples - if_else\n",
"\n",
"```python\n",
"ind = TQAltDataSet.Sub_Industry.latest.eq('').if_else(TQAltDataSet.Industry.latest, TQAltDataSet.Sub_Industry.latest)\n",
"```\n",
"此範例的條件為子產業別 ( Sub_Industry ) 是否沒有值,若符合條件則回傳主產業別 ( Industry ),否則回傳子產業別 ( Sub_Industry )。"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "180a6d22-465c-4d72-a07b-54533b82dba2",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" | \n",
" 主產業別 | \n",
" 子產業別 | \n",
" 是否符合條件 | \n",
" 回傳產業 | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-02-07 00:00:00+00:00 | \n",
" Equity(0 [1101]) | \n",
" M1100 水泥工業 | \n",
" | \n",
" True | \n",
" M1100 水泥工業 | \n",
"
\n",
" \n",
" Equity(1 [1301]) | \n",
" M1300 塑膠工業 | \n",
" | \n",
" True | \n",
" M1300 塑膠工業 | \n",
"
\n",
" \n",
" Equity(2 [1303]) | \n",
" M1300 塑膠工業 | \n",
" | \n",
" True | \n",
" M1300 塑膠工業 | \n",
"
\n",
" \n",
" Equity(3 [1802]) | \n",
" M1800 玻璃陶瓷 | \n",
" | \n",
" True | \n",
" M1800 玻璃陶瓷 | \n",
"
\n",
" \n",
" Equity(4 [2002]) | \n",
" M2000 鋼鐵工業 | \n",
" | \n",
" True | \n",
" M2000 鋼鐵工業 | \n",
"
\n",
" \n",
" Equity(5 [2101]) | \n",
" M2100 橡膠工業 | \n",
" | \n",
" True | \n",
" M2100 橡膠工業 | \n",
"
\n",
" \n",
" Equity(6 [2303]) | \n",
" M2300 電子工業 | \n",
" M2324 半導體業 | \n",
" False | \n",
" M2324 半導體業 | \n",
"
\n",
" \n",
" Equity(7 [2317]) | \n",
" M2300 電子工業 | \n",
" M2331 其他電子業 | \n",
" False | \n",
" M2331 其他電子業 | \n",
"
\n",
" \n",
" Equity(8 [2330]) | \n",
" M2300 電子工業 | \n",
" M2324 半導體業 | \n",
" False | \n",
" M2324 半導體業 | \n",
"
\n",
" \n",
" Equity(9 [2337]) | \n",
" M2300 電子工業 | \n",
" M2324 半導體業 | \n",
" False | \n",
" M2324 半導體業 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" 主產業別 子產業別 是否符合條件 \\\n",
"2022-02-07 00:00:00+00:00 Equity(0 [1101]) M1100 水泥工業 True \n",
" Equity(1 [1301]) M1300 塑膠工業 True \n",
" Equity(2 [1303]) M1300 塑膠工業 True \n",
" Equity(3 [1802]) M1800 玻璃陶瓷 True \n",
" Equity(4 [2002]) M2000 鋼鐵工業 True \n",
" Equity(5 [2101]) M2100 橡膠工業 True \n",
" Equity(6 [2303]) M2300 電子工業 M2324 半導體業 False \n",
" Equity(7 [2317]) M2300 電子工業 M2331 其他電子業 False \n",
" Equity(8 [2330]) M2300 電子工業 M2324 半導體業 False \n",
" Equity(9 [2337]) M2300 電子工業 M2324 半導體業 False \n",
"\n",
" 回傳產業 \n",
"2022-02-07 00:00:00+00:00 Equity(0 [1101]) M1100 水泥工業 \n",
" Equity(1 [1301]) M1300 塑膠工業 \n",
" Equity(2 [1303]) M1300 塑膠工業 \n",
" Equity(3 [1802]) M1800 玻璃陶瓷 \n",
" Equity(4 [2002]) M2000 鋼鐵工業 \n",
" Equity(5 [2101]) M2100 橡膠工業 \n",
" Equity(6 [2303]) M2324 半導體業 \n",
" Equity(7 [2317]) M2331 其他電子業 \n",
" Equity(8 [2330]) M2324 半導體業 \n",
" Equity(9 [2337]) M2324 半導體業 "
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def make_pipeline():\n",
"\n",
" Industry = TQAltDataSet.Industry.latest\n",
" Sub_Industry = TQAltDataSet.Sub_Industry.latest\n",
" check = TQAltDataSet.Sub_Industry.latest.eq('')\n",
" ind = TQAltDataSet.Sub_Industry.latest.eq('').if_else(TQAltDataSet.Industry.latest, TQAltDataSet.Sub_Industry.latest)\n",
" \n",
" return Pipeline(\n",
" columns={\n",
" '主產業別': Industry,\n",
" '子產業別': Sub_Industry,\n",
" '是否符合條件': check,\n",
" '回傳產業': ind\n",
" }\n",
" )\n",
"\n",
"run_pipeline(make_pipeline(), end, end).head(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e276fffd-eb56-4623-bedb-867fb68548b7",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:zipline-tej] *",
"language": "python",
"name": "conda-env-zipline-tej-py"
},
"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.10.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}