跳轉到

更新日誌

1.0.6

  • fix(report): 修正 importlib.resources 在 python<=3.6, python==3.8,3.7, python >=3.9 不能用的問題

1.0.5

  • fix(report): 修正 importlib.resources 在 python<=3.8 不能用的問題

1.0.4

  • feat(backtest): 新增回測與標的的相關測試功能
  • fix(backtest): 修正 removing next_weight force to 1 的問題
  • fix(us_market): 修正支援 market_close_at_timestamp support None 的問題
  • chore(pandas 3.0): 支援新版的 Pandas >= 3.0.0 new resample string format
  • feat(backtest): 新增 sl_enter 和 tp_enter,來代表立即停損停利,直到下次換股再買入的標示

1.0.3

Fix

  • 修復支援網頁端執行回測
  • 修正 report.current_trades 假如有重複的股票名稱,以最新的部位為主
  • 修正儀表板顯示每月營收的計算方式,從 1 號到 31 號,變成跟之前一樣,從上月 31 號到此月 31 號
  • 修正 trail_stop 在買同一張股票時,提早出場的問題
  • 隔天要進場,同時又停損停利,針對 stop_trading_next_period 對於 report.action 以及 report.next_weights 的顯示資料進行修正,並增加單元測試。

1.0.2

Refactor

  • 移除 tailwind default css,改而使用 vite 輸出的 style.css
  • 移除 web server 改而使用 iframe docsrc (經過測試支援所有平台)

Fix

  • 顯示在 docker 上 web server port 無法使用的問題

1.0.1

Features

  • 顯示移動停損
  • 儀表板右上角控制 light/dark 模式
  • 另存儀表板至指定的路徑下

Fix

  • 修正 Windows 下 report 預設讀寫編碼為 cp950 造成的問題。
  • 修正停損無法顯示
  • Colab 中 CSS 字體與排版調整
  • light/dark 模式中,圖表座標軸顏色響應
  • 修正圖表記憶體沒有釋放,導致 tab 切換延遲的問題

1.0.0

Features

  • 新增 Position.from_report 的單元測試
  • 在多種條件下測試 report.next_weights 功能
  • 推出全新儀表板介面
  • 支援 Python 3.12 版本

Fix

  • 修正了在啟用止損或止盈時,報告中 report.next_weights 的正規化問題
  • 解決了在 Pandas 的 reindex 函數中,分類型欄位無法正確索引的問題
  • 修正了在添加運算子時,分類型欄位繪圖錯誤的問題

0.5.13

Features

  • 整合 Pytest(提交 e7a487c):加入了 Pytest,讓測試更高效。
  • finlab.ml 日內交易報酬率計算(提交 8804fb1):新增了計算日內交易的 label
  • finlab.ml.feature.ta 特徵計算支持多進程(提交 1ab0463):在特徵計算模組中引入多進程支持,提升性能。

Fix

  • 優化 'get_location' 函數(提交 253e98c):改進了 'data.py' 中的 'get_location' 函數,使其更好地處理失敗情況。
  • 市場信息支持時區(提交 aa2756b & 79b3671):在 'market_info.py' 中加入了時區信息的處理。
  • 移動停損修正(提交 6522bf7 & e7fc1f3):調整了 'backtest.py' 中的交易計算,修正移動停損的功能。
  • Dataframe 的導入修正(提交 626ada1):修正了 'dataframe.py' 中的導入問題。
  • 特徵初始化修正(提交 772d659):解決了 'ml/feature.py' 中的初始化問題。
  • 強制雲端下載(提交 7ec28a3):修復了 'data.py' 中的 'force_cloud_download' 實現。

Other updates

  • 重構(提交 26d1518):對 'online' 模組進行了重構,使代碼結構更好。
  • 文檔更新(提交 44a93d4, 9f5683f, cd4a894):更新了 'backtest.py'、'data.py' 和 'ml/feature.py' 的文檔。
  • 版本更新(提交 d361d7f):將版本升級到了 0.5.13。
  • 增加測試覆蓋率(提交 350dfbc):在 Docker 配置中增加了測試覆蓋率。
  • 測試系統的穩健性(提交 1647479):提升了測試系統的穩健性。
  • 在線測試 Position.to_list(提交 f3d086f):更新了 'online_test.py',增加了轉換為列表的功能。

