Skip to content

Instantly share code, notes, and snippets.

@nisbus
Created June 14, 2012 22:49
Show Gist options
  • Save nisbus/2933496 to your computer and use it in GitHub Desktop.
Save nisbus/2933496 to your computer and use it in GitHub Desktop.
Reduce
function(keys, values) {
var sumObj = {"qty_cross_ccy": 0,
"qty_base_ccy": 0,
"fees": 0,
"profit_loss_realized": 0,
"realized_pl_percentage": 0,
"average_buy_rate" : 0,
"average_sell_rate" : 0,
"asset_under_management" : 0};
if(keys != null){
sumObj.strategy = keys[0][0];
}else{
sumObj.strategy = values[0].strategy;
}
var total_aum = 0;
var total_pl = 0;
values.forEach(function(e,a){
total_aum += Number(e.asset_under_management);
total_pl += Number(e.profit_loss_realized);
});
sumObj.asset_under_management = total_aum;
sumObj.realized_pl_percentage = total_pl/total_aum;
values.forEach(function(e,a){
sumObj.qty_cross_ccy += Number(e.qty_cross_ccy);
sumObj.qty_base_ccy += Number(e.qty_base_ccy);
sumObj.fees += Number(e.fees);
sumObj.profit_loss_realized += Number(e.profit_loss_realized);
sumObj.average_buy_rate += Number(e.average_buy_rate);
sumObj.average_sell_rate += Number(e.average_sell_rate);
});
sumObj.average_buy_rate = sumObj.average_buy_rate/values.length;
sumObj.average_sell_rate = sumObj.average_sell_rate/values.length;
return sumObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment