exchange.IO("base", Url) //切换基地址,方便切换实盘和模拟盘,实盘地址:https://api.fmex.com
//挖矿分组,[下限百分比,上限百分比,挖矿系数]
var groups = [[0,1,1/10],[1,2,1/20],[2,3,1/20],[3,4,1/20],[4,6,1/40],[6,8,1/40],[8,10,1/40],[10,12,1/40],[12,14,1/40],[14,16,1/40],[16,18,1/40],[18,20,1/40],[20,100,1/20]]
var ordersInfo = {buyId:0, buyPrice:0, sellId:0, sellPrice:0, minPrice:0, maxPrice:0}
var depthInfo = {asks:[], bids:[]}
var lastProfitTime = 0 //控制打印收益时间
var lastRestTime = Date.now() //定时重置策略
var lastCoverTime = Date.now() //检查仓位并平仓
function adjustNum(num){ //控制价格精度
return num >= _N(num,0)+0.5 ? _N(num,0)+0.5 : _N(num,0)
}
function calcDepth(depth){ //计算最佳挂单位置
depthInfo = {asks:[], bids:[]}
var last_price = adjustNum((depth.asks[0] + depth.bids[0])/2) //上一次成交价
var j = 0 //买单深度迭代标记位置
var k = 0
var max_asks = 0
var max_bids = 0
for(var i=0;i<groups.length;i++){
var fact = groups[i][2] //官方的挖矿系数,可按需设置,如离盘口越远越大,减少成交风险
var buy_price_bound = [adjustNum(last_price*(1-groups[i][0]/100)), adjustNum(last_price*(1-groups[i][1]/100))] //价格上下边界
var sell_price_bound = [adjustNum(last_price*(1+groups[i][0]/100)), adjustNum(last_price*(1+groups[i][1]/100))]
var ask_amount = 0 //买单累积深度
var bid_amount = 0
while(depth.asks[j*2] <= sell_price_bound[1] && j*2 < depth.asks.length){
ask_amount += depth.asks[j*2+1]
j += 1
}
var my_ask_amount = (ordersInfo.sellPrice > sell_price_bound[0] && ordersInfo.sellPrice <= sell_price_bound[1]) ? Amount : 0
var ratio = _N(10000*Amount*fact/Math.max(ask_amount+Amount-my_ask_amount,Amount),5)
depthInfo.asks.push(['sell_'+groups[i][0]+'-'+groups[i][1]+'%', fact, sell_price_bound[0], sell_price_bound[1], ask_amount, ratio, depth.asks[depth.asks.length-2] >= sell_price_bound[1]])
if(ratio >= depthInfo.asks[max_asks][5] && depth.asks[depth.asks.length-2] >= sell_price_bound[1]){ //保证是完整的深度信息
max_asks = depthInfo.asks.length-1
} //大于等于保证相同挖矿效率下远离盘口的优先
while(depth.bids[k*2] >= buy_price_bound[1] && k*2 < depth.bids.length){
bid_amount += depth.bids[k*2+1]
k += 1
}
var my_bid_amount = (ordersInfo.buyPrice < buy_price_bound[0] && ordersInfo.buyPrice >= buy_price_bound[1]) ? Amount : 0
ratio = _N(10000*Amount*fact/Math.max(bid_amount+Amount-my_bid_amount,Amount),5)
depthInfo.bids.push(['buy_'+groups[i][0]+'-'+groups[i][1]+'%', fact, buy_price_bound[0], buy_price_bound[1], bid_amount, ratio, depth.bids[depth.bids.length-2] <= buy_price_bound[1]])
if(ratio >= depthInfo.bids[max_bids][5] && depth.bids[depth.bids.length-2] <= buy_price_bound[1]){
max_bids = depthInfo.bids.length-1
}
}
return [depthInfo.asks[max_asks][3]-0.5, depthInfo.bids[max_bids][3]+0.5] //由于无法确定边界归属,所以在最大边界价格处-0.5
}
function showTable(){
var table = {type: 'table', title: '挂单信息', cols: ['分组位置', '挖矿系数', '下界价格', '上界价格', '累积深度', '额度占比(万分之一)', '深度信息是否完整'], rows: []}
for(var i=0;i<depthInfo.asks.length;i++){
table.rows.push(depthInfo.asks[i])
}
for(var i=0;i<depthInfo.bids.length;i++){
table.rows.push(depthInfo.bids[i])
}
LogStatus('`' + JSON.stringify(table) + '`\n'+JSON.stringify(ordersInfo))
}
function reset(){ //重置策略,防止一些订单卡住,可能会影响其它正在运行的策略
var orders = exchange.GetOrders()
if(orders){
for(var i=0;i<orders.length;i++){
exchange.CancelOrder(orders[i].Id)
}
ordersInfo = {buyId:0, buyPrice:0, sellId:0, sellPrice:0, minPrice:0, maxPrice:0}
}
}
function onexit(){ //退出后撤销订单
reset()
}
function cover(pos,depth){
if(pos.length>0){
if(pos[0].Type == 0){ //平多仓,采用盘口吃单,会损失手续费,可改为盘口挂单,会增加持仓风险。
exchange.SetDirection('sell')
var sellId = exchange.Sell(depth.bids[0], Math.min(CoverAmount, pos[0].Amount), '平多仓')
if(sellId){exchange.CancelOrder(sellId)} //平仓单会立即撤销
}else{
exchange.SetDirection('buy')
var buyId = exchange.Buy(depth.asks[0], Math.min(CoverAmount, pos[0].Amount), '平空仓')
if(buyId){exchange.CancelOrder(buyId)}
}
}
}
function trade(price){
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)}
}
}
function main() {
exchange.SetContractType('swap')
exchange.SetMarginLevel(0) //全仓模式
reset()
while(true){
var req = HttpQuery(Url+'/v2/market/depth/L150/btcusd_p') //GetDepth默认20档深度,为获取更多深度信息,使用原始API接口
if(!req){continue}
var depth = JSON.parse(req).data
ordersInfo.minPrice = depth.bids[depth.bids.length-2]
ordersInfo.maxPrice = depth.asks[depth.asks.length-2]
var price = calcDepth(depth)
trade(price) //交易函数
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/171258
网友评论