The resource loading... loading...

OK hands-on teaches you how to use JS to pair FMZ extension APIs

Author: Orc quantified, Created: 2020-05-16 18:04:17, Updated: 2023-10-24 21:39:10

img

Other information

Hello, I'm Liu Ok quantum makeup. Due to the time I've been developing market trends, I'm reminding you of the following:Monitoring the mainframeIt is popular among people, and there is a simultaneous reminder of the same name service mark, so that the new and old cabbage has a new reference in the judgment of market trends. With this heat, we started to link the extension API of FMZ, to realize message communication between robots, and directly push the market reminder to the specified robot.

This article mainly covers: First, how developers can use the JS language to link FMZ's extension API. Example 1: Using the CommandRobot method with an extended API to monitor message communication between the mainframe robot and other robots. Case 2: Using the GetRobotDetail method of the extended API to achieve unified monitoring and display of multiple robot data.

First, the extension API for JS pairing FMZ

1)  Apply for AccessKey and SecretKey (hereinafter abbreviated as AK, SK)  We set up our FMZ web tab in the menu menu - > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > >img

2) Developed according to the extensions API documentation Let's first look at the key steps of requesting an API. The FMZ API interface is:

https://www.fmz.com/api/v1

2 Request the basic parameters

{
    'version'   : '1.0',                                //自定义版本号
    'access_key': '8a148320e0bxxxxxxxxxxxxxx19js99f',   //AK
    'method'    : 'GetNodeList',                        //具体调用的方法
    'args'      : [],                                   //具体method算法的参数列表
    'nonce'     : 1516292399361,                        //时间戳,单位毫秒
    'sign'      : '085b63456c93hfb243a757366600f9c2'    //签名(需要根据上面5个参数加密获取,下面有讲)
}

3, complete request URL spelled as a question mark

以GetNodeList方法为例
https://www.fmz.com/api/v1?
access_key=8a148320e0bxxxxxxxxxxxxxx19js99f&
nonce=1516292399361&
args=%5B%5D&
sign=085b63456c93hfb243a757366600f9c2&
version=1.0&
method=GetNodeList

4, how to sign

按照如下顺序进行参数拼接后,使用MD5加密算法加密字符串,并转换为十六进制数据字符串值,该值作为参数sign的值。
version + "|" + method + "|" + args + "|" + nonce + "|" + secretKey

5, in summary, has the following codes: The source code address is:Java-JS pairing FMZ extended API demo

var URL = "https://www.fmz.com/api/v1?";
var AK = "b3a53d3XXXXXXXXXXXXXXXXXXX866fe5";//这里替换成你自己的AccessKey
var SK = "1d9ddd7XXXXXXXXXXXXXXXXXXX85be17";//这里替换成你自己的SecretKey

function main() {
    //获取5个基础参数对象
    var param = getParam("1.0.0",AK,getArgs());
    Log("param:",param);
    //获取拼接参数md5加密后的结果
    var md5Result = md5(param);
    //赋值加密结果到基础参数对象中
    param.sign = md5Result;
    //获取请求api的URL
    var finalUrl = getFinalUrl(param);
    Log("finalUrl:",finalUrl);
    //执行请求并打印结果
    var info = HttpQuery(finalUrl);
    Log("info:",info);
}

//获取基础5个参数的对象
function getParam(version,ak,args){
    return {
        'version': version,
        'access_key': ak,
        'method': 'GetNodeList',
        'args': JSON.stringify(args),
        'nonce': new Date().getTime()
    }
}

//执行md5加密
function md5(param){
    var paramUrl = param.version+"|"+param.method+"|"+param.args+"|"+param.nonce+"|"+SK
    Log("paramUrl:",paramUrl);
    return Hash("md5", "hex", paramUrl)
}

//获取最终请求URL
function getFinalUrl(param){
    return URL+"access_key="+AK+"&nonce="+param.nonce+"&args="+param.args+"&sign="+param.sign+"&version="+param.version+"&method="+param.method;
}

//js中不支持...args的命名方式,所以改用arguments关键字获取参数数组
function getArgs(){
    return [].slice.call(arguments);
}

