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

模拟回测的收益概览只有卖出没有买入

Author: kite, Created: 2020-12-07 23:24:34, Updated:

img

img

如图所示,我测试货币现货交易的时候,发现收益概览的图表把买入算成了空仓,卖出没有统计,不知道有哪位大神可以指点一下。 另外就是exchange.GetPosition()获取持仓的方法在回测中提示不支持这个方法。 代码如下:

/*backtest
start: 2020-05-01 00:00:00
end: 2020-12-03 00:00:00
period: 1d
basePeriod: 1d
exchanges: [{"eid":"Huobi","currency":"BTC_USDT"}]
*/

var times = 0;
var lasttime = null;
var hasPosition = false; //持仓数量

function main() {
    Log("strategy started.");
    while(true){
        var records = _C(exchange.GetRecords, PERIOD_D1);
            records = records.slice(-20);
        var {Time} = records[records.length - 1];
        if(lasttime != Time){
            lasttime = Time;
            var fast = TA.MA(records, 5);
            var slow = TA.MA(records, 20);
            
            var lastFast1 = fast[fast.length - 2];
            var lastSlow1 = slow[slow.length - 2];
            var lastFast2 = fast[fast.length - 3];
            var lastSlow2 = slow[slow.length - 3];
            
            if(lastFast2 < lastSlow2 && lastFast1 > lastSlow1 && !hasPosition){
                //当前为金叉
                //$.Buy(0.001);
                exchange.Buy(-1, 0.01);
                hasPosition = true;
            }
            if(lastFast2 > lastSlow2 && lastFast1 < lastSlow1 && hasPosition){
                //当前为死叉
                exchange.Sell(-1, 0.01);
                hasPosition = false;
            }
        }
    }
}

function init() {
    const account = _C(exchange.GetAccount);
    Log('strategy inted.', exchange.GetName(), account);
}

More

小草 现货交易所没有仓位概念,建议你先看社区置顶初级教程。计算盈亏是和初始持仓不变进行比较

kite 了解了,多谢