Loading ...

内外盘期货品种实时差价图

Author: Zero, Date: 2018-03-26 10:08:24
Tags: Commodity-futures Chart


function main() {
    LogReset();
    LogProfitReset();
    SetErrorFilter('初始化');
    var symbolA = '';
    var symbolB = '';
    var eLocal = null;
    var eForeign = null;
    _.each(exchanges, function(e) {
        var isForeign = e.GetName() == "Futures_Esunny";
        var ct = isForeign ? FSymbol : LSymbol;
        if (isForeign) {
            eForeign = e;
        } else {
            eLocal = e;
        }
        Log(e.GetName(), "正在连接", (isForeign ? "外盘" : "内盘"), "交易服务器....");
        while (!e.IO("status")) Sleep(100);
        Log("正在订阅品种", ct);
        var symbol = _C(e.SetContractType, ct);
        var volumeMultiple = isForeign ? symbol.ProductDot : symbol.VolumeMultiple;
        var symbolName = isForeign ? (symbol.CommodityName + symbol.UpperNo) : symbol.InstrumentName;
        Log("订阅成功", "每手乘数:", volumeMultiple, "商品名:", symbolName, symbol);
        e.IO("mode", 0);
    });
    if (eLocal == null || eForeign == null) {
        throw "必须添加一个内盘期货跟一个外盘期货";
    }
    var cfgA = {
        title: {
            text: LSymbol + ' & ' + FSymbol + ' 最新价',
        },
        subtitle: {
            text: '每' + Interval + '秒更新一次',
        },
        xAxis: {
            type: 'datetime'
        },
        series: [{
            name: LSymbol,
            data: [],
        }, {
            name: FSymbol,
            data: [],
        }]
    }
    var cfgB = {
        title: {
            text: '差价图'
        },
        xAxis: {
            type: 'datetime'
        },
        series: [{
            name: LSymbol + ' - ' + FSymbol + ' 差价',
            type: 'line',
            data: [],
        }]
    }

    var cfgC = {
        __isStock: false,
        title: {
            text: 'USDCNY yahoo 汇率'
        },
        xAxis: {
            type: 'datetime'
        },
        series: [{
            name: 'USDCNY',
            type: 'line',
            data: [],
        }]
    };
    var chart = Chart([cfgA, cfgB, cfgC]);
    chart.reset();
    while (true) {
        if ($.IsTrading(LSymbol)) {
            var tickerA = eLocal.GetTicker();
            var tickerB = eForeign.GetTicker();
            if (tickerA && tickerB) {
                var ts = Unix() * 1000;
                chart.add(0, [ts, _N(tickerA.Last, 2)]);
                chart.add(1, [ts, _N(tickerB.Last * USDCNY, 2)]);
                chart.add(2, [ts, _N(tickerA.Last - tickerB.Last * USDCNY, 2)]);
                chart.add(3, [ts, USDCNY]);
            }
        } else {
            LogStatus("不在交易时间内....");
        }
        LogStatus(_D(), "Running...");
        Sleep(Interval * 1000);
    }
}

Related

More