Case 2: Messaging between robots using the CommandRobot method with an extended API

Based on the above code, we use the CommandRobot method to implement messaging between robots.

First, let's look at the two parameters required by the CommandRobot (RobotId, Cmd) method.

Parameter name Types Meaning
RobotId int Robot ID, which can be obtained from GetRobotList[...] or on the bot details page
Cmd String Messages sent to robots

Now that we know what the parameters mean, we can implement this method next.

The first step is to access the robot ID on the bot details page:img

2 Implementation of how to access the cmd message

//获取消息头信息
function getMessageBody(toUserName,msgType,content){
    return ({
        "toUserName":toUserName,//发送给谁
        "fromUserName":AOKE_INFO,//消息来源
        "createTime": new Date().getTime(),//当前时间戳
        "msgType":msgType,//消息类型
        "content":content,//消息内容
        "msgId":Math.random().toString(36).slice(-8)//消息ID
    })
}

//获取消息体趋势信息(消息头content字段的数据)
function getCtaDate(symbol,timeCycle,direction,nowCycleTime){
    return {
        "symbol":symbol,//交易币种
        "timeCycle":timeCycle,//趋势周期
        "direction":direction,//当前进入的方向,0:看空,1:看多
        "createTime":new Date().getTime(),//当前时间戳
        "nowCycleTime":nowCycleTime//当前进入的周期起始时间
    }
}

3, modify the message code

//发送消息前先获取消息
var sendMessage = getMessageBody("测试对象",'CTARemind',getCtaDate('BTC_USDT','120','0','2020-05-1620:00:00'));

//把机器人ID和消息体通过getArgs()方法获取,并传入基础参数。
var param = getParam("1.0.0",AK,getArgs(17777,sendMessage));

4、 Execute the main method, after sending the message, use the GetCommand))) method to get the message

function main(){
    while(true) { 
        var cmd = GetCommand()
        if (cmd) { 
            Log(cmd)
        }
        Sleep(1000) 
    }
}

The message was successfully sent:imgThe message was received successfully:img

Case 3: The GetRobotList and GetRobotDetail methods of the extended API are used to monitor and display the data of the robot.

So let's see the parameters of the two methods. GetRobotList ((offset, length, robotStatus, label): GetRobotList is a list of all robots that have ever existed.

Parameter name Types Meaning
offset int Page code of the query
length int Data length of the query page
robotStatus int Transmit-1 is for all.
label String Custom tags, which can filter out all bots with this tag

GetRobotDetail(RobotId):

Parameter name Types Meaning
RobotId int The robot ID

1, Get a list of robots using the GetRobotList method

//获取机器人列表信息
var robotListJson = getAPIInfo('GetRobotList',getArgs(OFF_SET,PAGE_LENGTH,-1));
var robotList = robotListJson.data.result.robots;

2) Get more details about the robot

//获取机器人详情信息
var robotDetailJson = getAPIInfo('GetRobotDetail',getArgs(robotId));
var robotDetail = robotDetailJson.data.result.robot;

3, Console output form data

function getLogPrient(infoArr){
    return table = {
            type: 'table',
            title: '奥克量化的机器人展示',
            cols: ['机器人ID','机器人名称','策略名称','下次扣费时间','已经消耗时间ms','已经消耗金额CNY','最近活跃时间','是否公开'],
            rows: infoArr
        };
}

4, in summary, has the following codes: The source code address is:VOC quantification - using an extended API to capture information from the robot and display it

var URL = "https://www.fmz.com/api/v1?";
var AK = "b3a53d3XXXXXXXXXXXXXXXXXXX866fe5";//这里替换成你自己的AccessKey
var SK = "1d9ddd7XXXXXXXXXXXXXXXXXXX85be17";//这里替换成你自己的SecretKey
var OFF_SET = 0;//查询的页码下标
var PAGE_LENGTH = 5;//查询页的数据长度

