exchange.IO("base", Url) //切换基地址,方便切换实盘和模拟盘,实盘地址:https://api.fmex.com
var ordersInfo = {buyId:0, buyPrice:0, sellId:0, sellPrice:0}
var depthInfo = {asks:[], bids:[]}
var lastProfitTime = 0 //控制打印收益时间
var lastRestTime = Date.now() //定时重置策略
var lastCoverTime = Date.now() //检查仓位并平仓
function calcDepth(depth){ //计算最佳挂单位置
depthInfo = {asks:[], bids:[]}
var max_asks = 0
var max_bids = 0
var ask_price = depth.Asks[0].Price
var bid_price = depth.Bids[0].Price
for(var i=0;i<11;i++){
var fact = i==0 ? 1/4 : 1/40 //官方的挖矿系数,可按需设置,如离盘口越远越大,减少成交风险
var my_ask_amount = depth.Asks[i].Price == ordersInfo.sellPrice ? Amount : 0 //排除掉自己订单的干扰
var my_bid_amount = depth.Bids[i].Price == ordersInfo.buyPrice ? Amount : 0
while(ask_price <= depth.Asks[i].Price){ //考虑到未被占用的深度位置
var ask_amount = ask_price == depth.Asks[i].Price ? depth.Asks[i].Amount : 0
var ratio = _N(10000*Amount*fact/Math.max(ask_amount+Amount-my_ask_amount,Amount),5) //避免因深度更新延时导致除0
depthInfo.asks.push(['sell_'+(i+1), ask_price, ask_amount, ratio])
if(ratio >= depthInfo.asks[max_asks][3]){max_asks = depthInfo.asks.length-1} //大于等于保证相同挖矿效率下远离盘口的优先
ask_price += 0.5
}
while(bid_price >= depth.Bids[i].Price){
var bid_amount = bid_price == depth.Bids[i].Price ? depth.Bids[i].Amount : 0
var ratio = _N(10000*Amount*fact/Math.max(bid_amount+Amount-my_bid_amount,Amount),5)
depthInfo.bids.push(['buy_'+(i+1), bid_price, bid_amount, ratio])
if(ratio >= depthInfo.bids[max_bids][3]){max_bids = depthInfo.bids.length-1}
bid_price -= 0.5
}
}
return [depthInfo.asks[max_asks][1], depthInfo.bids[max_bids][1]]
}
function showTable(){
var table = {type: 'table', title: '挂单信息', cols: ['位置', '价格', '数量', '额度占比(万分之一)'], rows: []}
for(var i=0;i0){
if(pos[0].Type == 0){ //平多仓,采用盘口吃单,会损失手续费,可改为盘口挂单,会增加持仓风险。
exchange.SetDirection('sell')
var sellId = exchange.Sell(depth.Bids[0].Price, Math.min(CoverAmount, pos[0].Amount), '平多仓')
if(sellId){exchange.CancelOrder(sellId)} //平仓单会立即撤销
}else{
exchange.SetDirection('buy')
var buyId = exchange.Buy(depth.Asks[0].Price, Math.min(CoverAmount, pos[0].Amount), '平空仓')
if(buyId){exchange.CancelOrder(buyId)}
}
}
}
function main() {
exchange.SetContractType('swap')
exchange.SetMarginLevel(0) //全仓模式
reset()
while(true){
var depth = _C(exchange.GetDepth)
var price = calcDepth(depth)
var sellPrice = price[0]
var buyPrice = price[1]
if(buyPrice != ordersInfo.buyPrice){
var cancelId = ordersInfo.buyId
exchange.SetDirection('buy')
var buyId = exchange.Buy(buyPrice, Amount)
ordersInfo.buyPrice = buyPrice
if(buyId){ordersInfo.buyId = buyId}else{ordersInfo.buyId = 0}
if(cancelId){exchange.CancelOrder(cancelId)} //先下单后撤单,保证始终有挂单
}
if(sellPrice != ordersInfo.sellPrice){
var cancelId = ordersInfo.sellId
exchange.SetDirection('sell')
var sellId = exchange.Sell(sellPrice, Amount)
ordersInfo.sellPrice = sellPrice
if(sellId){ordersInfo.sellId = sellId}else{ordersInfo.sellId = 0}
if(cancelId){exchange.CancelOrder(cancelId)}
}
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()-lastCoverTime > CoverTime*1000){
lastCoverTime = Date.now()
var pos = exchange.GetPosition()
if(pos){cover(pos,depth)}
}
if(Date.now()-lastRestTime > 10*60*1000){
lastRestTime = Date.now()
reset()
}
showTable()
Sleep(Intervel*1000)
}
}
转载于https://www.fmz.com/strategy/171042