{ "cells": [ { "cell_type": "markdown", "id": "1c0c1b3c", "metadata": { "id": "1c0c1b3c" }, "source": [ "# 下單方法介紹(一)\n", "\n", "\n", "# Zipline Order函數介紹-order和order_target\n", "Order函數是用於交易股票的函數,可以利用以下六種函數得出我們指定要交易的股票以及數量\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()`以及`order_target()`的使用辦法。\n", "本文件包含以下四個部份:\n", "> 1. [函數說明](#下單函數的參數與回傳值)\n", "> 2. [範例:order(搭配 limit_price)](#limit_price)\n", "> 3. [範例:order(搭配 stop_price)](#Stop_price)\n", "> 4. [範例:order_target](#order_target)\n", ">\n", "> ## 閱讀本篇之後可以搭配閱讀:\n", "> \n", "> 1. [Zipline Order(value & target_value).ipynb](https://github.com/tejtw/TQuant-Lab/blob/main/lecture/Zipline%20Order%20(value%20%26%20target_value).ipynb)\n", ">\n", "> 2. [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": "e03477ee", "metadata": { "id": "e03477ee" }, "source": [ "\n", "## 下單函數的參數與回傳值:\n", "### Import: \n", "使用前記得先做 import:`from zipline.api import order, order_target`或`from zipline.api import *`。\n", "\n", "\n", "### Parameters:\n", "- asset:*zipline.assets.Asset*,為該股票的`Asset`物件(`zipline.assets.Asset`,例如:Equity(0 [1101]),透過`symbol(\"1101\")`可將 symbol 轉成`Asset`物件)。\n", "- **amount/value/target/percent:數量、價值、目標、比重,依照每個下單函數各有不同,但一律正值代表 long(buy or cover),而負值代表 short(sell or short)。**\n", "- limit_price:*float, optional*\n", " - 限價。\n", " - 代表最高的買進價(或最低的賣出價)。\n", " - 預設為 None。\n", " \n", " ```python\n", " # 以order為例,其它下單函數概念一樣。\n", " from zipline.api import order\n", " \n", " order(asset, amount, limit_price=limit_price)\n", " ```\n", "- stop_price:*float, optional*\n", " - 止損價。\n", " - 若為買單,當市價 >= stop price,則用市價買入(若是賣單,則是在市價 <= stop price 後用市價賣出)。\n", " - 預設為 None。\n", " \n", " ```python\n", " # 以order為例,其它下單函數概念一樣。\n", " from zipline.api import order\n", " \n", " order(asset, amount, stop_price=stop_price)\n", " ```\n", "- style:*zipline.finance.execution.ExecutionStyle*\n", " - 預設為 None。\n", " - 設定限價及止損價。如果`limit_price`或`stop_price`已經提供了,就不能使用這個參數。\n", " \n", " ```python \n", " # 以order為例,其下單函數概念一樣。\n", " from zipline.finance.execution import LimitOrder, StopOrder, StopLimitOrder\n", " from zipline.api import order\n", "\n", " # Limit order:\n", " order(asset, amount, style=LimitOrder(limit_price))\n", " # Stop order:\n", " order(asset, amount, style=StopOrder(stop_price))\n", " # StopLimit order:\n", " order(asset, amount, style=StopLimitOrder(limit_price, stop_price))\n", " ```\n", "\n", "### Returns:\n", "order_id:*str or None*,每張訂單獨一無二的 16 進制編碼。\n", "\n", "---\n", "\n", "### 交易機制:\n", "- 市價皆為**收盤價**。\n", "- 因 zipline 交易機制的關係,下單後會到next bar才成交(**也就是今天下單,下一交易日才成交**),所以`stop_price`及`limit_price`皆是和**下一個交易日的收盤價**做比較。\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": "99875a04", "metadata": { "id": "99875a04" }, "source": [ "## 設定環境" ] }, { "cell_type": "code", "execution_count": 1, "id": "83c1fb45", "metadata": { "id": "83c1fb45", "outputId": "ab5200d9-fc1c-44e9-edc1-67fc3caddeab" }, "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", "\n", "# date\n", "start='2018-07-24'\n", "end='2018-08-14'\n", "os.environ['mdate'] = '20180724 20180814'\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'] = '1101 1102 IR0001'" ] }, { "cell_type": "code", "execution_count": 2, "id": "725dab47", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Merging daily equity files:\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "[2023-11-30 07:50:14.101796] INFO: zipline.data.bundles.core: Ingesting tquant.\n" ] } ], "source": [ "!zipline ingest -b tquant" ] }, { "cell_type": "code", "execution_count": 3, "id": "da077113", "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": "752a57ed", "metadata": { "id": "752a57ed" }, "source": [ "\n", "# 範例:order(搭配 limit_price)\n", "從2018/07/24到2018/08/14,每天買一張1101,limit_price = 45。\n", "\n", "[Return to Menu](#menu)" ] }, { "cell_type": "markdown", "id": "77121776", "metadata": { "id": "77121776" }, "source": [ "`zipline.api.order(asset, amount, limit_price=None, stop_price=None, style=None)`\n", "\n", "#### amount\n", "- 單位為**股**。\n", "- 若下單股數有**小數點**的情形,則會**取整數**後再進行下單。取整數的方法為:若股數和最近整數相差在 0.0001 以內,就取最接近整數,否則直接去掉小數(3.9999 -> 4.0 ; 5.5 -> 5.0 ; -5.5 -> -5.0)。\n", "\n", "#### limit_price = XX\n", "購買的價格必須<=XX才會買進" ] }, { "cell_type": "markdown", "id": "edfb3a3f", "metadata": { "id": "edfb3a3f" }, "source": [ "## 設定交易模型\n", "#### initialize(context):\n", "在回測開始時被調用的函數,進行初始化設定\n", "\n", "這邊透過context參數來儲存和共享各種回測所需的變數和參數\n", "\n", "* `context.tickers = ['1101']`\n", " 定義一個股票代碼列表,這裡我們指定 1101 的股票資料。\n", "* `context.asset = [symbol(ticker) for ticker in context.tickers]`\n", " 將股票代碼輸入進 context 中,並轉換為 `Asset` 物件。\n", "* `set_slippage(slippage.FixedSlippage(spread=0.00))`\n", " 設定滑價模型,這裡使用的是固定滑價模型,價差為 0。方便觀察。\n", "* `set_commission(commission.PerDollar(cost=commission_cost))`\n", " 設定交易費用模型,這裡設定一定比例的交易費用。\n", "* `set_benchmark(symbol('IR0001'))`\n", " 設定benchmark,將benchmark設為代碼為 'IR0001' 的資產,即發行量加權股價報酬指數。" ] }, { "cell_type": "code", "execution_count": 4, "id": "0c622aa2", "metadata": { "id": "0c622aa2" }, "outputs": [], "source": [ "def initialize(context):\n", " context.tickers = ['1101']\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'))" ] }, { "cell_type": "markdown", "id": "32cd5711", "metadata": { "id": "32cd5711" }, "source": [ "## 設定交易策略\n", "#### handle_data(context, data):\n", "在每個交易日被調用的函數,用於處理資料並進行交易\n", "- data用於提供回測過程中的資料。通過data,我們可以獲取股票的歷史價格、成交量等資訊。\n", "\n", "對context.asset中的每檔股票,下單購買 1000 股,限價為 45。\n", "```python\n", "for asset in context.asset: \n", " order(asset, 1000, limit_price = 45)\n", "```" ] }, { "cell_type": "code", "execution_count": 5, "id": "3a7281b3", "metadata": { "id": "3a7281b3" }, "outputs": [], "source": [ "def handle_data(context, data):\n", "\n", " for asset in context.asset:\n", " order(asset, 1000, limit_price = 45)" ] }, { "cell_type": "markdown", "id": "c995be5c", "metadata": { "id": "c995be5c" }, "source": [ "### 設定交易成本、回測\n", "- commission_cost:交易成本設置\n", "- capital_base:交易本金" ] }, { "cell_type": "code", "execution_count": 6, "id": "5944167b", "metadata": { "id": "5944167b" }, "outputs": [], "source": [ "commission_cost = 0.001425 + 0.003 / 2\n", "capital_base = 1e6" ] }, { "cell_type": "markdown", "id": "b07edbb3", "metadata": { "id": "b07edbb3" }, "source": [ "## 設置run_algorithm中的函數\n", "run_algorithm是用於運行zipline回測的主要程式碼,通過設定回測的起始日期、結束日期、初始資本金和所使用的數據,並使用initialize、handle_data和 analyze函數來進行回測運算。
\n", "### positions, transactions, orders = get_transaction_detail(performance)\n", " 使用get_transaction_detail產出以下三個DataFrame:\n", " - positions 持倉狀態\n", " - transactions 交易紀錄\n", " - orders 訂單紀錄" ] }, { "cell_type": "code", "execution_count": 7, "id": "bff1c794", "metadata": { "id": "bff1c794", "outputId": "d4277e88-049c-40ee-fb7e-f3df633ade90" }, "outputs": [], "source": [ "closing_price = tejapi.fastget('TWN/APIPRCD',\n", " coid=['1101'],\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": "da96e2c8", "metadata": { "id": "da96e2c8" }, "source": [ "## 講解說明" ] }, { "cell_type": "markdown", "id": "5a003a24", "metadata": {}, "source": [ "### 7/25\n", "- 可以觀察到在每日收盤價7/25的收盤價大於 limit_price 的 45 元,因此只有下單,沒有買入股票。" ] }, { "cell_type": "code", "execution_count": 8, "id": "73406970", "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", "
mdatecoidclose_d
02018-07-24110145.5
12018-07-25110145.1
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "0 2018-07-24 1101 45.5\n", "1 2018-07-25 1101 45.1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[0:2]" ] }, { "cell_type": "code", "execution_count": 9, "id": "e2f1b719", "metadata": { "id": "e2f1b719", "outputId": "ae972903-0d73-4c9f-f368-d2db44d52003", "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
2018-07-24 13:30:00+08:00011017c5082ab4b254b7eae1a047dd1fbe8592018-07-24 13:30:00+08:00None2018-07-24 13:30:00+08:00100000.0None45.0FalseFalseEquity(0 [1101])0
2018-07-25 13:30:00+08:00011011c10829f4e49414c8a0ece983f70e6c72018-07-25 13:30:00+08:00None2018-07-25 13:30:00+08:00100000.0None45.0FalseFalseEquity(0 [1101])0
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-24 13:30:00+08:00 0 1101 7c5082ab4b254b7eae1a047dd1fbe859 \n", "2018-07-25 13:30:00+08:00 0 1101 1c10829f4e49414c8a0ece983f70e6c7 \n", "\n", " dt reason \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 None \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 1000 0 \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 1000 0 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-24 13:30:00+08:00 0.0 None 45.0 False \n", "2018-07-25 13:30:00+08:00 0.0 None 45.0 False \n", "\n", " limit_reached asset status \n", "2018-07-24 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 0 " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 7/24~7/25只有下單,沒有買入股票\n", "orders.loc['2018-07-24':'2018-07-25']" ] }, { "cell_type": "code", "execution_count": 10, "id": "7ab4250d", "metadata": { "id": "7ab4250d", "outputId": "4d0228ec-1e4f-4253-ccad-1439f8b53868", "scrolled": true }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
sidsymbolamountdtpriceorder_idassetcommission
\n", "
" ], "text/plain": [ "Empty DataFrame\n", "Columns: [sid, symbol, amount, dt, price, order_id, asset, commission]\n", "Index: []" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 7/24~7/25沒有交易紀錄\n", "transactions.loc['2018-07-24':'2018-07-25']" ] }, { "cell_type": "markdown", "id": "5a40c368", "metadata": {}, "source": [ "### 7/26\n", "- 2018/7/26是**除權日**,配發 10% 股票股利。ratio = 0.908945。\n", " - orders 中的 amount 由 1000 股變 1100 股(1000 / 0.908945)。\n", " - 在7/24、7/25下的單(created),limit 皆變成 45 * 0.908945 = 40.9。\n", "- 因為7/26的收盤價 40.5 <= 40.9,所以兩單都在7/26成交(**status=1**),下單量也都調整成了 1100(amount)。而且**limit_reached在成交時也都變成了True**。\n", "\n", "- 但是7/26當天下的單(**created = '2018-07-26'**),limit_price 還是 45 沒有調整,amount 還是 1000。代表除權日後如果股價 <= 45(limit_price),則買進 1000 股。" ] }, { "cell_type": "code", "execution_count": 11, "id": "b0d95893", "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", "
mdatecoidclose_d
02018-07-24110145.5
12018-07-25110145.1
22018-07-26110140.5
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "0 2018-07-24 1101 45.5\n", "1 2018-07-25 1101 45.1\n", "2 2018-07-26 1101 40.5" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[0:3]" ] }, { "cell_type": "code", "execution_count": 12, "id": "572821fb", "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
2018-07-24 13:30:00+08:00011017c5082ab4b254b7eae1a047dd1fbe8592018-07-24 13:30:00+08:00None2018-07-24 13:30:00+08:00100000.00000None45.0FalseFalseEquity(0 [1101])0
2018-07-25 13:30:00+08:00011011c10829f4e49414c8a0ece983f70e6c72018-07-25 13:30:00+08:00None2018-07-25 13:30:00+08:00100000.00000None45.0FalseFalseEquity(0 [1101])0
2018-07-26 13:30:00+08:00011017c5082ab4b254b7eae1a047dd1fbe8592018-07-26 13:30:00+08:00None2018-07-24 13:30:00+08:0011001100130.30875None40.9FalseTrueEquity(0 [1101])1
2018-07-26 13:30:00+08:00011011c10829f4e49414c8a0ece983f70e6c72018-07-26 13:30:00+08:00None2018-07-25 13:30:00+08:0011001100130.30875None40.9FalseTrueEquity(0 [1101])1
2018-07-26 13:30:00+08:00011013cd522d9281948798b443b5f0d2046102018-07-26 13:30:00+08:00None2018-07-26 13:30:00+08:00100000.00000None45.0FalseFalseEquity(0 [1101])0
2018-07-27 13:30:00+08:00011013cd522d9281948798b443b5f0d2046102018-07-27 13:30:00+08:00None2018-07-26 13:30:00+08:0010001000117.87750None45.0FalseTrueEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-24 13:30:00+08:00 0 1101 7c5082ab4b254b7eae1a047dd1fbe859 \n", "2018-07-25 13:30:00+08:00 0 1101 1c10829f4e49414c8a0ece983f70e6c7 \n", "2018-07-26 13:30:00+08:00 0 1101 7c5082ab4b254b7eae1a047dd1fbe859 \n", "2018-07-26 13:30:00+08:00 0 1101 1c10829f4e49414c8a0ece983f70e6c7 \n", "2018-07-26 13:30:00+08:00 0 1101 3cd522d9281948798b443b5f0d204610 \n", "2018-07-27 13:30:00+08:00 0 1101 3cd522d9281948798b443b5f0d204610 \n", "\n", " dt reason \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 None \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 None \n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 None \n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 None \n", "2018-07-27 13:30:00+08:00 2018-07-27 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 1000 0 \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 1000 0 \n", "2018-07-26 13:30:00+08:00 2018-07-24 13:30:00+08:00 1100 1100 \n", "2018-07-26 13:30:00+08:00 2018-07-25 13:30:00+08:00 1100 1100 \n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 1000 0 \n", "2018-07-27 13:30:00+08:00 2018-07-26 13:30:00+08:00 1000 1000 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-24 13:30:00+08:00 0.00000 None 45.0 False \n", "2018-07-25 13:30:00+08:00 0.00000 None 45.0 False \n", "2018-07-26 13:30:00+08:00 130.30875 None 40.9 False \n", "2018-07-26 13:30:00+08:00 130.30875 None 40.9 False \n", "2018-07-26 13:30:00+08:00 0.00000 None 45.0 False \n", "2018-07-27 13:30:00+08:00 117.87750 None 45.0 False \n", "\n", " limit_reached asset status \n", "2018-07-24 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-26 13:30:00+08:00 True Equity(0 [1101]) 1 \n", "2018-07-26 13:30:00+08:00 True Equity(0 [1101]) 1 \n", "2018-07-26 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-27 13:30:00+08:00 True Equity(0 [1101]) 1 " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('created.dt.strftime(\"%Y-%m-%d\") in [\"2018-07-24\", \"2018-07-25\",\"2018-07-26\"]')" ] }, { "cell_type": "code", "execution_count": 13, "id": "92ce60b7", "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", "
sidsymbolamountdtpriceorder_idassetcommission
2018-07-26 13:30:00+08:000110111002018-07-26 13:30:00+08:0040.57c5082ab4b254b7eae1a047dd1fbe859Equity(0 [1101])None
2018-07-26 13:30:00+08:000110111002018-07-26 13:30:00+08:0040.51c10829f4e49414c8a0ece983f70e6c7Equity(0 [1101])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2018-07-26 13:30:00+08:00 0 1101 1100 2018-07-26 13:30:00+08:00 \n", "2018-07-26 13:30:00+08:00 0 1101 1100 2018-07-26 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2018-07-26 13:30:00+08:00 40.5 7c5082ab4b254b7eae1a047dd1fbe859 \n", "2018-07-26 13:30:00+08:00 40.5 1c10829f4e49414c8a0ece983f70e6c7 \n", "\n", " asset commission \n", "2018-07-26 13:30:00+08:00 Equity(0 [1101]) None \n", "2018-07-26 13:30:00+08:00 Equity(0 [1101]) None " ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2018-07-26']" ] }, { "cell_type": "markdown", "id": "dd07a513", "metadata": {}, "source": [ "### 7/27\n", "- 因為7/27收盤價為 40.3 <= 45,於是用 40.3 元成交 1000 股。" ] }, { "cell_type": "code", "execution_count": 14, "id": "6a2a0aff", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_d
32018-07-27110140.3
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "3 2018-07-27 1101 40.3" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[3:4]" ] }, { "cell_type": "code", "execution_count": 15, "id": "b8a0ed52", "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
2018-07-26 13:30:00+08:00011013cd522d9281948798b443b5f0d2046102018-07-26 13:30:00+08:00None2018-07-26 13:30:00+08:00100000.0000None45.0FalseFalseEquity(0 [1101])0
2018-07-27 13:30:00+08:00011013cd522d9281948798b443b5f0d2046102018-07-27 13:30:00+08:00None2018-07-26 13:30:00+08:0010001000117.8775None45.0FalseTrueEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-26 13:30:00+08:00 0 1101 3cd522d9281948798b443b5f0d204610 \n", "2018-07-27 13:30:00+08:00 0 1101 3cd522d9281948798b443b5f0d204610 \n", "\n", " dt reason \\\n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 None \n", "2018-07-27 13:30:00+08:00 2018-07-27 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 1000 0 \n", "2018-07-27 13:30:00+08:00 2018-07-26 13:30:00+08:00 1000 1000 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-26 13:30:00+08:00 0.0000 None 45.0 False \n", "2018-07-27 13:30:00+08:00 117.8775 None 45.0 False \n", "\n", " limit_reached asset status \n", "2018-07-26 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-27 13:30:00+08:00 True Equity(0 [1101]) 1 " ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('created.dt.strftime(\"%Y-%m-%d\") == \"2018-07-26\"')" ] }, { "cell_type": "code", "execution_count": 16, "id": "894ff299", "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
2018-07-27 13:30:00+08:000110110002018-07-27 13:30:00+08:0040.33cd522d9281948798b443b5f0d204610Equity(0 [1101])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2018-07-27 13:30:00+08:00 0 1101 1000 2018-07-27 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2018-07-27 13:30:00+08:00 40.3 3cd522d9281948798b443b5f0d204610 \n", "\n", " asset commission \n", "2018-07-27 13:30:00+08:00 Equity(0 [1101]) None " ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2018-07-27']" ] }, { "cell_type": "markdown", "id": "637b7289", "metadata": {}, "source": [ "### 補充:\n", "若原本 limit_price 設為 43.5,其餘條件不變,那7/24、7/25下的那兩單,要一直等到7/31股價 <= 43.5 * 0.908945 = 39.54 後,才會各以當天收盤價 39.35 及 1100 股成交。" ] }, { "cell_type": "code", "execution_count": 17, "id": "f78faf9b", "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", "
mdatecoidclose_d
02018-07-24110145.50
12018-07-25110145.10
22018-07-26110140.50
32018-07-27110140.30
42018-07-30110140.70
52018-07-31110139.35
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "0 2018-07-24 1101 45.50\n", "1 2018-07-25 1101 45.10\n", "2 2018-07-26 1101 40.50\n", "3 2018-07-27 1101 40.30\n", "4 2018-07-30 1101 40.70\n", "5 2018-07-31 1101 39.35" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[0:6]" ] }, { "cell_type": "markdown", "id": "1ac1e3db", "metadata": { "id": "1ac1e3db" }, "source": [ "\n", "# 範例:order(搭配 stop_price)\n", "[Return to Menu](#menu)" ] }, { "cell_type": "markdown", "id": "035b292c", "metadata": { "id": "035b292c" }, "source": [ "## 從2018/07/24到2018/08/14,每天買一張1101,stop_price = 43\n", "### stop_price = xx\n", "當價格>=xx時買入股票\n", "\n", "### handle_data設置\n", "這邊將交易策略進行修改:每天買一張 1101,購買的金額要 >= 43。" ] }, { "cell_type": "code", "execution_count": 18, "id": "5b7bd672", "metadata": { "id": "5b7bd672" }, "outputs": [], "source": [ "def initialize(context):\n", "\n", " context.tickers = ['1101']\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", " for asset in context.asset:\n", " order(asset, 1000, stop_price = 43)\n", "\n", " record(close=data.current(context.asset, 'close'))" ] }, { "cell_type": "code", "execution_count": 19, "id": "d4f82708", "metadata": { "id": "d4f82708", "outputId": "2d4dc886-49e2-43b1-e246-51ac8485d7a5" }, "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", " trading_calendar=get_calendar(calendar_name),\n", " bundle=bundle_name)\n", "\n", "positions, transactions, orders = get_transaction_detail(performance)" ] }, { "cell_type": "markdown", "id": "207bd38a", "metadata": { "id": "207bd38a" }, "source": [ "## 講解說明\n", "\n", "### 7/25\n", " \n", "- 1101 在7/25(還沒發股票股利)收盤價為 45.1 >= 43(stop_price),所以7/24的單在7/25以 45.1 * 1000 股成交。成交時 'stop_reached' 也會變成 True。" ] }, { "cell_type": "code", "execution_count": 20, "id": "8b4d700f", "metadata": { "id": "8b4d700f", "outputId": "76863e3c-815e-4f99-90bf-b071d5830d3b", "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", "
mdatecoidclose_d
02018-07-24110145.5
12018-07-25110145.1
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "0 2018-07-24 1101 45.5\n", "1 2018-07-25 1101 45.1" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[0:2]" ] }, { "cell_type": "code", "execution_count": 21, "id": "c2f440b8", "metadata": { "scrolled": false }, "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
2018-07-24 13:30:00+08:00011019ab43e2f8af8492b8849fb2ba0d31f782018-07-24 13:30:00+08:00None2018-07-24 13:30:00+08:00100000.000043.0NoneFalseFalseEquity(0 [1101])0
2018-07-25 13:30:00+08:00011019ab43e2f8af8492b8849fb2ba0d31f782018-07-25 13:30:00+08:00None2018-07-24 13:30:00+08:0010001000131.917543.0NoneTrueFalseEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-24 13:30:00+08:00 0 1101 9ab43e2f8af8492b8849fb2ba0d31f78 \n", "2018-07-25 13:30:00+08:00 0 1101 9ab43e2f8af8492b8849fb2ba0d31f78 \n", "\n", " dt reason \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 None \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 1000 0 \n", "2018-07-25 13:30:00+08:00 2018-07-24 13:30:00+08:00 1000 1000 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-24 13:30:00+08:00 0.0000 43.0 None False \n", "2018-07-25 13:30:00+08:00 131.9175 43.0 None True \n", "\n", " limit_reached asset status \n", "2018-07-24 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 1 " ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('created.dt.strftime(\"%Y-%m-%d\") == \"2018-07-24\"')" ] }, { "cell_type": "code", "execution_count": 22, "id": "764042bb", "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
2018-07-25 13:30:00+08:000110110002018-07-25 13:30:00+08:0045.19ab43e2f8af8492b8849fb2ba0d31f78Equity(0 [1101])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2018-07-25 13:30:00+08:00 0 1101 1000 2018-07-25 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2018-07-25 13:30:00+08:00 45.1 9ab43e2f8af8492b8849fb2ba0d31f78 \n", "\n", " asset commission \n", "2018-07-25 13:30:00+08:00 Equity(0 [1101]) None " ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2018-07-25']" ] }, { "cell_type": "markdown", "id": "5391f2d0", "metadata": {}, "source": [ "### 7/26\n", "\n", "- 7/26為除權日,7/25的單處理邏輯是:下單量調整成 1000 / 0.908945 = 1100,stop_price 調整為 43 * 0.908945 = 39.08。\n", " - 因為7/26收盤價 40.5 >= 39.08(新 stop_price),所以會以 40.5 * 1100 股成交。" ] }, { "cell_type": "code", "execution_count": 23, "id": "d606ce11", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
mdatecoidclose_d
22018-07-26110140.5
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "2 2018-07-26 1101 40.5" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[2:3]" ] }, { "cell_type": "code", "execution_count": 24, "id": "46a92306", "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
2018-07-25 13:30:00+08:0001101aeceec987b19442ebd37483fb5a6acab2018-07-25 13:30:00+08:00None2018-07-25 13:30:00+08:00100000.0000043.00NoneFalseFalseEquity(0 [1101])0
2018-07-26 13:30:00+08:0001101aeceec987b19442ebd37483fb5a6acab2018-07-26 13:30:00+08:00None2018-07-25 13:30:00+08:0011001100130.3087539.08NoneTrueFalseEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-25 13:30:00+08:00 0 1101 aeceec987b19442ebd37483fb5a6acab \n", "2018-07-26 13:30:00+08:00 0 1101 aeceec987b19442ebd37483fb5a6acab \n", "\n", " dt reason \\\n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 1000 0 \n", "2018-07-26 13:30:00+08:00 2018-07-25 13:30:00+08:00 1100 1100 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-25 13:30:00+08:00 0.00000 43.00 None False \n", "2018-07-26 13:30:00+08:00 130.30875 39.08 None True \n", "\n", " limit_reached asset status \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-26 13:30:00+08:00 False Equity(0 [1101]) 1 " ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('created.dt.strftime(\"%Y-%m-%d\") == \"2018-07-25\"')" ] }, { "cell_type": "code", "execution_count": 25, "id": "62b0c3b7", "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
2018-07-26 13:30:00+08:000110111002018-07-26 13:30:00+08:0040.5aeceec987b19442ebd37483fb5a6acabEquity(0 [1101])None
\n", "
" ], "text/plain": [ " sid symbol amount dt \\\n", "2018-07-26 13:30:00+08:00 0 1101 1100 2018-07-26 13:30:00+08:00 \n", "\n", " price order_id \\\n", "2018-07-26 13:30:00+08:00 40.5 aeceec987b19442ebd37483fb5a6acab \n", "\n", " asset commission \n", "2018-07-26 13:30:00+08:00 Equity(0 [1101]) None " ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "transactions.loc['2018-07-26']" ] }, { "cell_type": "markdown", "id": "da1f030a", "metadata": {}, "source": [ "- 7/26和之後下的單子,因為收盤價一直沒有達到 43,7/26到8/13的單子都累積到了8/14才各自用 43.3 * 1000 股成交。" ] }, { "cell_type": "code", "execution_count": 26, "id": "6d8c8253", "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", "
mdatecoidclose_d
32018-07-27110140.30
42018-07-30110140.70
52018-07-31110139.35
62018-08-01110141.05
72018-08-02110140.60
82018-08-03110140.45
92018-08-06110140.35
102018-08-07110140.15
112018-08-08110140.60
122018-08-09110140.50
132018-08-10110141.50
142018-08-13110142.15
152018-08-14110143.30
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "3 2018-07-27 1101 40.30\n", "4 2018-07-30 1101 40.70\n", "5 2018-07-31 1101 39.35\n", "6 2018-08-01 1101 41.05\n", "7 2018-08-02 1101 40.60\n", "8 2018-08-03 1101 40.45\n", "9 2018-08-06 1101 40.35\n", "10 2018-08-07 1101 40.15\n", "11 2018-08-08 1101 40.60\n", "12 2018-08-09 1101 40.50\n", "13 2018-08-10 1101 41.50\n", "14 2018-08-13 1101 42.15\n", "15 2018-08-14 1101 43.30" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "closing_price[3:]" ] }, { "cell_type": "markdown", "id": "7a0ba317", "metadata": { "id": "7a0ba317" }, "source": [ "- 在下面這張 positions 表格可以看到,7/25的 1000 股(amount),是除權前就成交的(7/24下單,7/25成交)。\n", "- 而7/25的單在除權日(7/26)成交,所以才會變成買入 1100 股,原本帳上持有的 1000 股也在7/26變成 1100 股,帳上共 2200 股(amount)。\n", "- 7/26至8/13共計 13 交易日的單子,則全部在8/14成交,因為下單跟成交都在除權後發生,沒有受到影響,每一單都是 1000 股。" ] }, { "cell_type": "code", "execution_count": 27, "id": "a5650bd0", "metadata": { "id": "a5650bd0", "outputId": "caa8f1aa-854f-4080-87c3-b48f637300e3", "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", "
sidsymbolassetamountcost_basislast_sale_price
2018-07-25 13:30:00+08:0001101Equity(0 [1101])100045.23191845.10
2018-07-26 13:30:00+08:0001101Equity(0 [1101])220040.86423140.50
2018-07-27 13:30:00+08:0001101Equity(0 [1101])220040.86423140.30
2018-07-30 13:30:00+08:0001101Equity(0 [1101])220040.86423140.70
2018-07-31 13:30:00+08:0001101Equity(0 [1101])220040.86423139.35
2018-08-01 13:30:00+08:0001101Equity(0 [1101])220040.86423141.05
2018-08-02 13:30:00+08:0001101Equity(0 [1101])220040.86423140.60
2018-08-03 13:30:00+08:0001101Equity(0 [1101])220040.86423140.45
2018-08-06 13:30:00+08:0001101Equity(0 [1101])220040.86423140.35
2018-08-07 13:30:00+08:0001101Equity(0 [1101])220040.86423140.15
2018-08-08 13:30:00+08:0001101Equity(0 [1101])220040.86423140.60
2018-08-09 13:30:00+08:0001101Equity(0 [1101])220040.86423140.50
2018-08-10 13:30:00+08:0001101Equity(0 [1101])220040.86423141.50
2018-08-13 13:30:00+08:0001101Equity(0 [1101])220040.86423142.15
2018-08-14 13:30:00+08:0001101Equity(0 [1101])1520043.05577643.30
\n", "
" ], "text/plain": [ " sid symbol asset amount cost_basis \\\n", "2018-07-25 13:30:00+08:00 0 1101 Equity(0 [1101]) 1000 45.231918 \n", "2018-07-26 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-07-27 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-07-30 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-07-31 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-01 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-02 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-03 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-06 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-07 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-08 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-09 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-10 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-13 13:30:00+08:00 0 1101 Equity(0 [1101]) 2200 40.864231 \n", "2018-08-14 13:30:00+08:00 0 1101 Equity(0 [1101]) 15200 43.055776 \n", "\n", " last_sale_price \n", "2018-07-25 13:30:00+08:00 45.10 \n", "2018-07-26 13:30:00+08:00 40.50 \n", "2018-07-27 13:30:00+08:00 40.30 \n", "2018-07-30 13:30:00+08:00 40.70 \n", "2018-07-31 13:30:00+08:00 39.35 \n", "2018-08-01 13:30:00+08:00 41.05 \n", "2018-08-02 13:30:00+08:00 40.60 \n", "2018-08-03 13:30:00+08:00 40.45 \n", "2018-08-06 13:30:00+08:00 40.35 \n", "2018-08-07 13:30:00+08:00 40.15 \n", "2018-08-08 13:30:00+08:00 40.60 \n", "2018-08-09 13:30:00+08:00 40.50 \n", "2018-08-10 13:30:00+08:00 41.50 \n", "2018-08-13 13:30:00+08:00 42.15 \n", "2018-08-14 13:30:00+08:00 43.30 " ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "positions" ] }, { "cell_type": "markdown", "id": "612e519c", "metadata": { "id": "612e519c" }, "source": [ "\n", "# 範例:order_target\n", "[Return to Menu](#menu)" ] }, { "cell_type": "markdown", "id": "a7471383", "metadata": { "id": "a7471383" }, "source": [ "`zipline.api.order_target(asset, target, limit_price=None, stop_price=None, style=None)`\n", "\n", "- `order_target`的概念和`order`很像,差別是`order_target`會透過買/賣讓**帳上的股票數量達到指定的數量('target')**,而不像`order`直接買/賣該數量('amount')。\n", "- 若下單股數若有**小數點**情形,則會**取整數**後再進行下單。取整數的方法為:若股數和最近整數相差在 0.0001 以內,就取最接近整數,否則直接去掉小數(3.9999 -> 4.0 ; 5.5 -> 5.0 ; -5.5 -> -5.0)。\n", "- target 單位為**股**。" ] }, { "cell_type": "markdown", "id": "ebc8c24b", "metadata": { "id": "ebc8c24b" }, "source": [ "## 把本單元所有概念結合\n", "### 設定交易策略\n", "#### handle_data\n", "1. ```python\n", " if context.i == 0: # 2018-07-24\n", " for asset in context.asset:\n", " order(asset, 1000)\n", " ```\n", " 在回測的第一個時間點(context.i 為 0,2018-07-24),下單購買 1000 股的1101。\n", " \n", "2. ```python\n", " if context.i == 1: # 2018-07-25\n", " for asset in context.asset:\n", " order_target(asset, 1100)\n", " ```\n", " 在回測的第二個時間點(context.i 為 1,2018-07-25),對投資組合中的每檔股票進行調整,使其持有量達到 1100 股。 \n", " \n", "3. ```python\n", " if context.i == 3: # 2018-07-27\n", " for asset in context.asset:\n", " order_target(asset, 2000)\n", " ```\n", " 在回測的第四個時間點(context.i 為 3,2018-07-27),對投資組合中的每檔股票進行調整,使其持有量達到 2000 股。\n", " \n", "4. ```python\n", " if context.i == 5: # 2018-07-31\n", " for asset in context.asset:\n", " order_target(asset, 3000, stop_price = 40, limit_price = 40.3)\n", " ```\n", " 在回測的第六個時間點(context.i 為 5,2018-07-31),對投資組合中的每檔股票進行調整,使其持有量達到 3000 股。此外,此訂單還設定了(stop_price)為 40 和(limit_price)為 40.3,代表當股價在 >= 40 及 <= 40.3 時才會進行交易。\n", " \n", "5. \n", "```python\n", "record(close=data.current(context.asset, 'close'))\n", "context.i += 1\n", "```\n", "記錄每檔股票的收盤價,並將 `context.i` 遞增 1,表示回測進入下一個時間點。" ] }, { "cell_type": "code", "execution_count": 28, "id": "8c4aa31c", "metadata": { "id": "8c4aa31c" }, "outputs": [], "source": [ "def initialize(context):\n", " context.i = 0\n", " context.tickers = ['1101']\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: # 2018-07-24\n", " for asset in context.asset:\n", " order(asset, 1000)\n", "\n", " if context.i == 1: # 2018-07-25\n", " for asset in context.asset:\n", " order_target(asset, 1100)\n", "\n", " if context.i == 3: # 2018-07-27\n", " for asset in context.asset:\n", " order_target(asset, 2000)\n", "\n", " if context.i == 5: # 2018-07-30\n", " for asset in context.asset:\n", " order_target(asset, 3000, stop_price = 40, limit_price = 40.3)\n", "\n", " record(close=data.current(context.asset, 'close'))\n", " context.i += 1" ] }, { "cell_type": "code", "execution_count": 29, "id": "a0a3bcda", "metadata": { "id": "a0a3bcda", "outputId": "c05c76ab-6a10-426d-b2f5-6356ef4db869" }, "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", " trading_calendar=get_calendar(calendar_name),\n", " bundle=bundle_name)\n", "\n", "positions, transactions, orders = get_transaction_detail(performance)" ] }, { "cell_type": "markdown", "id": "960ad785", "metadata": { "id": "960ad785" }, "source": [ "## 講解說明" ] }, { "cell_type": "markdown", "id": "33c965e9", "metadata": { "id": "33c965e9" }, "source": [ "### 7/24、7/25\n", "- 7/24時用最基本的order功能下單 1000 股的台泥(1101)。並在7/25成交。\n", "- 7/25用 order_target 將目標股數設為 1100,於是下單 100 股。" ] }, { "cell_type": "code", "execution_count": 30, "id": "2c4e83a9", "metadata": { "id": "2c4e83a9", "outputId": "ef6341f9-cdf6-49aa-fd2b-ad8a157e3690", "scrolled": false }, "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", "
sidsymboliddtreasoncreatedamountfilledcommissionstoplimitstop_reachedlimit_reachedassetstatus
2018-07-24 13:30:00+08:0001101820fc923a6ec4f85a374b399011c3d742018-07-24 13:30:00+08:00None2018-07-24 13:30:00+08:00100000.0000NoneNoneFalseFalseEquity(0 [1101])0
2018-07-25 13:30:00+08:0001101820fc923a6ec4f85a374b399011c3d742018-07-25 13:30:00+08:00None2018-07-24 13:30:00+08:0010001000131.9175NoneNoneFalseFalseEquity(0 [1101])1
2018-07-25 13:30:00+08:0001101dbc6e7a6a4474f46920d8008ac93dcdb2018-07-25 13:30:00+08:00None2018-07-25 13:30:00+08:0010000.0000NoneNoneFalseFalseEquity(0 [1101])0
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-24 13:30:00+08:00 0 1101 820fc923a6ec4f85a374b399011c3d74 \n", "2018-07-25 13:30:00+08:00 0 1101 820fc923a6ec4f85a374b399011c3d74 \n", "2018-07-25 13:30:00+08:00 0 1101 dbc6e7a6a4474f46920d8008ac93dcdb \n", "\n", " dt reason \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 None \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-24 13:30:00+08:00 2018-07-24 13:30:00+08:00 1000 0 \n", "2018-07-25 13:30:00+08:00 2018-07-24 13:30:00+08:00 1000 1000 \n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 100 0 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-24 13:30:00+08:00 0.0000 None None False \n", "2018-07-25 13:30:00+08:00 131.9175 None None False \n", "2018-07-25 13:30:00+08:00 0.0000 None None False \n", "\n", " limit_reached asset status \n", "2018-07-24 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 1 \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 0 " ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.loc['2018-07-24':'2018-07-25']" ] }, { "cell_type": "markdown", "id": "b212bf90", "metadata": {}, "source": [ "在下一個交易日(7/26)遇到除權日,ratio = 0.908945,所以7/25的單子amount會調整成 100 / 0.908945 = 110。" ] }, { "cell_type": "code", "execution_count": 31, "id": "419b12d1", "metadata": { "id": "419b12d1", "outputId": "ad8b6bbf-1cc0-4ad6-fdb5-6e9b82d8ce4b", "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
2018-07-25 13:30:00+08:0001101dbc6e7a6a4474f46920d8008ac93dcdb2018-07-25 13:30:00+08:00None2018-07-25 13:30:00+08:0010000.000000NoneNoneFalseFalseEquity(0 [1101])0
2018-07-26 13:30:00+08:0001101dbc6e7a6a4474f46920d8008ac93dcdb2018-07-26 13:30:00+08:00None2018-07-25 13:30:00+08:0011011013.030875NoneNoneFalseFalseEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-25 13:30:00+08:00 0 1101 dbc6e7a6a4474f46920d8008ac93dcdb \n", "2018-07-26 13:30:00+08:00 0 1101 dbc6e7a6a4474f46920d8008ac93dcdb \n", "\n", " dt reason \\\n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 None \n", "2018-07-26 13:30:00+08:00 2018-07-26 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-25 13:30:00+08:00 2018-07-25 13:30:00+08:00 100 0 \n", "2018-07-26 13:30:00+08:00 2018-07-25 13:30:00+08:00 110 110 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-25 13:30:00+08:00 0.000000 None None False \n", "2018-07-26 13:30:00+08:00 13.030875 None None False \n", "\n", " limit_reached asset status \n", "2018-07-25 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-26 13:30:00+08:00 False Equity(0 [1101]) 1 " ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.query('created.dt.strftime(\"%Y-%m-%d\") == \"2018-07-25\"')" ] }, { "cell_type": "markdown", "id": "2d338ab5", "metadata": {}, "source": [ "原本帳上的 1000 股也在除權日調整成 1100 股,所以7/26收盤時手上共有 1100 + 110 = 1210 股。 不會是原先設定的 1100 股。" ] }, { "cell_type": "code", "execution_count": 32, "id": "ede206e1", "metadata": { "id": "ede206e1", "outputId": "f4eee615-da39-4a65-a5a7-a7188bebabf7", "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", "
sidsymbolassetamountcost_basislast_sale_price
2018-07-25 13:30:00+08:0001101Equity(0 [1101])100045.23191845.1
2018-07-26 13:30:00+08:0001101Equity(0 [1101])121041.06531540.5
\n", "
" ], "text/plain": [ " sid symbol asset amount cost_basis \\\n", "2018-07-25 13:30:00+08:00 0 1101 Equity(0 [1101]) 1000 45.231918 \n", "2018-07-26 13:30:00+08:00 0 1101 Equity(0 [1101]) 1210 41.065315 \n", "\n", " last_sale_price \n", "2018-07-25 13:30:00+08:00 45.1 \n", "2018-07-26 13:30:00+08:00 40.5 " ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "positions.loc['2018-07-25':'2018-07-26']" ] }, { "cell_type": "markdown", "id": "9cac63c0", "metadata": { "id": "9cac63c0" }, "source": [ "### 7/27\n", "在7/27時用 order_target 將手上股數調整成 2000,算出還需要 2000 - 1210 = 790股,下單後隔日成交。" ] }, { "cell_type": "code", "execution_count": 33, "id": "ac1159c7", "metadata": { "id": "ac1159c7", "outputId": "3d6d428e-063a-408d-cf10-1139e0cd38bf", "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
2018-07-27 13:30:00+08:0001101d08de58cacf9486ea3600687569cc1542018-07-27 13:30:00+08:00None2018-07-27 13:30:00+08:0079000.000000NoneNoneFalseFalseEquity(0 [1101])0
2018-07-30 13:30:00+08:0001101d08de58cacf9486ea3600687569cc1542018-07-30 13:30:00+08:00None2018-07-27 13:30:00+08:0079079094.047525NoneNoneFalseFalseEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-27 13:30:00+08:00 0 1101 d08de58cacf9486ea3600687569cc154 \n", "2018-07-30 13:30:00+08:00 0 1101 d08de58cacf9486ea3600687569cc154 \n", "\n", " dt reason \\\n", "2018-07-27 13:30:00+08:00 2018-07-27 13:30:00+08:00 None \n", "2018-07-30 13:30:00+08:00 2018-07-30 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-27 13:30:00+08:00 2018-07-27 13:30:00+08:00 790 0 \n", "2018-07-30 13:30:00+08:00 2018-07-27 13:30:00+08:00 790 790 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-27 13:30:00+08:00 0.000000 None None False \n", "2018-07-30 13:30:00+08:00 94.047525 None None False \n", "\n", " limit_reached asset status \n", "2018-07-27 13:30:00+08:00 False Equity(0 [1101]) 0 \n", "2018-07-30 13:30:00+08:00 False Equity(0 [1101]) 1 " ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "orders.loc['2018-07-27':'2018-07-30']" ] }, { "cell_type": "code", "execution_count": 34, "id": "6bf52975", "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", "
sidsymbolassetamountcost_basislast_sale_price
2018-07-30 13:30:00+08:0001101Equity(0 [1101])200040.96803940.7
\n", "
" ], "text/plain": [ " sid symbol asset amount cost_basis \\\n", "2018-07-30 13:30:00+08:00 0 1101 Equity(0 [1101]) 2000 40.968039 \n", "\n", " last_sale_price \n", "2018-07-30 13:30:00+08:00 40.7 " ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "positions[3:4]" ] }, { "cell_type": "markdown", "id": "dad58e6b", "metadata": { "id": "dad58e6b" }, "source": [ "### 7/31\n", "在7/31時下單:`order_target(asset, 3000, stop_price = 40, limit_price = 40.3)`\n", "- 這代表當股價 >= 40 以後,買 1000 股將帳上持有股數從 2000 股調整成 3000 股,但是限制買入價不能超過 40.3。\n", "- 在8/1時股價就超過 40,所以一直要到8/7時股價 <= 40.3 時才會買入 1000 股。" ] }, { "cell_type": "code", "execution_count": 35, "id": "ccefaf2b", "metadata": { "id": "ccefaf2b", "outputId": "b256ad77-4a32-423e-d241-d9a3a9f2b17c", "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
2018-07-31 13:30:00+08:0001101a03305260c3d4760a7c586b5153970fb2018-07-31 13:30:00+08:00None2018-07-31 13:30:00+08:00100000.040.040.3FalseFalseEquity(0 [1101])0
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-07-31 13:30:00+08:00 0 1101 a03305260c3d4760a7c586b5153970fb \n", "\n", " dt reason \\\n", "2018-07-31 13:30:00+08:00 2018-07-31 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-07-31 13:30:00+08:00 2018-07-31 13:30:00+08:00 1000 0 \n", "\n", " commission stop limit stop_reached \\\n", "2018-07-31 13:30:00+08:00 0.0 40.0 40.3 False \n", "\n", " limit_reached asset status \n", "2018-07-31 13:30:00+08:00 False Equity(0 [1101]) 0 " ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 下order_target(asset, 3000, stop_price = 40, limit_price = 40.3)\n", "orders.loc['2018-07-31']" ] }, { "cell_type": "code", "execution_count": 36, "id": "aad2fde3", "metadata": { "id": "aad2fde3", "outputId": "795d75c6-8317-45b6-ce93-ca02f3564d0c" }, "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", "
mdatecoidclose_d
42018-07-30110140.70
52018-07-31110139.35
62018-08-01110141.05
72018-08-02110140.60
82018-08-03110140.45
92018-08-06110140.35
102018-08-07110140.15
\n", "
" ], "text/plain": [ " mdate coid close_d\n", "4 2018-07-30 1101 40.70\n", "5 2018-07-31 1101 39.35\n", "6 2018-08-01 1101 41.05\n", "7 2018-08-02 1101 40.60\n", "8 2018-08-03 1101 40.45\n", "9 2018-08-06 1101 40.35\n", "10 2018-08-07 1101 40.15" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 8/1-8/6股價 > 40.3,8/7 <= 40.3\n", "closing_price[4:11]" ] }, { "cell_type": "code", "execution_count": 37, "id": "37789e9e", "metadata": { "id": "37789e9e", "outputId": "b3679588-cd30-4f42-b179-76dd2d673792", "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
2018-08-07 13:30:00+08:0001101a03305260c3d4760a7c586b5153970fb2018-08-07 13:30:00+08:00None2018-07-31 13:30:00+08:0010001000117.43875None40.3FalseTrueEquity(0 [1101])1
\n", "
" ], "text/plain": [ " sid symbol id \\\n", "2018-08-07 13:30:00+08:00 0 1101 a03305260c3d4760a7c586b5153970fb \n", "\n", " dt reason \\\n", "2018-08-07 13:30:00+08:00 2018-08-07 13:30:00+08:00 None \n", "\n", " created amount filled \\\n", "2018-08-07 13:30:00+08:00 2018-07-31 13:30:00+08:00 1000 1000 \n", "\n", " commission stop limit stop_reached \\\n", "2018-08-07 13:30:00+08:00 117.43875 None 40.3 False \n", "\n", " limit_reached asset status \n", "2018-08-07 13:30:00+08:00 True Equity(0 [1101]) 1 " ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 8/7成交\n", "orders.loc['2018-08-07']" ] }, { "cell_type": "code", "execution_count": 38, "id": "07a471bf", "metadata": { "id": "07a471bf", "outputId": "1536587e-21b5-404d-ad55-9aeb0e951e52" }, "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", "
sidsymbolassetamountcost_basislast_sale_price
2018-07-31 13:30:00+08:0001101Equity(0 [1101])200040.96803939.35
2018-08-01 13:30:00+08:0001101Equity(0 [1101])200040.96803941.05
2018-08-02 13:30:00+08:0001101Equity(0 [1101])200040.96803940.60
2018-08-03 13:30:00+08:0001101Equity(0 [1101])200040.96803940.45
2018-08-06 13:30:00+08:0001101Equity(0 [1101])200040.96803940.35
2018-08-07 13:30:00+08:0001101Equity(0 [1101])300040.73450640.15
\n", "
" ], "text/plain": [ " sid symbol asset amount cost_basis \\\n", "2018-07-31 13:30:00+08:00 0 1101 Equity(0 [1101]) 2000 40.968039 \n", "2018-08-01 13:30:00+08:00 0 1101 Equity(0 [1101]) 2000 40.968039 \n", "2018-08-02 13:30:00+08:00 0 1101 Equity(0 [1101]) 2000 40.968039 \n", "2018-08-03 13:30:00+08:00 0 1101 Equity(0 [1101]) 2000 40.968039 \n", "2018-08-06 13:30:00+08:00 0 1101 Equity(0 [1101]) 2000 40.968039 \n", "2018-08-07 13:30:00+08:00 0 1101 Equity(0 [1101]) 3000 40.734506 \n", "\n", " last_sale_price \n", "2018-07-31 13:30:00+08:00 39.35 \n", "2018-08-01 13:30:00+08:00 41.05 \n", "2018-08-02 13:30:00+08:00 40.60 \n", "2018-08-03 13:30:00+08:00 40.45 \n", "2018-08-06 13:30:00+08:00 40.35 \n", "2018-08-07 13:30:00+08:00 40.15 " ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# 8/7帳上由2000股->3000股\n", "positions[4:10]" ] }, { "cell_type": "markdown", "id": "1e6c8b4c", "metadata": {}, "source": [ "[Return to Menu](#menu)" ] } ], "metadata": { "colab": { "provenance": [] }, "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 }