Loading ...

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

Author: 小小梦, Created: 2018-04-08 14:12:54, Updated: 2018-08-24 14:06:00

新增 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 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 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 ! ^^