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

请教梦总

Author: Neo1898, Created: 2022-02-25 08:04:50, Updated: 2022-02-25 08:52:20

引用了梦总的一段代码,发现在某个时候会报错:TypeError: cannot read property ‘Price’ of null at main (FILE:553),导致程序退出,请教梦总,是否是因为自定义字段为null导致的,还是说没获取到price信息导致的,我将var pos = exchange.GetPosition()改为var pos = _C(exchange.GetPosition()),是否可以解决这个问题呢;因为模拟回测无法测试出这个bug,必须实盘,所以先请教下梦总,谢谢!

var currTotalEq = getTotalEquity()
var pos = _C(exchange.GetPosition())
//var pos = exchange.GetPosition()
if (currTotalEq && pos) {
                    // LogStatus(_D(), "当前总权益:", currTotalEq, "持仓:", pos)
                    var tblPos = {
                        "type" : "table",
                        "title" : "持仓",
                        "cols" : ["持仓数量", "持仓方向", "持仓均价", "持仓盈亏", "合约代码", "自定义字段 / " + SpecifyPosField],
                        "rows" : []
                    }
                    var descType = ["多头仓位", "空头仓位"]
                    for (var posIndex = 0 ; posIndex < pos.length ; posIndex++) {
                        tblPos.rows.push([pos[posIndex].Amount, descType[pos[posIndex].Type], pos[posIndex].Price, pos[posIndex].Profit, pos[posIndex].ContractType, SpecifyPosField == "" ? "--" : pos[posIndex].Info[SpecifyPosField]])
                    }
                    
                    var tbl = {
                        "type" : "table",
                        "title" : "数据",
                        "cols" : ["当前总权益", "实际盈亏", "当前价格", "买单价格/数量", "卖单价格/数量"],
                        "rows" : []
                    }
                    var buyOrder = null 
                    var sellOrder = null 
                    for (var orderIndex = 0 ; orderIndex < orders.length ; orderIndex++) {
                        if (orders[orderIndex].Type == ORDER_TYPE_BUY) {
                            buyOrder = orders[orderIndex]
                        } else {
                            sellOrder = orders[orderIndex]
                        }
                    }
                    var realProfit = currTotalEq - totalEq
                    if (exchange.GetName() == "Futures_Binance") {
                        _.each(pos, function(p) {
                            realProfit += parseFloat(p.Info.unRealizedProfit)
                        })                        
                    }
                    var t = exchange.GetTicker()
                    tbl.rows.push([currTotalEq, realProfit, t ? t.Last : "--", (buyOrder.Price + "/" + buyOrder.Amount), (sellOrder.Price + "/" + sellOrder.Amount)])

More

Neo1898 将var pos = exchange.GetPosition()改为var pos = _C(exchange.GetPosition()),是否可以解决呢,取到值了再往下走

Neo1898 553行附近的代码是: var t = exchange.GetTicker() tbl.rows.push([currTotalEq, realProfit, t ? t.Last : "--", (buyOrder.Price + "/" + buyOrder.Amount), (sellOrder.Price + "/" + sellOrder.Amount)]) 是否是没取到ticker数据,那改为var t = _C(exchange.GetTicker()),是否就可以了。

Neo1898 谢谢,梦总!

发明者量化-小小梦 ``` tbl.rows.push([currTotalEq, realProfit, t ? t.Last : "--", (buyOrder ? buyOrder.Price : "--") + "/" + (buyOrder ? buyOrder.Amount :"--"), (sellOrder ? sellOrder.Price : "--") + "/" + (sellOrder ? sellOrder.Amount : "--")]) ``` 问题总之就是,让buyOrder是null的时候,不要去访问buyOrder.Amount 或者 buyOrder.Price 就不会报错了。

Neo1898 就算有下订单,价格那里是有数值的,但是Amount那里始终都是没有数据的,也不报错,这个就很奇怪了,是哪里有问题呢?

Neo1898 没有下单的情况:改了price,还得改amount,但是尝试了 tbl.rows.push([currTotalEq, realProfit, t ? t.Last : "--", (buyOrder ? buyOrder.Price : "--" + "/" + buyOrder ? buyOrder.Amount :"--"), (sellOrder ? sellOrder.Price : "--" + "/" + sellOrder ? sellOrder.Amount : "--")]) 还是报TypeError: cannot read property 'Amount' of null at main (__FILE__:554),是代码没更新吗?还是哪里有错?

发明者量化-小小梦 可以,就是访问 某个变量的属性时,判断这个变量是不是null ,是就不访问, 就不会报错了。

Neo1898 tbl.rows.push([currTotalEq, realProfit, t ? t.Last : "--", (buyOrder ? buyOrder.Price : "--" + "/" + buyOrder.Amount), (sellOrder ? sellOrder.Price : "--" + "/" + sellOrder.Amount)]) 那就是上面的这样,对吧

发明者量化-小小梦 buyOrder ?buyOrder.Price : "--" 这样处理就可以了。

发明者量化-小小梦 可以传入空字符串。

Neo1898 谢谢指正,那我有些步骤是没有下单的,所以订单信息为null,必须允许传入null,应该要怎么改呢

轻轻的云 var t = _C(exchange.GetTicker)

轻轻的云 用了容错里面不能套()了,改为 var pos = _C(exchange.GetPosition)

Neo1898 tbl.rows.push,是这个不允许传入null值是吗?我有些步骤是没有下单的,所以订单信息为null,必须允许传入null,应该要怎么改呢,谢谢梦总!

Neo1898 后续我不下单了,这里是否允许为null呢,哪里可以改为允许为null呢,应该不下单,那订单号应该是null

发明者量化-小小梦 可能是 buyOrder 或者 sellOrder 是null 导致的,可以打印检查一下。