功能新增
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期初,經除權調整) | |