The resource loading... loading...

Efficient cluster management using FMZ's extended API is an advantage in quantitative trading

Author: Inventors quantify - small dreams, Created: 2023-11-19 20:57:18, Updated: 2023-12-09 23:21:49

img

With the popularity and development of quantitative trading, investors often need to manage a large number of live accounts, which poses huge challenges to trading decision-making, monitoring and execution. In order to improve management efficiency and reduce operational difficulty, traders on FMZ can use FMZ's extended API for crowd control management. This article will explore the advantages of using FMZ extended API in quantitative trading and how to achieve efficient crowd control management.

Many users have their own client disks that need to be managed and maintained, and when there are a lot of client disks, they need a more convenient way to manage maintenance (less often dozens of disks, more often hundreds of disks). FMZ provides a powerful extension API, making cluster management through FMZ's extension API an ideal option.

Central surveillance

With FMZ's extended API, you can centrally monitor the trading activity and asset status of all live accounts. It can be done by checking the holdings of each account, historical trading records, or real-time monitoring of the profit and loss status of the account.

// 全局变量
var isLogMsg = true   // 控制日志是否打印
var isDebug = false   // 调试模式
var arrIndexDesc = ["all", "running", "stop"]
var descRobotStatusCode = ["空闲中", "运行中", "停止中", "已退出", "被停止", "策略有错误"]
var dicRobotStatusCode = {
    "all" : -1,
    "running" : 1,
    "stop" : 4,
}

// 扩展的日志函数
function LogControl(...args) {
    if (isLogMsg) {
        Log(...args)
    }
}

// FMZ扩展API调用函数
function callFmzExtAPI(accessKey, secretKey, funcName, ...args) {
    var params = {
        "version" : "1.0",
        "access_key" : accessKey,
        "method" : funcName,
        "args" : JSON.stringify(args),
        "nonce" : Math.floor(new Date().getTime())
    }

    var data = `${params["version"]}|${params["method"]}|${params["args"]}|${params["nonce"]}|${secretKey}`
    params["sign"] = Encode("md5", "string", "hex", data)
    
    var arrPairs = []
    for (var k in params) {
        var pair = `${k}=${params[k]}`
        arrPairs.push(pair)
    }
    var query = arrPairs.join("&")
    
    var ret = null
    try {
        LogControl("url:", baseAPI + "/api/v1?" + query)
        ret = JSON.parse(HttpQuery(baseAPI + "/api/v1?" + query))
        if (isDebug) {
            LogControl("Debug:", ret)
        }
    } catch(e) {
        LogControl("e.name:", e.name, "e.stack:", e.stack, "e.message:", e.message)
    }
    Sleep(100)  // 控制频率
    return ret 
}

// 获取指定策略Id的所有运行中的实盘信息
function getAllRobotByIdAndStatus(accessKey, secretKey, strategyId, robotStatusCode, maxRetry) {
    var retryCounter = 0
    var length = 100
    var offset = 0
    var arr = []

    if (typeof(maxRetry) == "undefined") {
        maxRetry = 10
    }

    while (true) {
        if (retryCounter > maxRetry) {
            LogControl("超过最大重试次数", maxRetry)
            return null
        }
        var ret = callFmzExtAPI(accessKey, secretKey, "GetRobotList", offset, length, robotStatusCode)
        if (!ret || ret["code"] != 0) {
            Sleep(1000)
            retryCounter++
            continue
        }

        var robots = ret["data"]["result"]["robots"]
        for (var i in robots) {
            if (robots[i].strategy_id != strategyId) {
                continue
            }
            arr.push(robots[i])
        }

        if (robots.length < length) {
            break
        }
        offset += length
    }

    return arr 
}

function main() {
    var robotStatusCode = dicRobotStatusCode[arrIndexDesc[robotStatus]]
    var robotList = getAllRobotByIdAndStatus(accessKey, secretKey, strategyId, robotStatusCode)
    if (!robotList) {
        Log("获取实盘数据失败")
    }
    
    var robotTbl = {"type": "table", "title": "实盘列表", "cols": [], "rows": []}
    robotTbl.cols = ["实盘Id", "实盘名称", "实盘状态", "策略名称", "实盘收益"]

    _.each(robotList, function(robotInfo) {
        robotTbl.rows.push([robotInfo.id, robotInfo.name, descRobotStatusCode[robotInfo.status], robotInfo.strategy_name, robotInfo.profit])
    })

    LogStatus(_D(), "`" + JSON.stringify(robotTbl) + "`")
}

Design of policy parameters:

img

Interactive design:

img

This is a real-time drive:

img

One key execution

Cluster management makes it very easy to execute a transaction with one key. You can simultaneously buy, sell, and even out on multiple disks, without having to open different disks individually. This not only improves execution efficiency, but also reduces the possibility of operating errors.

Once we have the disk list information, we can send instructions to the disk to perform a set of established operations. For example: disk cleanup, disk pause protection, disk mode switching. All of these can be done through FMZ's extended API.CommandRobotI'm not going to lie.

We're still writing the code, just adding some interaction and extended API interfaces in the main function.CommandRobotThe call:

function main() {
    var robotStatusCode = dicRobotStatusCode[arrIndexDesc[robotStatus]]
    var robotList = getAllRobotByIdAndStatus(accessKey, secretKey, strategyId, robotStatusCode)
    if (!robotList) {
        Log("获取实盘数据失败")
    }
    
    var robotTbl = {"type": "table", "title": "实盘列表", "cols": [], "rows": []}
    robotTbl.cols = ["实盘Id", "实盘名称", "实盘状态", "策略名称", "实盘收益"]

    _.each(robotList, function(robotInfo) {
        robotTbl.rows.push([robotInfo.id, robotInfo.name, descRobotStatusCode[robotInfo.status], robotInfo.strategy_name, robotInfo.profit])
    })

    LogStatus(_D(), "`" + JSON.stringify(robotTbl) + "`")

    while(true) {
        LogStatus(_D(), ", 等待接收交互命令", "\n", "`" + JSON.stringify(robotTbl) + "`")

        var cmd = GetCommand()
        if (cmd) {
            var arrCmd = cmd.split(":")
            if (arrCmd.length == 1 && cmd == "coverAll") {
                _.each(robotList, function(robotInfo) {
                    var strCmd = "清仓"               // 可以定义所需的消息格式
                    if (robotInfo.status != 1) {     // 只有”活着“的实盘才能接收命令
                        return 
                    }
                    var ret = callFmzExtAPI(accessKey, secretKey, "CommandRobot", parseInt(robotInfo.id), strCmd)
                    LogControl("向id:", robotInfo.id, "的实盘发送命令:", strCmd, ", 执行结果:", ret)
                })
            }
        }
        Sleep(1000)
    }
}

img

The cluster control policy sends instructions to "Test 1A" and "Test 1B".

img

img

Strategic synchronization

Using FMZ's extended API, you can easily implement batch modification policy parameters, batch start and stop of the disk. Given the content of the article, about the parameters of the batch modification strategy, we will start our next article in detail.

The ending

In quantitative trading, crowd control management using FMZ's extended API enables traders to more efficiently monitor, execute and adjust multiple live accounts. This centralized management approach not only improves operational efficiency but also helps to better implement risk control and strategy synchronization. For traders who manage large numbers of live accounts, FMZ's extended API provides a powerful and flexible tool that makes quantifying transactions easier and more manageable.


More