Loading ...

新增 Deribit 期货交易所支持 及 使用范例

Author: 小小梦, Created: 2018-04-08 14:12:54, Updated: 2020-01-16 15:45:29

新增 Deribit 期货交易所支持 及 使用范例

https://www.deribit.com API 文档地址: https://www.deribit.com/pages/docs/api

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

交易所界面

img

测试代码

// 测试合约代码: 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())
}

日志 img

多平了一张,所以导致持有负值仓位。

修改代码,再开一张合约订单,持仓一张合约

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())
}

结果之前的 负值的 合约持仓 被抵消了。

img

交易所交易日志 img


More

Alchemist Thank you for this!

小小梦 Thank you for your support ! ^^