pyConcords 0.5.4
Python client for send order to Concords Securities.
License: MIT
Requires Python: >=3.5
Keywords: stock,futures,option,TWSE,TPEX,TAIFEX

# pyConcords

![PyPI](https://img.shields.io/pypi/v/pyConcords)
![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pyConcords)
![PyPI - Format](https://img.shields.io/pypi/format/pyConcords)
![PyPI - License](https://img.shields.io/pypi/l/pyConcords)
![PyPI - Downloads](https://img.shields.io/pypi/dm/pyConcords)
![contact](https://img.shields.io/badge/contact-康和證券集團-red)

康和綜合證券程式下單API整合了.net framework 4.5架構下的交易介面，讓使用者可以輕鬆的運用python執行快速下單。

>pyConcords is a securities, futures, option API, which send orders to Concord Securities Group.
This package integrated with .net framework 4.5, and is compatible with Python 3.5+.
It is distributed under the MIT license.

- [Installation](#installation)
    - [Preinstall](#preinstall)
    - [Binaries](#binaries)
- [Quick Starts](#quick-starts)
    - [Login](#login)
- [Place Order](#place-order)
    - [Stock](#stock)
    - [Futures](#futures)
    - [Foreign Futures](#foreign-futures)
- [Register Event](#register-event)
- [Reference](#reference)


## Installation
### Preinstall
>API usage needs application in advance. In order to use this package, please contact us.

### Binaries
simply use pip to install
```
pip install pyConcords
```

## Quick Starts
### Login
```python
from pyConcords import StockAPI

agent = StockAPI()
agent.login("YOUR_PERSON_ID", "YOUR_PASSWORD")
```
```python
from pyConcords import FuturesAPI

agent = FuturesAPI()
agent.login("YOUR_PERSON_ID", "YOUR_PASSWORD")
```
```python
from pyConcords import ForeignFuturesAPI

agent = ForeignFuturesAPI()
agent.login("YOUR_PERSON_ID", "YOUR_PASSWORD")
```

## Place Order
### Stock
```python
from pyConcords.Stock import constant

result = agent.order(action = constant.Action.NEW_ORDER.value,
                     branch_no = '8450',
                     account_no = "YOUR_ACCOUNT",
                     lot_type = constant.LotType.ROUND_LOT.value,
                     order_type = constant.OrderType.ORDINARY.value,
                     side = constant.Side.BUY.value,
                     day_trade_flag = constant.DayTradeFlag.N.value,
                     symbol = '6016',
                     quantity = 1,
                     price = 10.0, 
                     price_flag = constant.PriceFlag.FIX.value,
                     time_in_force = constant.TimeInForce.ROD.value)
```
### Futures
```python
from pyConcords.Futures import constant

result = agent.order(action = constant.Action.NEW_ORDER.value,
                     branch_no = 'F029000',
                     account_no = "YOUR_ACCOUNT",
                     market_type = constant.Market.FUTURES.value,
                     compound_type = constant.CompoundType.FUTURES_SINGLE.value,
                     commodity = 'TXFI0',
                     time_in_force = constant.TimeInForce.ROD.value,
                     writeoff = constant.WriteOff.AUTO.value,
                     order_type = constant.OrderType.LIMIT.value,
                     side = constant.Side.BUY.value,
                     quantity = 1,
                     price = 12000.00)
```
### Foreign Futures
```python
agent.order(action=constant.Action.NEW_ORDER.value,
            branch_no = 'F029000',
            account_no = "YOUR_ACCOUNT",
            exchange = 'NYM',
            side1 = constant.Side.BUY.value,
            market_type1 = constant.Market.FUTURES.value,
            commodity1 = 'QM',
            commodity_month1 = '202010',
            strike_price1 = 0,
            put_call1 = constant.PutCall.NONE.value,
            side2 = '',
            market_type2 = '',
            commodity2 = '',
            commodity_month2 = '',
            strike_price2 = 0,
            put_call2 = constant.PutCall.NONE.value,
            order_type = constant.ForeignOrderType.LIMIT.value,
            price = 0,
            price_numerator = 0,
            price_denominator = 0,
            stop_price = 42.95,
            stop_price_numerator = 0,
            stop_price_denominator = 0,
            quantity = 1,
            writeoff = constant.Writeoff.OPEN.value)
```

## Register Event
>If you want to catch order or match report, one can define a function and register

### Stock
```python
def OrderReportHandler(result, msg):
        order_result = {'result'    : result.ResultType,
                        'status'    : result.StatusCode,
                        'desc'      : result.CodeDesc,
                        'order_guid'    : result.OrderGuid,
                        'net_no'    : result.ClOrdID
                        }

        if result.ResultType=='1':
            report = result.ExcReport
            exe_report = {'order_id'  : report.OrderID,
                        'orig_net_no'   : report.OrigClOrdID,
                        'execute_status'    : report.ExecType,
                        'account_no'    : report.Account,
                        'symbol'    : report.Symbol,
                        'side'      : report.Side,
                        'quantity'  : report.OrderQty,
                        'price'     : report.Price,
                        'strike_quantity'   : report.LastQty,
                        'strike_price'   : report.LastPx,
                        'transaction_time'  : report.TransactTime}

agent.OrderReportEvent += OrderReportHandler
```

### Futures
```python
def OrderReportHandler(code, msg):
    dict_msg = dict((agent.msg_tag[k], v) for k,v in [tag.split('=') for tag in msg.split('|')])
    print(dict_msg)

agent.OnFOrderReport += OrderReportHandler
```

## Reference
https://www.concords.com.tw/download/20200318_證券API客戶使用元件及文件.zip
https://www.concordfutures.com.tw/WebSiteUpload//Files/Document/20200728_期貨API.zip