{
"cells": [
{
"cell_type": "markdown",
"id": "2ad82a80",
"metadata": {},
"source": [
"# 下單方法介紹(二)\n",
"\n",
"\n",
"# Zipline order_value 和 order_target_value\n",
"\n",
"> ## Zipline有六種下單函數:\n",
"\torder():購買指定股數。\n",
"\torder_value():購買指定價值的股票。\n",
"\torder_percent():購買指定價值(整個投資組合價值(portfolio value)的一個特定比例)的股票。\n",
"\torder_target():交易直到該股票帳上總股數達到指定數量為止。\n",
" order_target_value():交易到帳上該股票價值達到指定價值為止。\n",
" order_target_percent():將股票在投資組合的比重調整到指定的比例。\n",
">\n",
"> ## 本篇將會介紹`order_value()`以及`order_target_value()`的使用辦法。\n",
"> 本文件包含以下兩個部份:\n",
" > - [函數介紹](#函數介紹)\n",
" > - [範例:order_value與order_target_value](#Order_value與Order_target_value)\n",
">\n",
">\n",
"> ## 閱讀本篇之前請先閱讀:\n",
">\n",
"> [Zipline Order (order & order_target).ipynb](https://github.com/tejtw/TQuant-Lab/blob/main/lecture/Zipline%20Order%20(order%20%26%20order_target).ipynb)\n",
"> ## 閱讀本篇之後可以搭配閱讀:\n",
"> \n",
"> [Zipline Order(percent & target_percent).ipynb](https://github.com/tejtw/TQuant-Lab/blob/main/lecture/Zipline%20Order%20(percent%20%26%20target_percent).ipynb)"
]
},
{
"cell_type": "markdown",
"id": "d8683ce0",
"metadata": {},
"source": [
"\n",
"\n",
"## 函數介紹:\n",
"\n",
"使用前記得先做 import:`from zipline.api import order_value, order_target_value`或`from zipline.api import *`。\n",
"\n",
"
\n",
"\n",
"* order_value:買賣指定價值的股票。\n",
"\n",
"`zipline.api.order_value(asset, value, limit_price=None, stop_price=None, style=None)`\n",
"\n",
"**value**:*float*股票價值\n",
"\n",
"* order_target_value:透過買/賣使手上股票在**下單當下**的市場價值達到指定目標。\n",
"\n",
"`zipline.api.order_target_value(asset, target, limit_price=None, stop_price=None, style=None)`\n",
"\n",
"**target**:*float*目標價值\n",
"\n",
"---\n",
"\n",
"### 交易機制:\n",
"- 市價皆為**收盤價**。\n",
"- 因zipline交易機制的關係,下單後會到next bar才成交(**也就是今天下單,下一交易日才成交**),所以`stop_price`及`limit_price`皆是和**下一個交易日的收盤價**做比較。\n",
"- `order_value`與`order_target_value`運作邏輯類似,都是以**下單日的收盤價**做基準,將下單的價值**轉換為 amount**。實際交易時再用**當天的收盤價**,和**下單時算出的 amount 成交**,所以**下單時指定的價值不一定會與實際成交時價值一致**。\n",
"- 與其他 order 系列函數相同,下單時算出的 amount 若有**小數點**,則會**取整數**後再進行下單。取整數的方法為:若股數和最近整數相差在 0.0001 以內,就取最接近整數,否則直接去掉小數(3.9999 -> 4.0 ; 5.5 -> 5.0 ; -5.5 -> -5.0)。\n",
"- 若遇到**股票分割、股票股利等股數變動**情形:\n",
" - 在**除權日之前**下的單,`stop_price`及`limit_price`會在**除權日**進行調整。\n",
" - 新的`stop_price`及`limit_price`都是**原本數值乘以 ratio(也就是僅除權的調整係數)** 並 round 到小數以下第二位。(**僅除權的調整係數**使用 TEJ API 的 TWN/APIPRCD(交易資料-股價資料)中的 adjfac_a 欄位進行計算)\n",
" \n",
"### 規則補充:\n",
"- order 系列函數通常在`handle_data`階段使用且不得在`before_trading_start`階段使用。\n",
"- 當使用 limit order、stop order、stop limit order 等**條件單(contingent order)**時,會產生額外的交易成本(**機會成本 opportunity cost**),因這些條件單的使用可能會影響潛在利潤。order 系列函數都提供相關功能可以模擬該成本。"
]
},