finlab.online
finlab.online.base_account.Account
Bases: ABC
股票帳戶的 abstract class 可以繼承此 Account,來實做券商的帳戶買賣動作,目前已經實做 SinopacAccount (永豐證券) 以及 FugleAccount (玉山富果),來進行交易。可以用以下方式建構物件並用來交易:
永豐證券
import os
from finlab.online.sinopac_account import SinopacAccount
# 舊版請使用
# shioaji < 1.0.0 and finlab < 0.3.18
os.environ['SHIOAJI_ACCOUNT']= '永豐證券帳號'
os.environ['SHIOAJI_PASSWORD']= '永豐證券密碼'
# 新版請使用
# shioaji >= 1.0.0 and finlab >= 0.3.18
os.environ['SHIOAJI_API_KEY'] = '永豐證券API_KEY'
os.environ['SHIOAJI_SECRET_KEY'] = '永豐證券SECRET_KEY'
os.environ['SHIOAJI_CERT_PERSON_ID']= '身份證字號'
# shioaji
os.environ['SHIOAJI_CERT_PATH']= '永豐證券憑證路徑'
os.environ['SHIOAJI_CERT_PASSWORD'] = '永豐證券憑證密碼' # 預設與身份證字號
acc = SinopacAccount()
from finlab.online.fugle_account import FugleAccount
import os
os.environ['FUGLE_CONFIG_PATH'] = '玉山富果交易設定檔(config.ini.example)路徑'
os.environ['FUGLE_MARKET_API_KEY'] = '玉山富果的行情API Token'
acc = FugleAccount()
cancel_order
abstractmethod
刪除委託單
建議使用 刪除委託單此功能前,先使用 update_order() 來更新委託單的狀況!如下
ATTRIBUTE | DESCRIPTION |
---|---|
order_id |
券商所提供的委託單 ID
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
代表成功更新委託單 |
Source code in finlab/online/base_account.py
create_order
abstractmethod
產生新的委託單
PARAMETER | DESCRIPTION |
---|---|
action |
買賣方向,通常為 'BUY' 或是 'SELL'
TYPE:
|
stock_id |
股票代號 ex: '2330'
TYPE:
|
quantity |
委託股票的總數量(張數),允許小數點
TYPE:
|
price |
股票買賣的價格(限價單)
TYPE:
|
force |
是否用最差之價格(長跌停)強制成交? 當成交量足夠時,可以比較快成交,然而當成交量低時,容易有大的滑價
TYPE:
|
wait_for_best_price |
是否用最佳之價格(長跌停),無限時間等待?當今天要出場時,可以開啟等漲停價來購買,當今天要買入時,可以掛跌停價等待買入時機。
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
order id 券商提供的委託單編號 |
Source code in finlab/online/base_account.py
get_orders
abstractmethod
拿到現在所有委託單
RETURNS | DESCRIPTION |
---|---|
Dict[str, Order]
|
所有委託單 id 與委託單資料 Example
|
get_position
abstractmethod
get_stocks
abstractmethod
拿到現在股票報價
ATTRIBUTE | DESCRIPTION |
---|---|
stock_ids |
一次拿取所有股票的報價,ex: ['1101', '2330']
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
報價資料, Example
|
Source code in finlab/online/base_account.py
get_total_balance
abstractmethod
update_order
abstractmethod
產生新的委託單
ATTRIBUTE | DESCRIPTION |
---|---|
order_id |
券商所提供的委託單 ID
TYPE:
|
price |
更新的限價
TYPE:
|
quantity |
更新的待成交量
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
無跳出 erorr 代表成功更新委託單 |
Source code in finlab/online/base_account.py
finlab.online.order_executor.Position
Position(stocks, margin_trading=False, short_selling=False, day_trading_long=False, day_trading_short=False)
使用者可以利用 Position 輕鬆建構股票的部位,並且利用 OrderExecuter 將此部位同步於實際的股票帳戶。
建構股票部位
ATTRIBUTE | DESCRIPTION |
---|---|
stocks |
TYPE:
|
margin_trading |
做多部位是否使用融資
TYPE:
|
short_selling |
做空部位是否使用融券
TYPE:
|
day_trading_long |
做多部位為當沖先做多
TYPE:
|
day_trading_short |
做空部位為當沖先做空
TYPE:
|
Examples:
設計部位,持有一張和 100 股 1101
output將兩個部位相加
from finlab.online.order_executor import Position
p1 = Position({'1101': 1})
p2 = Position({'2330': 1})
p1 + p2
[
{'stock_id': '1101', 'quantity': 1.0, 'order_condition': <OrderCondition.CASH: 1>},
{'stock_id': '2330', 'quantity': 1.0, 'order_condition': <OrderCondition.CASH: 1>}
]
Source code in finlab/online/order_executor.py
from_json
classmethod
Load a JSON file from the given path and convert it to a list of positions.
PARAMETER | DESCRIPTION |
---|---|
path |
The path to the JSON file.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in finlab/online/order_executor.py
from_list
classmethod
利用 dict
建構股票部位
Source code in finlab/online/order_executor.py
from_report
classmethod
利用回測完的報告 finlab.report.Report
建構股票部位。
ATTRIBUTE | DESCRIPTION |
---|---|
report |
回測完的結果報告。
TYPE:
|
fund |
希望部屬的資金。
TYPE:
|
price |
股票代號對應到的價格,若無則使用最近個交易日的收盤價。
TYPE:
|
odd_lot |
是否考慮零股。預設為 False,只使用整張操作。
TYPE:
|
board_lot_size |
一張股票等於幾股。預設為1000,一張等於1000股。
TYPE:
|
allocation |
資產配置演算法選定,預設為
TYPE:
|
Example
Source code in finlab/online/order_executor.py
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
|
from_weight
classmethod
from_weight(weights, fund, price=None, odd_lot=False, board_lot_size=1000, allocation=greedy_allocation, precision=None, **kwargs)
利用 weight
建構股票部位
ATTRIBUTE | DESCRIPTION |
---|---|
weights |
股票詳細部位
TYPE:
|
fund |
資金大小
TYPE:
|
price |
股票代號對應到的價格,若無則使用最近個交易日的收盤價。
TYPE:
|
odd_lot |
是否考慮零股
TYPE:
|
board_lot_size |
一張股票等於幾股
TYPE:
|
precision |
計算張數時的精度,預設為 None 代表依照 board_lot_size 而定,而 1 代表 0.1 張,2 代表 0.01 張,以此類推。
TYPE:
|
allocation |
資產配置演算法選定,預設為預設為
TYPE:
|
margin_trading |
做多部位是否使用融資
TYPE:
|
short_selling |
做空部位是否使用融券
TYPE:
|
day_trading_long |
做多部位為當沖先做多
TYPE:
|
day_trading_short |
做空部位為當沖先做空
TYPE:
|
Examples:
例如,用 100 萬的資金,全部投入,持有 1101 和 2330 各一半:
from finlab.online.order_executor import Position
Position.from_weight({
'1101': 0.5,
'2330': 0.5,
}, fund=1000000)
[
{'stock_id': '1101', 'quantity': 13, 'order_condition': <OrderCondition.CASH: 1>},
{'stock_id': '2330', 'quantity': 1, 'order_condition': <OrderCondition.CASH: 1>}
]
Source code in finlab/online/order_executor.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
|
to_json
Converts the position dictionary to a JSON file and saves it to the specified path.
PARAMETER | DESCRIPTION |
---|---|
path |
The path where the JSON file will be saved.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in finlab/online/order_executor.py
finlab.online.order_executor.OrderExecutor
對比實際帳戶與欲部屬的股票部位,進行同步 Arguments: target_position (Position): 想要部屬的股票部位。 account (Account): 目前支援永豐與富果帳戶,請參考 Account 來實做。
Source code in finlab/online/order_executor.py
cancel_orders
create_orders
產生委託單,將部位同步成 self.target_position 預設以該商品最後一筆成交價設定為限價來下單
ATTRIBUTE | DESCRIPTION |
---|---|
market_order |
以類市價盡量即刻成交:所有買單掛漲停價,所有賣單掛跌停價
TYPE:
|
best_price_limit |
掛芭樂價:所有買單掛跌停價,所有賣單掛漲停價
TYPE:
|
view_only |
預設為 False,會實際下單。若設為 True,不會下單,只會回傳欲執行的委託單資料(dict)
TYPE:
|
extra_bid_pct |
以該百分比值乘以價格進行追價下單,如設定為 0.05 時,將以當前價的 +(-)5% 的限價進買入(賣出),也就是更有機會可以成交,但是成交價格可能不理想; 假如設定為 -0.05 時,將以當前價的 -(+)5% 進行買入賣出,也就是限價單將不會立即成交,然而假如成交後,價格比較理想。參數有效範圍為 -0.1 到 0.1 內。
TYPE:
|
Source code in finlab/online/order_executor.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
|
execute_orders
execute_orders(orders, market_order=False, best_price_limit=False, view_only=False, extra_bid_pct=0)
產生委託單,將部位同步成 self.target_position 預設以該商品最後一筆成交價設定為限價來下單
ATTRIBUTE | DESCRIPTION |
---|---|
orders |
欲下單的部位,通常是由
TYPE:
|
market_order |
以類市價盡量即刻成交:所有買單掛漲停價,所有賣單掛跌停價
TYPE:
|
best_price_limit |
掛芭樂價:所有買單掛跌停價,所有賣單掛漲停價
TYPE:
|
view_only |
預設為 False,會實際下單。若設為 True,不會下單,只會回傳欲執行的委託單資料(dict)
TYPE:
|
extra_bid_pct |
以該百分比值乘以價格進行追價下單,如設定為 0.05 時,將以當前價的 +(-)5% 的限價進買入(賣出),也就是更有機會可以成交,但是成交價格可能不理想; 假如設定為 -0.05 時,將以當前價的 -(+)5% 進行買入賣出,也就是限價單將不會立即成交,然而假如成交後,價格比較理想。參數有效範圍為 -0.1 到 0.1 內。
TYPE:
|
Source code in finlab/online/order_executor.py
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 |
|
generate_orders
Generate orders based on the difference between target position and present position.
Returns: orders (dict): Orders to be executed.
Source code in finlab/online/order_executor.py
show_alerting_stocks
產生下單部位是否有警示股,以及相關資訊
Source code in finlab/online/order_executor.py
update_order_price
更新委託單,將委託單的限價調整成當天最後一筆價格。 (讓沒成交的限價單去追價) Attributes: extra_bid_pct (float): 以該百分比值乘以價格進行追價下單,如設定為 0.1 時,將以超出(低於)現價之10%價格下單,以漲停(跌停)價為限。參數有效範圍為 0 到 0.1 內
Source code in finlab/online/order_executor.py
finlab.online.base_account.Order
dataclass
Order status
委託單的狀態
ATTRIBUTE | DESCRIPTION |
---|---|
order_id |
委託單的 id,與券商 API 所提供的 id 一致
TYPE:
|
stock_id |
股票代號 ex: '2330'
TYPE:
|
action |
買賣方向,通常為 'BUY' 或是 'SELL'
TYPE:
|
price |
股票買賣的價格(限價單)
TYPE:
|
quantity |
委託股票的總數量(張數),允許小數點
TYPE:
|
filled_quantity |
以成交股票的數量(張數),允許小數點
TYPE:
|
status |
委託狀態,可以設定為:'NEW', 'PARTIALLY_FILLED', 'FILLED', 'CANCEL'
TYPE:
|
time |
委託時間
TYPE:
|
org_order |
券商所提供的委託物件格式
TYPE:
|
finlab.online.panel.order_panel
下單 GUI 介面 Arguments: account (Account): 請參考 Account 針對不同券商來建構相對應的操作帳戶