使用FMZ的扩展API实现高效群控管理在量化交易中的优势

Author: 小小梦, Created: 2023-11-19 20:57:18, Updated: 2023-12-09 23:21:49

img

随着量化交易的普及和发展,投资者通常需要管理大量的实盘账户,这给交易决策、监控和执行带来了巨大的挑战。为了提高管理效率和降低操作难度,在FMZ上交易者可以使用FMZ的扩展API进行群控管理。本文将探讨在量化交易中采用FMZ扩展API的优势以及如何实现高效的群控管理。

有不少用户都有自己的客户实盘需要管理维护,当客户实盘非常多时就需要一个更加方便的方式管理维护(少则十几个实盘,多则上百个实盘)。FMZ提供了强大的扩展API,通过FMZ的扩展API进行群控管理成为一种理想选择。

集中监控

通过FMZ的扩展API,您可以集中监控所有实盘账户的交易活动和资产状况。无论是检查各账户的持仓情况、历史交易记录,还是实时监测账户的盈亏状况,都可以实现。

// 全局变量
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) + "`")
}

策略参数设计:

img

交互设计:

img

实盘运行:

img

一键执行

群控管理使得一键执行交易变得非常简便。您可以同时对多个实盘进行买入、卖出、平仓等操作,而无需逐个打开不同的实盘去操作。这不仅提高了执行效率,还减少了操作失误的可能性。

拿到实盘列表信息后,我们可以向实盘发送指令,执行一系列的既定操作。例如:实盘清仓,实盘暂停保护,实盘模式切换。这些都可以通过FMZ的扩展APICommandRobot实现。

我们在继续编写代码,只需在主函数中增加一些交互和扩展API接口CommandRobot的调用:

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

群控策略便向「测试1 A」和「测试1 B」发送了指令。

img

img

策略同步

使用FMZ的扩展API,您可以轻松实现批量修改策略参数,批量启动、停止实盘。 鉴于篇幅内容,关于批量修改策略实盘参数、启动我们下篇文章中详细道来。

结语

在量化交易中,通过使用FMZ的扩展API进行群控管理,交易者可以更高效地监控、执行和调整多个实盘账户。这种集中化的管理方式不仅提高了操作效率,还有助于更好地实施风险控制和策略同步。 对于管理大量实盘账户的交易者而言,FMZ的扩展API为其提供了一个强大而灵活的工具,使得量化交易变得更加便捷和可控。


More