
{
"cell_type": "markdown",
"id": "fb6a8ba0",
"metadata": {},
"source": [
"## 設定環境"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "660e446d",
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
"import datetime\n",
"import tejapi\n",
"import time\n",
"import os\n",
"import warnings\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"# tej_key\n",
"tej_key ='your key'\n",
"tejapi.ApiConfig.api_key = tej_key\n",
"os.environ['TEJAPI_BASE'] = \"https://api.tej.com.tw\"\n",
"os.environ['TEJAPI_KEY'] = tej_key\n",
"\n",
"# date\n",
"# set date\n",
"start='2022-09-16'\n",
"end='2022-10-16'\n",
"\n",
"os.environ['mdate'] = '20220916 20221016'\n",
"\n",
"tz = 'UTC'\n",
"start_dt, end_dt = pd.Timestamp(start, tz = tz), pd.Timestamp(end, tz = tz)\n",
"\n",
"# calendar\n",
"calendar_name='TEJ'\n",
"\n",
"# bundle_name\n",
"bundle_name = 'tquant'\n",
"\n",
"os.environ['ticker'] = '2603 IR0001'"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "0ed7119e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Merging daily equity files:\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"[2023-11-30 07:51:09.233092] INFO: zipline.data.bundles.core: Ingesting tquant.\n"
]
}
],
"source": [
"!zipline ingest -b tquant"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "64b924fa",
"metadata": {},
"outputs": [],
"source": [
"from zipline.api import *\n",
"from zipline import run_algorithm\n",
"from zipline.finance import commission, slippage\n",
"from zipline.utils.calendar_utils import get_calendar\n",
"\n",
"from zipline.utils.run_algo import get_transaction_detail"
]
},
{
"cell_type": "markdown",
"id": "01c5b164",
"metadata": {},
"source": [
"\n",
"## 範例:order_value與order_target_value\n",
"\n",
"[Return to Menu](#menu)"
]
},
{
"cell_type": "markdown",
"id": "a296b050",
"metadata": {},
"source": [
"### 交易策略\n",
"\n",
"#### def handle_data(context, data):\n",
"\n",
"在回測的第一個時間點(context.i 為 0,2022-09-16),購買`context.asset`中每檔股票,每檔價值皆為 40000 元。\n",
"```python\n",
"if context.i == 0:\n",
" for asset in context.asset:\n",
" order_value(asset, 40000)\n",
"```\n",
"\n",
"在回測的第十一個時間點(context.i 為 10,2022-09-30),對投資組合中的每檔股票進行調整,使其帳上持有的價值達到 60000 元。\n",
"```python\n",
"if context.i == 10:\n",
" for asset in context.asset:\n",
" order_target_value(asset, 60000)\n",
"```\n",
"在回測的第十六個時間點(context.i 為 15,2022-10-07),對投資組合中的每檔股票進行調整,使其帳上持有的價值減少到 30000 元。\n",
"```python\n",
"if context.i == 15:\n",
" for asset in context.asset:\n",
" order_target_value(asset, 30000)\n",
"```\n",
"最後,使用 record 函數記錄每檔股票的收盤價。然後遞增 context.i 的值,表示回測已進入下一個時間點。 \n",
"```python\n",
"record(close=data.current(context.asset, 'close'))\n",
"context.i += 1\n",
"```\n",
"記錄每檔股票的收盤價,並將 `context.i` 遞增 1,表示回測進入下一個時間點。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "8594bfe9",
"metadata": {},
"outputs": [],
"source": [
"def initialize(context):\n",
" context.i = 0\n",
" context.tickers = ['2603']\n",
" context.asset = [symbol(ticker) for ticker in context.tickers] \n",
" set_slippage(slippage.FixedSlippage(spread=0.00))\n",
" set_commission(commission.PerDollar(cost=commission_cost))\n",
" set_benchmark(symbol('IR0001'))\n",
" \n",
"def handle_data(context, data):\n",
" \n",
" if context.i == 0: # 2022-09-16\n",
" for asset in context.asset:\n",
" order_value(asset, 40000)\n",
" \n",
" if context.i == 10: # 2022-09-30\n",
" for asset in context.asset:\n",
" order_target_value(asset, 60000)\n",
"\n",
" if context.i == 15: # 2022-10-07\n",
" for asset in context.asset:\n",
" order_target_value(asset, 30000)\n",
" \n",
" record(close=data.current(context.asset, 'close'))\n",
" context.i += 1\n",
"\n",
"commission_cost = 0.001425 + 0.003 / 2 # commission + tax \n",
"capital_base = 1e6"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9531ef30",
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"closing_price = tejapi.fastget('TWN/APIPRCD',\n",
" coid=['2603'],\n",
" opts={'columns':['mdate','coid','close_d']},\n",
" mdate={'gte':start_dt,'lte':end_dt },\n",
" paginate=True)\n",
"\n",
"performance = run_algorithm(start=start_dt,\n",
" end=end_dt,\n",
" initialize=initialize,\n",
" handle_data=handle_data,\n",
" capital_base=capital_base,\n",
" trading_calendar=get_calendar(calendar_name),\n",
" bundle=bundle_name)\n",
"\n",
"positions, transactions, orders = get_transaction_detail(performance)"
]
},
{
"cell_type": "markdown",
"id": "3d650429",
"metadata": {},
"source": [
"## 9/16\n",
"- 在9/16時下單買進價值 40000 元的長榮(2603)股票,用9/16的收盤價計算出應買股數為 40000 / 80.8 $\\approx$ 495。\n",
"- 下個交易日(9/19)遇到減資,ratio是 2.314356,所以下單量調整成 495 / 2.314356 $\\approx$ 213,並以當天收盤價 169 元成交。"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "13ddf528",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" mdate | \n",
" coid | \n",
" close_d | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2022-09-16 | \n",
" 2603 | \n",
" 80.8 | \n",
"
\n",
" \n",
" 1 | \n",
" 2022-09-19 | \n",
" 2603 | \n",
" 169.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" mdate coid close_d\n",
"0 2022-09-16 2603 80.8\n",
"1 2022-09-19 2603 169.0"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"closing_price.query('mdate <= \"2022-09-19\"')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "e0880a8e",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sid | \n",
" symbol | \n",
" id | \n",
" dt | \n",
" reason | \n",
" created | \n",
" amount | \n",
" filled | \n",
" commission | \n",
" stop | \n",
" limit | \n",
" stop_reached | \n",
" limit_reached | \n",
" asset | \n",
" status | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-09-16 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" df7703877ef749c9af6551dd24992a8b | \n",
" 2022-09-16 13:30:00+08:00 | \n",
" None | \n",
" 2022-09-16 13:30:00+08:00 | \n",
" 495 | \n",
" 0 | \n",
" 0.000000 | \n",
" None | \n",
" None | \n",
" False | \n",
" False | \n",
" Equity(0 [2603]) | \n",
" 0 | \n",
"
\n",
" \n",
" 2022-09-19 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" df7703877ef749c9af6551dd24992a8b | \n",
" 2022-09-19 13:30:00+08:00 | \n",
" None | \n",
" 2022-09-16 13:30:00+08:00 | \n",
" 213 | \n",
" 213 | \n",
" 105.291225 | \n",
" None | \n",
" None | \n",
" False | \n",
" False | \n",
" Equity(0 [2603]) | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sid symbol id \\\n",
"2022-09-16 13:30:00+08:00 0 2603 df7703877ef749c9af6551dd24992a8b \n",
"2022-09-19 13:30:00+08:00 0 2603 df7703877ef749c9af6551dd24992a8b \n",
"\n",
" dt reason \\\n",
"2022-09-16 13:30:00+08:00 2022-09-16 13:30:00+08:00 None \n",
"2022-09-19 13:30:00+08:00 2022-09-19 13:30:00+08:00 None \n",
"\n",
" created amount filled \\\n",
"2022-09-16 13:30:00+08:00 2022-09-16 13:30:00+08:00 495 0 \n",
"2022-09-19 13:30:00+08:00 2022-09-16 13:30:00+08:00 213 213 \n",
"\n",
" commission stop limit stop_reached \\\n",
"2022-09-16 13:30:00+08:00 0.000000 None None False \n",
"2022-09-19 13:30:00+08:00 105.291225 None None False \n",
"\n",
" limit_reached asset status \n",
"2022-09-16 13:30:00+08:00 False Equity(0 [2603]) 0 \n",
"2022-09-19 13:30:00+08:00 False Equity(0 [2603]) 1 "
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"orders.loc['2022-09-16':'2022-09-19']"
]
},