- 策略广场
- 价值平均定投
价值平均定投
Author:
小码哥, Date: 2018-01-04 15:00:09
Tags:
辅助交易
/*backtest
start: 2017-12-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() {
Log(exchange.GetAccount());
//最近一次投资的日期
var lastInvestDate = '';
var n = 0;
while (true) {
//每次轮询,间隔时间为60秒
Sleep(60 * 1000);
//如果当前日期和最近一次投资日期相同,说明当天已经投过了,跳过
var date = dateFormat(new Date(), "yyyy-MM-dd");
if (date == lastInvestDate) {
continue;
}
lastInvestDate = date;
Log("日期: " + date);
//第N期投入
n++;
//当前买1和卖1的价格
var depth = exchange.GetDepth();
var buy1price = depth.Bids[0].Price;
var sell1price = depth.Asks[0].Price;
//当前账户中标的价值与期望价值之差,就是当期应购入的价值
//expectGrowthRate 预期增长率,如果是0.8%(0.008)就是预期每天投资标的价值会自增长0.8%
//如果没有预期增长率,可能买一段时间之后就再也不买进了,因为自增长就可能已经满足价值增长的预期了
var account = exchange.GetAccount();
var expectValue = singleInvestAmount * n * Math.pow(1 + expectGrowthRate, n);
var currentValue = account.Stocks * buy1price;
//在单次目标投入的10%以上才操作,不浪费手续费
if (expectValue - currentValue > singleInvestAmount * 0.1) {
exchange.Buy(buy1price, (expectValue - currentValue) / buy1price);
} else if (expectValue - currentValue < - singleInvestAmount * 0.1) {
exchange.Sell(sell1price, (currentValue - expectValue) / sell1price);
}
}
}
template: strategy.tpl:40:21: executing "strategy.tpl" at <.api.GetStrategyListByName>: wrong number of args for GetStrategyListByName: want 7 got 6