Retrieving aggregated bars with Polygon.io and Rust.

Published on Jan 28, 2025, by TradeTinkerer

What are aggregated bars?

Aggregated bars are a type of charting tool used to group or aggregate price data over a specified period of time. Instead of showing individual price movements (like traditional candlesticks or bar charts), aggregated bars combine multiple price movements within a defined time range into one single bar. This can help simplify price action and make it easier to spot trends, reduce noise, and make analysis more manageable.

For example, instead of plotting every minute or second’s movement of a stock, you could use aggregated bars to combine every 5-minute, 30-minute, or even daily movements into a single bar. The “bar” typically displays the open, high, low, and close values for that aggregated period.

This technique is often used by traders to smooth out the noise and gain a clearer view of the overall trend or momentum in the stock. Would you be interested in how to set this up or where it’s commonly used?

How to retrieve aggregated bars with help of the Polygon.io API in Rust?

The first thing which has to happen, is authenticating to the Polygon.io API. I will not go into detail here, because there is already another post which explains this in detail:

Getting started with the Polygon.io and Rust

The next thing we need to do is define the Structs, so that we can parse the response we will get from Polygon.io.

The Polygon.io response will look something like this:

{
	"adjusted": true,
	"next_url": "https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/1578114000000/2020-01-10?cursor=bGltaXQ9MiZzb3J0PWFzYw",
	"queryCount": 2,
	"request_id": "6a7e466379af0a71039d60cc78e72282",
	"results": [
		{
			"c": 75.0875,
			"h": 75.15,
			"l": 73.7975,
			"n": 1,
			"o": 74.06,
			"t": 1577941200000,
			"v": 135647456,
			"vw": 74.6099
		},
		{
			"c": 74.3575,
			"h": 75.145,
			"l": 74.125,
			"n": 1,
			"o": 74.2875,
			"t": 1578027600000,
			"v": 146535512,
			"vw": 74.7026
		}
	],
	"resultsCount": 2,
	"status": "OK",
	"ticker": "AAPL"
}

More information on the response and what everything means can be found in the Polygon.io documentation.

In Rust we will create two structs to represent this response:

#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct AggregatedBars {
    pub ticker: String,
    pub query_count: u8,
    pub results_count: u8,
    pub adjusted: bool,
    pub results: Vec<Bar>,
}

#[derive(Deserialize, Debug)]
pub struct Bar {
    pub v: f64,
    pub vw: f64,
    pub o: f64,
    pub c: f64,
    pub h: f64,
    pub l: f64,
    pub t: i64,
    pub n: i64,
}

Then we build the request URL:

let url = format!("https://api.polygon.io/v2/aggs/ticker/{symbol}/range/{multiplier}/{timespan}/{from}/{to}?adjusted={adjusted}&sort={sort}");

The final step is to make and parse the request.

For making the request we will use the popular rust crate “reqwest”.

let client = reqwest::Client::new();

let response = client
    .get(url)
    .header("Authorization", token)
    .send()
    .await
    .unwrap()
    .json::<AggregatedBars>()
    .await;

In this example we also use the amazing “clap” rust crate, so that we can use this as a CLI application and parse all the arguments and use them in the request URL.

For the usage of clap there will probably be a separate post.

The full code for this example can be found on Github.