
{
"cell_type": "markdown",
"id": "3f0df68a",
"metadata": {},
"source": [
"## 9/30\n",
"- 9/30利用 order_target_value 下單,希望將 2603 的持股價值調整為 60000 元。\n",
"- 首先計算9/30帳上長榮的市值 146(last_sale_price)* 213股 = 31098,所以還需要 ( 60000 - 31098 ) / 146 $\\approx$ 197股(無條件捨去)。"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "8a4ce92a",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sid | \n",
" symbol | \n",
" asset | \n",
" amount | \n",
" cost_basis | \n",
" last_sale_price | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-09-30 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" Equity(0 [2603]) | \n",
" 213 | \n",
" 169.494325 | \n",
" 146.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sid symbol asset amount cost_basis \\\n",
"2022-09-30 13:30:00+08:00 0 2603 Equity(0 [2603]) 213 169.494325 \n",
"\n",
" last_sale_price \n",
"2022-09-30 13:30:00+08:00 146.0 "
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"positions.loc['2022-09-30']"
]
},
{
"cell_type": "markdown",
"id": "2fdfa0b3",
"metadata": {},
"source": [
"下一個交易日(10/3)成交。"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "b5b6cdd1",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sid | \n",
" symbol | \n",
" id | \n",
" dt | \n",
" reason | \n",
" created | \n",
" amount | \n",
" filled | \n",
" commission | \n",
" stop | \n",
" limit | \n",
" stop_reached | \n",
" limit_reached | \n",
" asset | \n",
" status | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-09-30 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" 81259ca0671a45a9b447e597843195f6 | \n",
" 2022-09-30 13:30:00+08:00 | \n",
" None | \n",
" 2022-09-30 13:30:00+08:00 | \n",
" 197 | \n",
" 0 | \n",
" 0.000000 | \n",
" None | \n",
" None | \n",
" False | \n",
" False | \n",
" Equity(0 [2603]) | \n",
" 0 | \n",
"
\n",
" \n",
" 2022-10-03 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" 81259ca0671a45a9b447e597843195f6 | \n",
" 2022-10-03 13:30:00+08:00 | \n",
" None | \n",
" 2022-09-30 13:30:00+08:00 | \n",
" 197 | \n",
" 197 | \n",
" 86.145637 | \n",
" None | \n",
" None | \n",
" False | \n",
" False | \n",
" Equity(0 [2603]) | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sid symbol id \\\n",
"2022-09-30 13:30:00+08:00 0 2603 81259ca0671a45a9b447e597843195f6 \n",
"2022-10-03 13:30:00+08:00 0 2603 81259ca0671a45a9b447e597843195f6 \n",
"\n",
" dt reason \\\n",
"2022-09-30 13:30:00+08:00 2022-09-30 13:30:00+08:00 None \n",
"2022-10-03 13:30:00+08:00 2022-10-03 13:30:00+08:00 None \n",
"\n",
" created amount filled \\\n",
"2022-09-30 13:30:00+08:00 2022-09-30 13:30:00+08:00 197 0 \n",
"2022-10-03 13:30:00+08:00 2022-09-30 13:30:00+08:00 197 197 \n",
"\n",
" commission stop limit stop_reached \\\n",
"2022-09-30 13:30:00+08:00 0.000000 None None False \n",
"2022-10-03 13:30:00+08:00 86.145637 None None False \n",
"\n",
" limit_reached asset status \n",
"2022-09-30 13:30:00+08:00 False Equity(0 [2603]) 0 \n",
"2022-10-03 13:30:00+08:00 False Equity(0 [2603]) 1 "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"orders.loc['2022-09-30':'2022-10-03']"
]
},
{
"cell_type": "markdown",
"id": "eb573fc3",
"metadata": {},
"source": [
"## 10/7\n",
"- 在10/7利用order_target_value()下單,希望將 2603 的持股價值調整為 30000 元。因為帳上長榮股票市值超過 30000,需要賣掉一定數量來達到目標。\n",
"- 用10/7的收盤價和帳上持有數量,計算出價值為 155.5(last_sale_price) * 410 = 63755,需要賣掉 ( 63755 - 30000 ) / 155.5 $\\approx$ 217股(無條件捨去)。"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "33c3c5bf",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sid | \n",
" symbol | \n",
" asset | \n",
" amount | \n",
" cost_basis | \n",
" last_sale_price | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-10-07 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" Equity(0 [2603]) | \n",
" 410 | \n",
" 160.097407 | \n",
" 155.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sid symbol asset amount cost_basis \\\n",
"2022-10-07 13:30:00+08:00 0 2603 Equity(0 [2603]) 410 160.097407 \n",
"\n",
" last_sale_price \n",
"2022-10-07 13:30:00+08:00 155.5 "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"positions.loc['2022-10-07']"
]
},