I'm working on a project to monitor my test crypto trading bots. And there is one thing left I didn't succeed in fixing.
The bot output is sending me all current positions, in my case 1 to a maximum of 15. But I need some way to make a sum and present me with one total value
In this case, I want to calculate the sum of,
payload.currentPositions.DCA[0].totalCost
payload.currentPositions.DCA[1].totalCost
payload.currentPositions.DCA[2].totalCost
can be up to payload.currentPositions.DCA[15].totalCost
Unfortunately I could not make it work with the Jsonata because I have to send all data in one session to my spreadsheet, and I could not join both Jsonata's in the change node..
So I had another look at @Colin his example, but have some difficulties with this one. I tried:
let initialValue = 0
let sum = [{x: msg.payload.currentPositions.DCA[0].currentValue}, {x: msg.payload.currentPositions.DCA[1].currentValue}, {x: msg.payload.currentPositions.DCA[2].currentValue}, {x: msg.payload.currentPositions.DCA[3].currentValue}].reduce(function (accumulator, currentValue) {
return accumulator + currentValue.x
}, initialValue)
msg.payload ={};
msg.payload = sum
But there are between 0 and 15 DCA's open at a certain time, so I could just add the msg.payload.currentPositions.DCA.currentValue} 15 times, but if the object does not exists, it stops with error "TypeError: Cannot read property 'currentValue' of undefined".
Is there some way to make this more smart?
Thanks!
```
notice backticks , they present code in easy form to copy, and prevent forum corrupting code. Please remember to use these in future posting, to make life easier