- Square
- 简单网格测试
简单网格测试
Author:
cnxzcxy, Date: 2018-09-12 15:54:58
Tags:
Grid
/*backtest
start: 2018-08-01 00:00:00
end: 2018-09-01 15:00:00
period: 15m
exchanges: [{"eid":"Huobi","currency":"LTC_BTC","stocks":0}]
*/
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)){
buyMoney = account.Balance * 0.09;
basePrice = depth.Asks[0].Price;
buyPrice = basePrice * (1-percent * 0);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 1);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 2);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 3);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 4);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 5);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 6);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 7);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 8);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 9);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
buyPrice = basePrice * (1-percent * 10);
runBuyOrder.push(exchange.Buy(buyPrice, buyMoney / buyPrice));
Log(date);
Log('交易所:', exchange.GetName(), ' 账户:', exchange.GetAccount());
Log('卖单:', runSellOrder, ' 买单:', runBuyOrder);
}
//检查订单轮询
allOrders = runBuyOrder.concat(runSellOrder);
allOrders.forEach(function(orderId){
order = exchange.GetOrder(orderId);
//检查卖单
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);
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);
sellPrice = order.Price * (1 + percent);
runSellOrder.push(exchange.Sell(sellPrice, order.DealAmount));
}
}
});
}
function main(){
while(true){
onTick();
}
}
Related
More