一个简单的网格交易量化程序

分享一个简单地网格交易程序,这个程序实现了网格交易的基本原理,细节上肯定需要完善,用作实际交易还是不行的,但是作为说明网格交易的原理,是足够了。这个程序仅作参考。

/*backtest   回测
 start: 2018-08-01 00:00:00    起始日期
 end: 2018-09-01 15:00:00   结束日期
 period: 15m  时间周期15分K线
 exchanges: [{"eid":"Huobi","currency":"LTC_BTC","stocks":0}]  回测数据说明 交易平台 huobi,货币对 LTC/BTC,
 */

var runBuyOrder = [];   定义买盘网格数组订单
var runSellOrder = [];   定义卖盘网格数组订单 
var basePrice = 0;  初始价格
var percent = 0.05;  价格间距

function onTick(){  
     var date = _D();   定义日期
     var depth = exchange.GetDepth();  获取市场深度
     account = exchange.GetAccount();  获取账户

//初始化账户
if((account.Balance > 0) && (basePrice == 0)){ 假如账户有资金且基准价为0,则初始化账户
    buyMoney = account.Balance * 0.09; 定义每网格交易仓位,为9%
    basePrice = depth.Asks[0].Price; 定义基准价格为卖1价

    buyPrice = basePrice * (1-percent * 0); 网格1
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 1); 网格2
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 2); 网格3
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 3); 网格4
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 4); 网格5
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 5); 网格6
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 6); 网格7
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 7); 网格8
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 8); 网格9
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 9); 网格10
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    buyPrice = basePrice * (1-percent * 10); 网格11
    runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));

    Log(date); 输出日志日期
    Log('交易所:', exchange.GetName(), ' 账户:', exchange.GetAccount());输出交易所,账户名
    Log('卖单:', runSellOrder, ' 买单:', runBuyOrder); 输出卖单记录和买单记录

}

//检查订单轮询
allOrders = runBuyOrder.concat(runSellOrder); 连接买卖单数组且定义为allOrders数组
allOrders.forEach(function(orderId){  循环
    order = exchange.GetOrder(orderId); 依据id获取order
    //检查卖单
    if(runSellOrder.indexOf(order.Id) > -1){ 假如卖单存在
        if((order.Status == ORDER_STATE_CLOSED) && (order.Type == ORDER_TYPE_SELL)){  如果卖单是关闭状态,并且是卖单,说明网格中的卖单被执行
            Log(date); 输出日志日期
            Log('卖单成功 价格:', order.Price, ' 数量:', order.DealAmount);  输出卖单的详细信息 价格与数量
            Log('交易所:', exchange.GetName(), ' 账户:', exchange.GetAccount());  输出卖单的详细信息 交易所与账户
            //从本地数据库里面去掉并挂新的买单
            index = runSellOrder.indexOf(order.Id);  
            runSellOrder.splice(index, 1);  删除卖单数组中的1个卖单
            buyPrice = order.Price * (1 - percent); 设置买单价格
            runBuyOrder.push(exchange.Buy(buyPrice, order.DealAmount * order.Price / buyPrice)); 挂买单
        }

    }
    //检查买单
    if(runBuyOrder.indexOf(order.Id) > -1){ 遍历买单
        if((order.Status == ORDER_STATE_CLOSED) && (order.Type == ORDER_TYPE_BUY)){  检测到买单被执行
            Log(date); 输出日志
            Log('买单成功 价格:', order.Price, ' 数量:',  输出日志order.DealAmount);
            Log('交易所:', exchange.GetName(), ' 账户:',  exchange.GetAccount()); 输出日志
            //从本地数据库里面去掉并挂新的卖单
            index = runBuyOrder.indexOf(order.Id); 
            runBuyOrder.splice(index, 1); 删除数组中1个买单
            sellPrice = order.Price * (1 + percent); 设置卖单价格
            runSellOrder.push(exchange.Sell(sellPrice, order.DealAmount));  挂卖单
        }

    }
});

}
function main(){  运行主程序
     while(true){ 循环
         onTick(); 函数
     }
}

为您推荐

发表评论

邮箱地址不会被公开。 必填项已用*标注