# Help with aggregation query to calculate relative volume

**URL:** <https://discuss.elastic.co/t/help-with-aggregation-query-to-calculate-relative-volume/245103>\
**Category:** Elasticsearch\
**Created:** [August 15, 2020, 5:55pm UTC](https://discuss.elastic.co/t/help-with-aggregation-query-to-calculate-relative-volume/245103 "2020-08-15T17:55:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JGor](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@JGor](https://discuss.elastic.co/u/JGor)\
**Post date:** [August 15, 2020, 5:55pm UTC](https://discuss.elastic.co/t/help-with-aggregation-query-to-calculate-relative-volume/245103/1 "2020-08-15T17:55:31Z")

</div>

> **Relative Volume** (often times called RVOL) is an indicator that tells traders how current trading **volume** is compared to past trading **volume** over a given period.

Hello guys,

I was wondering if someone can point me to the right direction. I am trying to calculate the mentioned metrics and I'm a bit stuck with how to calculate the difference between a current value at time T and an average value calculated from X days before to yesterday (T-X -\> T-1)

My documents look like this:

| Epic | Date | Total volume |
| --- | --- | --- |
| ABC | 2020/08/01 @ 08:05:00 | 100 |
| ABC | 2020/08/02 @ 08:05:00 | 50 |
| ABC | 2020/08/03 @ 08:05:00 | 10 |

Steps I am trying to achieve:

1. Calculate the average value for last X days on field Volume grouped by time component. (What is the average on Volume for the last X days every day at the time 08:05 for example) **I know how to do this part.**
2. Calculate the difference between today's value and value acquired in step 1. **I don't know how to do this part.**

Is this something that [Serial Differencing](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-serialdiff-aggregation.html) can help me with? Although I do not aggregate using a `date_histogram`. I aggregate only using Hour and Minute time component.

Thank you for any suggestions  
JG

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 12, 2020, 5:55pm UTC](https://discuss.elastic.co/t/help-with-aggregation-query-to-calculate-relative-volume/245103/2 "2020-09-12T17:55:57Z")

</div>

Closed on September 12, 2020, 5:55pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
