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;i<orders.length;i++){
exchange.CancelOrder(orders[i].Id)
}
ordersInfo = {buyId:0, buyPrice:0, buyAmount:0, sellId:0, sellPrice:0, sellAmount:0, pos:0}
}
}
function onexit(){ //退出后撤销订单
reset()
}
function getPostiton(){
pos = exchange.GetPosition()
if(!pos){return}
if(pos.length>0){
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)
}
}
FMEX简单交易挖矿机器人
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 互联网时代,新金融逐渐成为大众选择财富增值的一个渠道 互联网时代,新金融逐渐成为大众选择财富增值的一个渠道,老百姓...
- 原文地址:https://www.2cto.com/kf/201904/801224.html 备注: 方式一 首...