Zipline-tej

Latest version: v2.1.0

Safety actively analyzes 706259 Python packages for vulnerabilities to keep your Python projects secure.

Scan your dependencies

Page 1 of 5

2.2.0rc2

2.2.0rc1

2.2.0rc0

統計

新增

1. 如果有context.margin_table那麼會依照給予的狀態計算初始保證金跟保留保證金

2. context中的Account 新增 available_cash , preserved_cash

3. preserved_cash 定義為 `放空現股的股價 * 放空數量 * 2`

(*2的原因是因為賣出會導致現金增加,但其實不能使用,須扣除;且需額外繳交保證金,這裡預設乘數為1)

4. available_cash 定義為 `cash - preserved_cash`

5. default metrics 新增 `Preserved_Cash` 欄位

6. api新增 `set_auto_close_amount` , `set_auto_close_percent` , 參數皆為 profit , loss

set_auto_close_amount

* 為當持有部位當天收盤時,損益大(小)於profit(loss) 則隔日會下單平倉,其中amount代表金額

set_auto_close_percent

* 為當持有部位當天收盤時,損益大(小)於profit(loss) 則隔日會下單平倉,其中percent代表報酬率

修改

1. context中的 Account 的 settled_cash 由 cash 改用 available_cash

2. 股票期貨在給現金股利時,會乘上`price_multiplier`

3. 股票期貨在發放退回現金股利時,當天必須持有部位

2.1.0

功能新增

1. 新增設定回測成交價格設定,於initialize中加入`set_execution_price_type`,參數為 open , high , close , low。

python
def initialize(context):
...
context.set_execution_price_type('open') open , high , close , low
...



2. 於 `zipline.data.run_ingest` 中新增 `simple_ingest` 功能,使得ingest更加容易。

python
import os
os.environ['TEJAPI_KEY'] = '<your_key>'
...
from zipline.data.run_ingest import simple_ingest
價量資料
simple_ingest(name = 'tquant' , tickers = ['2330'] , start_date = '20200101' , end_date = '20220101')
基本面資料
simple_ingest(name = 'fundamentals' , tickers = ['2330'] , start_date = '20200101' , end_date = '20220101' , fields = ["eps"])
...


3. 新增預設網域,未來在 `import zipline` 之前,僅需設定 `TEJAPI_KEY` 即可,預設網域為 `https://api.tej.com.tw`。

python
原先
import os
os.environ['TEJAPI_BASE'] = "https://api.tej.com.tw"
os.environ['TEJAPI_KEY'] = '<your_key>'
import zipline

現在
import os
os.environ['TEJAPI_KEY'] = '<your_key>'
import zipline


4. Annotation 部分新增 `先買後賣交易限制`。

5. class TargetPercentPipeAlgo 新增 `min_long_count` 與 `min_short_count` 參數,以用於確定再平衡的**最小做多檔數**必須大於`min_long_count`或**最小做空檔數**大於`min_short_count`才會進行再平衡。

6. 將 PctSlippage 滑價模型加入至zipline.api

python
from zipline.api import PctSlippage
...


7. 新增 TW_Slippage 滑價模型


TW_Slippage 說明 :

"""

Simple custom model for Taiwan Market.

Parameters

spread : float, optional

Size of the assumed spread for all assets.

Orders to buy will be filled at either limit_up price or ``execute_price + (spread * tick_diff)``.

Orders to sell will be filled at either limit_down price or ``execute_price - (spread * tick_diff)``.

volume_limit : float, optional

Maximum percent of historical volume that can fill in each bar. 0.5

means 50% of historical volume. 1.0 means 100%. Default is 0.025 (i.e.,

2.5%).

"""


python
from zipline.finance.slippage import TW_Slippage
from zipline.api import set_slippage
...
def initialize(context) :
...
set_slippage(TW_Slippage(spread = 2 , volume_limit = 0.025))
...
...



功能調整

1. 在回測期間基於當前交易價格,而非收盤價,修改 `order_value`、`order_target_value`、`order_percent` 和 `order_target_percent` 函數來計算數量和目標金額,確保在多變的市場條件下進行更精確的交易模擬。

以下將針對有改動的部分以表格進行列示

1. order_value

| 成交方式 | price_type | 下單股數 |
| --- | --- | --- |
| current_bar | open | value/ open(t) |
| current_bar | high | value/ high(t) |
| current_bar | low | value/ low(t) |

2. order_target_value

| 成交方式 | price_type | 目標股數 | 下單股數 |
| --- | --- | --- | --- |
| current_bar | open | value/open(t) | 目標股數 - 帳上持有股數(t期初,經除權調整) |
| current_bar | high | value/high(t) | 目標股數 - 帳上持有股數(t期初,經除權調整) |
| current_bar | low | value/low(t) | 目標股數 - 帳上持有股數(t期初,經除權調整) |

3. order_percent

| 成交方式 | price_type | 下單價值 | 下單股數 |
| --- | --- | --- | --- |
| current_bar | open | portfolio_value (t期初,經除權調整) * percent | value/open(t) |
| current_bar | high | portfolio_value (t期初,經除權調整) * percent | value/high(t) |
| current_bar | low | portfolio_value (t期初,經除權調整) * percent | value/low(t) |
| current_bar | close | portfolio_value (t期初,經除權調整) * percent | value/close(t) |

4. order_target_percent

| 成交方式 | price_type | 目標價值 | 目標股數 | 下單股數 |
| --- | --- | --- | --- | --- |
| current_bar | open | portfolio_value (t期初,經除權調整) * percent | value/open(t) | 目標價值 - 帳上持有股數(t期初,經除權調整) |
| current_bar | high | portfolio_value (t期初,經除權調整) * percent | value/high(t) | 目標價值 - 帳上持有股數(t期初,經除權調整) |
| current_bar | low | portfolio_value (t期初,經除權調整) * percent | value/low(t) | 目標價值 - 帳上持有股數(t期初,經除權調整) |
| current_bar | close | portfolio_value (t期初,經除權調整) * percent | value/close(t) | 目標價值 - 帳上持有股數(t期初,經除權調整) | |

2.1.0rc2

新增台灣滑價模型

2.1.0rc1

Page 1 of 5

© 2025 Safety CLI Cybersecurity Inc. All Rights Reserved.