
{
"cell_type": "markdown",
"id": "99d1ac05",
"metadata": {},
"source": [
"- 在下個交易日(10/11),以當天收盤價 156 賣出"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "775e7018",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" sid | \n",
" symbol | \n",
" id | \n",
" dt | \n",
" reason | \n",
" created | \n",
" amount | \n",
" filled | \n",
" commission | \n",
" stop | \n",
" limit | \n",
" stop_reached | \n",
" limit_reached | \n",
" asset | \n",
" status | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-10-07 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" 4321592d8ad044e3948c70129676e278 | \n",
" 2022-10-07 13:30:00+08:00 | \n",
" None | \n",
" 2022-10-07 13:30:00+08:00 | \n",
" -217 | \n",
" 0 | \n",
" 0.0000 | \n",
" None | \n",
" None | \n",
" False | \n",
" False | \n",
" Equity(0 [2603]) | \n",
" 0 | \n",
"
\n",
" \n",
" 2022-10-11 13:30:00+08:00 | \n",
" 0 | \n",
" 2603 | \n",
" 4321592d8ad044e3948c70129676e278 | \n",
" 2022-10-11 13:30:00+08:00 | \n",
" None | \n",
" 2022-10-07 13:30:00+08:00 | \n",
" -217 | \n",
" -217 | \n",
" 99.0171 | \n",
" None | \n",
" None | \n",
" False | \n",
" False | \n",
" Equity(0 [2603]) | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" sid symbol id \\\n",
"2022-10-07 13:30:00+08:00 0 2603 4321592d8ad044e3948c70129676e278 \n",
"2022-10-11 13:30:00+08:00 0 2603 4321592d8ad044e3948c70129676e278 \n",
"\n",
" dt reason \\\n",
"2022-10-07 13:30:00+08:00 2022-10-07 13:30:00+08:00 None \n",
"2022-10-11 13:30:00+08:00 2022-10-11 13:30:00+08:00 None \n",
"\n",
" created amount filled \\\n",
"2022-10-07 13:30:00+08:00 2022-10-07 13:30:00+08:00 -217 0 \n",
"2022-10-11 13:30:00+08:00 2022-10-07 13:30:00+08:00 -217 -217 \n",
"\n",
" commission stop limit stop_reached \\\n",
"2022-10-07 13:30:00+08:00 0.0000 None None False \n",
"2022-10-11 13:30:00+08:00 99.0171 None None False \n",
"\n",
" limit_reached asset status \n",
"2022-10-07 13:30:00+08:00 False Equity(0 [2603]) 0 \n",
"2022-10-11 13:30:00+08:00 False Equity(0 [2603]) 1 "
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"orders.query('created.dt.strftime(\"%Y-%m-%d\") == \"2022-10-07\"')"
]
},
{
"cell_type": "markdown",
"id": "5f4d7e9b",
"metadata": {},
"source": [
"### Exposure:\n",
"- 在 performance (`run_algorithm` 產出的報表)中的 `long_exposure` (帳上股票長部位市值)可以看到,在10/3第一次調整,目標是 6 萬元,但因為10/3市價相對於下單日(9/30)提高了,然而還是會用9/30算出來的 amount 去買,因此 long_exposure 超出了目標。\n",
"- 同理,在10/11第二次調整,目標是 3 萬,但因為10/11市價相對於下單日(10/7)提高了,然而還是會用10/7算出來的 amount 去買,因此 long_exposure 也超出了目標。"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "0d38805b",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" period_open | \n",
" long_exposure | \n",
"
\n",
" \n",
" \n",
" \n",
" 2022-10-03 13:30:00+08:00 | \n",
" 2022-10-03 09:01:00+08:00 | \n",
" 61295.0 | \n",
"
\n",
" \n",
" 2022-10-11 13:30:00+08:00 | \n",
" 2022-10-11 09:01:00+08:00 | \n",
" 30108.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" period_open long_exposure\n",
"2022-10-03 13:30:00+08:00 2022-10-03 09:01:00+08:00 61295.0\n",
"2022-10-11 13:30:00+08:00 2022-10-11 09:01:00+08:00 30108.0"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"performance[['period_open','long_exposure']].query('period_open.dt.strftime(\"%Y-%m-%d\") in [\"2022-10-03\",\"2022-10-11\"]')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "311c5384",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" mdate | \n",
" coid | \n",
" close_d | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 2022-09-16 | \n",
" 2603 | \n",
" 80.8 | \n",
"
\n",
" \n",
" 1 | \n",
" 2022-09-19 | \n",
" 2603 | \n",
" 169.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 2022-09-20 | \n",
" 2603 | \n",
" 172.5 | \n",
"
\n",
" \n",
" 3 | \n",
" 2022-09-21 | \n",
" 2603 | \n",
" 166.5 | \n",
"
\n",
" \n",
" 4 | \n",
" 2022-09-22 | \n",
" 2603 | \n",
" 156.0 | \n",
"
\n",
" \n",
" 5 | \n",
" 2022-09-23 | \n",
" 2603 | \n",
" 158.0 | \n",
"
\n",
" \n",
" 6 | \n",
" 2022-09-26 | \n",
" 2603 | \n",
" 145.5 | \n",
"
\n",
" \n",
" 7 | \n",
" 2022-09-27 | \n",
" 2603 | \n",
" 153.5 | \n",
"
\n",
" \n",
" 8 | \n",
" 2022-09-28 | \n",
" 2603 | \n",
" 144.0 | \n",
"
\n",
" \n",
" 9 | \n",
" 2022-09-29 | \n",
" 2603 | \n",
" 144.0 | \n",
"
\n",
" \n",
" 10 | \n",
" 2022-09-30 | \n",
" 2603 | \n",
" 146.0 | \n",
"
\n",
" \n",
" 11 | \n",
" 2022-10-03 | \n",
" 2603 | \n",
" 149.5 | \n",
"
\n",
" \n",
" 12 | \n",
" 2022-10-04 | \n",
" 2603 | \n",
" 150.5 | \n",
"
\n",
" \n",
" 13 | \n",
" 2022-10-05 | \n",
" 2603 | \n",
" 155.5 | \n",
"
\n",
" \n",
" 14 | \n",
" 2022-10-06 | \n",
" 2603 | \n",
" 155.5 | \n",
"
\n",
" \n",
" 15 | \n",
" 2022-10-07 | \n",
" 2603 | \n",
" 155.5 | \n",
"
\n",
" \n",
" 16 | \n",
" 2022-10-11 | \n",
" 2603 | \n",
" 156.0 | \n",
"
\n",
" \n",
" 17 | \n",
" 2022-10-12 | \n",
" 2603 | \n",
" 147.0 | \n",
"
\n",
" \n",
" 18 | \n",
" 2022-10-13 | \n",
" 2603 | \n",
" 144.5 | \n",
"
\n",
" \n",
" 19 | \n",
" 2022-10-14 | \n",
" 2603 | \n",
" 152.5 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" mdate coid close_d\n",
"0 2022-09-16 2603 80.8\n",
"1 2022-09-19 2603 169.0\n",
"2 2022-09-20 2603 172.5\n",
"3 2022-09-21 2603 166.5\n",
"4 2022-09-22 2603 156.0\n",
"5 2022-09-23 2603 158.0\n",
"6 2022-09-26 2603 145.5\n",
"7 2022-09-27 2603 153.5\n",
"8 2022-09-28 2603 144.0\n",
"9 2022-09-29 2603 144.0\n",
"10 2022-09-30 2603 146.0\n",
"11 2022-10-03 2603 149.5\n",
"12 2022-10-04 2603 150.5\n",
"13 2022-10-05 2603 155.5\n",
"14 2022-10-06 2603 155.5\n",
"15 2022-10-07 2603 155.5\n",
"16 2022-10-11 2603 156.0\n",
"17 2022-10-12 2603 147.0\n",
"18 2022-10-13 2603 144.5\n",
"19 2022-10-14 2603 152.5"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"closing_price"
]
},
{
"cell_type": "markdown",
"id": "04e2689c",
"metadata": {},
"source": [
"[Return to Menu](#menu)"
]
}
],
"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.8.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}