{ "cells": [ { "cell_type": "markdown", "id": "1cd4cd21", "metadata": {}, "source": [ "\n", "# 滑價模型(Slippage Models)" ] }, { "cell_type": "markdown", "id": "85c4b736-b718-41df-9af3-f8b92a0b75d7", "metadata": {}, "source": [ "- 交易成本(transaction costs)被廣泛認為是影響投資績效的重要因素。它們不僅影響投資績效,還影響了將資產轉換成現金的難易度。\n", "\n", "- 在真實世界的交易中存在許多種類的交易成本,其中一種是**間接成本(indirect cost)**,間接成本包含了**滑價(slippage)**、**流動性(liquidity)**等。因為股價隨時都在變動,下單時的些微時間差也可能造成**預期的價格與成交價有落差**,而這個價差就是**滑價**。**流動性**則會影響交易的難易度,我們通常可以用**交易量**來間接評估流動性。若股票平均來說交易量高,則通常代表該股票流動性高、可以迅速進行交易,同時滑價的影響也會降低。\n", "\n", "- 若沒有考慮滑價及流動性可能會**高估投資策略的獲利**,特別是在投資組合中有**成交量較低(流動性差)**的股票、**資金量(capital base)大**或過度**集中交易單一個股**時,影響會更為明顯。這也是回測(backtesting)的一大目的,考量投資策略在真實世界運行的可能性。" ] }, { "cell_type": "markdown", "id": "6071a4b0-5979-4692-9b9a-4ab5437f727e", "metadata": {}, "source": [ "## zipline.api.set_slippage(self, equities=None, futures=None)\n", "\n", "設定回測時所使用的滑價模型。\n", "\n", "> ### Parameters:\n", "> - equities *(EquitySlippageModel, optional)* -用於交易股票的滑價模型。\n", "> - EquitySlippageModel:`zipline.finance.slippage`\n", "> - futures *(FutureSlippageModel, optional)* -用於交易期貨的滑價模型。(目前不支援)\n", "> - FutureSlippageModel:`zipline.finance.slippage`\n", "> \n", "> ### Raises:\n", "> **SetSlippagePostInit**-`set_slippage` **只能**在 `initialize` 階段使用。\n", ">\n", "> ### Notes:\n", "> - `set_slippage` 只能一次用**一種**方法。\n", ">\n", "> ### See also:\n", "> - `zipline.finance.slippage.FixedSlippage`\n", "> - `zipline.finance.slippage.VolumeShareSlippage`\n", "> - `zipline.finance.slippage.FixedBasisPointsSlippage`\n", ">\n", "> ### Examples:\n", "> ```python\n", "> from zipline.api import set_slippage\n", "> from zipline.finance import slippage\n", "> \n", "> def initialize(context):\n", "> set_slippage(slippage.<其中一種 slippage models>)\n", "> ```" ] }, { "cell_type": "markdown", "id": "1cc7d710-b2f3-4247-b36a-87efcbdcd24c", "metadata": {}, "source": [ "## class zipline.finance.slippage.SlippageModel\n", "> 滑價模型的抽象基類(Abstract Base Class)。\n", ">\n", "> 滑價模型可用來估計交易成交價與設定交易量限制。Zipline 目前有四種模型:\n", "> 1. `FixedSlippage`:設定固定 spread 的滑價,**不能**設定成交量限制。\n", "> 2. `VolumeShareSlippage`:根據該筆交易佔總交易量百分比來計算滑價,並可設定成交量限制。\n", "> 3. `FixedBasisPointsSlippage`:設定固定基點的滑價,並可設定成交量限制。\n", "> 4. `NoSlippage`:不設置滑價。\n", "\n", "## class zipline.finance.slippage.FixedSlippage(spread=0.0)\n", "> - 設定固定 spread 的滑價,**不能**設定成交量限制。\n", "> - 在每筆交易的成交價格額外加入 $\\pm \\frac{spread}{2}$。\n", "> - 如果是買入,則成交價格 = $ price + \\frac{spread}{2}$ ;若是賣出,則成交價格 = $ price - \\frac{spread}{2}$。$ price = 當日收盤價$。\n", ">\n", "> ### Parameters:\n", "> - spread *(float, optional)* - 用來估計成交價與當日收盤價的價差。\n", "\n", "## class zipline.finance.slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1)\n", "> 利用**該筆交易佔當天總交易量的百分比**(volume share)來計算滑價,考慮滑價後的成交價計算方法如下(買入的話,符號為`+`;賣出的話,符號為`-`):\n", "> \n", "> $$ price \\times [1 \\pm ({price\\_impact}) \\times ({volume\\_share}^2)], $$ \n", "> \n", "> *$ price = $ 當日收盤價,* \n", "> *$ volume\\_share = $ 此單交易量佔總交易量百分比數,最高為 $ volume\\_limit $。*\n", "> \n", "> 設定當日**交易量限制**: \n", "> $$ historical\\_volume \\times volume\\_limit, $$\n", "> *$ historical\\_volume = $ 當日成交量。*\n", ">\n", "> ### Parameters:\n", "> - volume_limit *(float, optional)* - \n", "> - 限制買賣量佔總交易量的最大百分比,預設 = 2.5 %。\n", "> - 此限制考慮如果買賣大量股票,會對股價造成過大影響,導致偏離歷史的價格,若利用當天收盤價進行模擬交易就會高估獲利。\n", "> - price_impact *(float, optional)* - 滑價影響程度,其值越大時,滑價影響程度越大,預設 = 0.1。\n", "\n", "## class zipline.finance.slippage.FixedBasisPointsSlippage(basis_points=5.0, volume_limit=0.1)\n", "> **為 zipline 預設模型**,設定**固定基點**的滑價,其計算方法為(買入的話,符號為`+`;賣出的話,符號為`-`):\n", "> $$ price \\times [(1 \\pm basis\\_points \\times 0.0001)] $$\n", ">\n", "> 設定當日**交易量限制**:\n", "> $$ historical\\_volume \\times volume\\_limit, $$\n", "> *$ historical\\_volume = $ 當日成交量。*\n", ">\n", "> ### Parameters:\n", "> - basis_point *(float, optional)* - 設置滑價基點,基點越大,滑價程度越大,預設 = 5.0。\n", "> - volume_limit *(float, optional)* - \n", "> - 買賣量佔總交易量的最高百分比,預設 = 0.1。\n", "> - 此限制考慮如果買賣大量股票,會對股價造成過大影響,導致偏離歷史的價格,若利用當天收盤價進行模擬交易就會高估獲利。\n", "\n", "## class zipline.finance.slippage.NoSlippage\n", "> 不設置滑價。\n" ] }, { "cell_type": "markdown", "id": "fdcf6ed3-fd10-4b8c-9411-5f2dbc2fb715", "metadata": {}, "source": [ "### Notes:\n", "- 滑價計算時,**價格以成交日收盤價為準,數量也以成交時為準**。也就是說,如果因為股數變動造成 amount 有任何變化,計算上都是用成交時新的 amount。\n", "- 如果 `initialize(context)`: 裡面沒有設定`set_slippage()`,系統預設使用 `FixedBasisPointsSlippage(basis_points = 5.0, volume_limit = 0.1)`。\n", "- 如果希望完全不考慮交易量及滑價限制,則使用 `set_slippage(slippage.NoSlippage())`。\n", "\n", "[Go Top](#top)" ] }, { "cell_type": "markdown", "id": "490f2471-4180-4018-8023-24288ff31ba9", "metadata": {}, "source": [ "### Examples-SlippageModel\n", "以下範例比較各種模型計算方法。" ] }, { "cell_type": "markdown", "id": "6c4c4e15-d5d7-4018-a2e7-b501ff81ee23", "metadata": {}, "source": [ "#### Import settings" ] }, { "cell_type": "code", "execution_count": 1, "id": "66281888", "metadata": {}, "outputs": [], "source": [ "import pandas as pd \n", "import numpy as np\n", "import tejapi\n", "import os\n", "\n", "# tej_key\n", "os.environ['TEJAPI_BASE'] = 'https://api.tej.com.tw'\n", "os.environ['TEJAPI_KEY'] = 'your key'\n", "\n", "# set date\n", "os.environ['mdate'] = \"20221201 20221231\"\n", "\n", "# ticker\n", "os.environ['ticker'] = \"IR0001 1216 5844\"" ] }, { "cell_type": "code", "execution_count": 2, "id": "84b5728c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Merging daily equity files:\n", "Currently used TEJ API key call quota 82/100000 (0.08%)\n", "Currently used TEJ API key data quota 173625/10000000 (1.74%)\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[2024-03-01 09:16:44.522971] INFO: zipline.data.bundles.core: Ingesting tquant.\n", "[2024-03-01 09:16:45.031562] INFO: zipline.data.bundles.core: Ingest tquant successfully.\n" ] } ], "source": [ "# ingest\n", "!zipline ingest -b tquant" ] }, { "cell_type": "code", "execution_count": 3, "id": "d02b9f9f", "metadata": {}, "outputs": [], "source": [ "from zipline.finance import commission, slippage\n", "from zipline.api import *\n", "\n", "from zipline import run_algorithm \n", "from zipline.utils.run_algo import get_transaction_detail" ] }, { "cell_type": "markdown", "id": "69f71749-a5d0-4661-90da-de07ac793f2a", "metadata": {}, "source": [ "#### ***FixedSlippage***\n", "\n", "[Go Top](#top)\n", "\n", "設置交易策略\n", ">**成本設定**\n", "> ```python\n", "> def initialize(context):\n", "> \n", "> ...\n", "> set_slippage(slippage.FixedSlippage(spread = 0.2))\n", "> \n", "> set_commission(commission.PerDollar(cost = commission_cost))\n", "> ... \n", "> ```\n", "\n", ">**下單設定**\n", "> - 在回測的第一個交易時間點(i 等於 0,2022-12-01)時:\n", "> ```python\n", "> def handle_data(context, data):\n", "> \n", "> if context.i == 0: # 2022-12-01\n", "> for asset in context.asset:\n", "> order(asset, 5000)\n", "> ...\n", "> ```\n", "> \n", "> - 在回測的第八個交易時間點(i 等於 7,2022-12-12)時\n", "> ```python\n", "> ...\n", "> if context.i == 7: # 2022-12-12\n", "> for asset in context.asset:\n", "> order(asset, -2000) \n", "> ```" ] }, { "cell_type": "code", "execution_count": 4, "id": "a3bf786b", "metadata": {}, "outputs": [], "source": [ "start_dt = pd.Timestamp('2022-12-01', tz='UTC')\n", "end_dt = pd.Timestamp('2022-12-31', tz='UTC')\n", "\n", "def initialize(context):\n", " context.i = 0\n", " context.tickers = ['1216']\n", " context.asset = [symbol(ticker) for ticker in context.tickers] \n", " \n", " # 設定滑價模型來進行模擬 \n", " # set_slippage()只接收一個spread參數\n", " set_slippage(slippage.FixedSlippage(spread = 0.2))\n", " \n", " # 這裡在接收commission.PerDollar()回傳結果後輸入參數\n", " set_commission(commission.PerDollar(cost = commission_cost))\n", " \n", " # 設定benchmark\n", " set_benchmark(symbol('IR0001'))\n", " \n", "def handle_data(context, data):\n", " \n", " if context.i == 0: # 2022-12-01\n", " for asset in context.asset:\n", " order(asset, 5000)\n", "\n", " if context.i == 7: # 2022-12-12\n", " for asset in context.asset:\n", " order(asset, -2000)\n", "\n", " context.i += 1\n", "\n", "commission_cost = 0.001425 + 0.003 / 2\n", "capital_base = 1e6" ] }, { "cell_type": "code", "execution_count": 5, "id": "10a55a66", "metadata": {}, "outputs": [], "source": [ "# 評估結果\n", "closing_price = tejapi.fastget('TWN/APIPRCD',\n", " coid=['1216'],\n", " opts={'columns':['mdate','coid','close_d','vol']},\n", " mdate={'gte':start_dt,'lte':end_dt },\n", " paginate=True)\n", "\n", "closing_price['vol'] = closing_price['vol'] * 1000\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", " bundle='tquant')\n", "\n", "positions, transactions, orders = get_transaction_detail(performance)" ] }, { "cell_type": "markdown", "id": "6716e08d", "metadata": {}, "source": [ "***FixedSlippage - 情況 1: 買入時計算滑價***\n", "\n", "- 12/1時下單買 5 張統一(1216)股票,12/2成交。\n", "- 收盤價是 65.0,但因為我們設定 spread = 0.2,所以成交價(transactions.price)是 65 + 0.2 / 2 = 65.1,手續費('commission')是 65.1 * 5000 * 0.002925 = 952.0875(手續費是預先設定好的,這次用 PerDollar)。" ] }, { "cell_type": "code", "execution_count": 6, "id": "64dd5ecf", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_dvol
12022-12-02121665.015184000.0
\n", "
" ], "text/plain": [ " mdate coid close_d vol\n", "1 2022-12-02 1216 65.0 15184000.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price.query('(mdate == \"2022-12-02\")')" ] }, { "cell_type": "code", "execution_count": 7, "id": "76e14d71", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-01 13:30:00+08:000121690e99aef673d495c9c97c724ac70cb842022-12-01 13:30:00+08:00None2022-12-01 13:30:00+08:00500000.0000NoneNoneFalseFalseEquity(0 [1216])0
2022-12-02 13:30:00+08:000121690e99aef673d495c9c97c724ac70cb842022-12-02 13:30:00+08:00None2022-12-01 13:30:00+08:0050005000952.0875NoneNoneFalseFalseEquity(0 [1216])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-01 13:30:00+08:00 0 1216 90e99aef673d495c9c97c724ac70cb84 \n", "2022-12-02 13:30:00+08:00 0 1216 90e99aef673d495c9c97c724ac70cb84 \n", "\n", " dt reason \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 None \n", "2022-12-02 13:30:00+08:00 2022-12-02 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 5000 0 \n", "2022-12-02 13:30:00+08:00 2022-12-01 13:30:00+08:00 5000 5000 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-01 13:30:00+08:00 0.0000 None None False \n", "2022-12-02 13:30:00+08:00 952.0875 None None False \n", "\n", " limit_reached asset status \n", "2022-12-01 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-02 13:30:00+08:00 False Equity(0 [1216]) 1 " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('(created.dt.strftime(\"%Y-%m-%d\") == \"2022-12-01\")')" ] }, { "cell_type": "code", "execution_count": 8, "id": "8c1276fb", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
2022-12-02 13:30:00+08:000121650002022-12-02 13:30:00+08:0065.190e99aef673d495c9c97c724ac70cb84Equity(0 [1216])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2022-12-02 13:30:00+08:00 0 1216 5000 2022-12-02 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2022-12-02 13:30:00+08:00 65.1 90e99aef673d495c9c97c724ac70cb84 \n", "\n", " asset commission \n", "2022-12-02 13:30:00+08:00 Equity(0 [1216]) None " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2022-12-02']" ] }, { "cell_type": "markdown", "id": "7bae2849", "metadata": {}, "source": [ "***FixedSlippage - 情況 2: 賣出時計算滑價***\n", "\n", "- 在12/12賣出 2 張統一(1216)股票,12/13成交。\n", "- 12/13收盤價 65.4,由於是賣單,所以成交價是 65.4 - 0.2 / 2 = 65.3,手續費計算方法一樣。 " ] }, { "cell_type": "code", "execution_count": 9, "id": "3c806f72", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_dvol
82022-12-13121665.49677000.0
\n", "
" ], "text/plain": [ " mdate coid close_d vol\n", "8 2022-12-13 1216 65.4 9677000.0" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price.query('(mdate == \"2022-12-13\")')" ] }, { "cell_type": "code", "execution_count": 10, "id": "4fa094e4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-12 13:30:00+08:0001216f2ad3408822b423da627ec1fc30abd9c2022-12-12 13:30:00+08:00None2022-12-12 13:30:00+08:00-200000.000NoneNoneFalseFalseEquity(0 [1216])0
2022-12-13 13:30:00+08:0001216f2ad3408822b423da627ec1fc30abd9c2022-12-13 13:30:00+08:00None2022-12-12 13:30:00+08:00-2000-2000382.005NoneNoneFalseFalseEquity(0 [1216])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-12 13:30:00+08:00 0 1216 f2ad3408822b423da627ec1fc30abd9c \n", "2022-12-13 13:30:00+08:00 0 1216 f2ad3408822b423da627ec1fc30abd9c \n", "\n", " dt reason \\\n", "2022-12-12 13:30:00+08:00 2022-12-12 13:30:00+08:00 None \n", "2022-12-13 13:30:00+08:00 2022-12-13 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-12 13:30:00+08:00 2022-12-12 13:30:00+08:00 -2000 0 \n", "2022-12-13 13:30:00+08:00 2022-12-12 13:30:00+08:00 -2000 -2000 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-12 13:30:00+08:00 0.000 None None False \n", "2022-12-13 13:30:00+08:00 382.005 None None False \n", "\n", " limit_reached asset status \n", "2022-12-12 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-13 13:30:00+08:00 False Equity(0 [1216]) 1 " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 在12/12賣出兩張統一 (1216) 股票,12/13成交。\n", "orders.query('(created.dt.strftime(\"%Y-%m-%d\") == \"2022-12-12\")')" ] }, { "cell_type": "code", "execution_count": 11, "id": "188ce608", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
2022-12-13 13:30:00+08:0001216-20002022-12-13 13:30:00+08:0065.3f2ad3408822b423da627ec1fc30abd9cEquity(0 [1216])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2022-12-13 13:30:00+08:00 0 1216 -2000 2022-12-13 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2022-12-13 13:30:00+08:00 65.3 f2ad3408822b423da627ec1fc30abd9c \n", "\n", " asset commission \n", "2022-12-13 13:30:00+08:00 Equity(0 [1216]) None " ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 成交價是65.4 - 0.2 / 2 = 65.3\n", "transactions.loc['2022-12-13']" ] }, { "cell_type": "markdown", "id": "077c33d0-bbfe-48e7-b5a1-2f441662c1ec", "metadata": {}, "source": [ "#### ***VolumeShareSlippage***\n", "\n", "[Go Top](#top)\n", "\n", "設置交易策略\n", ">**滑價設定**\n", "> ```python\n", "> def initialize(context):\n", "> ...\n", "> set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))\n", "> ...\n", "> ``` \n", "\n", ">**下單設定**\n", "> - 在回測的第一個交易時間點(i 等於 0,2022-12-01)時:\n", "> ```python\n", "> def handle_data(context, data):\n", "> if context.i == 0: # 2022-12-01\n", "> for asset in context.asset:\n", "> order(asset, 1500000) \n", "> ...\n", "> ``` \n", "> \n", ">- 在回測的第十一個交易時間點(i 等於 10,2022-12-15)時\n", "> ```python\n", "> ...\n", "> if context.i == 10: # 2022-12-15\n", "> for asset in context.asset:\n", "> order(asset, -200000) \n", "> ```" ] }, { "cell_type": "code", "execution_count": 12, "id": "77e32d54", "metadata": {}, "outputs": [], "source": [ "def initialize(context):\n", " context.i = 0\n", " context.tickers = ['1216']\n", " context.asset = [symbol(ticker) for ticker in context.tickers] \n", " \n", "# set_slippage\n", " set_slippage(slippage.VolumeShareSlippage(volume_limit=0.025, price_impact=0.1))\n", " \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-12-01\n", " for asset in context.asset:\n", " order(asset, 1500000) \n", " \n", " if context.i == 10: # 2022-12-15\n", " for asset in context.asset:\n", " order(asset, -200000) \n", "\n", " context.i += 1\n", "\n", "capital_base = 1e8" ] }, { "cell_type": "code", "execution_count": 13, "id": "82344285", "metadata": {}, "outputs": [], "source": [ "performance = run_algorithm(start=start_dt,\n", " end=end_dt,\n", " initialize=initialize,\n", " handle_data=handle_data,\n", " capital_base=capital_base,\n", " bundle='tquant')\n", "\n", "positions, transactions, orders = get_transaction_detail(performance)" ] }, { "cell_type": "markdown", "id": "eda87fa2", "metadata": {}, "source": [ "***VolumeShareSlippage - 情況 1: 買入時計算滑價***\n", "\n", "- 在12/1下單 1500 張統一,但觀察成交量資料發現,這段期間每日成交量大約只有數千到一萬多張(從 TEJ API(TWN/APIPRCD)取得的成交量(vol)單位是千股,但為了一致性,所以我們這邊**將千股轉換成股**,**利用 order 下單時的單位也是股**)。\n", "- 因為我們設定 volume_limit = 0.025,使得 zipline 會把這筆訂單拆成數天慢慢消化,每天成交量不超過該股票總成交量的 2.5%。" ] }, { "cell_type": "code", "execution_count": 14, "id": "c7779a2c", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_dvol
02022-12-01121665.918963000.0
12022-12-02121665.015184000.0
22022-12-05121665.49704000.0
32022-12-06121664.613171000.0
42022-12-07121665.213674000.0
52022-12-08121665.09149000.0
62022-12-09121666.010505000.0
72022-12-12121665.86986000.0
82022-12-13121665.49677000.0
92022-12-14121666.07408000.0
102022-12-15121665.57422000.0
112022-12-16121665.310721000.0
122022-12-19121665.34931000.0
132022-12-20121665.65923000.0
142022-12-21121665.46343000.0
152022-12-22121665.93365000.0
162022-12-23121665.92802000.0
172022-12-26121665.81558000.0
182022-12-27121666.23767000.0
192022-12-28121666.25396000.0
202022-12-29121666.36697000.0
212022-12-30121666.66968000.0
\n", "
" ], "text/plain": [ " mdate coid close_d vol\n", "0 2022-12-01 1216 65.9 18963000.0\n", "1 2022-12-02 1216 65.0 15184000.0\n", "2 2022-12-05 1216 65.4 9704000.0\n", "3 2022-12-06 1216 64.6 13171000.0\n", "4 2022-12-07 1216 65.2 13674000.0\n", "5 2022-12-08 1216 65.0 9149000.0\n", "6 2022-12-09 1216 66.0 10505000.0\n", "7 2022-12-12 1216 65.8 6986000.0\n", "8 2022-12-13 1216 65.4 9677000.0\n", "9 2022-12-14 1216 66.0 7408000.0\n", "10 2022-12-15 1216 65.5 7422000.0\n", "11 2022-12-16 1216 65.3 10721000.0\n", "12 2022-12-19 1216 65.3 4931000.0\n", "13 2022-12-20 1216 65.6 5923000.0\n", "14 2022-12-21 1216 65.4 6343000.0\n", "15 2022-12-22 1216 65.9 3365000.0\n", "16 2022-12-23 1216 65.9 2802000.0\n", "17 2022-12-26 1216 65.8 1558000.0\n", "18 2022-12-27 1216 66.2 3767000.0\n", "19 2022-12-28 1216 66.2 5396000.0\n", "20 2022-12-29 1216 66.3 6697000.0\n", "21 2022-12-30 1216 66.6 6968000.0" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 1216每日成交量大約只有數千到一萬多張(vol單位是千股)\n", "closing_price.query('(mdate >= \"2022-12-01\")')" ] }, { "cell_type": "code", "execution_count": 15, "id": "919cab29", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-01 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-01 13:30:00+08:00None2022-12-01 13:30:00+08:00150000000.0NoneNoneFalseFalseEquity(0 [1216])0
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-01 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "\n", " dt reason \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 0 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-01 13:30:00+08:00 0.0 None None False \n", "\n", " limit_reached asset status \n", "2022-12-01 13:30:00+08:00 False Equity(0 [1216]) 0 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 在12/1下單一千五百張統一\n", "orders.loc['2022-12-01']" ] }, { "cell_type": "markdown", "id": "805582d1", "metadata": {}, "source": [ "12/2的總成交量是 15184000 股,由於我們設定 volume_limit = 0.025,因此 VolumeShareSlippage 會把12/2的成交量限制在2.5%,也就是379600股。\n", "\n", "成交價(transactions.price)計算方法是:原始收盤價 * ( 1 + price_impact * volume_share ^ 2 ) = 65 * ( 1 + 0.1 * 0.025 ^ 2 ) $\\approx$ 65.004063\n", "- price_impact 是預先設定好的0.1,且因為此處為買單,所以符號為正。" ] }, { "cell_type": "code", "execution_count": 16, "id": "cc16f855", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-02 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-02 13:30:00+08:00None2022-12-01 13:30:00+08:00150000037960072175.960716NoneNoneFalseFalseEquity(0 [1216])0
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-02 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "\n", " dt reason \\\n", "2022-12-02 13:30:00+08:00 2022-12-02 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-02 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 379600 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-02 13:30:00+08:00 72175.960716 None None False \n", "\n", " limit_reached asset status \n", "2022-12-02 13:30:00+08:00 False Equity(0 [1216]) 0 " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.loc['2022-12-02']" ] }, { "cell_type": "code", "execution_count": 17, "id": "da747c8a", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
2022-12-02 13:30:00+08:00012163796002022-12-02 13:30:00+08:0065.00406378c217d3db9347b5b1de610937398c99Equity(0 [1216])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2022-12-02 13:30:00+08:00 0 1216 379600 2022-12-02 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2022-12-02 13:30:00+08:00 65.004063 78c217d3db9347b5b1de610937398c99 \n", "\n", " asset commission \n", "2022-12-02 13:30:00+08:00 Equity(0 [1216]) None " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2022-12-02']" ] }, { "cell_type": "markdown", "id": "76e439cc", "metadata": {}, "source": [ "orders 資料中的 filled 為累積成交量:\n", "- 12/7的 filled = 1293325 股,代表 12/2 到 12/7 成交的累計股數 = 379600 + 242600 + 329275 + 341850 = 1293325。\n", "- 截至12/8已經買滿了 1500 張,所以最後 status 就會從0 變成 1,代表當初下的 1500 張已經全數成交。" ] }, { "cell_type": "code", "execution_count": 18, "id": "d832fc26", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-01 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-01 13:30:00+08:00None2022-12-01 13:30:00+08:00150000000.000000NoneNoneFalseFalseEquity(0 [1216])0
2022-12-02 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-02 13:30:00+08:00None2022-12-01 13:30:00+08:00150000037960072175.960716NoneNoneFalseFalseEquity(0 [1216])0
2022-12-05 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-05 13:30:00+08:00None2022-12-01 13:30:00+08:001500000622200118587.028226NoneNoneFalseFalseEquity(0 [1216])0
2022-12-06 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-06 13:30:00+08:00None2022-12-01 13:30:00+08:001500000951475180809.074486NoneNoneFalseFalseEquity(0 [1216])0
2022-12-07 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-07 13:30:00+08:00None2022-12-01 13:30:00+08:0015000001293325246007.362624NoneNoneFalseFalseEquity(0 [1216])0
2022-12-08 13:30:00+08:000121678c217d3db9347b5b1de610937398c992022-12-08 13:30:00+08:00None2022-12-01 13:30:00+08:0015000001500000285303.452191NoneNoneFalseFalseEquity(0 [1216])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-01 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "2022-12-02 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "2022-12-05 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "2022-12-06 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "2022-12-07 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "2022-12-08 13:30:00+08:00 0 1216 78c217d3db9347b5b1de610937398c99 \n", "\n", " dt reason \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 None \n", "2022-12-02 13:30:00+08:00 2022-12-02 13:30:00+08:00 None \n", "2022-12-05 13:30:00+08:00 2022-12-05 13:30:00+08:00 None \n", "2022-12-06 13:30:00+08:00 2022-12-06 13:30:00+08:00 None \n", "2022-12-07 13:30:00+08:00 2022-12-07 13:30:00+08:00 None \n", "2022-12-08 13:30:00+08:00 2022-12-08 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 0 \n", "2022-12-02 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 379600 \n", "2022-12-05 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 622200 \n", "2022-12-06 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 951475 \n", "2022-12-07 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 1293325 \n", "2022-12-08 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 1500000 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-01 13:30:00+08:00 0.000000 None None False \n", "2022-12-02 13:30:00+08:00 72175.960716 None None False \n", "2022-12-05 13:30:00+08:00 118587.028226 None None False \n", "2022-12-06 13:30:00+08:00 180809.074486 None None False \n", "2022-12-07 13:30:00+08:00 246007.362624 None None False \n", "2022-12-08 13:30:00+08:00 285303.452191 None None False \n", "\n", " limit_reached asset status \n", "2022-12-01 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-02 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-05 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-06 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-07 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-08 13:30:00+08:00 False Equity(0 [1216]) 1 " ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('(created.dt.strftime(\"%Y-%m-%d\") == \"2022-12-01\")')" ] }, { "cell_type": "markdown", "id": "ac52c762", "metadata": {}, "source": [ "***VolumeShareSlippage - 情況 2: 賣出時計算滑價***\n", "\n", "- 在 12/15 下單賣出 200 張統一,因為在12/16總交易量是 10721 張,volume_share = 200 / 10721 $\\approx$ 0.018655小於 0.025,所以12/16一天就能賣掉。\n", "- 成交價(transactions.price)是 65.3 * ( 1 - 0.1 * 0.018655 ^ 2 ) = 65.297728(賣出的話是減)。" ] }, { "cell_type": "code", "execution_count": 19, "id": "b57ac94b", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_dvol
112022-12-16121665.310721000.0
122022-12-19121665.34931000.0
132022-12-20121665.65923000.0
142022-12-21121665.46343000.0
152022-12-22121665.93365000.0
162022-12-23121665.92802000.0
172022-12-26121665.81558000.0
182022-12-27121666.23767000.0
192022-12-28121666.25396000.0
202022-12-29121666.36697000.0
212022-12-30121666.66968000.0
\n", "
" ], "text/plain": [ " mdate coid close_d vol\n", "11 2022-12-16 1216 65.3 10721000.0\n", "12 2022-12-19 1216 65.3 4931000.0\n", "13 2022-12-20 1216 65.6 5923000.0\n", "14 2022-12-21 1216 65.4 6343000.0\n", "15 2022-12-22 1216 65.9 3365000.0\n", "16 2022-12-23 1216 65.9 2802000.0\n", "17 2022-12-26 1216 65.8 1558000.0\n", "18 2022-12-27 1216 66.2 3767000.0\n", "19 2022-12-28 1216 66.2 5396000.0\n", "20 2022-12-29 1216 66.3 6697000.0\n", "21 2022-12-30 1216 66.6 6968000.0" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price.query('(mdate >= \"2022-12-16\")')" ] }, { "cell_type": "code", "execution_count": 20, "id": "23544985", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-15 13:30:00+08:000121699d0247c150842eca070ca2e4df58d2e2022-12-15 13:30:00+08:00None2022-12-15 13:30:00+08:00-20000000.000000NoneNoneFalseFalseEquity(0 [1216])0
2022-12-16 13:30:00+08:000121699d0247c150842eca070ca2e4df58d2e2022-12-16 13:30:00+08:00None2022-12-15 13:30:00+08:00-200000-20000038199.170592NoneNoneFalseFalseEquity(0 [1216])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-15 13:30:00+08:00 0 1216 99d0247c150842eca070ca2e4df58d2e \n", "2022-12-16 13:30:00+08:00 0 1216 99d0247c150842eca070ca2e4df58d2e \n", "\n", " dt reason \\\n", "2022-12-15 13:30:00+08:00 2022-12-15 13:30:00+08:00 None \n", "2022-12-16 13:30:00+08:00 2022-12-16 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-15 13:30:00+08:00 2022-12-15 13:30:00+08:00 -200000 0 \n", "2022-12-16 13:30:00+08:00 2022-12-15 13:30:00+08:00 -200000 -200000 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-15 13:30:00+08:00 0.000000 None None False \n", "2022-12-16 13:30:00+08:00 38199.170592 None None False \n", "\n", " limit_reached asset status \n", "2022-12-15 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-16 13:30:00+08:00 False Equity(0 [1216]) 1 " ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('(created.dt.strftime(\"%Y-%m-%d\") == \"2022-12-15\")')" ] }, { "cell_type": "code", "execution_count": 21, "id": "19212b69", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
2022-12-16 13:30:00+08:0001216-2000002022-12-16 13:30:00+08:0065.29772899d0247c150842eca070ca2e4df58d2eEquity(0 [1216])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2022-12-16 13:30:00+08:00 0 1216 -200000 2022-12-16 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2022-12-16 13:30:00+08:00 65.297728 99d0247c150842eca070ca2e4df58d2e \n", "\n", " asset commission \n", "2022-12-16 13:30:00+08:00 Equity(0 [1216]) None " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2022-12-16']" ] }, { "cell_type": "markdown", "id": "81aeda59-3f00-4b02-8175-1c6480e77650", "metadata": {}, "source": [ "#### ***FixedBasisPointsSlippage***\n", "\n", "[Go Top](#top)\n", "\n", "設置交易策略\n", ">**滑價設定**\n", "> ```python\n", "> def initialize(context):\n", "> ...\n", "> set_slippage(slippage.FixedBasisPointsSlippage(basis_points=5.0, volume_limit=0.025))\n", "> ...\n", "> ``` \n", "\n", ">**下單設定**\n", ">- 在回測的第一個交易時間點(i 等於 0,2022-12-01)時\n", "> ```python\n", "> def handle_data(context, data):\n", "> if context.i == 0: # 2022-12-01\n", "> for asset in context.asset:\n", "> order(asset, 1500000) \n", "> ...\n", "> ``` \n", ">- 在回測的第十一個交易時間點(i 等於 10,2022-12-15)時\n", "> ```python\n", "> ...\n", "> if context.i == 10: # 2022-12-15\n", "> for asset in context.asset:\n", "> order(asset, -200000) \n", "> ```" ] }, { "cell_type": "code", "execution_count": 22, "id": "809b01c3", "metadata": {}, "outputs": [], "source": [ "def initialize(context):\n", " context.i = 0\n", " context.tickers = ['1216']\n", " context.asset = [symbol(ticker) for ticker in context.tickers] \n", " \n", "# set_slippage\n", " set_slippage(slippage.FixedBasisPointsSlippage(basis_points=5.0, volume_limit=0.025))\n", " \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-12-01\n", " for asset in context.asset:\n", " order(asset, 1500000) \n", " \n", " if context.i == 10: # 2022-12-15\n", " for asset in context.asset:\n", " order(asset, -200000) \n", "\n", " context.i += 1\n", "\n", "capital_base = 1e8" ] }, { "cell_type": "code", "execution_count": 23, "id": "d528f7e2", "metadata": {}, "outputs": [], "source": [ "performance = run_algorithm(start=start_dt,\n", " end=end_dt,\n", " initialize=initialize,\n", " handle_data=handle_data,\n", " capital_base=capital_base,\n", " bundle='tquant')\n", "\n", "positions, transactions, orders = get_transaction_detail(performance)" ] }, { "cell_type": "markdown", "id": "9d5991cf", "metadata": {}, "source": [ "***FixedBasisPointsSlippage - 情況 1: 買入時計算滑價***\n", "\n", "在12/1下單 1500 張統一,成交量限制和`VolumeShareSlippage`範例相同,不多做敘述。所以12/1下的單到了12/8才完全成交。\n", "\n", "以12/2為例,成交價(transactions.price)計算方法是:原始收盤價 * ( 1 + basis_point * 0.0001 ) = 65 * ( 1 + 5 * 0.0001 ) $\\approx$ 65.0325\n", "- basis_point 是預先設定好的 5,且因為此處為買單,所以符號為正。" ] }, { "cell_type": "code", "execution_count": 24, "id": "05507aca", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_dvol
12022-12-02121665.015184000.0
\n", "
" ], "text/plain": [ " mdate coid close_d vol\n", "1 2022-12-02 1216 65.0 15184000.0" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price.query('(mdate == \"2022-12-02\")')" ] }, { "cell_type": "code", "execution_count": 25, "id": "6d9db8df", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-01 13:30:00+08:000121670d23d77a871433fab9aca4db0f575982022-12-01 13:30:00+08:00None2022-12-01 13:30:00+08:00150000000.000000NoneNoneFalseFalseEquity(0 [1216])0
2022-12-02 13:30:00+08:000121670d23d77a871433fab9aca4db0f575982022-12-02 13:30:00+08:00None2022-12-01 13:30:00+08:00150000037960072207.535725NoneNoneFalseFalseEquity(0 [1216])0
2022-12-05 13:30:00+08:000121670d23d77a871433fab9aca4db0f575982022-12-05 13:30:00+08:00None2022-12-01 13:30:00+08:001500000622200118638.906809NoneNoneFalseFalseEquity(0 [1216])0
2022-12-06 13:30:00+08:000121670d23d77a871433fab9aca4db0f575982022-12-06 13:30:00+08:00None2022-12-01 13:30:00+08:001500000951475180888.173512NoneNoneFalseFalseEquity(0 [1216])0
2022-12-07 13:30:00+08:000121670d23d77a871433fab9aca4db0f575982022-12-07 13:30:00+08:00None2022-12-01 13:30:00+08:0015000001293325246114.984119NoneNoneFalseFalseEquity(0 [1216])0
2022-12-08 13:30:00+08:000121670d23d77a871433fab9aca4db0f575982022-12-08 13:30:00+08:00None2022-12-01 13:30:00+08:0015000001500000285428.715536NoneNoneFalseFalseEquity(0 [1216])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-01 13:30:00+08:00 0 1216 70d23d77a871433fab9aca4db0f57598 \n", "2022-12-02 13:30:00+08:00 0 1216 70d23d77a871433fab9aca4db0f57598 \n", "2022-12-05 13:30:00+08:00 0 1216 70d23d77a871433fab9aca4db0f57598 \n", "2022-12-06 13:30:00+08:00 0 1216 70d23d77a871433fab9aca4db0f57598 \n", "2022-12-07 13:30:00+08:00 0 1216 70d23d77a871433fab9aca4db0f57598 \n", "2022-12-08 13:30:00+08:00 0 1216 70d23d77a871433fab9aca4db0f57598 \n", "\n", " dt reason \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 None \n", "2022-12-02 13:30:00+08:00 2022-12-02 13:30:00+08:00 None \n", "2022-12-05 13:30:00+08:00 2022-12-05 13:30:00+08:00 None \n", "2022-12-06 13:30:00+08:00 2022-12-06 13:30:00+08:00 None \n", "2022-12-07 13:30:00+08:00 2022-12-07 13:30:00+08:00 None \n", "2022-12-08 13:30:00+08:00 2022-12-08 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-01 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 0 \n", "2022-12-02 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 379600 \n", "2022-12-05 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 622200 \n", "2022-12-06 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 951475 \n", "2022-12-07 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 1293325 \n", "2022-12-08 13:30:00+08:00 2022-12-01 13:30:00+08:00 1500000 1500000 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-01 13:30:00+08:00 0.000000 None None False \n", "2022-12-02 13:30:00+08:00 72207.535725 None None False \n", "2022-12-05 13:30:00+08:00 118638.906809 None None False \n", "2022-12-06 13:30:00+08:00 180888.173512 None None False \n", "2022-12-07 13:30:00+08:00 246114.984119 None None False \n", "2022-12-08 13:30:00+08:00 285428.715536 None None False \n", "\n", " limit_reached asset status \n", "2022-12-01 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-02 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-05 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-06 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-07 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-08 13:30:00+08:00 False Equity(0 [1216]) 1 " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('(created.dt.strftime(\"%Y-%m-%d\") == \"2022-12-01\")')" ] }, { "cell_type": "code", "execution_count": 26, "id": "913c3eb8", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
2022-12-02 13:30:00+08:00012163796002022-12-02 13:30:00+08:0065.032570d23d77a871433fab9aca4db0f57598Equity(0 [1216])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2022-12-02 13:30:00+08:00 0 1216 379600 2022-12-02 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2022-12-02 13:30:00+08:00 65.0325 70d23d77a871433fab9aca4db0f57598 \n", "\n", " asset commission \n", "2022-12-02 13:30:00+08:00 Equity(0 [1216]) None " ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2022-12-02']" ] }, { "cell_type": "markdown", "id": "30945d9a", "metadata": {}, "source": [ "***FixedBasisPointsSlippage - 情況 2: 賣出時計算滑價***\n", "\n", "在12/15下單賣出 200 張統一。\n", "\n", "成交價計算方法是:原始收盤價 * ( 1 - basis_point * 0.0001 ) = 65.3 * ( 1 - 5 * 0.0001 ) $\\approx$ 65.26735\n", "- basis_point 是預先設定好的 5,且因為此處為賣單,所以符號為負。" ] }, { "cell_type": "code", "execution_count": 27, "id": "441fb9a2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_dvol
112022-12-16121665.310721000.0
\n", "
" ], "text/plain": [ " mdate coid close_d vol\n", "11 2022-12-16 1216 65.3 10721000.0" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price.query('(mdate == \"2022-12-16\")')" ] }, { "cell_type": "code", "execution_count": 28, "id": "c69356f9", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2022-12-15 13:30:00+08:00012160edbb52f0eb44f0c9448b1d32bd05a982022-12-15 13:30:00+08:00None2022-12-15 13:30:00+08:00-20000000.00000NoneNoneFalseFalseEquity(0 [1216])0
2022-12-16 13:30:00+08:00012160edbb52f0eb44f0c9448b1d32bd05a982022-12-16 13:30:00+08:00None2022-12-15 13:30:00+08:00-200000-20000038181.39975NoneNoneFalseFalseEquity(0 [1216])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2022-12-15 13:30:00+08:00 0 1216 0edbb52f0eb44f0c9448b1d32bd05a98 \n", "2022-12-16 13:30:00+08:00 0 1216 0edbb52f0eb44f0c9448b1d32bd05a98 \n", "\n", " dt reason \\\n", "2022-12-15 13:30:00+08:00 2022-12-15 13:30:00+08:00 None \n", "2022-12-16 13:30:00+08:00 2022-12-16 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2022-12-15 13:30:00+08:00 2022-12-15 13:30:00+08:00 -200000 0 \n", "2022-12-16 13:30:00+08:00 2022-12-15 13:30:00+08:00 -200000 -200000 \n", "\n", " commission stop limit stop_reached \\\n", "2022-12-15 13:30:00+08:00 0.00000 None None False \n", "2022-12-16 13:30:00+08:00 38181.39975 None None False \n", "\n", " limit_reached asset status \n", "2022-12-15 13:30:00+08:00 False Equity(0 [1216]) 0 \n", "2022-12-16 13:30:00+08:00 False Equity(0 [1216]) 1 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('(created.dt.strftime(\"%Y-%m-%d\") == \"2022-12-15\")')" ] }, { "cell_type": "code", "execution_count": 29, "id": "ba13d426", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
2022-12-16 13:30:00+08:0001216-2000002022-12-16 13:30:00+08:0065.267350edbb52f0eb44f0c9448b1d32bd05a98Equity(0 [1216])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2022-12-16 13:30:00+08:00 0 1216 -200000 2022-12-16 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2022-12-16 13:30:00+08:00 65.26735 0edbb52f0eb44f0c9448b1d32bd05a98 \n", "\n", " asset commission \n", "2022-12-16 13:30:00+08:00 Equity(0 [1216]) None " ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2022-12-16']" ] }, { "cell_type": "markdown", "id": "25a48c6c", "metadata": {}, "source": [ "[Go Top](#top)" ] } ], "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.10.13" } }, "nbformat": 4, "nbformat_minor": 5 }