0.5.12

  • 修正了 run_online_test.sh 中的在線測試更新價格,以包含 FUGLE 配置的環境變量。
  • 調整了 online_test.py 中的 test_update_price,以正確比較浮點數量。
  • 修改了 event_study.py,通過重新索引工作日使其與非交易日兼容。

0.5.11

  • 修復 position 為零時,會報 error 的問題

0.5.10

  • 永豐 API 下單大於 1000 元無法下單的問題
  • 修正 FinlabDataframe 的 memory leak error
  • 修正登入顯示的樣式,避免 colab 登入時出現 error
  • 修正 data.search 的問題

0.5.9

  • 修正 dataframe operator 運算時的 reshape 問題
  • 改善 df[bool_selector] 無法使用的問題

0.5.8

  • 改善 data.get速度
  • 更新永豐 API 下單效率(然而引出一些問題,已經修復)

0.5.7 (2023-11-4)

Fix 🚀

  • FinlabDataFrame:若未設置則正確使用 UUID 修正了__hash__方法。
  • hold_until方法:修正了selfexitrankreindex問題。
  • market_info.get_market_info:更正了reindex邏輯,確保使用更新後的索引。
  • fetch_data:修正了報告部分的日均計算錯誤。
  • data.py:為 HTTP 請求添加了User-Agent標頭以防止取得錯誤。
  • data.py:修正了保存空DataFrame的問題。
  • ffn_core.py:修正了calc_sharpe方法中除以零的錯誤。

Features 🚀

  • tools: 新增 finlab.tools.event_study 研究事件交易等相關功能。
  • backtest.py:引入了fast_mode,在不考慮止損或止盈的情況下進行快速回測。
  • dataframe.py:將index_str_to_date方法進行了緩存,提高性能。
  • report.add_trade_info:導入 Cython 優化運算速度。
  • setup.py:編譯 Cython 擴展時啟用 O3 優化。
  • backtest.py:為sim函數新增文檔,詳細說明新特性和參數。
  • dataframe.py:實現了索引優化,將字符串索引轉換為日期並進行緩存。
  • 調整setup.py,將 Python 版本要求從 >=3.7 改為 >=3.6。
  • 更新子模組至最新提交。

Tests 🚀

  • report_test.py模塊新增測試,以保證查詢功能的正確性。
  • report_test.py新增多個針對不同數據類型的測試案例。
  • 更新run_test_docker.sh腳本,包含新的測試案例。

0.5.2 (2023-09-28)

Fix 🚀

  • 在少部分的 OS 上出現 recursive import 的問題
  • 修正 liquility analysis 中產生 NaN 時,被 Pandas 暗自轉換成 float 造成的問題
  • trade_at_price 修正
  • 修正在嘗試 merge 過程中消耗大量記憶體的問題

0.5.0 (2023-09-14)

Features 🚀

  • 新增移動停損功能 sim(..., trail_stop=0.1) 來設定移動停損。
  • data.universe 支援 market='etf' 另外,也可以選 ETF 的種類如:domestic_etf, foreign_etf, leveraged_etf, vanilla_futures_etf, leveraged_futures_etf
  • data.get 支援從美國、台灣兩地更新資料,會自動選擇比較近的伺服器來索取資料。
  • data.get 自動偵測並連結 Google Drive 來存放下載的資料。
  • data.get 將全面支援在本地端更新資料,以後再也不用全面下載所有資料,而是下載 patch 並藉由本地端的程式來進行維護,只有在必要的時候進行整批資料的重置(預設開啟)
  • sim(..., trade_at_price) 可選擇其他價格來模擬買賣,可選'close'、'open'、'open_close_avg'、'high_low_avg'或 'price_avg'。
  • 新增底層功能以確保新的網站能夠兼容

