I have a flow that issues a GET http request and returns a JSON array of stock market data:
Time series data of 5 minute interval candles: Datetime { 1. open, 2. high, 3. low, 4. close, 5. volume ).
I can see the JSON data in the debug window everything looks good!
I would like to use the close attribute and create a new attribute called close_hod (close high of day ).
If the close is higher than the (close high of day)
then the close is assigned to the (close high of day).
I would also like to calculate close_stop_loss attribute which is 2% below the
(close high of day).
I would like to create a new JSON array that contains the original JSON array plus the two additional attributes in the time series data at 5 min intervals:
I'm coming back to your topic, but it appears that due to your use of the numbered list your JSON array in the post has turned into a formatted list too. Can you confirm your object looks like this?
To add additional properties with values, you can use a change node. You can use either a function node, or JSONata in a change node to calculate those values. The JSONata approach would be my personal choice here, but that's up to you.
This part is unclear to me. If your input data only has open , high, low, close, and volume, where does the initial "close high of day" come from? In pseudo code it reads as if close > hod => close = hod. But I don't see this close_hod in the initial data (after all you specify that it has yet to be created/added), nor something to use as hod.
This can be calculated by taking the close_hod derived from the previous step and multiply it by 0.98. You can do this, again, in a function node or with JSONata. In fact, depending on your answer on how close_hod is derived, it can likely be done in a single JSONata query.
Looking at your incoming data it's more horrible than I had hoped it was. It's very readable, so that's a plus, but it's not the easiest to work with. What you see here are keys with spaces in them. In most topics you see you can refer to parts of an object through dot notation, i.e. msg.payload.data, however, here you need to use the fully written notation: msg.payload["Meta Data"]["1. Information"] for example. Note that this is case sensitive, and you need to surround the entire name with quotes.
Slight correction here, the data you show in the debug window is an object with property "Time Series (5min)", which contains another object with each property a timestamp. This timestamp holds another nested object, which is what you described. The difference between an array of date objects, and an object with date objects is a major one in how you've to solve this.
Bolding added by myself, this first Close_High_of_Day, where/how is this defined? You specify this is an attribute to add by yourself, but where does it initially come from? You describe the action as "if close > Close_High_of_Day, then Close_High_of_Day = close"
Is this an aggregation, or do they have to be added to each object?