# 下单流程

## 订阅消息流程：

### 1.获取token

调用接口：[获取token](https://docs.multimarkets.org/client-api/open-api/message/get-msg-token)&#x20;

取出返回结果中的data字段备用，这个就是token

### 2.订阅成交消息：

接口文档说明：[消息订阅对接](https://docs.multimarkets.org/client-api/open-api/message/msg-subscribe)

#### 2.1 先建立连接

PRE环境地址：wss\://pre-api-test.cmfbl.com/openapi-c/openMsg/{appId}

#### 2.2 登录发送消息：

```
{"device":1,"head":{"lang":"en-US","msgType":"login","sendTime":1698299047686,"token":"dbe3d3e6-0b10-4bdc-9778-dfc76c6d9341app"},"seqId":3,"trace":"x-1698299047686-4"}  
```

#### 2.3 订阅发送消息：

```
{"data":{"tradeTypes":"2"},"device":1,"head":{"lang":"en-US","msgType":"subscribe_asset","sendTime":1698299047686,"token":"dbe3d3e6-0b10-4bdc-9778-dfc76c6d9341app"},"seqId":5,"trace":"x-1698299047686-6"}  
```

#### 2.4 另外每15秒发送一下心跳消息，保持心跳：

```
{"device":1,"head":{"lang":"en-US","msgType":"ping","sendTime":1698299057667,"token":"dbe3d3e6-0b10-4bdc-9778-dfc76c6d9341app"},"seqId":7,"trace":"x-1698299057667-8"}
```

## 交易下单流程：

### 1.请求产品信息：

调用接口：[产品简要数据](https://docs.multimarkets.org/client-api/open-api/base/symbol-base-info)&#x20;

取出symbolId,symbolDigits备用。

### 2.请求客户信息：

调用接口：[查询客户信息](https://docs.multimarkets.org/client-api/open-api/customer/customer-info)&#x20;

取出accountList中的trade\_type=2的那条数据中的accountId,digits,currency备用。

### 3.交易下单请求：

调用接口：[下单](https://docs.multimarkets.org/client-api/open-api/trade/contract-order)&#x20;

参数示例：\
http header:

```
Version: 0.01  
Group: tradeApi  
Trace: [Custom unique string] 
Content-Type: application/json  
Accept: application/json  
Apikey: [Already provided.]  
Timestamp: [Millisecond timestamp]
CompanyId: [Already provided (Test field: 439)] 
Signature: Signature, the generation rules are here <https://mtf.readme.io/reference/sgin, with example reference.>
```

http body：

```
{
     "accountCurrency":"CNY", //Parameters obtained in step 2
     "accountDigits":2, //Parameters obtained in step 2
     "accountId":1034124, //Parameters obtained in step 2
     "bizType":1, //Currently only supports these four types: business types, 1-market price opening; 2-market price flat; 13-limit price opening order; 14-limit price closing order
     "crossLevelNum":1, //fixed with 1
     "direction":1, //Order buying and selling direction, 1-buy; 2-sell;
     "entryType":1, //Order type 1 by quantity, 2 by amount
     "expireType":1, //limit order expiration type, 1-valid on the day; 2-valid on the week;
     "requestNum":"0.100000", //Order lot size or order amount, used with entryType
     "requestPrice":"167655", //The decimal place obtained in step 1 is used to convert the quotation into an integer. For example, if the decimal place is 3 and the quotation is 1.23, it will be converted to: 1230
     "requestTime":1698299972000,
     "symbolId":8390, //Parameters obtained in step 1
     "tradeType":2 //Fixed to 2
}
```

### 4.处理下结果：

收到第3步的回应后取出code看是否为"0", 当结果是"0"时，代表下单委托成功，此时取出data中的orderId字段，也成交消息中的orderId字段对应起来，就形成完整的交易流程。\
成交消息中的字段说明：

```
{
     "trace":"x-1698310473323-102_b_bb76b676-8d8e-4278-82be-6ae7e063c9d3",
     "msgCode":"trade", //Only need to pay attention to msgCode=trade
     "content":{
         "accountId":1034124,
         "companyId":439,
         "bizType":"POSITION_OPEN", //POSITION_OPEN: Market price opening was successful, POSITION_CLOSE: Market price closing was successful, ME_POSITION_LIMIT_OPEN: Limit price opening was successful, ME_POSITION_LIMIT_CLOSE: Limit price closing was successful POSITION_OPEN_FAIL: Market price opening failed, ME_POSITION_LIMIT_FAIL: Limit price opening was Warehouse failed
         "customerId":2482,
         "paramMap":{
             "date":"1698310474529",
             "orderType":"1",
             "symbolId":"8390",
             "dealExecuteTime":"1698310474529",
             "orderBizType":"1",
             "orderId":"21257491", //Order ID
             "pendingId":"0",
             "dealId":"21262382",
             "accountCurrency":"CNY",
             "orderStatus":"2", //Order status: 1-order received; 2-order completed; 3-order partially completed; 4-order canceled; 5-order partially canceled; 6-order rejected; 7-order expired ;
             "dealExecuteNum":"8000.0",
             "source":"H5",
             "dealPrice":"1.67094", //Transaction price
             "requestAmount":"0.08",
             "takeProfitMsg":"--",
             "currency":"CNY",
             "symbolName":"EURAUD",
             "contractSize":"100000", //Contract size
             "sourceExecuteVolSum":"0.100000",
             "commission":"0",
             "sourceExecuteVol":"0.100000",
             "direction":"2",
             "dealAmount":"0.08", //Number of lots traded
             "accountId":"1034124",
             "companyId":"439",
             "positionId":"2023102650229", //Position ID
             "dealExecutePrice":"1.67094",
             "playType":"2",
             "orderDirection":"2",
             "symbolDigits":"5",
             "stopLossMsg":"--",
             "Margin":"59371.20"
         },
         "tradeType":2
     },
     "timestamp":1698310474563
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-cn.multimarkets.org/client-api/open-api/xia-dan-liu-cheng.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