Fixes

  • 修正做空的停損停利
  • FinlabDataFrame.hold_until 現在輸出為 bool (而非以前的 int)
  • FinlabDataFrame 更廣泛的支援自動 reshape 與填值的運算,例如「&=」、「+=」等目前已支援
  • 移除了強制設置的 logger
  • finlab.online 修正了獲取總餘額時的時區問題。
  • data.FileStorage 解決了 Windows 系統上的文件路徑錯誤。
  • ffn 移除即將不支援的語法,更新了 pandas2.1 間容的語法。
  • 自動偵測 backtest(position) 中的 position.index 轉換為 datetime
  • 解決 report.trades 太長上傳的問題
  • 解決回測時上次出場的標的,這次不進場的錯誤

0.4.6 (2023-08-11)

Fixes 🔧

  • 修正優勢價格買賣(extra_bid_pct)在委託價格超過漲跌停價時的委託失敗問題,委託價以漲跌停為上限。
  • 解決了下單 API 中 Position.from_report 停損停利部位判斷錯誤的問題。
  • 解決了永豐 API 零股改價失敗問題,自動刪單再重下。
  • 月營收索引改為 datetime 格式,解決 data.set_storage 模式下強制下載月營收資料的問題。
  • 避免 finlab.dataframe.industry rank() 出現 nan 數值。

Features 🚀

  • 加入策略報籌率統計分析圖表功能 finlab.plot.StrategyReturnStats。
  • Fugle 行情 API v1.0 版 相容 finlab.online,不相容 v0.3。 提醒用戶請將行情 API v0.3 版本升級至最新的 v1.0 版本,舊版本將被 Fugle 官方棄用,要注意在行情 API v1.0 版本中,採用了不同的身份驗證方式,因此 v0.3 的 API 金鑰將無法適用於 v1.0 的 HTTP API 和 WebSocket API。您需要重新申請 API 金鑰以使用最新版本的 API。請前往 金鑰申請頁面 進行申請 https://developer.fugle.tw/docs/key/%E3%80%82
  • 新增下單 API 於「更新委託單價格」時支援優勢價格買賣(extra_bid_pct)的功能,以高於成交價的 x%買入,低於成交價的 x%賣出。

0.4.5 (2023-07-05)

Fixes 🔧

  • 解決了在 hold_until 中 rank 參數 inf 導致的排名功能失效問題。
  • 修正了 alphalens 在 pandas 2.0 版本上的不相容問題。
  • 解決了永豐 API 下單時,零股市價無法下單的問題,現在改成漲跌停下單。
  • 針對 candle plot 的標題顯示位移問題進行了修正。
  • 修正了 report.position 丟入 sim 後得到一致結果的問題。
  • 解決了 edge ratio 時間序列排序問題,現在可以按時間順序排序。

Features 🚀

  • 加入 us_universe 選擇美股子產業的功能。
  • 新增下單 API 支援優勢價格買賣的功能,現在可以以高於成交價的 x%買入,低於成交價的 x%賣出。

Doc 📚

  • 新增美股策略範例,提供用戶參考和學習。
  • 新增在 data.indicator 中使用美股時的 market 參數說明,使用者可以更容易理解其使用方式。

0.4.3 (2023-05-11)

Warnings: Backtesting result and performance will change.

Features 🚀

  • Improved RAM usage by removing sorting and swaplevel for machine learning features (e081c924).
  • Optimized backtest report (8babbd7a).
  • Implemented cross-validation for machine learning (e55828c0).
  • support infomation for stop loss and take profit assets (5f9272f3).
  • Hold_until functionality made available for US data (8437d62).
  • Added indicators control in plot_stats (7364d58).

Fixes 🔧

  • Fixed deadline considering business date for DataFrame (312f2fe1).
  • Fixed the issue where a float object had no attribute 'month' (d51a5901).
  • Fixed run_all_test (6203f56f).
  • Prevented deletion during map iteration in backtest_core (2d1d9b05).
  • Fixed login info (d177ed47).
  • Fixed alpha annualize estimation in analysis (26a8becf).
  • Fixed issue with short selling in online module (919521b9).
  • Synced win_ratio in maeMfeAnalysis (a9a31189).
  • Added rotc deadline in DataFrame (ac10597).

