Quantide | 匡醍量化

East Money Quant API: Installation, Config & Testing Guide

中 📅 2023-12-13 👁 views this month —

[Table of Contents]

1. Installation and Configuration

1.1. Installation

  1. On your Windows machine, install https://emt.eastmoneysec.com/down and download/install the second software:

  2. Install conda on the same machine. We recommend installing Miniconda and creating a virtual runtime environment (Python version 3.8):

    conda create -n gmclient python=3.8
  3. Install gmadaptor:

    pip install gmadaptor-1.1.3-py3-none-any.whl

1.2. Permission Request

Please join the East Money Quant Simulation Group: 971584613, and contact the administrator to request live trading permissions. Review the pinned files in the group first. The primary threshold for approval is a required initial capital of 1 million RMB. You can withdraw funds after approval.

1.3. Simulation and Testing

While waiting for live trading permissions, you can open a simulation account at https://emt.18.cn/apply/test-apply-client to debug your program and configuration.

After applying, record your standard capital account number and password, as shown below:

East Money Quant API: Installation, Config & Testing Guide

In the login interface, select "Simulation Trading":

East Money Quant API: Installation, Config & Testing Guide

After logging in, the interface displays as follows:

East Money Quant API: Installation, Config & Testing Guide

1.3.1. Account Configuration

The following steps apply to both live and simulation accounts.

1.3.1.1. gmadaptor Configuration File

You need to configure the live trading account from your quant software into the gmadaptor configuration file. In your user directory, create the gmadaptor/config directory and place the following file:

# defaults.yaml
log_level: INFO

server_info:
    port: 9000
    # client 使用这一token来访问 gmadaptor 提供的服务
    access_token : "84ae0899-7a8d-44ff-9983-4fa7cbbc424b"

gm_info:
    fake: false
    # 文件单输出目录
    gm_output: "~/gmadaptor/FileOrders/out"
    trade_fees:
        commission: 2.5
        stamp_duty: 10.0
        transfer_fee: 0.1
        minimum_cost: 5.0
    accounts:
        # 账号名
        - name: fileorder_s01
          acct_id: 1a66e81c-ae5d-11ec-aef5-00163e0a4100
          # 文件单输入目录。东财量化终端将从这里读取文件单
          acct_input: "~/gmadaptor/FileOrders/inputs/fileorder_s01"

In the above configuration, access_token can be any value you specify. Any client accessing this service must hold this token.

The gm_output/acct_input file will be automatically created by gmadaptor upon startup if it does not exist. Ensure that gmadaptor has read/write permissions for these folders.

The value under accounts > name comes from the name you specified when creating a file order in the EMC terminal, as shown in item 2 in the image below:

East Money Quant API: Installation, Config & Testing Guide

The accounts > acct_id comes from item 3 below; click ID to copy it:

East Money Quant API: Installation, Config & Testing Guide

1.3.1.2. Configuring EMC

In Quant > File Order > File Order Output, configure items 4, 5, 6, and 7 in the image below. For item 4, select the path set in gm_output in our configuration file above; for item 5, select csv as the output format; for item 6, select "Auto Start"; and for item 7, select all items.

East Money Quant API: Installation, Config & Testing Guide

In Quant > File Order > File Order Input, configure items 3 and 4 in the image below. For item 3, select the acct_input path set in our configuration file above; for item 4, select "Auto Start".

1

1.3.2. Simulation Run

In the gmclient virtual environment created earlier, execute the following command to start the gmadaptor server:

python -m gmadaptor.server

If the following interface appears, it indicates that the server has started successfully:

1

At this point, open another conda window using the same gmclient virtual environment, and test via the following command:

python -m gmtest %account %token %server %port

Here, account refers to gm_info > accounts > account_id in the gmadaptor configuration file, and token refers to server_info > access_token.

Here, server refers to the IP address of the machine where gmadaptor is located, and port refers to the port number. If not provided, these default to localhost and 9000, respectively.

If the configuration is correct, this will print the initial account funds, current positions, and information about one buy and one sell order.

1.4. Operation and Maintenance

Additionally, start a scheduled task to launch EMC around 8:45 AM every day.

1.4.1. Startup

Use the following script to start:

@echo off
call C:\ProgramData\Anaconda3\Scripts\activate.bat C:\ProgramData\anaconda3
call conda activate gmclient
python -m gmadaptor.server
pause

1.4.2. Daily Maintenance

The EMC quant terminal can sometimes be unstable. We can improve stability by scheduling automatic restarts. Use the following code to exit EMC after market close:

REM kill process
TASKKILL /F /IM EMCTrade.exe

REM sleep 5 seconds
TIMEOUT  /T 5

REM remove all file orders after process killed

DEL /Q C:\zillionare\FileOrders\real_input\*.csv

Warning

If there are unarchived files in the input/output directories, quantitative trading will not start automatically. The last line of the above code serves to clean up unarchived files. This also requires users to manually verify orders to ensure these files can be automatically deleted.

2. Client-Server Interaction

2.1. Client Request

Clients request gmadaptor via HTTP requests. The following examples use synchronous requests, but you can change them to asynchronous requests as needed. Server authentication for clients is handled via headers; see the examples below (any one example is sufficient). Clients send data to the server using the POST method, and all gmadaptor methods only respond to POST requests. If the request completes successfully, the return code is 200.

In all operations, stock codes must appear in the format of short code + exchange suffix, where SSE is .XSHG and SZSE is .XSHE. During simulation testing, operations on stocks have specific responses (e.g., for a certain stock, buying will always result in partial fills regardless of the parameters provided; for another stock, it will always return "limit buy restricted," etc.). For specific response documentation, please contact the administrator in the East Money Quant Q Group.

In requests that change state (such as buy operations), you will often need the cid and timeout parameters. Their function is that the call waits for EMC to process the request and return results within the specified timeout period; however, EMC may not return results within the specified timeout period, such as when a buy order price is too low to ever fill, resulting in no return result. In such cases, the call returns after the timeout. Subsequent queries for buy results will then rely on the cid parameter. Note that cid is a required parameter, while timeout is optional.

The cid parameter (client entrust ID) is generated by the client. We recommend using the following code:

import uuid

cid = str(uuid.uuid4())

After generating the cid, please save it on the client side until the transaction ends and it is no longer needed.

For brevity, the following examples may omit this code:

import httpx
headers = {
    "Authorization": "84ae0899-7a8d-44ff-9983-4fa7cbbc424b",
    "Account-ID": "780dc4fda3d0af8a2d3ab0279bfa48c9"
}

_url_prefix = "http://192.168.100.100:9000/"

buy_entrust_no = None
sell_entrust_no = None

2.2. Return Results

Errors can occur at three levels. First, the HTTP layer (including bad request or Internal server error); second, the gmadaptor layer; third, errors returned by EMC.

For first-layer errors, we check via HTTP status codes. For example, if our client is httpx, we can check if response.status_code is 200.

gmadaptor always returns responses via json, which includes three fields:

status: int,如果为零,则表明在此层没有发生错误,即gmadaptor已经将请求正确上报
msg: str, human readable message
data: dict 如果一切顺利,则返回数据在此项中

Third-layer errors are provided by the emc trader. Even if gmadaptor correctly reports the request, the emc trader may fail to execute it, in which case it will also provide error information via status and reason.

The following example shows the output of response.json():

{
    "status": 0,
    "msg": "OK",
    "data": {
        "code": "000001.XSHE",
        "price": 0.0,
        "volume": 100,
        "order_side": 1,
        "bid_type": 2,
        "time": "2023-04-04 15:27:38.921555",
        "entrust_no": "0d23bb4e-d81e-4ef2-ab21-c58e0fe6814f",
        "status": -1,
        "average_price": 0.0,
        "filled": 0,
        "filled_amount": 0,
        "eid": "",
        "trade_fees": 0,
        "reason": "[Counter] [EMC_PC]不支持该下单类型",
        "recv_at": "2023-04-04 15:27:38.924565"
    }
}

Therefore, even if the status given by gmadaptor is successful, it does not necessarily mean the order was successfully placed. Another example is placing a buy order at an extremely low price; as long as the parameters are legal and accepted by EMC, gmadaptor will return success, but whether the order actually fills must be queried via entrust_no.

2.3. Asset Table

# 请求资金信息
import httpx
headers = {
    "Authorization": "84ae0899-7a8d-44ff-9983-4fa7cbbc424b",
    "Account-ID": "780dc4fda3d0af8a2d3ab0279bfa48c9"
}

_url_prefix = "http://192.168.100.100:9000/"

