Sumber dimuat naik... memuat...

Pertukaran niaga hadapan Deribit yang baru disokong dan contoh penggunaan

Penulis:Pencipta Kuantiti - Impian Kecil, Dicipta: 2018-04-08 14:12:54, Dikemas kini: 2020-01-16 15:45:29

Pertukaran niaga hadapan Deribit yang baru disokong dan contoh penggunaan

https://www.deribit.comAlamat dokumen API:https://www.deribit.com/pages/docs/api

# 注意
# 1、调用 GetAccount 时不需要设置 合约代码。
# 2、获取行情 等信息 需要设置 合约代码。
# 3、注意使用 SetDirection 设置方向。
# 4、平仓时需注意 是否有仓位 可以平仓,如果 仓位不足以平仓,也是 可以平掉的,会持有 负数仓位,重新开仓会抵消这个 负数仓位。(实测)
# 5、永续合约 名称:BTC-PERPETUAL

Antara muka bursa

img

Kode Ujian

// 测试合约代码: BTC-27APR18
function E(obj) {
    if (!obj) {
        return obj
    }

    var cloneObj = function(obj) { // 深拷贝 对象函数
        var str, newobj = obj.constructor === Array ? [] : {};
        if (typeof obj !== 'object') {
            return;
        } else if (JSON) {
            str = JSON.stringify(obj); //系列化对象
            newobj = JSON.parse(str); //还原
        } else {
            for (var i in obj) {
                newobj[i] = typeof obj[i] === 'object' ?
                    cloneObj(obj[i]) : obj[i];
            }
        }
        return newobj;
    }

    if (obj && typeof(obj) == "object" && typeof(obj.Info) !== "undefined") {
        var newObj = cloneObj(obj)
        delete newObj.Info
        return newObj
    } else if (typeof(obj.length) == "number") {
        var newArray = []
        for (var n = 0; n < obj.length; n++) {
            newArray.push(E(obj[n]))
        }
        return newArray
    } else {
        // Log(obj, "is not object or not have attribute 'Info'")
        return obj
    }
}

function main() {
    exchange.SetContractType("BTC-27APR18")
    Log(exchange.GetAccount())
    Sleep(1000)
    
    var ticker = exchange.GetTicker()
    
    Log("ticker:", E(exchange.GetTicker()) )
    exchange.SetDirection("buy")
    var id1 = exchange.Buy(ticker.Sell, 2)
    
    Log("id1:", id1)
    
    // /*
    Log(exchange.GetOrder(id1))
    
    Log(E(exchange.GetAccount()))
    
    Sleep(2000)
    
    var pos = exchange.GetPosition()
    Log("pos:", pos)
    
    ticker = exchange.GetTicker()
    exchange.SetDirection("closebuy")
    var id2 = exchange.Sell(ticker.Buy, 1)
    
    Log("id2:", id2)
    
    Sleep(2000)
    pos = exchange.GetPosition()
    Log("pos:", pos)
    
    var order
    Log(order = exchange.GetOrders())
    
    Log(E(exchange.GetAccount()))
    
    while(order.length != 0){
        for(var i = 0; i < order.length; i++){
            exchange.CancelOrder(order[i].Id, order[i].Id)
            Sleep(2000)
        }
        order = exchange.GetOrders()
    }
    
    
    ticker = exchange.GetTicker()
    exchange.SetDirection("closebuy")
    id2 = exchange.Sell(ticker.Buy, 2)
    // */
    pos = exchange.GetPosition()
    Log("pos:", pos)
    Log(order = exchange.GetOrders())
}

Logimg

Jika anda tidak mempunyai akaun, anda tidak akan mempunyai akaun yang sama.

Mengubah kod, membuka pesanan kontrak, menyimpan kontrak.

function E(obj) {
    if (!obj) {
        return obj
    }

    var cloneObj = function(obj) { // 深拷贝 对象函数
        var str, newobj = obj.constructor === Array ? [] : {};
        if (typeof obj !== 'object') {
            return;
        } else if (JSON) {
            str = JSON.stringify(obj); //系列化对象
            newobj = JSON.parse(str); //还原
        } else {
            for (var i in obj) {
                newobj[i] = typeof obj[i] === 'object' ?
                    cloneObj(obj[i]) : obj[i];
            }
        }
        return newobj;
    }

    if (obj && typeof(obj) == "object" && typeof(obj.Info) !== "undefined") {
        var newObj = cloneObj(obj)
        delete newObj.Info
        return newObj
    } else if (typeof(obj.length) == "number") {
        var newArray = []
        for (var n = 0; n < obj.length; n++) {
            newArray.push(E(obj[n]))
        }
        return newArray
    } else {
        // Log(obj, "is not object or not have attribute 'Info'")
        return obj
    }
}

function main() {
    exchange.SetContractType("BTC-27APR18")
    Log(exchange.GetAccount())
    Sleep(1000)
    
    var ticker = exchange.GetTicker()
    
    Log("ticker:", E(exchange.GetTicker()) )
    exchange.SetDirection("buy")
    var id1 = exchange.Buy(ticker.Sell, 1)
    
    Log("id1:", id1)
    
    pos = exchange.GetPosition()
    Log("pos:", pos)
    Log(order = exchange.GetOrders())
}

Hasilnya, kontrak pegangan yang negatif sebelum ini telah dipadamkan.

img

Log dagangan bursaimg


Lebih lanjut