George Lydakis
George Lydakis

Reputation: 509

Elasticsearch query to get median/mean value of every x minutes/hours/days

I have an index of Forex tick data and I'd like to create a query to get for example bid prices within a start and end end date at a certain "resolution". What I mean by resolution is that I want 1 value, the median or the mean, for every x minutes or hours.

I'm guessing I need to use the date histogram aggregation together with the statistics aggregation—but how?

A sample not correct query that I'm currently working on is:

{
        "fields": ["ask_price", "bid_price", "tick_date"],
        "query": {
            "match": {
                "currency_pair": currencyPair
            }
        },
        "filter": {
            "range": {
                "gte": startDate,
                "lte": endDate,
                "format": "yyyy-MM-dd"
            }
        },
        "aggs": {
            "resolution": {
                "date_histogram": {
                    "field": "tick_date",
                    "interval": "1m"
                }
            }
        }
}

Upvotes: 5

Views: 7570

Answers (2)

George Lydakis
George Lydakis

Reputation: 509

I draw my answer taking into account @Richa's response. I do have to use a Subaggregation So the final and correct query will be:

    {
        "query": {
            "match": {
                "currency_pair": currencyPair
            }
        },
        "filter": {
            "range": {
                "tick_date": {
                    "gte": startDate,
                    "lte": endDate,
                    "format": "yyyy-MM-dd"
                }
            }
        },
        "aggs": {
            "resolution": {
                "date_histogram": {
                    "field": "tick_date",
                    "interval": "15m"
                },
                "aggs": {
                    "avg_bid": {
                        "avg": {
                            "field": "bid_price"
                        }
                    },
                    "avg_ask": {
                        "avg": {
                            "field": "ask_price"
                        }
                    },
                    "median_bid": {
                        "percentiles": {
                            "field": "bid_price",
                            "percents": [50]
                        }
                    },
                    "median_ask": {
                        "percentiles": {
                            "field": "bid_ask",
                            "percents": [50]
                        }
                    }
                }
            }
        }

Upvotes: 3

Richa
Richa

Reputation: 7649

Use subAggregation like this:

{
    "fields": ["ask_price", "bid_price", "tick_date"],
    "query": {
        "match": {
            "currency_pair": currencyPair
        }
    },
    "filter": {
        "range": {
            "gte": startDate,
            "lte": endDate,
            "format": "yyyy-MM-dd"
        }
    },
    "aggs": {
        "resolution": {
            "date_histogram": {
                "field": "tick_date",
                "interval": "1m"
            },

          "aggs": {
           "statistics": {
             "stats": {
              "field": "field_name_whose_stats_are_to_be_found"
                 }
               }
             }
          }
       }
   }

Upvotes: 2

Related Questions