Hello @Mark_Duncan, thank you for clarifying the question.
Side comment: we're improving date_nano
datatype but aggregations cannot have granularity of nanosecond at the moment. This is not necessary in this case.
Demo data
PUT discusstransform
{
"mappings": {
"recv_timestamp": {
"type": "date_nanos"
},
"last_fill_time": {
"type": "date_nanos"
},
"exchange_timestamp": {
"type": "date_nanos"
}
}
}
PUT discusstransform/_doc/1
{
"recv_timestamp": "2020-05-05T09:43:25.838916Z",
"last_fill_time": "0001-01-01T00:00:00.000000Z",
"notional": 0,
"side": "sell",
"quantity": 0.0017,
"instrument": "blah",
"limit_or_market": "limit",
"type": "orders",
"last_fill_qty": 0.0017,
"client_oid": "",
"last_fill_px": 9050.2,
"filled_notional": 15.38534,
"@timestamp": "2020-05-05T09:43:30.626Z",
"exchange_timestamp": "2020-05-05T09:43:25.830000Z",
"price": 9050.2,
"filled_size": 0,
"extra_fields": null,
"exchange": "blah",
"margin_trading": 2,
"order_type": "0",
"order_id": "4845450774989824",
"status": "filled"
}
PUT discusstransform/_doc/2
{
"recv_timestamp": "2020-05-05T09:43:25.838916Z",
"last_fill_time": "0001-01-01T00:00:00.000000Z",
"notional": 0,
"side": "buy",
"quantity": 0.0017,
"instrument": "blah",
"limit_or_market": "limit",
"type": "orders",
"last_fill_qty": 0.0017,
"client_oid": "",
"last_fill_px": 9050.2,
"filled_notional": 15.38534,
"@timestamp": "2020-05-05T09:43:30.626Z",
"exchange_timestamp": "2020-05-05T09:43:25.830000Z",
"price": 9050.2,
"filled_size": 0,
"extra_fields": null,
"exchange": "blah",
"margin_trading": 2,
"order_type": "0",
"order_id": "4845450774989824",
"status": "filled"
}
I cannot get exactly what is the metric or value you want to extract from last_fill_qty
.
The following transform will generate 2 documents, one for the sell
and one for the buy
side, per day.
POST _transform/_preview
{
"source": {
"index": [
"discusstransform"
]
},
"dest": {
"index": "transform_discusstransform"
},
"pivot": {
"group_by": {
"day": {
"date_histogram": {
"field": "recv_timestamp",
"calendar_interval": "1d"
}
},
"side": {
"terms": {
"field": "side.keyword"
}
}
},
"aggregations": {
"sum": {
"sum": {
"field": "last_fill_qty"
}
},
"avg": {
"avg": {
"field": "last_fill_qty"
}
},
"min": {
"min": {
"field": "last_fill_qty"
}
},
"max": {
"max": {
"field": "last_fill_qty"
}
}
}
}
}
If you want to obtain one single document, the other option might be to use scripted metrics.
Example: how to obtain the latest value of last_fill_qty
for buy
and sell
, per day, based on the recv_timestamp
.
POST _transform/_preview
{
"source": {
"index": [
"discusstransform"
]
},
"dest": {
"index": "transform_discusstransform"
},
"pivot": {
"group_by": {
"day": {
"date_histogram": {
"field": "recv_timestamp",
"calendar_interval": "1d"
}
}
},
"aggregations": {
"values": {
"scripted_metric": {
"init_script": """
state.last_fill_qty = ['sell':0,'buy':0];
state.recv_timestamp_last = ['sell':0,'buy':0];
""",
"map_script": """
def recv_timestamp = doc.recv_timestamp.value.toInstant().toEpochMilli();
if(doc['side.keyword'].value == 'sell') {
if(state.recv_timestamp_last.sell < recv_timestamp) {
state.last_fill_qty.sell = doc.last_fill_qty.value;
state.recv_timestamp_last.sell = recv_timestamp;
}
} else if (doc['side.keyword'].value == 'buy') {
if(state.recv_timestamp_last.buy < recv_timestamp) {
state.last_fill_qty.buy = doc.last_fill_qty.value;
state.recv_timestamp_last.buy = recv_timestamp;
}
}
""",
"combine_script": "return state;",
"reduce_script": """
def last_fill_qty = ['sell':0,'buy':0];
def recv_timestamp_last = ['sell':0,'buy':0];
for(s in states) {
if(s.recv_timestamp_last.sell > recv_timestamp_last.sell) {
last_fill_qty.sell = s.last_fill_qty.sell;
recv_timestamp_last.sell = s.recv_timestamp_last.sell;
}
if(s.recv_timestamp_last.buy > recv_timestamp_last.buy) {
last_fill_qty.buy = s.last_fill_qty.buy;
recv_timestamp_last.buy = s.recv_timestamp_last.buy;
}
}
return ['sell_last_fill_qty': last_fill_qty.sell, 'buy_last_fill_qty': last_fill_qty.buy ]
"""
}
}
}
}
}
Result:
{
"preview" : [
{
"values" : {
"sell_last_fill_qty" : 0.0017000000225380063,
"buy_last_fill_qty" : 0.0017000000225380063
},
"day" : 1588636800000
}
],
"mappings" : {
"properties" : {
"day" : {
"type" : "date"
}
}
}
}