I have an array that contains a couple of hundred trades
objects like so:
Input:
[{
"trades": {
"model": "Trades_Information"
"fields": {
"orderCloseTime": "2019-03-19T16:20:56",
"orderOpenPrice": "1.40000",
"orderClosePrice": "1.44000",
"orderProfit": "75",
"orderLots": "1.4"
},
[...]
}]
Now I would like to loop all objects and map the results (some calculations included in the loop) to new arrays which will update an Apexchart
.
For me the trickie part right now is to get the correct mapping dynamically to the last 12 months since each new array must contain the values in the correct order according to the last 12 months array which I create as follows:
var date = new Date();
var lastMonths = [],
monthNames = ['Dec', 'Nov', 'Oct', 'Sep', 'Aug', 'Jul', 'Jun', 'May', 'Apr', 'Mar', 'Feb', 'Jan'];
for (var i = 0; i < 12; i++) {
lastMonths.push(monthNames[date.getMonth()]);
date.setMonth(date.getMonth() - 1);
}
console.log(lastMonths);
// ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
So I somehow end up with this brief structure:
// Arrays needed for Apexchart
NetPipsMonth = [];
NetProfitMonth = [];
TotalVolumeMonth = [];
TotalTradesMonth = [];
lastMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
// Loop all trades in trades-object
for (var i = 0; i < tradesTotal; i++) {
// Filter all trades by closed ones
closeTime = trades[i].fields.orderCloseTime;
if (closeTime != null) {
// Make some calculations that output one value per new array per trade object
// Push the results to each array in right order according to lastMonths array
// Mapping with `orderCloseTime`
}
}
I don't know how to realize the mapping by orderCloseTime
in order to have the results in the right place within the needed arrays.
My ideas/thoughts in human language:
- Define the month of the
orderCloseTime
for each trade and tell JS which spot in the new arrays is March (example above) using the order oflastMonths
and increase it by the result of this particular trade's values to end up with the monthly sum after looping all trade objects.
lastMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
NetProfitMonth = [ , , 75 , , ,];
TotalVolumeMonth = [ , , 1.4 , , ... ,];
- Maybe create a nested object and populate this while looping to have all values in one main object and build the needed arrays for
apexchart
out of this main object after looping is finished - Build 12x4 loops for every month containing each calculation and sum the results afterwards to get monthly sums included in 4 arrays (that would end up in a huge amount of code)
Looking forward to your ideas and enjoy the holidays!