def get_balance():
    r = httpx.post(_url_prefix + "balance", headers=headers)
    resp = r.json()
    if r.status_code == 200 and resp['status'] == 0:
        print("\n------ 账户资金信息 ------")
        print(resp["data"])

2.4. Position Table

def get_positions():
    r = httpx.post(_url_prefix + "positions", headers=headers)
    
    resp = r.json()
    if r.status_code == 200 and resp['status'] == 0:
        print("\n----- 持仓信息 ------")
        print(resp["data"])

2.5. Limit Buy

    r = httpx.post(_url_prefix + "buy", headers=headers, json={
        "security": "000001.XSHE",
        "price": 13,
        "volume": 100,
        "cid": str(uuid.uuid4()),
        "timeout": 1
    })

    print(r.json())

2.6. Market Buy

def market_buy():
    global buy_entrust_no
    r = httpx.post(_url_prefix + "market_buy", headers=headers, json={
        "security": "000001.XSHE",
        "volume": 100,
        "cid": cid,
        "timeout": 1
    })

    resp = r.json()
    if r.status_code == 200 and resp["status"] == 0:
        print("\n ------ 委买成功 ------")
        print(resp["status"], resp["msg"], resp["data"])
        buy_entrust_no = resp["data"]["entrust_no"]
    else:
        print("委买失败:", r.status_code, resp)

2.7. Limit Sell

def sell():
    global sell_entrust_no

    r = httpx.post(_url_prefix + "sell", headers=headers, json={
        "security": "000001.XSHE",
        "price": 10,
        "volume": 100,
        "cid": cid,
        "timeout": 1
    })

    resp = r.json()
    if r.status_code == 200 and resp["status"] == 0:
        print("\n ------ 限价委卖成功 ------")
        data = resp["data"]
        print(data)
        sell_entrust_no = data["entrust_no"]
    else:
        print("卖出失败:", r.status_code, resp)

2.8. Market Sell

def market_sell():
    r = httpx.post(_url_prefix + "market_sell", headers=headers, json = {
        "security": "000001.XSHE",
        "volume": 100,
        "cid": cid
    })

    resp = r.json()
    if r.status_code == 200 and resp["status"] == 0:
        print("\n ------ 市价委卖成功 ------")
        print(resp["data"])
    else:
        print(resp)

2.9. Cancel Order

def cancel_entrust():
    global buy_entrust_no

    r = httpx.post(_url_prefix + "cancel_entrust", headers=headers, json = {
        "entrust_no": buy_entrust_no,
        "timeout": 1
    })

    resp = r.json()
    print(resp["status"], resp["msg"], resp["data"])

2.10. Query Today’s Orders

def today_entrusts():
    r = httpx.post(_url_prefix + "today_entrusts", headers=headers, json = {
        # 此处可以传入记录的委托号。传入空数组时,表明取当天所有委托。
        "entrust_no": [],
        "timeout": 1
    })

    resp = r.json()
    print(resp["status"], resp["msg"], resp["data"])

3. Troubleshooting and Help

For East Money File Orders, please refer to: https://emquant.18.cn/file-help/?doc=file_order East Money Quant Q Group: 971584613

Even with automatic daily restarts of EMC, occasional connection anomalies or other errors may occur. In such cases, manual execution may be required:

  1. Reconnect
  2. Clear file orders and restart

Matching Configuration Rules

In simulation trading tests, EMTrader specifies corresponding responses for each instrument. For example, for instrument 000572, buying will always result in full fills, while for instrument 000010, it will always be rejected. This is for testing convenience. East Money provides a document titled "Matching Configuration Rules," which may be updated at any time. Therefore, you need to add their technical staff on QQ to obtain it before testing.

Part of the content of this document in March 2023 is as follows:

[全部成交]
000572	full
000725	full

[分笔成交] 
分成两笔:
000002	lot      	2

[部分成交] 成交一半
000001	part
000004	part
...
[挂单]	只有响应,没有成交
018014	pending
020417	pending
...

[拒单]	
000010	reject
010609	reject

[拒绝撤单] 部分成交,不可撤单
000008	cancel_reject
000151	cancel_reject

Contact Information

If you need help using this module or wish to participate in the Big Fortune Quant Programming Practical Course, please add Kuanfen’s WeChat:

East Money Quant API: Installation, Config & Testing Guide