FMEX简单交易挖矿机器人

exchange.IO("base", Url) //切换基地址,方便切换实盘和模拟盘,实盘地址:https://api.fmex.com

var ordersInfo = {buyId:0, buyPrice:0, buyAmount:0, sellId:0, sellPrice:0, sellAmount:0, pos:0}
var depthInfo = {asks:[], bids:[]}
var lastProfitTime = 0 //控制打印收益时间
var lastRestTime = Date.now()   //定时重置策略
var lastCoverTime = 0  //检查仓位

function showTable(){
    var table = {type: 'table', title: '信息', cols: ['买一价', '卖一价', '买挂单量', '卖挂单量', '当前持仓量'],
                 rows: [[ordersInfo.buyPrice, ordersInfo.sellPrice, ordersInfo.buyAmount, ordersInfo.sellAmount, ordersInfo.pos]]}
    LogStatus('`' + JSON.stringify(table) + '`\n'+JSON.stringify(ordersInfo))
}

function reset(){ //重置策略,防止一些订单卡住,可能会影响其它正在运行的策略
    var orders = exchange.GetOrders()
    if(orders){
        for(var i=0;i0){
        if(pos[0].Type == 0){ //多仓
            ordersInfo.pos = pos[0].Amount
        }else{
            ordersInfo.pos = -pos[0].Amount
        }
    }
}

function main() {
    exchange.SetContractType('swap')
    exchange.SetMarginLevel(0) //全仓模式
    reset()
    while(true){
        var ticker = _C(exchange.GetTicker)
        var sellPrice = ticker.Sell
        var buyPrice = ticker.Buy
        if(Date.now()-lastCoverTime > CoverTime*1000){
            lastCoverTime = Date.now()
            getPostiton()
        }
        if(ordersInfo.pos <= HoldAmount){
            if(buyPrice != ordersInfo.buyPrice){
                var cancelId = ordersInfo.buyId
                exchange.SetDirection('buy')
                var buyId = exchange.Buy(buyPrice, Amount)
                ordersInfo.buyPrice = buyPrice
                ordersInfo.buyAmount = Amount
                if(buyId){ordersInfo.buyId = buyId}else{ordersInfo.buyId = 0}
                if(cancelId){exchange.CancelOrder(cancelId)} //先下单后撤单,保证始终有挂单
            }
        }else{
            if(ordersInfo.buyId){
                exchange.CancelOrder(ordersInfo.buyId)
            }
            ordersInfo.buyId = 0
            ordersInfo.buyPrice = 0
            ordersInfo.buyAmount = 0
        }
        if(ordersInfo.pos >= -HoldAmount){
            if(sellPrice != ordersInfo.sellPrice){
                var cancelId = ordersInfo.sellId
                exchange.SetDirection('sell')
                var sellId = exchange.Sell(sellPrice, Amount)
                ordersInfo.sellPrice = sellPrice
                ordersInfo.sellAmount = Amount
                if(sellId){ordersInfo.sellId = sellId}else{ordersInfo.sellId = 0}
                if(cancelId){exchange.CancelOrder(cancelId)}
            }
        }else{
            if(ordersInfo.sellId){
                exchange.CancelOrder(ordersInfo.sellId)
            }
            ordersInfo.sellId = 0
            ordersInfo.sellPrice = 0
            ordersInfo.sellAmount = 0
        }
        if(Date.now()-lastProfitTime > ProfitTime*1000){
            lastProfitTime = Date.now()
            var account = exchange.GetAccount()
            if(account){
                LogProfit(_N(account.Info.data.BTC[0]+account.Info.data.BTC[1]+account.Info.data.BTC[2],6))
            }
        }

        if(Date.now()-lastRestTime > 10*60*1000){
            lastRestTime = Date.now()
            reset()
        }
        showTable()
        Sleep(Intervel*1000)
    }
}

转载于https://www.fmz.com/strategy/171560

你可能感兴趣的:(FMEX简单交易挖矿机器人)