¿Cómo debo debug esta pregunta?
En caso de que no haya posiciones en la cuenta, utilice el siguiente código para obtener información sobre las posiciones
while(true){
var position=_C(exchange.GetPosition)
if(position==null){
continue
}
type=position[0].Type
if(type==PD_LONG){
if(exsell!==sellPrice){
CancelPendingOrders()
exchange.SetDirection("closebuy")
exchange.Sell(sellPrice,position[0].Amount)
exsell=sellPrice
}
}
Cuando la cuenta no tiene posiciones, getposition debe obtener una matriz vacía[[ ], entonces ejecuta el continuue de la sentencia if para que el ciclo comience de nuevo, pero la prueba de disco duro sigue produciendo errores.
TypeError: cannot read property 'Type' of undefined at main (FILE:129)
Intenté cambiar las condiciones de if, como la posición[0]==null o typeof(position)==undefined no funciona o ejecuta el paréntesis posterior tipo=position[0].Typebox hace que la estrategia se equivoque
¿Cómo se puede resolver este problema?
解决办法如下:
var position = _C(exchange.GetPosition);
if(position.length>0)
{
//请把代码放在这个大括号里. 原理如下: 先判断这个持仓函数返回的数组的长度, 长度大于0, 表示有持仓信息了, 直接用null, undefine判断都不行. 然后才能访问position[0]
}
遇到相同的问题了,感觉有必要针对exchange.GetPosition单独设置一个容错函数,否则在没有持仓的情况下,这个函数几乎完全无法使用
顺便请问下楼主,最后是如何解决的
看下了帖子 ,发现问题了, 在您调用 exchange.GetPosition() 的时候 如果 你没有持仓 , 会返回 [] , 一个空数组, 这个 空数组 并不等于 null , 就是你判断
position == null 的时候 ,是 false 的, 所以不会 触发 continue, 然后 你访问 position[0] 这个元素是 不存在的,所以是 undefine ,再调用 他的 Type 属性 就报错了。
- 1

