I need to plot the Volume-Weighted Average Prive (VWAP) of trades having a price_per_unit
and a quantitiy
over a certain time range.
As a result of the aggregation every bucket of the date_histogram
should contain the VWAP of the all trades that happened so far.
I'm not really sure if this is possible using Elasticsearch and also not what would be the right way to approach it (like using a script?)?
The basic mapping for a trade
document is quite simple:
"trade": {
"properties":
"trade_id": {"type": "string", "index": "not_analyzed"},
"product_id": {"type": "string", "index": "not_analyzed"},
"quantity": {'type': 'double'}, // number of units
"execution_time": {'type': 'date'},
"price_per_unit": {'type': 'double'},
}
}
Whereas execution_time
should be used for the date_histogram
and the total price of the trade is the product ofprice_per_unit
and quantity
. Therefore the VWAP = sum(price_per_unit * quantity) / sum(quantity)
.