商品期货多图表画线类库应用演示

Author: 雨幕(youquant), Date: 2022-04-06 13:59:55
Tags:


var subscribeSymbols = []
function main() {
    LogReset(1)
    SetErrorFilter("Error: 90 CTP")
    _.each(symbols.split(","), function(symbol) {
        subscribeSymbols.push(symbol)
    })
    var tbl = {type : "table", title : "商品期货数据", cols : ["合约代码", "当前价格", "持仓", "盈亏", "开多", "开空", "平多", "平空"], rows: []}
    var q = $.NewTaskQueue()
    var p = $.NewPositionManager()
    while (true) {
        var msg = "未连接"       
        if (exchange.IO("status")) {
            msg = "已经连接"
            tbl = {type : "table", title : "商品期货数据", cols : ["合约代码", "当前价格", "持仓(多)", "持仓(空)", "盈亏", "开多", "开空", "平多", "平空"], rows: []}
            _.each(subscribeSymbols, function(symbol) {
                var tblRow = [symbol, "--", "--", "--", "--", "--", "--", "--", "--"]
                if ($.IsTrading(symbol)) {
                    var info = exchange.SetContractType(symbol)            
                    var r = exchange.GetRecords()                    
                    if (info && r && r.length > 0) {
                        $.PlotMultRecords(symbol, symbol + "`Kline", r, {layout: 'single', col: 3, height: '400px'})
                        $.PlotMultHLine(symbol, r[r.length - 1].Close, "currPrice", "red", "ShortDot")
                        var pos = [p.GetPosition(info.InstrumentID, PD_LONG), p.GetPosition(info.InstrumentID, PD_SHORT)]
                        var openLong = {"type": "button", "name": "开多仓1手", "cmd": "buy_" + info.InstrumentID, "description": symbol +"开多仓1手", "class": "btn btn-xs btn-danger"}
                        var coverLong = {"type": "button", "name": "平多仓1手", "cmd": "closebuy_" + info.InstrumentID, "description": symbol +"平多仓1手", "class": "btn btn-xs btn-warning"}
                        var openShort = {"type": "button", "name": "开空仓1手", "cmd": "sell_" + info.InstrumentID, "description": symbol +"开空仓1手", "class": "btn btn-xs btn-success"}
                        var coverShort = {"type": "button", "name": "平空仓1手", "cmd": "closesell_" + info.InstrumentID, "description": symbol +"平空仓1手", "class": "btn btn-xs btn-warning"}
                        tblRow = [symbol + "(" + info.InstrumentID + ")", r[r.length - 1].Close, (pos[0] ? pos[0].Amount : 0), (pos[1] ? pos[1].Amount : 0), (pos[0] ? pos[0].Profit : 0) + (pos[1] ? pos[1].Profit : 0), openLong, openShort, coverLong, coverShort]                        
                    }
                }
                tbl.rows.push(tblRow)                
            })
            q.poll()
        }        
        LogStatus(_D(), msg, "\n", "`" + JSON.stringify(tbl) + "`")        
        var cmd = GetCommand()
        if (cmd) {
            var arrCmd = cmd.split(":")
            if (arrCmd.length == 2) {
                if (arrCmd[0] == "addSymbol") {
                    var addIndex = -1
                    for (var i = 0 ; i < subscribeSymbols.length ; i++) {
                        if (subscribeSymbols[i] == arrCmd[1]) {
                            addIndex = i
                        }
                    }
                    if (addIndex == -1) {
                        subscribeSymbols.push(arrCmd[1])
                    }
                } else if (arrCmd[0] == "removeSymbol") {
                    var removeIndex = -1
                    for (var i = 0 ; i < subscribeSymbols.length ; i++) {
                        if (subscribeSymbols[i] == arrCmd[1]) {
                            removeIndex = i
                        }
                    }
                    if (removeIndex != -1) {
                        subscribeSymbols.splice(removeIndex, 1)
                        $.removeChart(arrCmd[1])
                    }                    
                }
            } else if (arrCmd.length == 1) {
                arrCmd = cmd.split("_")
                if (arrCmd.length == 2) {
                    var direction = arrCmd[0]
                    var cmdSymbol = arrCmd[1]
                    q.pushTask(exchange, cmdSymbol, direction, 1, function(task, ret) {
                        Log(task.desc, ret)
                    })
                }
            }
        }        
        Sleep(1000)
    }
}

更多内容