- Square
- 交易对精度标定
交易对精度标定
Author:
斯巴达玩量化, Date: 2022-07-05 17:00:21
Tags:
/*
-- 策略引用该模板以后直接用 $.Test() 调用此方法
-- main 函数在策略中不会触发, 只做为模板调试的入口
-- GetExPrecision 函数的精度标定只支持到个位, 暂不支持交易对精度为 十位/百位/千位... 的情况
-- 【警告】若盘口深度较低,不足以精确表达真实精度,则函数有可能失去准确性
*/
let gCache = {};
$.GetPrecision = function(depth) {
let maxLenAmt = 0;
let maxLenPrice = 0;
depth.Asks.forEach(function(ask) {
let price = ask["Price"].toString();
if (price.toString().indexOf('.') > -1) {
let priceP = price.split(".")[1].length;
if (priceP > maxLenPrice) {
maxLenPrice = priceP;
}
}
let amt = ask["Amount"].toString();
if (amt.indexOf('.') > -1) {
let amtP = amt.split(".")[1].length;
if (amtP > maxLenAmt) {
maxLenAmt = amtP;
}
}
})
return [maxLenPrice, maxLenAmt];
}
// 返回数组 [价格的精度大小, 数量的精度大小]
$.GetExPrecision = function(ex, force) {
if (IsVirtual()) {
return null;
}
let key = ex.GetName() + '_ex_precision_' + ex.GetCurrency();
let cache = gCache[key];
if (!force && cache) {
return cache;
}
let r = $.GetPrecision(_C(ex.GetDepth));
gCache[key] = r;
Log("缓存精度信息至本地", key, r);
return r;
}
function main() {
$.GetExPrecision(exchange, false);
}
More