引用了梦总的一段代码,发现在某个时候会报错: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)])