kgisuperpy 1.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,2087 @@
1
+ Metadata-Version: 2.4
2
+ Name: kgisuperpy
3
+ Version: 1.0.3
4
+ Summary: KGI project
5
+ Author: KGI Securities
6
+ Author-email: ec.service@kgi.com
7
+ Description-Content-Type: text/markdown
8
+ License-File: LICENSE
9
+ Requires-Dist: pythonnet
10
+ Dynamic: author
11
+ Dynamic: author-email
12
+ Dynamic: description
13
+ Dynamic: description-content-type
14
+ Dynamic: license-file
15
+ Dynamic: requires-dist
16
+ Dynamic: summary
17
+
18
+ # README.md
19
+
20
+ # SuperPy
21
+
22
+ ---
23
+
24
+ # 更新紀錄
25
+
26
+ | **日期** | 項目 | 版本 |
27
+ | --- | --- | --- |
28
+ | 2024/07/05 | 初版 | 1.0.0 |
29
+ | 2024/09/18 | 1. 新增新編指數 <br> 2. 新增子帳登入功能 | 1.0.1 |
30
+ | 2024/12/09 | 調整說明 | 1.0.2 |
31
+ | 2025/09/30 | 調整證券下單回報功能 增加[4002]電文條件判斷 | 1.0.3 |
32
+
33
+ ---
34
+
35
+ # **快速入門**
36
+
37
+ ⚠️ 在開始前請還需完成前置作業,包含**開戶**、**服務條款** 及 **Token**。
38
+
39
+ 依據主管機關規範,使用API元件應簽署『證券API服務風險預告書暨使用同意書』
40
+
41
+ [我要線上申請API](https://h5webtrade.kgieworld.com.tw/ServicesWeb/Auth/Login?orign=o&applytype=21)
42
+
43
+ ## 安裝
44
+
45
+ 使用 pip 簡單安裝
46
+
47
+ ```bash
48
+ pip install kgisuperpy
49
+
50
+ ```
51
+
52
+ 更新superpy
53
+
54
+ ```bash
55
+ pip install -U kgisuperpy
56
+
57
+ ```
58
+
59
+ ## SuperPy物件
60
+
61
+ ➡️ In
62
+
63
+ ```python
64
+ import superpy as sp
65
+ sp.SuperPy()
66
+
67
+ ```
68
+
69
+ ✅ Out
70
+
71
+ ```python
72
+ Init signature:
73
+ sp.SuperPy(
74
+ simulation: bool = False,
75
+ )
76
+ Docstring:
77
+ superpy api
78
+ Functions:
79
+ login
80
+ logout
81
+ list_accounts
82
+ set_default_account
83
+ place_order
84
+ update_order
85
+ update_status
86
+ list_trades
87
+ Objects:
88
+ Quote
89
+ Contracts
90
+ Order
91
+ Init docstring:
92
+ initialize superpy to start trading
93
+ Args:
94
+ simulation (bool):
95
+ - False: to trading on real market (just use your account to start trading)
96
+ - True: become simulation account(need to contract as to open simulation account)
97
+
98
+ ```
99
+
100
+ ## **模擬模式**
101
+
102
+ 使用者能先在模擬環境熟悉我們所提供的服務,可避免在正式環境操作失誤造成財物的損失。以下會詳細說明在測試環境所提供的功能。
103
+
104
+ ☑️ **模擬環境**
105
+
106
+ ```python
107
+ import superpy as sp
108
+ api = sp.SuperPy(simulation=True) #Default
109
+
110
+ ```
111
+
112
+ ## **正式模式**
113
+
114
+ ☑️ 正式**環境**
115
+
116
+ ```python
117
+ import superpy as sp
118
+ api = sp.SuperPy(simulation=False)
119
+
120
+ ```
121
+
122
+ ---
123
+
124
+ # 登入
125
+
126
+ ➡️ In
127
+
128
+ ```python
129
+ import superpy as sp
130
+ api = sp.SuperPy()
131
+ api.login(
132
+ userID = "YOUR_USER_ID",
133
+ password = "YOUR_PASSWORD")
134
+
135
+ ```
136
+
137
+ ✅ Out
138
+
139
+ ```python
140
+ [
141
+ FutureAccount(person_id='A123456789', broker_id='F004009', account_id='1234567', signed=True),
142
+ StockAccount(person_id='A123456789', broker_id='9268', account_id='1234567', signed=True)
143
+ ]
144
+
145
+ ```
146
+
147
+ - 如果在帳號清單中找不到 `signed`,請至**服務條款**了解使用API服務所需要步驟。
148
+
149
+ 🔆 **Login Arguments**
150
+
151
+ ```python
152
+ UserID (str): 用戶ID
153
+ password (str): 密碼
154
+ fetch_contract (bool): 是否從快取中讀取商品檔或從伺服器下載商品檔 (預設值: True)
155
+ contracts_timeout (int): 獲取商品檔 timeout (預設值: 0 ms)
156
+ contracts_cb (typing.Callable): 獲取商品檔 callback (預設值: None)
157
+ subscribe_trade (bool): 是否訂閱委託/成交回報 (預設值: True)
158
+ receive_window (int): 登入動作有效執行時間 (預設值: 30,000 毫秒)
159
+
160
+ ```
161
+
162
+ ### 帳號列表
163
+
164
+ ➡️ In
165
+
166
+ ```python
167
+ accounts = api.list_accounts()
168
+
169
+ ```
170
+
171
+ ✅ Out
172
+
173
+ ```python
174
+ # print(accounts)
175
+ [
176
+ FutureAccount(person_id='A123456789', broker_id='F004009', account_id='1234567', signed=True),
177
+ FutureAccount(person_id='A123456789', broker_id='F004009', account_id='7654321'),
178
+ StockAccount(person_id='A123456789', broker_id='9268', account_id='7654321'),
179
+ StockAccount(person_id='A123456789', broker_id='9268', account_id='1234567', signed=True)
180
+ ]
181
+
182
+ ```
183
+
184
+ ## 預設帳號
185
+
186
+ ➡️ In
187
+
188
+ ```python
189
+ # 期貨預設帳號
190
+ print(api.futopt_account)
191
+
192
+ ```
193
+
194
+ ✅ Out
195
+
196
+ ```python
197
+ FutureAccount(person_id='A123456789', broker_id='F004009', account_id='1234567', signed=True)
198
+
199
+ ```
200
+
201
+ ## 設定預設帳號
202
+
203
+ ➡️ In
204
+
205
+ ```python
206
+ # 預設的期貨帳號從 ACCOUNT_ID_1轉換成 ACCOUNT_ID_2
207
+ api.set_default_account(accounts[1])
208
+ print(api.futopt_account)
209
+
210
+ ```
211
+
212
+ ✅ Out
213
+
214
+ ```python
215
+ FutureAccount(person_id='A123456789', broker_id='F004009', account_id='7654321')
216
+
217
+ ```
218
+
219
+ 下單Order物件中需要指定帳號。更多資訊請參考現貨和期權下單。
220
+
221
+ ```python
222
+ order = api.Order(
223
+ price=12,
224
+ quantity=1,
225
+ action=sp.constant.Action.Buy,
226
+ price_type=sp.constant.StockPriceType.LMT,
227
+ order_type=sp.constant.OrderType.ROD,
228
+ order_lot=sp.constant.StockOrderLot.Common,
229
+ account=api.stock_account
230
+ )
231
+
232
+ ```
233
+
234
+ ## 子帳登入
235
+
236
+ 子帳登入方式,登入 ID 格式為: Trader@ID.Broker+Account;Ex: 1001@A123456789.F0040001234567
237
+
238
+ - 登入情境
239
+
240
+ ```python
241
+ # 同下單帳號不同子帳
242
+ 1001@A123456789.F0040091234567,,0000|1002@A123456789.F0040091234567,,0000
243
+ # 群組子帳
244
+ G1@A123456789.F0040091234567,,0000
245
+ ```
246
+
247
+ 傳入參數loginInfos供子帳登入
248
+
249
+ ➡️ In
250
+
251
+ ```python
252
+ import superpy as sp
253
+ api = sp.SuperPy()
254
+ api.login(
255
+ userID = "YOUR_USER_ID",
256
+ password = "YOUR_PASSWORD",
257
+ loginInfos = "1001@A123456789.F0040001234567,,password"
258
+ )
259
+
260
+ ```
261
+
262
+ ✅ Out
263
+
264
+ ```python
265
+ [
266
+ FutureAccount(person_id='A123456789', broker_id='F004000', account_id='1234567', signed=True, trader='1001'),
267
+ ]
268
+ ```
269
+
270
+ 子帳下單
271
+
272
+ ```python
273
+ accounts = api.list_accounts()
274
+ api.set_default_account(accounts[0])
275
+ print(api.futopt_account)
276
+ #FutureAccount(person_id='A123456789', broker_id='F004000', account_id='1234567', signed=True, trader='1001')
277
+
278
+ order = api.Order(
279
+ action=sp.constant.Action.Buy,
280
+ price=20000,
281
+ quantity=1,
282
+ price_type=sp.constant.FuturesPriceType.LMT,
283
+ order_type=sp.constant.OrderType.ROD,
284
+ octype=sp.constant.FuturesOCType.Auto,
285
+ account=api.futopt_account
286
+ )
287
+ ```
288
+
289
+ ## **登出**
290
+
291
+ 登出功能將關閉客戶端及服務端之間的連接,在不使用的時候終止程式是一個良好的習慣。
292
+
293
+ ```python
294
+ api.logout()
295
+ # True
296
+
297
+ ```
298
+
299
+ ---
300
+
301
+ # 商品檔
302
+
303
+ 商品檔將在很多地方被使用,例如下單、訂閱行情…等。
304
+
305
+ ### **取得商品檔**
306
+
307
+ 下方提供兩種方法取得商品檔:
308
+
309
+ - 方法1:登入成功後,將開始下載商品檔。但這個下載過程將不會影響其他的操作。若您想了解是否下載完成,可利用`Contracts.status`去得到下載狀態。`contracts_timeout` 設定為10000,它將等待10秒下載商品檔。
310
+
311
+ ☑️ In
312
+
313
+ ```python
314
+ import superpy as sp
315
+ api = sp.SuperPy()
316
+ api.login(
317
+ userID = "YOUR_USER_ID",
318
+ password = "YOUR_PASSWORD",
319
+ contracts_timeout=10000,
320
+ )
321
+
322
+ ```
323
+
324
+ - 方法2:若不想在登入時下載商品檔,將`fetch_contract` 設定為`False`。利用 `fetch_contracts` 下載商品檔
325
+
326
+ ☑️ In
327
+
328
+ ```python
329
+ import superpy as sp
330
+ api = sp.SuperPy()
331
+ api.login(
332
+ userID = "YOUR_USER_ID",
333
+ password = "YOUR_PASSWORD",
334
+ fetch_contract=False,
335
+ )
336
+ api.fetch_contracts(contract_download=True)
337
+
338
+ ```
339
+
340
+ ### **商品檔資訊**
341
+
342
+ 目前我們所提供的商品包含:證券、期貨、選擇權以及~~指數~~。可從下列方法更詳細得到我們所提供的商品。
343
+
344
+ - **證券**
345
+
346
+ ➡️ In
347
+
348
+ ```python
349
+ api.Contracts.Stocks["2883"]
350
+
351
+ ```
352
+
353
+ ✅ Out
354
+
355
+ ```python
356
+ Stock {
357
+ 'exchange': 'T',
358
+ 'code': '2883',
359
+ 'symbol': '2883',
360
+ 'name': '凱基金',
361
+ 'limit_up': 15.8,
362
+ 'limit_down': 13.0,
363
+ 'reference': 14.4
364
+ }
365
+
366
+ ```
367
+
368
+ 🔆 Stock
369
+
370
+ ```python
371
+ exchange (Exchange): 交易所 {OES, OTC, TSE ...等}
372
+ code (str): 商品代碼
373
+ symbol (str): 符號
374
+ name (str): 商品名稱
375
+ category (str): 類別
376
+ unit (int): 單位
377
+ limit_up (float): 漲停價
378
+ limit_down (float): 跌停價
379
+ reference (float): 參考價
380
+ update_date (str): 更新日期
381
+ margin_trading_balance (int): 融資餘額
382
+ short_selling_balance (int): 融券餘額
383
+ day_trade (DayTrade): 可否當沖 {Yes, No, OnlyBuy}
384
+
385
+ ```
386
+
387
+ - **期貨**
388
+
389
+ ➡️ In
390
+
391
+ ```python
392
+ api.Contracts.Futures["TXFF4"]
393
+
394
+ ```
395
+
396
+ ✅ Out
397
+
398
+ ```python
399
+ Future {
400
+ 'exchange': 'F',
401
+ 'code': 'TXFF4',
402
+ 'symbol': 'TXFF4',
403
+ 'name': '大台指',
404
+ 'category': 'TXF',
405
+ 'limit_up': 0.0,
406
+ 'limit_down': 0.0
407
+ }
408
+
409
+ ```
410
+
411
+ 🔆 Future
412
+
413
+ ```python
414
+ code (str): 商品代碼
415
+ symbol (str): 符號
416
+ name (str): 商品名稱
417
+ category (str): 類別
418
+ delivery_month (str): 交割月份
419
+ delivery_date (str): 結算日
420
+ underlying_kind (str): 標的類型
421
+ unit (int): 單位
422
+ limit_up (float): 漲停價
423
+ limit_down (float): 跌停價
424
+ reference (float): 參考價
425
+ update_date (str): 更新時間
426
+
427
+ ```
428
+
429
+ - 選擇權
430
+
431
+ ➡️ In
432
+
433
+ ```python
434
+ api.Contracts.Options["TXO18000R3"]
435
+
436
+ ```
437
+
438
+ ✅ Out
439
+
440
+ ```python
441
+ Option {
442
+ 'exchange': 'O',
443
+ 'code': 'TXO21200F4',
444
+ 'symbol': 'TXO21200F4',
445
+ 'name': 'TXO台指選',
446
+ 'category': 'TXO',
447
+ 'limit_up': 0.0,
448
+ 'limit_down': 0.0
449
+ }
450
+
451
+ ```
452
+
453
+ 🔆 **Option**
454
+
455
+ ```python
456
+ code (str): 商品代碼
457
+ symbol (str): 符號
458
+ name (str): 商品名稱
459
+ category (str): 類型
460
+ delivery_month (str): 交割月份
461
+ delivery_date (str): 交割日期
462
+ strike_price (int or float): 屢約價
463
+ option_right (OptionRight): 買賣權別
464
+ underlying_kind (str): 標的類型
465
+ limit_up (float): 漲停價
466
+ limit_down (float): 跌停價
467
+ reference (float): 參考價
468
+ update_date (str): 更新時間
469
+
470
+ ```
471
+
472
+ ---
473
+
474
+ # 委託下單
475
+
476
+ ## **證券下單**
477
+
478
+ ☑️ **StockOrder 物件格式**
479
+
480
+ ```python
481
+ >> sp.order.StockOrder?
482
+ Init signature:
483
+ sp.order.StockOrder(
484
+ *,
485
+ action: superpy.constant.Action,
486
+ price: Union[float],
487
+ quantity: superpy.order.ConstrainedIntValue,
488
+ id: str = '',
489
+ seqno: str = '',
490
+ ordno: str = '',
491
+ account: superpy.account.Account = None,
492
+ custom_field: str = '',
493
+ ca: str = '',
494
+ price_type: superpy.constant.StockPriceType,
495
+ order_type: superpy.constant.OrderType,
496
+ order_lot: superpy.constant.StockOrderLot = <StockOrderLot.Common: 'Common'>,
497
+ order_cond: superpy.constant.StockOrderCond = <StockOrderCond.Cash: 'Cash'>,
498
+ daytrade_short: bool = False,
499
+ ) -> None
500
+
501
+ ```
502
+
503
+ 🔆 證券委託單
504
+
505
+ ```python
506
+ price (float or int): 價格
507
+ quantity (int): 委託數量
508
+ action (str): {Buy: 買, Sell: 賣}
509
+ price_type (str): {LMT: 限價, MKT: 市價, MKP: 範圍市價, FallStopPrice: 跌停, UnchangePrice: 平盤, RiseStopPrice: 漲停}
510
+ # 註:因交易所只有限價跟巿價,故設定price_type為非市價單時,回報會統一為限價+實際價格
511
+ order_type (str): 委託類別 {ROD, IOC, FOK}
512
+ order_cond (str): {Cash:現股, MarginTrading:融資, ShortSelling:融券, ShortUnLimit:借券賣出, EDN:賣出擔保品}
513
+ order_lot (str): { Common:整股, Fixing:定盤, Odd:盤後零股, IntradayOdd:盤中零股 }
514
+ daytrade_short (bool): 先賣後買
515
+ custom_field (str): 備註,只允許輸入大小寫英文字母及數字,且長度最長為 6
516
+ account (:obj:Account): 下單帳號
517
+ ca (binary): 憑證
518
+
519
+ ```
520
+
521
+ ### 下單
522
+
523
+ 下單時必須提供商品資訊`contract`及下單資訊`order`。
524
+
525
+ 🔆 **下單**
526
+
527
+ ```python
528
+ api.place_order?
529
+ Signature:
530
+ api.place_order(
531
+ contract: superpy.contracts.Contract,
532
+ order: superpy.order.Order,
533
+ timeout: int = 5000,
534
+ cb: Callable[[superpy.order.Trade], NoneType] = None,
535
+ ) -> superpy.order.Trade
536
+ Docstring:
537
+ placing order
538
+
539
+ ```
540
+
541
+ ☑️ 商品檔 Contracts
542
+
543
+ ```python
544
+ contract = api.Contracts.Stocks["2883"]
545
+
546
+ ```
547
+
548
+ ☑️ 委託單 Order
549
+
550
+ ```python
551
+ order = api.Order(
552
+ price=17,
553
+ quantity=3,
554
+ action=sp.constant.Action.Buy,
555
+ price_type=sp.constant.StockPriceType.LMT,
556
+ order_type=sp.constant.OrderType.ROD,
557
+ # order_lot=sp.constant.StockOrderLot.Common, # Default
558
+ # order_cond=sp.constant.StockOrderCond.Cash, # Default
559
+ # daytrade_short=False,
560
+ account=api.stock_account
561
+ )
562
+
563
+ ```
564
+
565
+ ➡️ In
566
+
567
+ ```python
568
+ trade = api.place_order(contract, order)
569
+
570
+ ```
571
+
572
+ ✅ Out
573
+
574
+ ```python
575
+ Trade(
576
+ contract=Stock {
577
+ 'exchange': 'T',
578
+ 'code': '2883',
579
+ 'symbol': '2883',
580
+ 'name': '凱基金',
581
+ 'limit_up': 15.8,
582
+ 'limit_down': 13.0,
583
+ 'reference': 14.4
584
+ },
585
+ order=Order {
586
+ 'action': <Action.Buy: 'Buy'>,
587
+ 'price': 14.4,
588
+ 'quantity': 3,
589
+ 'account': {
590
+ 'person_id': 'A123456789',
591
+ 'broker_id': '9268',
592
+ 'account_id': '1234567',
593
+ 'signed': True
594
+ },
595
+ 'custom_field': '',
596
+ 'price_type': <StockPriceType.LMT: 'LMT'>,
597
+ 'order_type': <OrderType.ROD: 'ROD'>,
598
+ 'order_lot': <StockOrderLot.Common: 'Common'>,
599
+ 'order_cond': <StockOrderCond.Cash: 'Cash'>,
600
+ 'daytrade_short': False,
601
+ 'ordno': 'Y004X',
602
+ 'id': '1220006369'
603
+ },
604
+ status=OrderStatus {
605
+ 'id': '1220006369',
606
+ 'status': <Status.Submitted: 'Submitted'>,
607
+ 'status_code': '0',
608
+ 'order_datetime': datetime.datetime(2024, 5, 20, 14, 13, 31, 380000),
609
+ 'order_quantity': '3'
610
+ }
611
+ )
612
+
613
+ ```
614
+
615
+ 下單完同時也會收到從交易所傳回來的資料,詳情內容可詳見**下單回報**。
616
+
617
+ 🔆 委託單狀態
618
+
619
+ - `PendingSubmit`: 傳送中
620
+ - `PreSubmitted`: 預約單
621
+ - `Submitted`: 傳送成功
622
+ - `Failed`: 失敗
623
+ - `Cancelled`: 已刪除
624
+ - `Filled`: 完全成交
625
+ - `Filling`: 部分成交
626
+
627
+ ### 改單
628
+
629
+ 🔆 **改單**
630
+
631
+ ```python
632
+ api.update_order?
633
+ Signature:
634
+ api.update_order(
635
+ trade: superpy.order.Trade,
636
+ price: Union[pydantic.types.StrictInt, float] = None,
637
+ qty: int = None,
638
+ timeout: int = 5000,
639
+ cb: Ca lable[[superpy.order.Trade], NoneType] = None,
640
+ ) -> superpy.order.Trade
641
+ Docstring: update the order price or qty
642
+
643
+ ```
644
+
645
+ - 改價
646
+
647
+ ➡️ In
648
+
649
+ ```python
650
+ trade = api.update_order(trade=trade, price=17.5)
651
+
652
+ ```
653
+
654
+ ✅ Out
655
+
656
+ ```python
657
+ Trade(
658
+ contract=Stock {
659
+ 'exchange': 'T',
660
+ 'code': '2883',
661
+ 'symbol': '2883',
662
+ 'name': '凱基金',
663
+ 'limit_up': 15.8,
664
+ 'limit_down': 13.0,
665
+ 'reference': 14.4
666
+ },
667
+ order=Order {
668
+ 'action': <Action.Buy: 'Buy'>,
669
+ 'price': 14.4,
670
+ 'quantity': 3,
671
+ 'account': {
672
+ 'person_id': 'A123456789',
673
+ 'broker_id': '9268',
674
+ 'account_id': '1234567',
675
+ 'signed': True
676
+ },
677
+ 'custom_field': '',
678
+ 'price_type': <StockPriceType.LMT: 'LMT'>,
679
+ 'order_type': <OrderType.ROD: 'ROD'>,
680
+ 'order_lot': <StockOrderLot.Common: 'Common'>,
681
+ 'order_cond': <StockOrderCond.Cash: 'Cash'>,
682
+ 'daytrade_short': False,
683
+ 'ordno': 'Y004X',
684
+ 'id': '1220006369'
685
+ },
686
+ status=OrderStatus {
687
+ 'id': '1220006369',
688
+ 'status': <Status.Submitted: 'Submitted'>,
689
+ 'status_code': '0',
690
+ 'order_datetime': datetime.datetime(2024, 5, 20, 14, 13, 31, 400000),
691
+ 'order_quantity': '3',
692
+ 'modified_price': 14.3
693
+ }
694
+ )
695
+
696
+ ```
697
+
698
+ - 改量(減量)
699
+
700
+ `update_order` 只能用來**減少**原委託單的委託數量。
701
+
702
+ ➡️ In
703
+
704
+ ```python
705
+ trade = api.update_order(trade=trade, qty=1)
706
+
707
+ ```
708
+
709
+ ✅ Out
710
+
711
+ ```python
712
+ Trade(
713
+ contract=Stock {
714
+ 'exchange': 'T',
715
+ 'code': '2883',
716
+ 'symbol': '2883',
717
+ 'name': '凱基金',
718
+ 'limit_up': 15.8,
719
+ 'limit_down': 13.0,
720
+ 'reference': 14.4
721
+ },
722
+ order=Order {
723
+ 'action': <Action.Buy: 'Buy'>,
724
+ 'price': 14.4,
725
+ 'quantity': 3,
726
+ 'account': {
727
+ 'person_id': 'A123456789',
728
+ 'broker_id': '9268',
729
+ 'account_id': '1234567',
730
+ 'signed': True
731
+ },
732
+ 'custom_field': '',
733
+ 'price_type': <StockPriceType.LMT: 'LMT'>,
734
+ 'order_type': <OrderType.ROD: 'ROD'>,
735
+ 'order_lot': <StockOrderLot.Common: 'Common'>,
736
+ 'order_cond': <StockOrderCond.Cash: 'Cash'>,
737
+ 'daytrade_short': False,
738
+ 'ordno': 'Y004X',
739
+ 'id': '1220006369'
740
+ },
741
+ status=OrderStatus {
742
+ 'id': '1220006369',
743
+ 'status': <Status.Submitted: 'Submitted'>,
744
+ 'status_code': '0',
745
+ 'order_datetime': datetime.datetime(2024, 5, 20, 14, 13, 31, 410000),
746
+ 'order_quantity': '3',
747
+ 'modified_price': 14.3,
748
+ 'cancel_quantity': 1
749
+ }
750
+ )
751
+
752
+ ```
753
+
754
+ ---
755
+
756
+ ### 刪單
757
+
758
+ ➡️ In
759
+
760
+ ```python
761
+ trade = api.cancel_order(trade)
762
+
763
+ ```
764
+
765
+ ✅ Out
766
+
767
+ ```python
768
+ Trade(
769
+ contract=Stock {
770
+ 'exchange': 'T',
771
+ 'code': '2883',
772
+ 'symbol': '2883',
773
+ 'name': '凱基金',
774
+ 'limit_up': 15.8,
775
+ 'limit_down': 13.0,
776
+ 'reference': 14.4
777
+ },
778
+ order=Order {
779
+ 'action': <Action.Buy: 'Buy'>,
780
+ 'price': 14.4,
781
+ 'quantity': 3,
782
+ 'account': {
783
+ 'person_id': 'A123456789',
784
+ 'broker_id': '9268',
785
+ 'account_id': '1234567',
786
+ 'signed': True
787
+ },
788
+ 'custom_field': '',
789
+ 'price_type': <StockPriceType.LMT: 'LMT'>,
790
+ 'order_type': <OrderType.ROD: 'ROD'>,
791
+ 'order_lot': <StockOrderLot.Common: 'Common'>,
792
+ 'order_cond': <StockOrderCond.Cash: 'Cash'>,
793
+ 'daytrade_short': False,
794
+ 'ordno': 'Y004X',
795
+ 'id': '1220006369'
796
+ },
797
+ status=OrderStatus {
798
+ 'id': '1220006369',
799
+ 'status': <Status.Submitted: 'Submitted'>,
800
+ 'status_code': '0',
801
+ 'order_datetime': datetime.datetime(2024, 5, 20, 14, 13, 31, 420000),
802
+ 'order_quantity': '3',
803
+ 'modified_price': 14.3,
804
+ 'cancel_quantity': '2'
805
+ }
806
+ )
807
+
808
+ ```
809
+
810
+ ---
811
+
812
+ ## **期貨下單**
813
+
814
+ ☑️ **FuturesOrder 物件格式**
815
+
816
+ ```python
817
+ >> sp.order.FuturesOrder?
818
+ Init signature:
819
+ sp.order.FuturesOrder(
820
+ *,
821
+ action: superpy.constant.Action,
822
+ price: Union[pydantic.types.StrictInt, float],
823
+ quantity: superpy.order.ConstrainedIntValue,
824
+ id: str = '',
825
+ seqno: str = '',
826
+ ordno: str = '',
827
+ account: superpy.account.Account = None,
828
+ custom_field: superpy.order.ConstrainedStrValue = '',
829
+ ca: str = '',
830
+ price_type: superpy.constant.FuturesPriceType,
831
+ order_type: superpy.constant.OrderType,
832
+ octype: superpy.constant.FuturesOCType = <FuturesOCType.Auto: 'Auto'>,
833
+ ) -> None
834
+
835
+ ```
836
+
837
+ 🔆 期貨委託單
838
+
839
+ ```python
840
+ price (float or int): 價格
841
+ quantity (int): 委託數量
842
+ action (str): {Buy: 買, Sell: 賣}
843
+ price_type (str): {LMT: 限價, MKT: 市價, MKP: 範圍市價}
844
+ order_type (str): 委託類別 {ROD, IOC, FOK}
845
+ octype (str): {Auto: 自動, New: 新倉, Cover: 平倉, DayTrade: 當沖}
846
+ account (:obj:Account): 下單帳號
847
+ ca (binary): 憑證
848
+
849
+ ```
850
+
851
+ ### 下單
852
+
853
+ 下單時必須提供商品資訊`contract`及下單資訊`order`。
854
+
855
+ 🔆 **下單**
856
+
857
+ ```python
858
+ api.place_order?
859
+ Signature:
860
+ api.place_order(
861
+ contract: superpy.contracts.Contract,
862
+ order: superpy.order.Order,
863
+ timeout: int = 5000,
864
+ cb: Callable[[superpy.order.Trade], NoneType] = None,
865
+ ) -> superpy.order.Trade
866
+ Docstring:
867
+ placing order
868
+
869
+ ```
870
+
871
+ ☑️ 商品檔 Contracts
872
+
873
+ ```python
874
+ contract = api.Contracts.Futures["TXFF4"]
875
+
876
+ ```
877
+
878
+ ☑️ 委託單 Order
879
+
880
+ ```python
881
+ order = api.Order(
882
+ action=sp.constant.Action.Buy,
883
+ price=21300,
884
+ quantity=3,
885
+ price_type=sp.constant.FuturesPriceType.LMT,
886
+ order_type=sp.constant.OrderType.ROD,
887
+ octype=sp.constant.FuturesOCType.Auto,
888
+ account=api.futopt_account
889
+ )
890
+
891
+ ```
892
+
893
+ ➡️ In
894
+
895
+ ```python
896
+ trade = api.place_order(contract, order)
897
+
898
+ ```
899
+
900
+ ✅ Out
901
+
902
+ ```python
903
+ Trade(
904
+ contract=Future {
905
+ 'exchange': 'F',
906
+ 'code': 'TXFF4',
907
+ 'symbol': 'TXFF4',
908
+ 'name': '大台指',
909
+ 'category': 'TXF',
910
+ 'limit_up': 0.0,
911
+ 'limit_down': 0.0
912
+ },
913
+ order=Order {
914
+ 'action': <Action.Buy: 'Buy'>,
915
+ 'price': 21300,
916
+ 'quantity': 3,
917
+ 'account': {
918
+ 'person_id': 'A123456789',
919
+ 'broker_id': 'F004009',
920
+ 'account_id': '1234567',
921
+ 'signed': True
922
+ },
923
+ 'custom_field': '',
924
+ 'price_type': <FuturesPriceType.LMT: 'LMT'>,
925
+ 'order_type': <OrderType.ROD: 'ROD'>,
926
+ 'octype': <FuturesOCType.Auto: 'Auto'>,
927
+ 'ordno': 'wAaaq',
928
+ 'id': '00380617'
929
+ }
930
+ status=OrderStatus {
931
+ 'id': '00380617',
932
+ 'status': <Status.Submitted: 'Submitted'>,
933
+ 'status_code': '10000000',
934
+ 'order_datetime': datetime.datetime(2024, 5, 20, 15, 6, 4),
935
+ 'order_quantity': '3',
936
+ 'web_id': '126'
937
+ }
938
+ )
939
+
940
+ ```
941
+
942
+ 下單完同時也會收到從交易所傳回來的資料,詳情內容可詳見**下單回報**。
943
+
944
+ 🔆 委託單狀態
945
+
946
+ - `PendingSubmit`: 傳送中
947
+ - `PreSubmitted`: 預約單
948
+ - `Submitted`: 傳送成功
949
+ - `Failed`: 失敗
950
+ - `Cancelled`: 已刪除
951
+ - `Filled`: 完全成交
952
+ - `Filling`: 部分成交
953
+
954
+ ### 改單
955
+
956
+ 🔆 **改單**
957
+
958
+ ```python
959
+ api.update_order?
960
+ Signature:
961
+ api.update_order(
962
+ trade: superpy.order.Trade,
963
+ price: Union[pydantic.types.StrictInt, float] = None,
964
+ qty: int = None,
965
+ timeout: int = 5000,
966
+ cb: Ca lable[[superpy.order.Trade], NoneType] = None,
967
+ ) -> superpy.order.Trade
968
+ Docstring: update the order price or qty
969
+
970
+ ```
971
+
972
+ - 改價
973
+
974
+ ➡️ In
975
+
976
+ ```python
977
+ trade = api.update_order(trade=trade, price=21350)
978
+
979
+ ```
980
+
981
+ ✅ Out
982
+
983
+ ```python
984
+ Trade(
985
+ contract=Future {
986
+ 'exchange': 'F',
987
+ 'code': 'TXFF4',
988
+ 'symbol': 'TXFF4',
989
+ 'name': '大台指',
990
+ 'category': 'TXF',
991
+ 'limit_up': 0.0,
992
+ 'limit_down': 0.0
993
+ },
994
+ order=Order {
995
+ 'action': <Action.Buy: 'Buy'>,
996
+ 'price': 21300,
997
+ 'quantity': 3,
998
+ 'account': {
999
+ 'person_id': 'A123456789',
1000
+ 'broker_id': 'F004009',
1001
+ 'account_id': '1234567',
1002
+ 'signed': True
1003
+ },
1004
+ 'custom_field': '',
1005
+ 'price_type': <FuturesPriceType.LMT: 'LMT'>,
1006
+ 'order_type': <OrderType.ROD: 'ROD'>,
1007
+ 'octype': <FuturesOCType.Auto: 'Auto'>,
1008
+ 'ordno': 'wAaaq',
1009
+ 'id': '00380617'
1010
+ }
1011
+ status=OrderStatus {
1012
+ 'id': '00380617',
1013
+ 'status': <Status.Submitted: 'Submitted'>,
1014
+ 'status_code': '10000000',
1015
+ 'order_datetime': datetime.datetime(2024, 5, 20, 15, 11, 14),
1016
+ 'order_quantity': '3',
1017
+ 'web_id': '126',
1018
+ 'modified_price': 21350
1019
+ }
1020
+ )
1021
+
1022
+ ```
1023
+
1024
+ - 改量(減量)
1025
+
1026
+ `update_order` 只能用來**減少**原委託單的委託數量。
1027
+
1028
+ ➡️ In
1029
+
1030
+ ```python
1031
+ trade = api.update_order(trade=trade, qty=1)
1032
+
1033
+ ```
1034
+
1035
+ ✅ Out
1036
+
1037
+ ```python
1038
+ Trade(
1039
+ contract=Future {
1040
+ 'exchange': 'F',
1041
+ 'code': 'TXFF4',
1042
+ 'symbol': 'TXFF4',
1043
+ 'name': '大台指',
1044
+ 'category': 'TXF',
1045
+ 'limit_up': 0.0,
1046
+ 'limit_down': 0.0
1047
+ },
1048
+ order=Order {
1049
+ 'action': <Action.Buy: 'Buy'>,
1050
+ 'price': 21300,
1051
+ 'quantity': 3,
1052
+ 'account': {
1053
+ 'person_id': 'A123456789',
1054
+ 'broker_id': 'F004009',
1055
+ 'account_id': '1234567',
1056
+ 'signed': True
1057
+ },
1058
+ 'custom_field': '',
1059
+ 'price_type': <FuturesPriceType.LMT: 'LMT'>,
1060
+ 'order_type': <OrderType.ROD: 'ROD'>,
1061
+ 'octype': <FuturesOCType.Auto: 'Auto'>,
1062
+ 'ordno': 'wAaaq',
1063
+ 'id': '00380617'
1064
+ }
1065
+ status=OrderStatus {
1066
+ 'id': '00380617',
1067
+ 'status': <Status.Submitted: 'Submitted'>,
1068
+ 'status_code': '10000000',
1069
+ 'order_datetime': datetime.datetime(2024, 5, 20, 15, 11, 15),
1070
+ 'order_quantity': '3',
1071
+ 'web_id': '126',
1072
+ 'modified_price': 21350,
1073
+ 'cancel_quantity': 1
1074
+ }
1075
+ )
1076
+
1077
+ ```
1078
+
1079
+ ---
1080
+
1081
+ ### 刪單
1082
+
1083
+ ➡️ In
1084
+
1085
+ ```python
1086
+ trade = api.cancel_order(trade)
1087
+
1088
+ ```
1089
+
1090
+ ✅ Out
1091
+
1092
+ ```python
1093
+ Trade(
1094
+ contract=Future {
1095
+ 'exchange': 'F',
1096
+ 'code': 'TXFF4',
1097
+ 'symbol': 'TXFF4',
1098
+ 'name': '大台指',
1099
+ 'category': 'TXF',
1100
+ 'limit_up': 0.0,
1101
+ 'limit_down': 0.0
1102
+ },
1103
+ order=Order {
1104
+ 'action': <Action.Buy: 'Buy'>,
1105
+ 'price': 21300,
1106
+ 'quantity': 3,
1107
+ 'account': {
1108
+ 'person_id': 'A123456789',
1109
+ 'broker_id': 'F004009',
1110
+ 'account_id': '1234567',
1111
+ 'signed': True
1112
+ },
1113
+ 'custom_field': '',
1114
+ 'price_type': <FuturesPriceType.LMT: 'LMT'>,
1115
+ 'order_type': <OrderType.ROD: 'ROD'>,
1116
+ 'octype': <FuturesOCType.Auto: 'Auto'>,
1117
+ 'ordno': 'wAaaq',
1118
+ 'id': '00380617'
1119
+ }
1120
+ status=OrderStatus {
1121
+ 'id': '00380617',
1122
+ 'status': <Status.Submitted: 'Submitted'>,
1123
+ 'status_code': '10000000',
1124
+ 'order_datetime': datetime.datetime(2024, 5, 20, 15, 11, 15),
1125
+ 'order_quantity': '3',
1126
+ 'web_id': '126',
1127
+ 'modified_price': 21350,
1128
+ 'cancel_quantity': 2
1129
+ }
1130
+ )
1131
+
1132
+ ```
1133
+
1134
+ ---
1135
+
1136
+ ## 查詢狀態
1137
+
1138
+ 在取得 `Trade` 狀態前,必須先利用`update_status`進行更新。如果無法成功刪單或改單,你可以對特定`trade`物件進行更新,並確認在`trade`中的`OrderStatus`,是否為可刪改狀態。`update_status` 預設查詢為名下所有帳號。若想查詢特定帳號,將帳號帶入`account`。
1139
+
1140
+ ☑️ **Update Status**
1141
+
1142
+ ```python
1143
+ api.update_status?
1144
+ ```
1145
+
1146
+ ✅ Out
1147
+
1148
+ ```python
1149
+ Signature:
1150
+ api.update_status(
1151
+ account: superpy.account.Account = None,
1152
+ trade: superpy.order.Trade = None,
1153
+ )
1154
+ Docstring: update status of all trades you have
1155
+ ```
1156
+
1157
+ ### **取得證券委託狀態**
1158
+
1159
+ ➡️ In
1160
+
1161
+ ```python
1162
+ api.update_status(api.stock_account)
1163
+ api.list_trades()
1164
+ ```
1165
+
1166
+ ✅ Out
1167
+
1168
+ ```python
1169
+ [
1170
+ Trade(
1171
+ contract=Stock {
1172
+ 'exchange': 'T',
1173
+ 'code': '2883',
1174
+ 'symbol': '2883',
1175
+ 'name': '凱基金',
1176
+ 'limit_up': 15.8,
1177
+ 'limit_down': 13.0,
1178
+ 'reference': 14.4
1179
+ },
1180
+ order=Order {
1181
+ 'action': <Action.Buy: 'Buy'>,
1182
+ 'price': 14.4,
1183
+ 'quantity': 3,
1184
+ 'account': {
1185
+ 'person_id': 'A123456789',
1186
+ 'broker_id': '9268',
1187
+ 'account_id': '1234567',
1188
+ 'signed': True
1189
+ },
1190
+ 'custom_field': '',
1191
+ 'price_type': <StockPriceType.LMT: 'LMT'>,
1192
+ 'order_type': <OrderType.ROD: 'ROD'>,
1193
+ 'order_lot': <StockOrderLot.Common: 'Common'>,
1194
+ 'order_cond': <StockOrderCond.Cash: 'Cash'>,
1195
+ 'daytrade_short': False,
1196
+ 'ordno': 'Y004X',
1197
+ 'id': '1220006369'
1198
+ },
1199
+ status=OrderStatus {
1200
+ 'id': '1220006369',
1201
+ 'status': <Status.Submitted: 'Submitted'>,
1202
+ 'status_code': '0',
1203
+ 'order_datetime': datetime.datetime(2024, 5, 20, 14, 13, 31, 380000),
1204
+ 'order_quantity': '3'
1205
+ }
1206
+ )
1207
+ ]
1208
+ ```
1209
+
1210
+ ### **取得期貨委託狀態**
1211
+
1212
+ ➡️ In
1213
+
1214
+ ```python
1215
+ api.update_status(api.futopt_account)
1216
+ api.list_trades()
1217
+ ```
1218
+
1219
+ ✅ Out
1220
+
1221
+ ```python
1222
+ [
1223
+ Trade(
1224
+ contract=Future {
1225
+ 'exchange': 'F',
1226
+ 'code': 'TXFF4',
1227
+ 'symbol': 'TXFF4',
1228
+ 'name': '大台指',
1229
+ 'category': 'TXF',
1230
+ 'limit_up': 0.0,
1231
+ 'limit_down': 0.0
1232
+ },
1233
+ order=Order {
1234
+ 'action': <Action.Buy: 'Buy'>,
1235
+ 'price': 21300,
1236
+ 'quantity': 3,
1237
+ 'account': {
1238
+ 'person_id': 'A123456789',
1239
+ 'broker_id': 'F004009',
1240
+ 'account_id': '1234567',
1241
+ 'signed': True
1242
+ },
1243
+ 'custom_field': '',
1244
+ 'price_type': <FuturesPriceType.LMT: 'LMT'>,
1245
+ 'order_type': <OrderType.ROD: 'ROD'>,
1246
+ 'octype': <FuturesOCType.Auto: 'Auto'>,
1247
+ 'ordno': 'wAaaq',
1248
+ 'id': '00380617'
1249
+ }
1250
+ status=OrderStatus {
1251
+ 'id': '00380617',
1252
+ 'status': <Status.Submitted: 'Submitted'>,
1253
+ 'status_code': '10000000',
1254
+ 'order_datetime': datetime.datetime(2024, 5, 20, 15, 6, 4),
1255
+ 'order_quantity': '3',
1256
+ 'web_id': '126'
1257
+ }
1258
+ )
1259
+ ]
1260
+ ```
1261
+
1262
+ ### **更新特定交易狀態**
1263
+
1264
+ ☑️ **更新特定交易狀態**
1265
+
1266
+ ```python
1267
+ # you can get trade from place_order
1268
+ # trade = api.place_order(contract, order)
1269
+
1270
+ # or get from api.list_trades
1271
+ # trade = api.list_trades()[0]
1272
+
1273
+ api.update_status(trade=trade)
1274
+ ```
1275
+
1276
+ ### **委託及成交狀態屬性**
1277
+
1278
+ 🔆 **委託狀態屬性**
1279
+
1280
+ ```python
1281
+ id (str): 關聯Order物件編碼
1282
+ status (:obj:Status): {
1283
+ Cancelled: 已刪除,
1284
+ Filled: 完全成交,
1285
+ PartFilled: 部分成交,
1286
+ Failed: 失敗,
1287
+ PendingSubmit: 傳送中,
1288
+ PreSubmitted: 預約單,
1289
+ Submitted: 傳送成功
1290
+ }
1291
+ status_code (str): 狀態碼
1292
+ order_datetime (datetime): 委託時間
1293
+ order_quantity (int): 委託數量
1294
+ modified_price (float): 改價金額
1295
+ cancel_quantity (int): 取消委託數量
1296
+ deals (:List:Deal): 成交資訊
1297
+ ```
1298
+
1299
+ 🔆 **成交屬性**
1300
+
1301
+ ```python
1302
+ seq (str): 成交序號
1303
+ price (int or float): 成交價
1304
+ quantity (int): 成交數量
1305
+ ```
1306
+
1307
+ ---
1308
+
1309
+ # 委託回報
1310
+
1311
+ 每次您使用 `place_order`、`update_order` 或者 `cancel_order` 時,預設皆會收到來自交易所的委託或成交回報。如果您不想收到任何回報通知,您可以參考訂閱委託回報將其關閉。我們亦提供了處理委託及成交回報的介面。如果您正在建立自己的交易系統,這會非常有幫助。
1312
+
1313
+ ## **處理委託及成交回報**
1314
+
1315
+ 您可以使用 `set_order_callback` 來處理委託及成交回報。以下範例顯示,自製的委託回報函數(`order_cb`)將先 print `my_order_callback` 然後才 print 委託及成交回報。
1316
+
1317
+ ☑️ **設定委託回報函式**
1318
+
1319
+ ```python
1320
+ def order_cb(stat, msg):
1321
+ print('my_order_callback')
1322
+ print(stat, msg)
1323
+ api.set_order_callback(order_cb)
1324
+
1325
+ ```
1326
+
1327
+ ## **委託回報**
1328
+
1329
+ 當期交所收到委託將會回傳回報。在回報中分為四部分,包括`operation`、`order`、`status`及`contract`。以下我們會在進行詳細的說明。
1330
+
1331
+ - **operation**
1332
+
1333
+ ```python
1334
+ op_type (str): {
1335
+ "New": 新單,
1336
+ "Cancel": 刪單,
1337
+ "UpdatePrice": 改價,
1338
+ "UpdateQty": 改量
1339
+ }
1340
+ op_code (str): {"00": 成功, others: 失敗}
1341
+ op_msg (str): 錯誤訊息
1342
+
1343
+ ```
1344
+
1345
+ - **status**
1346
+
1347
+ ```python
1348
+ id (str): 與成交回報的trade_id相同
1349
+ exchange_ts (int): 交易所時間
1350
+ modified_price (float or int): 改價
1351
+ cancel_quantity (int): 取消數量
1352
+ order_quantity (int): 委託數量
1353
+ web_id (str): 下單平台代碼
1354
+
1355
+ ```
1356
+
1357
+ - **order、contract:參考證券、期貨回報**
1358
+
1359
+ ## **成交回報**
1360
+
1361
+ 當搓合成功,期交所會傳送成交回報告知。搓合成功包含部分成交以及完全成交,可以從委託回報中的`id`去對應成交回報中的`trade_id`去確認是否為同一筆委託單。
1362
+
1363
+ ⚠️ 交易所回傳訊息優先順序成交回報大於委託回報,所以當委託立即成交可能會先收到成交回報。
1364
+
1365
+ ## 證券回報
1366
+
1367
+ ### 委託
1368
+
1369
+ ☑️ Order Callback
1370
+
1371
+ ```python
1372
+ OrderState.StockOrder {
1373
+ 'operation': {
1374
+ 'op_type': 'New',
1375
+ 'op_code': '0',
1376
+ 'op_msg': ''
1377
+ },
1378
+ 'order': {
1379
+ 'id': '1220006374',
1380
+ 'ordno': 'Y005M',
1381
+ 'account': {
1382
+ 'account_type': 'S',
1383
+ 'broker_id':
1384
+ '9268',
1385
+ 'account_id': '1234567'
1386
+ },
1387
+ 'action': 'Buy',
1388
+ 'price': '14.4',
1389
+ 'quantity': '3',
1390
+ 'order_type': 'ROD',
1391
+ 'price_type': 'MKT',
1392
+ 'order_cond': 'CASH',
1393
+ 'order_lot': 'Common'
1394
+ },
1395
+ 'status': {
1396
+ 'id': '1220006374',
1397
+ 'modified_price': 0,
1398
+ 'cancel_quantity': 0,
1399
+ 'order_quantity': '3'
1400
+ },
1401
+ 'contract': {
1402
+ 'security_type': 'STK',
1403
+ 'exchange': 'TSE',
1404
+ 'code': '2883',
1405
+ 'symbol': '',
1406
+ 'name': '',
1407
+ 'currency': 'TWD'
1408
+ }
1409
+ }
1410
+
1411
+ ```
1412
+
1413
+ 🔆 **委託回報資訊**
1414
+
1415
+ - order
1416
+
1417
+ ```python
1418
+ id (str): 與成交回報的trade_id相同
1419
+ seqno (str): 平台單號
1420
+ ordno (str): 委託單號
1421
+ account (dict): 帳號資訊
1422
+ action (str): 買賣別 {Buy, Sell}
1423
+ price (float or int): 委託價格
1424
+ quantity (int): 委託數量
1425
+ order_type (str): 委託類別 {ROD, IOC, FOK}
1426
+ price_type (str): {
1427
+ LMT: 限價,
1428
+ MKT: 市價,
1429
+ MKP: 範圍市價
1430
+ # 註:因交易所只有限價跟巿價,故設定price_type為非市價單時,回報會統一為限價+實際價格
1431
+ }
1432
+ order_cond (str): {
1433
+ Cash: 現股,
1434
+ MarginTrading: 融資,
1435
+ ShortSelling:融券,
1436
+ ShortUnLimit:借券賣出,
1437
+ EDN:賣出擔保品
1438
+ }
1439
+ order_lot (str): {
1440
+ Common: 整股,
1441
+ Fixing: 定盤,
1442
+ Odd: 盤後零股,
1443
+ IntradayOdd: 盤中零股
1444
+ }
1445
+ custom_field (str): 自訂欄位
1446
+
1447
+ ```
1448
+
1449
+ - contract
1450
+
1451
+ ```python
1452
+ security_type (str): 商品類別
1453
+ exchange (str): 交易所
1454
+ code (str): 商品代碼
1455
+ symbol (str): 符號
1456
+ name (str): 商品名稱
1457
+ currency (str): 幣別
1458
+
1459
+ ```
1460
+
1461
+ ### 成交
1462
+
1463
+ ☑️ Deal Callback
1464
+
1465
+ ```python
1466
+ OrderState.StockDeal {
1467
+ 'trade_id': '1220006374',
1468
+ 'ordno': 'Y005M',
1469
+ 'broker_id': '9268',
1470
+ 'account_id': '1234567',
1471
+ 'action': 'Buy',
1472
+ 'code': '2883',
1473
+ 'order_cond': 'CASH',
1474
+ 'order_lot': 'Common',
1475
+ 'price': '14.4',
1476
+ 'quantity': '3'
1477
+ }
1478
+
1479
+ ```
1480
+
1481
+ 🔆 **成交回報資訊**
1482
+
1483
+ ```python
1484
+ trade_id (str): 與委託回報id相同
1485
+ seqno (str): 平台單號
1486
+ ordno (str): 前五碼為同委託回報委託單號,後三碼為同筆委託成交交易序號。
1487
+ exchange_seq (str): 回報序號
1488
+ broker_id (str): 分行代碼
1489
+ account_id (str): 帳號
1490
+ action (str): 買賣別 {Buy, Sell}
1491
+ code (str): 商品代碼
1492
+ order_cond (str): {
1493
+ Cash: 現股,
1494
+ MarginTrading: 融資,
1495
+ ShortSelling: 融券,
1496
+ ShortUnLimit: 借券賣出,
1497
+ EDN: 賣出擔保品
1498
+ }
1499
+ order_lot (str): {
1500
+ Common: 整股,
1501
+ Fixing: 定盤,
1502
+ Odd: 盤後零股,
1503
+ IntradayOdd: 盤中零股
1504
+ }
1505
+ price (float or int): 成交價
1506
+ quantity (int): 成交量
1507
+ web_id (str): 平台代碼
1508
+ custom_field (str): 自訂欄位
1509
+ ts (int): 成交時間戳
1510
+
1511
+ ```
1512
+
1513
+ ## 期貨回報
1514
+
1515
+ ### 委託
1516
+
1517
+ ☑️ Order Callback
1518
+
1519
+ ```python
1520
+ OrderState.FuturesOrder {
1521
+ 'operation': {
1522
+ 'op_type': 'New',
1523
+ 'op_code': '0',
1524
+ 'op_msg': '10000000'
1525
+ },
1526
+ 'order': {
1527
+ 'id': '00380622',
1528
+ 'ordno': 'wAaas',
1529
+ 'account': {
1530
+ 'account_type': 'F',
1531
+ 'broker_id': 'F004009',
1532
+ 'account_id': '1234567'
1533
+ },
1534
+ 'action': 'Buy',
1535
+ 'price': '0021300.00',
1536
+ 'quantity': '3',
1537
+ 'order_type': 'ROD',
1538
+ 'price_type': 'MKT',
1539
+ 'oc_type': 'New'
1540
+ },
1541
+ 'status': {
1542
+ 'id': '00380622',
1543
+ 'modified_price': 0,
1544
+ 'cancel_quantity': 0,
1545
+ 'order_quantity': '3',
1546
+ 'web_id': '126'
1547
+ },
1548
+ 'contract': {
1549
+ 'security_type': 'FUT',
1550
+ 'exchange': 'TAIFEX',
1551
+ 'code': 'TXFF4'
1552
+ }
1553
+ }
1554
+
1555
+ ```
1556
+
1557
+ 🔆 **委託回報資訊**
1558
+
1559
+ - order
1560
+
1561
+ ```python
1562
+ id (str): 與成交回報的trade_id相同
1563
+ seqno (str): 平台單號
1564
+ ordno (str): 委託單號
1565
+ account (dict): 帳號資訊
1566
+ action (str): 買賣別
1567
+ price (float or int): 委託價
1568
+ quantity (int): 委託量
1569
+ order_cond (str): {
1570
+ Cash: 現股,
1571
+ MarginTrading: 融資,
1572
+ ShortSelling: 融券
1573
+ }
1574
+ order_type (str): 委託類別 {ROD, IOC, FOK}
1575
+ price_type (str): {LMT: 限價, MKT: 市價, MKP: 範圍市價}
1576
+ market_type (str): 市場別 {Day:日盤, Night:夜盤}
1577
+ oc_type(str): {New: 新倉, Cover: 平倉, Auto: 自動}
1578
+ subaccount(str): 子帳號
1579
+ combo (bool): 是否為組合單
1580
+
1581
+ ```
1582
+
1583
+ - contract
1584
+
1585
+ ```python
1586
+ security_type (str): 商品類別
1587
+ code (str): 商品代碼
1588
+ exchange (str): 交易所
1589
+ delivery_month (str): 交割月份
1590
+ delivery_date (str): 交割日期
1591
+ strike_price (float): 履約價
1592
+ option_right (str): {Future, OptionCall, OptionPut}
1593
+
1594
+ ```
1595
+
1596
+ ### 成交
1597
+
1598
+ ☑️ Deal Callback
1599
+
1600
+ ```python
1601
+ OrderState.FuturesDeal {
1602
+ 'trade_id': '00380622',
1603
+ 'ordno': 'wAaas',
1604
+ 'exchange_seq': '00000005',
1605
+ 'broker_id': 'F004009',
1606
+ 'account_id': '0118131',
1607
+ 'action': 'Buy',
1608
+ 'code': 'TXFF4',
1609
+ 'price': '0021300.00',
1610
+ 'quantity': '3',
1611
+ 'security_type': '1'
1612
+ }
1613
+
1614
+ ```
1615
+
1616
+ 🔆 **成交回報資訊**
1617
+
1618
+ ```python
1619
+ trade_id (str): 與委託回報id相同
1620
+ seqno (str): 平台單號
1621
+ ordno (str): 前五碼為同委託回報委託單號,後三碼為同筆委託成交交易序號。
1622
+ exchange_seq (str): 回報序號
1623
+ broker_id (str): 分行代碼
1624
+ account_id (str): 帳號
1625
+ action (str): 買賣別
1626
+ code (str): 商品代碼
1627
+ price (float or int): 成交價
1628
+ quantity (int): 成交量
1629
+ subaccount (str): 子帳號
1630
+ security_type (str): 商品類別
1631
+ delivery_month (str): 交割月份
1632
+ strike_price (float): 履約價
1633
+ option_right (str): {Future, OptionCall, OptionPut}
1634
+ market_type (str): {Day, Night}
1635
+ ts (int): 成交時間戳
1636
+
1637
+ ```
1638
+
1639
+ ---
1640
+
1641
+ # 行情資料
1642
+
1643
+ 利用訂閱商品檔的方式去取得即時行情。
1644
+
1645
+ ### **Callback**
1646
+
1647
+ 預設狀況下我們將即時行情使用`print`的方式呈現。可根據個人需求修改函數。請避免在函數內進行運算。
1648
+
1649
+ ## 訂閱證券行情
1650
+
1651
+ ☑️ **Subscribe**
1652
+
1653
+ ```python
1654
+ >> api.quote.subscribe?
1655
+ Signature:
1656
+ api.quote.subscribe(
1657
+ contract:superpy.contracts.Contract,
1658
+ quote_type:superpy.constant.QuoteType=<QuoteType.Tick: 'tick'>,
1659
+ intraday_odd:bool=False,
1660
+ )
1661
+
1662
+ ```
1663
+
1664
+ 🔆 **Quote Parameters**
1665
+
1666
+ ```python
1667
+ quote_type: 訂閱類型 {'tick', 'bidask'}
1668
+ intraday_odd: 盤中零股 {True, False}
1669
+
1670
+ ```
1671
+
1672
+ ## Tick
1673
+
1674
+ ### **整股**
1675
+
1676
+ **➡️ In**
1677
+
1678
+ ```python
1679
+ api.quote.subscribe(
1680
+ api.Contracts.Stocks["2330"],
1681
+ quote_type = sp.constant.QuoteType.Tick,
1682
+ )
1683
+
1684
+ ```
1685
+
1686
+ ### **盤中零股**
1687
+
1688
+ **➡️ In**
1689
+
1690
+ ```python
1691
+ api.quote.subscribe(
1692
+ api.Contracts.Stocks["2330"],
1693
+ quote_type = sp.constant.QuoteType.Tick,
1694
+ intraday_odd = True)
1695
+
1696
+ ```
1697
+
1698
+ **✅ Out**
1699
+
1700
+ ```python
1701
+ Exchange.TSE
1702
+ Tick: {
1703
+ 'code': '2330',
1704
+ 'datetime': 120048039,
1705
+ 'amount': 827.0,
1706
+ 'volume': 2.0,
1707
+ 'total_volume': 18668.0,
1708
+ 'simtrade': False,
1709
+ 'intraday_odd': 0
1710
+ }
1711
+
1712
+ ```
1713
+
1714
+ ### **屬性**
1715
+
1716
+ ```python
1717
+ code (str): 商品代碼
1718
+ datetime (datetime): 時間
1719
+ open (decimal): 開盤價
1720
+ avg_price (decimal): 均價
1721
+ close (decimal): 成交價
1722
+ high (decimal): 最高價(自開盤)
1723
+ low (decimal): 最低價(自開盤)
1724
+ amount (decimal): 成交額 (NTD)
1725
+ total_amount (decimal): 總成交額 (NTD)
1726
+ volume (int): 成交量 (整股:張, 盤中零股: 股)
1727
+ total_volume (int): 總成交量 (整股:張, 盤中零股: 股)
1728
+ tick_type (int): 內外盤別{1: 外盤, 2: 內盤, 0: 無法判定}
1729
+ chg_type (int): 漲跌註記{1: 漲停, 2: 漲, 3: 平盤, 4: 跌, 5: 跌停}
1730
+ price_chg (decimal): 漲跌
1731
+ pct_chg (decimal): 漲跌幅
1732
+ bid_side_total_vol (int): 買盤成交總量 (整股:張, 盤中零股: 股)
1733
+ ask_side_total_vol (int): 賣盤成交總量 (整股:張, 盤中零股: 股)
1734
+ bid_side_total_cnt (int): 買盤成交筆數
1735
+ ask_side_total_cnt (int): 賣盤成交筆數
1736
+ closing_oddlot_shares (int): 盤後零股成交股數(股)
1737
+ fixed_trade_vol (int): 定盤成交量 (整股:張, 盤中零股: 股)
1738
+ suspend (bool): 暫停交易
1739
+ simtrade (bool): 試撮
1740
+ intraday_odd (int): 盤中零股 {0: 整股, 1:盤中零股}
1741
+
1742
+ ```
1743
+
1744
+ ## **BidAsk**
1745
+
1746
+ ### **整股**
1747
+
1748
+ **➡️ In**
1749
+
1750
+ ```python
1751
+ api.quote.subscribe(
1752
+ api.Contracts.Stocks["2330"],
1753
+ quote_type = sp.constant.QuoteType.BidAsk,
1754
+ )
1755
+
1756
+ ```
1757
+
1758
+ ### **盤中零股**
1759
+
1760
+ **➡️ In**
1761
+
1762
+ ```python
1763
+ api.quote.subscribe(
1764
+ api.Contracts.Stocks["2330"],
1765
+ quote_type = sp.constant.QuoteType.BidAsk,
1766
+ intraday_odd = True)
1767
+
1768
+ ```
1769
+
1770
+ **✅ Out**
1771
+
1772
+ ```python
1773
+ Exchange.TSE
1774
+ BidAsk: {
1775
+ 'code': '2330',
1776
+ 'datetime': 120048039,
1777
+ 'bid_price': [827.0, 826.0, 825.0, 824.0, 823.0],
1778
+ 'bid_volume': [82, 72, 183, 119, 144],
1779
+ 'ask_price': [828.0, 829.0, 830.0, 831.0, 832.0],
1780
+ 'ask_volume': [4, 170, 266, 627, 185],
1781
+ 'simtrade': False,
1782
+ 'intraday_odd': 0
1783
+ }
1784
+
1785
+ ```
1786
+
1787
+ ### **屬性**
1788
+
1789
+ ```python
1790
+ code (str): 商品代碼
1791
+ datetime (datetime): 時間
1792
+ bid_price (:List:decimal): 委買價
1793
+ bid_volume (:List:int): 委買量 (張)
1794
+ diff_bid_vol (:List:int): 買價增減量 (張)
1795
+ ask_price (:List:decimal): 委賣價
1796
+ ask_volume (:List:int): 委賣量
1797
+ diff_ask_vol (:List:int): 賣價增減量 (張)
1798
+ suspend (bool): 暫停交易
1799
+ simtrade (bool): 試撮
1800
+
1801
+ ```
1802
+
1803
+ ## **Callback**
1804
+
1805
+ ### Tick
1806
+
1807
+ ☑️ **decorator方式**
1808
+
1809
+ ```python
1810
+ from superpy import TickSTKv1, Exchange
1811
+ @api.on_tick_stk_v1()
1812
+ def quote_callback(exchange: Exchange, tick:TickSTKv1):
1813
+ print(f"Exchange: {exchange}, Tick: {tick}")
1814
+
1815
+ ```
1816
+
1817
+ ☑️ **傳統方式**
1818
+
1819
+ ```python
1820
+ from superpy import TickSTKv1, Exchange
1821
+ def quote_callback(exchange: Exchange, tick:TickSTKv1):
1822
+ print(f"Exchange: {exchange}, Tick: {tick}")
1823
+ api.quote.set_on_tick_stk_v1_callback(quote_callback)
1824
+
1825
+ ```
1826
+
1827
+ ### **BidAsk**
1828
+
1829
+ ☑️ **decorator方式**
1830
+
1831
+ ```python
1832
+ from superpy import BidAskSTKv1, Exchange
1833
+ @api.on_bidask_stk_v1()
1834
+ def quote_callback(exchange: Exchange, bidask:BidAskSTKv1):
1835
+ print(f"Exchange: {exchange}, BidAsk: {bidask}")
1836
+
1837
+ ```
1838
+
1839
+ ☑️ **傳統方式**
1840
+
1841
+ ```python
1842
+ from superpy import BidAskSTKv1, Exchange
1843
+ def quote_callback(exchange: Exchange, bidask:BidAskSTKv1):
1844
+ print(f"Exchange: {exchange}, BidAsk: {bidask}")
1845
+ api.quote.set_on_bidask_stk_v1_callback(quote_callback)
1846
+
1847
+ ```
1848
+
1849
+ ---
1850
+
1851
+ ## 訂閱期貨行情
1852
+
1853
+ ☑️ **Subscribe**
1854
+
1855
+ ```python
1856
+ >> api.quote.subscribe?
1857
+ Signature:
1858
+ api.quote.subscribe(
1859
+ contract:superpy.contracts.Contract,
1860
+ quote_type:superpy.constant.QuoteType=<QuoteType.Tick: 'tick'>,
1861
+ )
1862
+
1863
+ ```
1864
+
1865
+ 🔆 **Quote Parameters**
1866
+
1867
+ ```python
1868
+ quote_type: 訂閱類型 {'tick', 'bidask'}
1869
+
1870
+ ```
1871
+
1872
+ ## Tick
1873
+
1874
+ ### 範例
1875
+
1876
+ **➡️ In**
1877
+
1878
+ ```python
1879
+ api.quote.subscribe(
1880
+ api.Contracts.Futures['TXFF4'],
1881
+ quote_type = sp.constant.QuoteType.Tick,
1882
+ )
1883
+
1884
+ ```
1885
+
1886
+ **✅ Out**
1887
+
1888
+ ```python
1889
+ Exchange.TAIFEX
1890
+ Tick: {
1891
+ 'code': 'TXFF4',
1892
+ 'datetime': 12022614,
1893
+ 'amount': 21260.0,
1894
+ 'volume': 7,
1895
+ 'total_volume': 80869,
1896
+ 'bid_side_total_vol': 54879,
1897
+ 'ask_side_total_vol': 50711,
1898
+ 'simtrade': 0
1899
+ }
1900
+
1901
+ ```
1902
+
1903
+ ### **屬性**
1904
+
1905
+ ```python
1906
+ code (str): 商品代碼
1907
+ datetime (datetime.datetime): 日期
1908
+ open (Decimal): 開盤價
1909
+ underlying_price (Decimal): 標的物價格
1910
+ bid_side_total_vol(int): 買盤成交總量 (lot)
1911
+ ask_side_total_vol(int): 賣盤成交總量 (lot)
1912
+ avg_price (Decimal): 均價
1913
+ close (Decimal): 成交價
1914
+ high (Decimal): 最高價(自開盤)
1915
+ low (Decimal): 最低價(自開盤)
1916
+ amount (Decimal): 成交額 (NTD)
1917
+ total_amount (Decimal): 總成交額 (NTD)
1918
+ volume (int): 成交量 (lot)
1919
+ total_volume (int): 總成交量 (lot)
1920
+ tick_type (int): 內外盤別{1: 外盤, 2: 內盤, 0: 無法判定}
1921
+ chg_type (int): 漲跌註記{1: 漲停, 2: 漲, 3: 平盤, 4: 跌, 5: 跌停}
1922
+ price_chg (Decimal): 漲跌
1923
+ pct_chg (Decimal): 漲跌幅 (%)
1924
+ simtrade (int): 試撮
1925
+
1926
+ ```
1927
+
1928
+ ## **BidAsk**
1929
+
1930
+ ### 範例
1931
+
1932
+ **➡️ In**
1933
+
1934
+ ```python
1935
+ api.quote.subscribe(
1936
+ api.Contracts.Futures['TXFF4'],
1937
+ quote_type = sp.constant.QuoteType.BidAsk,
1938
+ )
1939
+
1940
+ ```
1941
+
1942
+ **✅ Out**
1943
+
1944
+ ```python
1945
+ Exchange.TAIFEX
1946
+ BidAsk: {
1947
+ 'code': 'TXFF4',
1948
+ 'datetime': 12022617,
1949
+ 'bid_price': [21260.0, 21259.0, 21258.0, 21257.0, 21256.0],
1950
+ 'bid_volume': [4, 8, 9, 13, 9],
1951
+ 'ask_price': [21262.0, 21263.0, 21264.0, 21265.0, 21266.0],
1952
+ 'ask_volume': [10, 11, 11, 19, 8],
1953
+ 'first_derived_bid_price': 0.0,
1954
+ 'first_derived_ask_price': 0.0,
1955
+ 'first_derived_bid_vol': 0,
1956
+ 'first_derived_ask_vol': 0,
1957
+ 'simtrade': 0
1958
+ }
1959
+
1960
+ ```
1961
+
1962
+ ### **屬性**
1963
+
1964
+ ```python
1965
+ code (str): 商品代碼
1966
+ datetime (datetime.datetime): 時間
1967
+ bid_total_vol (int): 委買量總計 (lot)
1968
+ ask_total_vol (int): 委賣量總計 (lot)
1969
+ bid_price (:List:decimal): 委買價
1970
+ bid_volume (:List:int): 委買量 (lot)
1971
+ diff_bid_vol (:List:int): 委買價增減量 (lot)
1972
+ ask_price (:List:decimal): 委賣價
1973
+ ask_volume (:List:int): 委賣量 (lot)
1974
+ diff_ask_vol (:List:int): 委賣價增減量 (lot)
1975
+ first_derived_bid_price (decimal): 衍生一檔委買價
1976
+ first_derived_ask_price (decimal): 衍生一檔委賣價
1977
+ first_derived_bid_vol (int): 衍生一檔委買量
1978
+ first_derived_ask_vol (int): 衍生一檔委賣量
1979
+ underlying_price (decimal): 標的物價格
1980
+ simtrade (int): 試撮
1981
+
1982
+ ```
1983
+
1984
+ ## **Callback**
1985
+
1986
+ ### Tick
1987
+
1988
+ ☑️ **decorator方式**
1989
+
1990
+ ```python
1991
+ from superpy import TickFOPv1, Exchange
1992
+ @api.on_tick_fop_v1()
1993
+ def quote_callback(exchange:Exchange, tick:TickFOPv1):
1994
+ print(f"Exchange: {exchange}, Tick: {tick}")
1995
+
1996
+ ```
1997
+
1998
+ ☑️ **傳統方式**
1999
+
2000
+ ```python
2001
+ from superpy import TickFOPv1, Exchange
2002
+ def quote_callback(exchange:Exchange, tick:TickFOPv1):
2003
+ print(f"Exchange: {exchange}, Tick: {tick}")
2004
+ api.quote.set_on_tick_fop_v1_callback(quote_callback)
2005
+
2006
+ ```
2007
+
2008
+ ### **BidAsk**
2009
+
2010
+ ☑️ **decorator方式**
2011
+
2012
+ ```python
2013
+ from superpy import BidAskFOPv1, Exchange
2014
+ @api.on_bidask_fop_v1()
2015
+ def quote_callback(exchange:Exchange, bidask:BidAskFOPv1):
2016
+ print(f"Exchange: {exchange}, BidAsk: {bidask}")
2017
+
2018
+ ```
2019
+
2020
+ ☑️ **傳統方式**
2021
+
2022
+ ```python
2023
+ from superpy import BidAskFOPv1, Exchange
2024
+ def quote_callback(exchange:Exchange, bidask:BidAskFOPv1):
2025
+ print(f"Exchange: {exchange}, BidAsk: {bidask}")
2026
+ api.quote.set_on_bidask_fop_v1_callback(quote_callback)
2027
+
2028
+ ```
2029
+
2030
+ ## 訂閱新編指數行情
2031
+
2032
+ [**本國指數國際證券辨識號碼一覽表**](https://isin.twse.com.tw/isin/C_public.jsp?strMode=11)
2033
+
2034
+ ☑️ **Subscribe**
2035
+
2036
+ ```python
2037
+ >> api.quote.subNewIndex?
2038
+ Signature:
2039
+ api.quote.subNewIndex(
2040
+ code
2041
+ )
2042
+ ```
2043
+
2044
+ ☑️ Un**Subscribe**
2045
+
2046
+ ```python
2047
+ >> api.quote.unsubNewIndex?
2048
+ Signature:
2049
+ api.quote.unsubNewIndex(
2050
+ code
2051
+ )
2052
+ ```
2053
+
2054
+ 🔆 **Quote Parameters**
2055
+
2056
+ ```python
2057
+ code: 新編指數代號
2058
+
2059
+ ```
2060
+
2061
+ ### 範例
2062
+
2063
+ **➡️ In**
2064
+
2065
+ ```python
2066
+ api.quote.subNewIndex("IX0001")
2067
+ ```
2068
+
2069
+ **✅ Out**
2070
+
2071
+ ```python
2072
+ Exchange.TSE
2073
+ NewIndex: {
2074
+ 'IndexNo': 'IX0001',
2075
+ 'IndexTime': 090000,
2076
+ 'LastestIndex': 123456789,
2077
+ }
2078
+
2079
+ ```
2080
+
2081
+ ### **屬性**
2082
+
2083
+ ```python
2084
+ IndexNo (str): 指數代號
2085
+ IndexTime (datetime.datetime): 指數時間
2086
+ LastestIndex (Decimal): 最新指數
2087
+ ```