Halo semuanya, aku
Artikel ini terutama memperkenalkan:
1) Memohon AccessKey dan SecretKey (selanjutnya disebut sebagai AK dan SK). Kami mendaftar di menu [Akun] -> [ApiKey] -> [Menciptakan ApiKey Baru] di situs resmi FMZ, dan kemudian mendapatkan dan merekam sekelompok AK dan SK. (AK dan SK di FMZ terlihat, kita dapat melihat data penuh AK dan SK di menu [API Interface] kapan saja.)
2) Mengembangkan sesuai dengan dokumen API diperluas Pertama, mari kita lihat langkah-langkah kunci dari API permintaan.
https://www.fmz.com/api/v1
{
'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)
}
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
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
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);
}
Berdasarkan kode di atas, kita akan menggunakan metode CommandRobot untuk mencapai komunikasi pesan antara robot.
Pertama, mari kita lihat dua parameter yang dibutuhkan oleh metode CommandRobot (RobotId, Cmd):
Nama Parameter | Jenis | Makna |
---|---|---|
RobotId | int | Robot ID, yang dapat diperoleh dengan GetRobotList (...) atau di halaman detail robot |
Cmd | String | Pesan dikirim ke robot |
Sekarang kita tahu arti parameter, mari kita menerapkan metode panggilan.
Dapatkan ID robot di halaman rincian robot
Menerapkan metode untuk mendapatkan pesan 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
}
}
//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));
function main(){
while(true) {
var cmd = GetCommand()
if (cmd) {
Log(cmd)
}
Sleep(1000)
}
}
Pesan dikirim dengan sukses:
Pesan diterima dengan sukses:
Demikian pula, mari kita lihat dulu deskripsi parameter dari dua metode berikutnya GetRobotList ((offset, panjang, robotStatus, label):
Nama Parameter | Jenis | Makna |
---|---|---|
penghapusan | int | Nomor halaman kueri |
panjang | int | Panjang data halaman kueri |
robotStatus | int | Pass in - 1 untuk mendapatkan semua |
label | String | Tag khusus dapat menyaring semua robot dengan tag ini |
GetRobotDetail ((RobotId):
Nama Parameter | Jenis | Makna |
---|---|---|
RobotId | int | Robot ID |
//Get robot list information
var robotListJson = getAPIInfo('GetRobotList',getArgs(OFF_SET,PAGE_LENGTH,-1));
var robotList = robotListJson.data.result.robots;
//Get robot detail information
var robotDetailJson = getAPIInfo('GetRobotDetail',getArgs(robotId));
var robotDetail = robotDetailJson.data.result.robot;
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
};
}
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;
}
Dalam ekstensi yang sebenarnya, semakin banyak fungsi yang menarik dapat direalisasikan. Misalnya, menggunakan metode CommandRobot, setiap robot mengirim deteksi detak jantung ke robot A. Jika robot A menemukan bahwa mesin tidak memiliki detak jantung, tetapi robot masih berjalan, ia dapat memberikan alarm melalui layanan FMZ. Dengan cara ini, alarm seperti _C() dead loop yang mengarah ke skenario kematian palsu dapat dihindari. Saya berharap bahwa melalui pengantar saya kali ini, platform FMZ dapat memiliki lebih banyak fungsi yang menarik untuk dikembangkan dan dibuka. Akhirnya, saya ingin berterima kasih kepada platform FMZ dan semua orang hebat seperti Xiaoxiaomeng, Mr. Zhang dan Mr. Z atas dukungan dan bantuan mereka.