> For the complete documentation index, see [llms.txt](https://algomaster.gitbook.io/tcore-api/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://algomaster.gitbook.io/tcore-api/python/tcoreapimq.py-jiao-yi-1.md).

# TCore Python API 交易

## trade\_connect

交易连线

{% tabs %}
{% tab title="Request" %}

```python
def trade_connect(self,port):
```

| **Parameters** | 说明                   |
| -------------- | -------------------- |
| port           | 连线port，搜寻 ZMQ log取得。 |
| {% endtab %}   |                      |

{% tab title="Return" %}
回复SessionKey
{% endtab %}
{% endtabs %}

## trade\_logout

交易断线

```python
def trade_logout(self,SessionKey):
```

| **Parameters** | 说明                    |
| -------------- | --------------------- |
| SessionKey     | connect时回传的sessionkey |

## account\_lookup

取得交易帐号

{% tabs %}
{% tab title="Request" %}

```python
def account_lookup(self,SessionKey):
```

| **Parameters** | 说明                    |
| -------------- | --------------------- |
| SessionKey     | connect时回传的sessionkey |
| {% endtab %}   |                       |

{% tab title="Retrun" %}
[返回交易帐号资料](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#jiao-yi-zhang-hao-zi-liao-hui-fu)
{% endtab %}

{% tab title="Description" %}

交易帐号变动会从先前创建的[交易callback线程处理函数](/tcore-api/python/tcore-python-api-kuai-su-an-zhuang-shuo-ming.md#jian-li-jiao-yi-callback-xian-cheng-chu-li-han-shu)，推送回来。并借由判断资料类型message\["DataType"]="ACCOUNTS"，做对应的函数处理。
{% endtab %}
{% endtabs %}

## restore\_report

回报回补

{% tabs %}
{% tab title="Request" %}

```python
def restore_report(self,SessionKey,QryIndex):
```

| **Parameters** | 说明                    |
| -------------- | --------------------- |
| SessionKey     | connect时回传的sessionkey |
| QryIndex       | 取得资料，从第几笔开始。          |
| {% endtab %}   |                       |

{% tab title="Return" %}
[返回回报回补资料](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#hui-bao-hui-bu-zi-liao-hui-fu)
{% endtab %}

{% tab title="Description" %}
分页查询需要带入QryIndex(带空则回第一页)，每页50笔资料，拿最后一笔QryIndex资讯可以往下查，最后一页则回空资料。
{% endtab %}
{% endtabs %}

## RestoreFillReport

回补成交回报

{% tabs %}
{% tab title="Request" %}

```python
def RestoreFillReport(self,key,qryIndex):
```

| **Parameters** | 说明                    |
| -------------- | --------------------- |
| SessionKey     | connect时回传的sessionkey |
| qryIndex       | 取得资料，从第几笔开始。          |
| {% endtab %}   |                       |

{% tab title="Return" %}
[成交回报栏位资料](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#cheng-jiao-hui-bu-zi-liao)
{% endtab %}

{% tab title="Description" %}
分页查询需要带入QryIndex(带空则回第一页)，每页50笔资料，拿最后一笔QryIndex资讯可以往下查，最后一页则回空资料。
{% endtab %}
{% endtabs %}

## new\_order

下单

{% tabs %}
{% tab title="Request" %}

```python
def new_order(self,SessionKey,Param):
```

| **Parameters** | 说明                                                                             |
| -------------- | ------------------------------------------------------------------------------ |
| SessionKey     | connect时回传的sessionkey                                                          |
| Param          | [下单参数](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#xia-dan-can-shu)，Json格式。 |
| {% endtab %}   |                                                                                |

{% tab title="Return" %}
ErrorCode :\
-10 Unknow Error \
-11 买卖别不对 \
-12 复式单商品代码解析错误\
-13 下单帐号, 不可下此交易所商品 \
-14 下单错误, 不支援的价格 或 OrderType 或 TimeInForce \
-15 不支援证券下单 \
-20 连线未建立 \
-22 价格的TickSize错误 \
-23 下单数量超过该商品的上下限 \
-24 下单数量错误 \
-25 价格不能小于和等于0 (市价类型不会去检查)
{% endtab %}
{% endtabs %}

## replace\_order

改单

```python
def replace_order(self,SessionKey,Param):
```

| **Parameters** | 说明                                                                             |
| -------------- | ------------------------------------------------------------------------------ |
| SessionKey     | connect时回传的sessionkey                                                          |
| Param          | [改单参数](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#gai-dan-can-shu)，Json格式。 |

## cancel\_order

删单

```python
def cancel_order(self,SessionKey,Param):
```

| **Parameters** | 说明                                                                                                                                                |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| SessionKey     | connect时回传的sessionkey                                                                                                                             |
| Param          | <p>删单参数，Json格式。</p><p><strong>ex.</strong></p><p><strong>{</strong></p><p>    <strong>"ReportID":"123456"</strong>  </p><p><strong>}</strong></p> |

## margin

资金查询

{% tabs %}
{% tab title="Request" %}

```python
def margin(self,SessionKey,BrkAcct):
```

| **Parameters** | 说明                    |
| -------------- | --------------------- |
| SessionKey     | connect时回传的sessionkey |
| BrkAcct        | BrokerID-Account      |
| {% endtab %}   |                       |

{% tab title="Return" %}
[返回资金帐务](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#zi-jin-zhang-wu-hui-fu)
{% endtab %}
{% endtabs %}

## position

部位查询

{% tabs %}
{% tab title="Request" %}

```python
def position(self,SessionKey,BrkAcct,QryIndex):
```

| **Parameters** | 说明                    |
| -------------- | --------------------- |
| SessionKey     | connect时回传的sessionkey |
| BrkAcct        | BrokerID-Account      |
| QryIndex       | 取得资料，从第几笔开始。          |
| {% endtab %}   |                       |

{% tab title="Return" %}
[返回部位](/tcore-api/python/jiao-yi-can-shu-yu-hui-fu.md#bu-wei-zhang-wu-hui-fu)
{% endtab %}

{% tab title="Description" %}
分页查询需要带入QryIndex(带空则回第一页)，每页50笔资料，拿最后一笔QryIndex资讯可以往下查，最后一页则回空资料。
{% endtab %}
{% endtabs %}

## TradePong

Pong回复以维持连线

```python
def TradePong(self, SessionKey):
```

## OptComb

组合指令

```python
def OptComb(self,key,Param):
```

## QueryOptCombOrder

查询组合回报

```python
def QueryOptCombOrder(self,key,AM,qryIndex):
```

## 主动推送回报(委托回报、成交回报)

{% tabs %}
{% tab title="Description" %}
最新回报变动会从先前创建的[交易callback线程处理函数](/tcore-api/python/tcore-python-api-kuai-su-an-zhuang-shuo-ming.md#jian-li-jiao-yi-callback-xian-cheng-chu-li-han-shu)，推送回来。并借由判断资料类型message\["DataType"]="EXECUTIONREPORT" 或 message\["DataType"] == "FILLEDREPORT"，做对应的函数处理。

回报资料为Json格式。
{% endtab %}
{% endtabs %}