Refactoring 💡

  • Refactored f_disclosure_date to_business_date, removed date too far, and improved speed (7dcb203b).
  • Refactored market selection according to DataFrame columns (039cbc96).
  • Fixed bugs and future warnings in the analysis module (ec93088f).
  • Refactored environment name capitalization in login module (635caed4).
  • Refactored login to use FINLAB_API_TOKEN (a5c9064d).
  • Informed free users that data is limited (bbabad14).

Tests 🧪

  • Added test_hold_until (ccc4ba89).

Doc 🗂️

  • Updated daily_sortino documentation (b7d1d91d).
  • Updated get_data documentation (276862c2).

0.4.2 (2023-04-27)

新增功能:

  • 在機器學習中加入了標籤交易價格的功能
  • 在報告中新增了 report.display() 的使用說明
  • 在市場資訊中新增了美國市場資訊

修復問題:

  • 在線上程式中修正了顯示字串的問題
  • 將回測功能回復到穩定版本
  • 調整回測功能中根據進出場價格調整持倉的問題
  • 修正 qlib.init 的偵測問題
  • 在報告中修正了基準指數時區的問題
  • 處理了 pandas 2.0.0 版本兼容性問題
  • 處理了 talib 依賴性問題
  • 修正了在回測功能中進出場比例為 nan 的問題
  • 在文檔中修正了 fugle Account numpy 的引用問題

其他:

  • 在機器學習中新增了相關文件
  • 在市場資訊中新增了相關文件

0.4.1 (2023-04-14)

  • (fix): fugle np not found

0.3.19 (2023-04-04)

  • (improve): memory usage in machine learning feature creation
  • (improve): machine learning feature for different freqnecy index
  • (fix): data.indicator support almost all talib functions
  • (fix): 2023/4/10 monthly revenue reveal change to 2023/4/12
  • (fix): sinopac calculate account balance

0.3.18 (2023-03-28)

  • (Breaking): update shioaji >= 1.0.0. Please refer to finlab.inline module
  • (feat): finlab.ml (document are not ready yet)
  • (feat): support qlib data
  • (feat): report add yearly and monthly return table
  • (fix): backtest entry exit signal both activate conflict

0.3.12 (2022-11-29)

  • (Breaking): report.get_stats() has new format
  • (feat): data.get now read expire date from server and update data dynamically
  • (feat): add a bunch of analysis for backtesting report
  • (feat): getpass password to hide api_token
  • (fix): report.weight and report.next_weights remove considering of price changed
  • (fix): backtest(..., resample_offset='10d') fix backtesting display date
  • (fix): finlab.online.order_executor show_alerting_stocks fix price
  • (fix): fugle order status not change even when it is filled
  • (fix): support update price of fraction lot size

0.3.11 (2022-11-10)

  • (Fix) finlab backtest asset shorting fee miner adjust
  • (Fix) finlab.report.weights and finlab.report.next_weights not defined
  • (Fix) fugle-trade remove flag accoding to 0.4.0
  • (Fix) fugle-trade fetch other source when stock.close is nan
  • (Fix) fugle-trade FugleAccount.get_position not latest position
  • (Feat) finlab.data.universe('STOCK_FUTURE') subset of TWII stocks
  • (Feat) add yearly return when upload report
  • (Feat) add fugle_trade version not compatable warning

0.3.10 (2022-10-13)

0.3.9 (2022-10-13)

  • (Fix) finlab.online.sinopac_account order_lot position zero
  • (Fix) finlab.online.fugle_account order_lot position duplicated
  • (Fix) finlab.backtest weight display remove normalized
  • (Fix) finlab.data windows FileStorage fix
  • (Fix) finlab.data indicator exception error

0.3.8 (2022-9-27)

  • (Fix) finlab.online.order_executor price +-10% limit order restricted
  • (Fix) finlab.online.order_executor Position.from_report handle price=0, weight=0, and duplicated stock id
  • (Fix) finlab.plot.StrategySunburst compatibility
  • (Fix) finlab.report.display display multiple chart

0.3.8.dev1 (2022-9-27)

  • (Fix) talib abstract module incompatible
  • (Fix) backtest result: alpha and beta
  • (Fix) finlab.online.panel
  • (Fix) finlab.online.fugle_account stock not traded error
  • (Fix) add cert custom password for sinopac
  • (Fix) remove duplicated columns from unnumeric_dtype.
  • (Fix) KY company disclosure deadline
  • (Chore) Candlestick data format generalized
  • (Chore) raise error when data.get not found
  • (Chore) logger.info to logger.warning in finlab.data
  • (Chore) fig return for report.display
  • (Feat) add retain_cost_when_rebalance flag

0.3.7.dev1 (2022-8-8)

  • 針對 touched_exit 做更細部的回測:
  • 原本觸價反彈後不會被列入觸價賣出,現在會模擬觸價賣出
  • 原本開盤跳空,會沒辦法賣在 0.07,現在也會賣在更低的位置

0.3.5.dev (2022-7-18)

  • 新增 finlab.optimize 枚舉條件組合回測
  • 新增 mae_mfe 圖表
  • mae_mfe 做空分析
  • report.get_trades()['position'] 改成 weighting
  • backtest touched_exit 修正
  • 修復 finlab.analysis bugs

0.3.3.dev (2022-7-1)

Bugs 修正

  • 修正放空股票的 weight 權重顯示問題
  • TreeMap 繪圖修正
  • 財報公佈歷史時間修正

Analysis 分析回測結果

新增 Analysis 架構,可以將回測結果完整分析。 之後的分析都將繼承 Analysis 並且顯示圖表,讓 report 使用起來更豐富!

liquidityAnalysis 流動性測試

策略會模擬股票在收盤或開盤價買進賣出,但實際上,股票也有可能沒辦法順利交易,例如遇到處置股、全額交割股,或是長跌停板。 liquidityAnalysis 會檢測回測時,是否有買到這類行的標的,並且計算比例,已確保策略的流動性符合您的資金需求。 查看更詳細的教學文章。

report.run_analysis('LiquidityAnalysis')

inequalityAnalysis 不等式測試

原本已經有策略,想要額外加入其他技術指標、籌碼、獲利指標,可以透過 inequalityAnalaysis 來檢視是否能夠提高選股績效。 以股價淨值比而言,我們想觀察以股價淨值比來進一步篩選股票的績效,可以用以下的方式:

report.run_analysis('InequalityAnalysis', name='price_earning_ratio:股價淨值比')

要如何看結果,可以參考更詳細的教學文章。

0.3.2.dev (2022-6-23)

SunBurst 檢視策略部位

可以顯示當前的股票,有哪些產業,產業的偏重是哪一塊,查看詳細內容

做空以及個股權重

FinLab Package 終於支援做空了,假如你原本就有一個好策略,可以做空 0050 試試看,雖然報酬率可以能會下降,但是風險也會大幅縮小!非常適合我們做多檔股票資產配置使用。

另外值得一提的是,新的版本 position 除了以前的布林訊號 (True/False) 外,也可以使用權重!所以下方程式碼,就是 50% 比重給 2330 做多,50% 給 1101 做空。

這樣的權重分配方式,完全兼容之前的版本,也就是 position 是 True / False 的狀況,因為 True 在電腦運算中,本來就代表 1 ,而 False 代表 0。權重都是 1 的情況下,程式就會跟以前一樣平均分配資產喔。

from finlab import data
from finlab import backtest

close = data.get('price:收盤價')

position = close < 0
position['2330'] = 0.5
position['1101'] = -0.5

r = backtest.sim(position)
r.display()

支援 Pandas_ta 計算技術指標

Pandas_ta 最近一兩年算是計算技術指標的後起之秀,原因在於 TaLib 有時候實太難安裝了,而且技術指標就只有那 100 個,都是老調重彈,沒有更新。有鑒於此,Pandas_ta 採用純 Pandas 計算技術指標,跟不同的作業系統有比較好的相容性。另外,Pandas_ta 也有很多最新的技術指標,例如 supertrend indicator。有非常多沒有看過的指標,像是「Even Better Sinewave 」,聽起來實在是非常有趣呀!這些都可以在 Pandas_ta 文檔中找到。

from finlab import data

values = data.indicator('supertrend')