Loading ...

商品期货跟单系统

Author: Hukybo, Date: 2018-03-09 14:20:12
Tags: Commodity-futures


var arr = [];

//下单函数
function trade(symbol, type, unit) {
    symbol = symbol.substring(0, symbol.length - 1);
    var acc = _C(exchanges[0].GetAccount).Info.Balance;
    for (var i = 1; i < exchanges.length; i++) {
        var ac = _C(exchanges[i].GetAccount).Info.Balance;
        var dws = unit / (acc / ac);
        var dw;
        if (type == "buy" || type == "sell") {
            dw = Math.floor(dws);
        } else {
            dw = Math.round(dws);
        }
        if (dw < 1) {
            Log(i, ' 号账户资金不足');
            continue;
        }
        var ntq = $.NewTaskQueue();
        ntq.pushTask(exchanges[i], symbol, type, unit, function(task, ret) {
            if (ret) {
                Log(i, ' 号账户 @ ', task.desc, "手, 成交价格:", ret.price);
            }
        });
        while (ntq.tasks.length > 0) {
            ntq.poll();
        }
    }
}

//匹配函数
function comparison(arr) {
    for (var y in arr[0]) {
        if (!arr[1][y]) {
            if (arr[0][y].Type == "多") {
                trade(y, "closebuy", arr[0][y].Amount);
            } else if (arr[0][y].Type == "空") {
                trade(y, "closesell", arr[0][y].Amount);
            }
        }
    }
    for (var x in arr[1]) {
        if (arr[0][x]) {
            if (arr[1][x].Amount - arr[0][x].Amount === 0) {
                continue;
            } else if (arr[1][x].Amount - arr[0][x].Amount > 0) {
                if (arr[1][x].Type == "多") {
                    trade(x, "buy", arr[1][x].Amount - arr[0][x].Amount);
                } else if (arr[1][x].Type == "空") {
                    trade(x, "sell", arr[1][x].Amount - arr[0][x].Amount);
                }
            } else if (arr[1][x].Amount - arr[0][x].Amount < 0) {
                if (arr[1][x].Type == "多") {
                    trade(x, "closebuy", arr[0][x].Amount - arr[1][x].Amount);
                } else if (arr[1][x].Type == "空") {
                    trade(x, "closesell", arr[0][x].Amount - arr[1][x].Amount);
                }
            }
        } else {
            if (arr[1][x].Type == "多") {
                trade(x, "buy", arr[1][x].Amount);
            } else if (arr[1][x].Type == "空") {
                trade(x, "sell", arr[1][x].Amount);
            }
        }
    }
}

//数据处理函数
function data() {
    var mp = _C(exchanges[0].GetPosition);
    var allDic = {};
    for (var i = 0; i < mp.length; i++) {
        var bs, name;
        if (mp[i].Type == 0 || mp[i].Type == 2) {
            bs = "多";
            name = mp[i].ContractType + bs;
        } else if (mp[i].Type == 1 || mp[i].Type == 3) {
            bs = "空";
            name = mp[i].ContractType + bs;
        }
        allDic[name] = {};
        allDic[name]["Amount"] = mp[i].Amount;
        allDic[name]["Type"] = bs;
    }
    arr.push(allDic);
    if (arr.length > 2) {
        arr.shift();
        comparison(arr);
    }
}

function main() {
    for (var i = 0; i < exchanges.length; i++) {
        if (exchanges[i].GetName() !== 'Futures_CTP') {
            throw '只支持 CTP !请检查第 ' + i + ' 个账户';
        }
    }
    SetErrorFilter("not login|ready|初始化");
    while (true) {
        if (exchanges[0].IO("status")) {
            data();
            LogStatus("与交易服务器连接成功");
        } else {
            LogStatus("\n警告! 与交易服务器连接断开", "#DF0101", "\n", "\n未登录状态或该交易所已经闭市。", "#DF0101");
        }
        Sleep(100);
    }
}

Related

More