Oak dạy bạn sử dụng JS để giao diện với FMZ mở rộng API

Tác giả:Lydia., Tạo: 2022-12-20 13:27:24, Cập nhật: 2023-09-20 11:12:14

img

Oak dạy bạn sử dụng JS để giao diện với FMZ mở rộng API

Lời giới thiệu

Xin chào mọi người, tôi là Oak Quant.giám sát thị trường, phổ biến, và có một lời nhắc đồng bộ về số dịch vụ cùng tên, cung cấp cho người dùng mới và cũ một tham chiếu mới trong việc đánh giá xu hướng thị trường. Để tận dụng sức nóng này, chúng tôi đã bắt đầu dock API mở rộng của FMZ để đạt được giao tiếp tin nhắn giữa các robot, và đẩy cảnh báo thị trường đến các robot được chỉ định trực tiếp. Bài viết này lấy hai kịch bản ứng dụng làm ví dụ và hy vọng rằng bạn có thể phát triển những thứ thú vị hơn...

Bài viết này chủ yếu giới thiệu:

  1. Làm thế nào để các nhà phát triển giao diện với các API mở rộng FMZ thông qua ngôn ngữ JS. (Tác phẩm này lấy phương pháp GetNodeList làm ví dụ.)
  2. Trường hợp 1: Sử dụng phương pháp CommandRobot của API mở rộng để thực hiện giao tiếp tin nhắn giữa robot giám sát và các robot khác.
  3. Trường hợp 2: Sử dụng phương pháp GetRobotDetail của API mở rộng để nhận ra việc giám sát và hiển thị thống nhất của nhiều dữ liệu robot.

1. Sử dụng JS để giao diện với API mở rộng của FMZ

1) Đơn xin AccessKey và SecretKey (sau đây gọi là AK và SK). Chúng ta áp dụng trong menu của [Account] -> [ApiKey] -> [Create New ApiKey] trên trang web chính thức của FMZ, và sau đó lấy và ghi lại một nhóm AK và SK. (AK và SK trong FMZ có thể nhìn thấy, chúng ta có thể xem toàn bộ dữ liệu của AK và SK trong menu [API Interface] bất cứ lúc nào.)

img

2) Phát triển theo tài liệu API mở rộng Đầu tiên, hãy xem các bước chính của API yêu cầu.

  1. Giao diện FMZ API:
https://www.fmz.com/api/v1
  1. Các thông số cơ bản của yêu cầu
{
    'version'   : '1.0',                                //Custom version number
    'access_key': '8a148320e0bxxxxxxxxxxxxxx19js99f',   //AK
    'method'    : 'GetNodeList',                        //The specific method to call
    'args'      : [],                                   //List of parameters for the specific method algorithm
    'nonce'     : 1516292399361,                        //Timestamp, in milliseconds
    'sign'      : '085b63456c93hfb243a757366600f9c2'    //Signature (need to be encrypted according to the above 5 parameters to obtain, as discussed below)
}
  1. URL yêu cầu hoàn chỉnh được ghép lại dưới dạng dấu câu hỏi và thông qua tham số
Take the GetNodeList method as an example:
https://www.fmz.com/api/v1?
access_key=8a148320e0bxxxxxxxxxxxxxx19js99f&
nonce=1516292399361&
args=%5B%5D&
sign=085b63456c93hfb243a757366600f9c2&
version=1.0&
method=GetNodeList
  1. Phương pháp ký hiệu
After the parameters are spliced in the following order, the MD5 encryption algorithm is used to encrypt the string and convert it to a hexadecimal data string value, which is used as the value of the parameter sign.
version + "|" + method + "|" + args + "|" + nonce + "|" + secretKey
  1. Tóm lại, mã sau đây có sẵn: Địa chỉ mã nguồn:[Oak Quant] - JS dock FMZ mở rộng API Demo
var URL = "https://www.fmz.com/api/v1?";
var AK = "b3a53d3XXXXXXXXXXXXXXXXXXX866fe5";//Replace here with your own AccessKey
var SK = "1d9ddd7XXXXXXXXXXXXXXXXXXX85be17";//Replace here with your own SecretKey

function main() {
    //Get 5 basic parameter objects
    var param = getParam("1.0.0",AK,getArgs());
    Log("param:",param);
    //Get the result after md5 encryption of splicing parameters
    var md5Result = md5(param);
    //Assign the encryption result to the basic parameter object
    param.sign = md5Result;
    //Get the URL of the request api
    var finalUrl = getFinalUrl(param);
    Log("finalUrl:",finalUrl);
    //Execute the request and print the results
    var info = HttpQuery(finalUrl);
    Log("info:",info);
}

//Get 5 basic parameter objects
function getParam(version,ak,args){
    return {
        'version': version,
        'access_key': ak,
        'method': 'GetNodeList',
        'args': JSON.stringify(args),
        'nonce': new Date().getTime()
    }
}

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

//Get the final request URL
function getFinalUrl(param){
    return URL+"access_key="+AK+"&nonce="+param.nonce+"&args="+param.args+"&sign="+param.sign+"&version="+param.version+"&method="+param.method;
}

//The naming method of... args is not supported in js, so the arguments keyword is used instead to obtain the parameter array
function getArgs(){
    return [].slice.call(arguments);
}

Trường hợp 2: Sử dụng phương pháp CommandRobot của API mở rộng để đạt được giao tiếp tin nhắn giữa các robot

Dựa trên mã trên, chúng tôi sẽ sử dụng phương pháp CommandRobot để đạt được thông điệp giao tiếp giữa các robot.

Đầu tiên, hãy xem xét hai thông số được yêu cầu bởi phương pháp CommandRobot (RobotId, Cmd):

Tên tham số Loại Ý nghĩa
RobotId int Robot ID, có thể được lấy bằng GetRobotList (...) hoặc trên trang chi tiết robot
Cmd Dòng Tin nhắn được gửi đến robot

Bây giờ chúng ta biết ý nghĩa của tham số, hãy thực hiện phương pháp gọi.

  1. Nhận ID robot trên trang chi tiết robot

  2. Thực hiện phương pháp lấy thông báo Cmd

//Get message header information
function getMessageBody(toUserName,msgType,content){
    return ({
        "toUserName":toUserName,//Who to send to
        "fromUserName":AOKE_INFO,//Source
        "createTime": new Date().getTime(),//Current timestamp
        "msgType":msgType,//Message Type
        "content":content,//Message content
        "msgId":Math.random().toString(36).slice(-8)//Message ID
    })
}

//Get message body trend information (data of message header content field)
function getCtaDate(symbol,timeCycle,direction,nowCycleTime){
    return {
        "symbol":symbol,//Trading currency
        "timeCycle":timeCycle,//Trend cycle
        "direction":direction,//Current entry direction, 0: berish, 1: bullish
        "createTime":new Date().getTime(),//Current timestamp
        "nowCycleTime":nowCycleTime//Start time of current cycle
    }
}
  1. Thay đổi mã gửi tin nhắn
//Get messages before sending them
var sendMessage = getMessageBody("Test object",'CTARemind',getCtaDate('BTC_USDT','120','0','2020-05-1620:00:00'));

