- 策略广场
- 网格(波段)香农版
网格(波段)香农版
Author:
小码哥, Date: 2018-01-06 17:56:23
Tags:
网格
/*backtest
start: 2017-10-01
end: 2018-01-01
period: 60
mode: 2
*/
function dateFormat(date, format) {
var o = {
"M+": date.getMonth() + 1, //月份
"d+": date.getDate(), //日
"h+": date.getHours(), //小时
"m+": date.getMinutes(), //分
"s+": date.getSeconds(), //秒
"q+": Math.floor((date.getMonth() + 3) / 3), //季度
"S": date.getMilliseconds() //毫秒
};
if (/(y+)/.test(format)) {
format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
}
for (var k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, (RegExp.$1.length == 1) ?
(o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
return format;
}
function main() {
// {"Balance":10,"Stocks":0,"FrozenBalance":0,"FrozenStocks":0}
var initAccount = exchange.GetAccount();
Log(initAccount);
var initBalance = initAccount.Balance;
Log("初始净值: " + initBalance);
//最近一次投资的日期
var lastInvestDate = '';
//初始买1和卖1的价格
var depth = exchange.GetDepth();
var initPrice = depth.Bids[0].Price;
while (true) {
//每次轮询,间隔时间为60秒
Sleep(60 * 1000);
var account = exchange.GetAccount();
var date = dateFormat(new Date(), "yyyy-MM-dd");
if (date != lastInvestDate) {
lastInvestDate = date;
//净值
var value = account.Balance + account.FrozenBalance + buy1price * (account.Stocks + account.FrozenStocks);
Log("净值: " + (Math.round(100 * value) / 100));
//是否跑赢持币不动
var r = 100 * ((value / initBalance) - (buy1price / initPrice));
Log("r: " + (Math.round(100 * r) / 100) + "%");
} else {
//continue;
}
//当前买1和卖1的价格
depth = exchange.GetDepth();
var buy1price = depth.Bids[0].Price;
var sell1price = depth.Asks[0].Price;
var T1 = (account.Stocks * buy1price + account.Balance) * targetBaseRatio / buy1price;
var T2 = (account.Stocks * sell1price + account.Balance) * targetBaseRatio / sell1price;
if (account.Stocks - T2 > minTradeLimit) {//涨了,要卖掉一些
exchange.Sell(buy1price, account.Stocks - T2);
} else if (T1 - account.Stocks > minTradeLimit) {//跌了,要买入,补仓
exchange.Buy(sell1price, T1 - account.Stocks);
}
}
}
相关内容
更多内容