资源加载中... loading...

单边网格

Author: 6821281, Date: 2018-04-24 12:04:22
Tags: Grid

测试的时候填写起始价格来测试是否正常 起始价格不填写为现价买入+价格区间买入 微信173970984


//获取账户余额
var  startPrice= 0;
function getBalancex(){
    var balance = exchange.GetAccount();
    Log("余额:", balance.Balance,"币余额:", balance.Stocks);
     return balance;
}    


//获取币种现价
function getPrice(typex){
    var NowPrice =exchange.GetTicker();
    if(typex=='buy'){
        
     Log("当前买入价格:"+(NowPrice.Buy))
     return NowPrice.Buy;
    }
    if(typex=='sell'){
     Log("当前卖出价格:"+(NowPrice.Sell))
     return NowPrice.Sell;
    }
}

//取消所有买单操作重新下单
function cancelOrders(){
    //获取所有未成交订单
    var orders = _C(exchange.GetOrders);
    for(var z in orders){
       //# Log("当前订单类型:",orders[z].Type,"订单Id:",orders[z].Id);
        if(orders[z].Type==0){
            exchange.CancelOrder(orders[z].Id)
            Log("取消之前的挂单成功,id:",orders[z].Id);
        }
    }
    /*
    for(var i=0;i<orders.length;i++){
        //只取消买单不取消卖单
        if(orders.Type==0){
            exchange.CancelOrder(orders.Id)
            Log("取消之前的挂单成功")
        }
    }*/
}



var saveBuyId= 0;
var doNowPrice = 0;
//下单操作 买入操作
function doTradingBuy(){
    cancelOrders()
    var price = getPrice("buy");
    if(startpricex>0){
        price=startpricex-moneybt;
    }else{
        price=price-moneybt;  //实际价格为当前价格减去区间价格
    }
    startPrice=price;
    var balancex = getBalancex();
    balanceAmount = balancex.Balance;
    
    //需要购买的数量
    doAmount = (balanceAmount*(buypersent/100))/price
    
    
    
    var id = 0; 
    if(doAmount>buylimit){
        saveBuyId=id= exchange.Buy(price, doAmount);
        Log("下单成功id:", id,"  价格:", price,"  数量:", doAmount);
    }else{
        Log("可买数量低于最小交易量:", buylimit,"  数量:", doAmount);
    }
}


//根据上次的ID来检查订单状态  然后出售
function doSellTrading2(){
    //获取订单状态
    Log("上一订单ID:", saveBuyId);
    if(saveBuyId!=0){
        var order = exchange.GetOrder(saveBuyId);
        if(order){
            if(order.Status==ORDER_STATE_CLOSED){
                leftStocks=leftStocks-orderList[i].Amount
                var sellPrice = order.Price+liruncha;
                var sellAmount =  order.Amount;
                var id=exchange.Sell(sellPrice, sellAmount);
                Log("出售成功id:", id,"  价格:", sellPrice,"  数量:", sellAmount);
            }
        }
    }

}









//定时扫描是否有买单成交  并出售对应数量
function doSellTrading(){
    //如果余额大于零  
    balan = getBalancex()
    leftStocks=balan.Stocks;
    if(balan.Stocks>0){
        var orderList = exchange.GetTrades()
        //理论检测前20条数据即可
        if(orderList){
            for(i=0;i<=100;i++){
                //Log("查看订单信息:",orderList);
                if(orderList[i].Type==0 && leftStocks>orderList[i].Amount){
                    leftStocks=leftStocks-orderList[i].Amount
                    var sellPrice = orderList[i].Price+liruncha;
                    var sellAmount =  orderList[i].Amount;
                    var id=exchange.Sell(sellPrice, sellAmount);
                    Log("出售成功id:", id,"  价格:", sellPrice,"  数量:", sellAmount);
                }
            }
        }
    }
}


function onTick(){
    //在这里写策略逻辑,将会不断调用exchange
    doSellTrading2()
    doTradingBuy()
}
/*
function main(){
    var id = exchange.Sell(99999, 1);
    var order = exchange.GetOrder(id);//参数id为订单号码,需填入你想要查询的订单的号码
    Log("Id", order.Id, "Price:", order.Price, "Amount:", order.Amount, "DealAmount:",
        order.DealAmount, "Status:", order.Status, "Type:", order.Type);
}
*/
function main(){
    exchange.SetPrecision(buyjingdu, buymount);
    while(true){
        onTick();
        Sleep(reffertime);
    }
}

Related

More