//Get the robot ID and message body through the getArgs() method, and pass in the basic parameters.
var param = getParam("1.0.0",AK,getArgs(17777,sendMessage));
  1. Chạy phương thức chính. Sau khi gửi tin nhắn, sử dụng phương thức GetCommand( để nhận tin nhắn
function main(){
    while(true) { 
        var cmd = GetCommand()
        if (cmd) { 
            Log(cmd)
        }
        Sleep(1000) 
    }
}

Tin nhắn được gửi thành công:

img

Tin nhắn được nhận thành công:

img

Trường hợp 3: Sử dụng các phương pháp GetRobotList và GetRobotDetail của API mở rộng để thực hiện giám sát và hiển thị dữ liệu của robot.

Tương tự như vậy, hãy nhìn vào mô tả tham số của hai phương thức tiếp theo GetRobotList ((offset, length, robotStatus, label):

Tên tham số Loại Ý nghĩa
bù đắp int Số trang truy vấn
chiều dài int Chiều dài dữ liệu của trang truy vấn
robotStatus int Quay vào - 1 để có được tất cả
nhãn Dòng Các thẻ tùy chỉnh có thể lọc tất cả các robot có thẻ này

GetRobotDetail ((RobotId):

Tên tham số Loại Ý nghĩa
RobotId int Robot ID
  1. Nhận danh sách Robot thông qua phương thức GetRobotList
//Get robot list information
var robotListJson = getAPIInfo('GetRobotList',getArgs(OFF_SET,PAGE_LENGTH,-1));
var robotList = robotListJson.data.result.robots;
  1. Nhận thông tin chi tiết về robot
//Get robot detail information
var robotDetailJson = getAPIInfo('GetRobotDetail',getArgs(robotId));
var robotDetail = robotDetailJson.data.result.robot;
  1. Dữ liệu bảng đầu ra của bảng điều khiển
function getLogPrient(infoArr){
    return table = {
            type: 'table',
            title: 'Oak Quant robot display',
            cols: [''Robot ID', 'Robot name', 'Strategy name', 'Next deduction time', 'Consumed time ms', 'Consumed amount CNY', 'Latest active time', 'Publish or not'],
            rows: infoArr
        };
}
  1. Tóm lại, mã sau đây có sẵn: Địa chỉ mã nguồn:[Oak Quant] - Sử dụng API mở rộng để có được thông tin về robot và hiển thị nó
var URL = "https://www.fmz.com/api/v1?";
var AK = "b3a53d3XXXXXXXXXXXXXXXXXXX866fe5";//Replace here with your own AccessKey
var SK = "1d9ddd7XXXXXXXXXXXXXXXXXXX85be17";//Replace here with your own SecretKey
var OFF_SET = 0;//Page number subscript of query
var PAGE_LENGTH = 5;//Data length of the query page

function main() {
    LogReset();
    while(true){
        //Get robot list information
        var robotListJson = getAPIInfo('GetRobotList',getArgs(OFF_SET,PAGE_LENGTH,-1));
        //Get the robot list information
        var robotList = robotListJson.data.result.robots;
        //Create an array to display robot information
        var infoArr = new Array();
        var infoArr_index = 0;
        for (index = 0; index < robotList.length; index++) {
            var robot = robotList[index];
            //Get the robot ID of the current loop
            var robotId = robot.id;
            //Get robot detail information
            var robotDetailJson = getAPIInfo('GetRobotDetail',getArgs(robotId));
            var robotDetail = robotDetailJson.data.result.robot;
            //Convert details to array objects
            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: 'Oak Quant robot display',
            cols: [''Robot ID', 'Robot name', 'Strategy name', 'Next deduction time', 'Consumed time ms', 'Consumed amount CNY', 'Latest active time', 'Publish or not'],
            rows: infoArr
        };
}

//Get API information by parameters
function getAPIInfo(method,dateInfo){
    //Get 5 basic parameter objects
    var param = getParam("1.0.0",AK,method,dateInfo);
    //Log("param:",param);
    //Get the result after md5 encryption of splicing parameters
    var md5Result = md5(param);
    //Assign the encryption result to the basic parameter object
    param.sign = md5Result;
    //Get the URL of the request api
    var finalUrl = getFinalUrl(param);
    //Log("finalUrl:",finalUrl);
    //Execute the request and print the results
    var info = HttpQuery(finalUrl);
    //Log("info:",info);
    return JSON.parse(info);
}

//Get the object of the basic 5 parameters
function getParam(version,ak,method,args){
    return {
        'version': version,
        'access_key': ak,
        'method': method,
        'args': JSON.stringify(args),
        'nonce': new Date().getTime()
    }
}

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

//Get the final request URL
function getFinalUrl(param){
    return URL+"access_key="+AK+"&nonce="+param.nonce+"&args="+param.args+"&sign="+param.sign+"&version="+param.version+"&method="+param.method;
}

//The naming method of... args is not supported in js, so the arguments keyword is used instead to obtain the parameter array
function getArgs(){
    return [].slice.call(arguments);
}

//Get the display details object: ['Robot ID', 'Robot Name', 'Strategy Name', 'Next Deduction Time', 'Time Consumed ms', 'Amount Consumed CNY', 'Latest Active Time', 'Whether to Publish'],
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?"Published":"Unpublished";
    return itemArr;
}

Kết luận

Trong phần mở rộng thực tế, có thể thực hiện nhiều chức năng thú vị hơn. Ví dụ, bằng cách sử dụng phương pháp CommandRobot, mỗi robot gửi phát hiện nhịp tim đến robot A. Nếu robot A phát hiện ra rằng một máy không có nhịp tim, nhưng robot vẫn đang chạy, nó có thể báo động thông qua dịch vụ FMZ. Bằng cách này, các báo động như vòng chết _C() dẫn đến kịch bản chết giả lập có thể được tránh. Tôi hy vọng rằng thông qua giới thiệu của tôi lần này, nền tảng FMZ có thể có nhiều chức năng thú vị hơn để phát triển và mở ra. Cuối cùng, tôi muốn cảm ơn nền tảng FMZ và tất cả những người vĩ đại như Xiaoxiaomeng, ông Zhang và ông Z vì sự hỗ trợ và giúp đỡ của họ.


Có liên quan

Thêm nữa