exchange.GetPosition() 使用注意事项,要先判断是否为空数组

Author: sunsjk, Created: 2018-11-07 08:17:28, Updated: 2019-04-17 15:52:12

    exchange.SetTimeout(3000);
    exchange.SetContractType("XBTUSD");

    //取得第一次币量
    position = _C(exchange.GetPosition);
    last_Amount = position[0].Amount; //持币数量  
    hold_price = Math.round(position[0].Price); //持币均价
    
    
    TypeError: cannot read property 'Amount' of undefined at main (__FILE__:38)

Related

More

sunsjk var position = _C(exchange.GetPosition); Log("持仓数组实际内容", position); var len = position.length; //取数组长度var Log("持仓数组长度", len); 结果 len :空仓是0,有仓是1 取数组长度,怎么是1,不是其它数字 谢谢小小梦

小小梦 GetPosition 返回的是持仓数组, 如果没有持仓返回的是 [] , 一个空数组, 如果没有持仓, 后面代码 调用position[0].Amount 肯定就报错了,因为position[0] 是不存在的。 这个是您策略程序没有处理好。