function main() {
    LogReset();
    while(true){
        //获取机器人列表信息
        var robotListJson = getAPIInfo('GetRobotList',getArgs(OFF_SET,PAGE_LENGTH,-1));
        //取出机器人列表信息
        var robotList = robotListJson.data.result.robots;
        //创建展示机器人信息的数组
        var infoArr = new Array();
        var infoArr_index = 0;
        for (index = 0; index < robotList.length; index++) {
            var robot = robotList[index];
            //取出当前循环到的机器人ID
            var robotId = robot.id;
            //获取机器人详情信息
            var robotDetailJson = getAPIInfo('GetRobotDetail',getArgs(robotId));
            var robotDetail = robotDetailJson.data.result.robot;
            //转换详情为数组对象
            var arr = getLogPrientItem(robotDetail);
            infoArr[infoArr_index] = arr;
            infoArr_index++;
        }
        Log("infoArr:",infoArr);
        LogStatus('`' + JSON.stringify(getLogPrient(infoArr)) + '`');
        Sleep(30000);
    }
}

function getLogPrient(infoArr){
    return table = {
            type: 'table',
            title: '奥克量化的机器人展示',
            cols: ['机器人ID','机器人名称','策略名称','下次扣费时间','已经消耗时间ms','已经消耗金额CNY','最近活跃时间','是否公开'],
            rows: infoArr
        };
}

//通过参数获取API信息
function getAPIInfo(method,dateInfo){
    //获取5个基础参数对象
    var param = getParam("1.0.0",AK,method,dateInfo);
    //Log("param:",param);
    //获取拼接参数md5加密后的结果
    var md5Result = md5(param);
    //赋值加密结果到基础参数对象中
    param.sign = md5Result;
    //获取请求api的URL
    var finalUrl = getFinalUrl(param);
    //Log("finalUrl:",finalUrl);
    //执行请求并打印结果
    var info = HttpQuery(finalUrl);
    //Log("info:",info);
    return JSON.parse(info);
}

//获取基础5个参数的对象
function getParam(version,ak,method,args){
    return {
        'version': version,
        'access_key': ak,
        'method': method,
        'args': JSON.stringify(args),
        'nonce': new Date().getTime()
    }
}

//执行md5加密
function md5(param){
    var paramUrl = param.version+"|"+param.method+"|"+param.args+"|"+param.nonce+"|"+SK
    //Log("paramUrl:",paramUrl);
    return Hash("md5", "hex", paramUrl)
}

//获取最终请求URL
function getFinalUrl(param){
    return URL+"access_key="+AK+"&nonce="+param.nonce+"&args="+param.args+"&sign="+param.sign+"&version="+param.version+"&method="+param.method;
}

//js中不支持...args的命名方式,所以改用arguments关键字获取参数数组
function getArgs(){
    return [].slice.call(arguments);
}

//获取展示详情对象'机器人ID','机器人名称','策略名称','下次扣费时间','已经消耗时间ms','已经消耗金额CNY','最近活跃时间','是否公开'],
function getLogPrientItem(robotDetail){
    var itemArr = new Array();
    var iteArr_index = 0;
    itemArr[iteArr_index++] = robotDetail.id;
    itemArr[iteArr_index++] = robotDetail.name;
    itemArr[iteArr_index++] = robotDetail.strategy_name;
    itemArr[iteArr_index++] = robotDetail.charge_time;
    itemArr[iteArr_index++] = robotDetail.charged;
    itemArr[iteArr_index++] = robotDetail.consumed/1e8;
    itemArr[iteArr_index++] = robotDetail.refresh;
    itemArr[iteArr_index++] = robotDetail.public == 0?"已公开":"未公开";
    return itemArr;
}

The effect shows:

img

The ending

In the actual expansion, even more fun features can be achieved. For example, using the CommandRobot method to have each robot send a heart rate test to robot A, if robot A detects that a machine has not had a heartbeat, but the robot is still running, it can be alerted by the FMZ service number. This avoids alarms such as _C (dead loop) that lead to a program fake death scene. Hopefully, with my introduction, FMZ platform can have more fun features that are developed and open-source. Finally, thank you for the support and help of the FMZ platform and all the great gods like Dream Team, Super Team, Z Team.


Related

More

Inventor number one shoucang

tnmmhmCollect and learn