---
title: "Handling Data"
description: "LEAN passes the data you request to the OnData method so you can make trading decisions. The Slice object groups the data together with a timestamp."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/forex/handling-data
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/forex/handling-data.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/03%20Securities/99%20Asset%20Classes/05%20Forex/02%20Handling%20Data
modified: 2026-11-01
index: https://www.quantconnect.com/llms.txt
---

# Handling Data

[Documentation](https://www.quantconnect.com/docs/v2.md) > [Writing Algorithms](https://www.quantconnect.com/docs/v2/writing-algorithms.md) > Securities > [Asset Classes](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes.md) > [Forex](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/forex.md) > Handling Data

> Code examples are shown in Python. Append `?lang=csharp` to this URL for the C# version of this page.

## Introduction

LEAN passes the data you request to the `on_data` method so you can make trading decisions. The `Slice` object that the `on_data` method receives groups all the data together at a single moment in time. To access the `Slice` outside of the `on_data` method, use the `current_slice` property of your algorithm.

All the data formats use `DataDictionary` objects to group data by `Symbol` and provide easy access to information. The plural of the type denotes the collection of objects. For instance, the `QuoteBars` `DataDictionary` is made up of `QuoteBar` objects. To access individual data points in the dictionary, you can index the dictionary with the Forex pair ticker or `symbol`, but we recommend you use the `symbol`.

To view the resolutions that are available for Forex data, see [Resolutions](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/forex/requesting-data.md#04-Resolutions).

## Quotes

`QuoteBar` objects are bars that consolidate NBBO quotes from the exchanges. They contain the open, high, low, and close prices of the bid and ask. The `open`, `high`, `low`, and `close` properties of the `QuoteBar` object are the mean of the respective bid and ask prices. If the bid or ask portion of the `QuoteBar` has no data, the `open`, `high`, `low`, and `close` properties of the `QuoteBar` copy the values of either the `bid` or `ask` instead of taking their mean.

![Quotebar decomposition](https://cdn.quantconnect.com/docs/i/dataformat-quotebar.png)

To get the `QuoteBar` objects in the `Slice`, index the `QuoteBars` property of the `Slice` with the Forex pair `symbol`. If the Forex pair doesn't actively get quotes or you are in the same time step as when you added the Forex pair subscription, the `Slice` may not contain data for your `symbol`. To avoid issues, check if the `Slice` contains data for your Forex pair before you index the `Slice` with the Forex pair `symbol`.

```python
def on_data(self, slice: Slice) -> None:
    # Obtain the mapped QuoteBar of the symbol if any
    quote_bar = slice.quote_bars.get(self._symbol)   # None if not found
```

You can also iterate through the `QuoteBars` dictionary. The keys of the dictionary are the `Symbol` objects and the values are the `QuoteBar` objects.

```python
def on_data(self, slice: Slice) -> None:
    # Iterate all received Symbol-QuoteBar key-value pairs
    for symbol, quote_bar in slice.quote_bars.items():
        ask_price = quote_bar.ask.close
```

`QuoteBar` objects let LEAN incorporate spread costs into your [simulated trade fills](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/trade-fills/key-concepts.md) to make backtest results more realistic.

`QuoteBar` objects have the following properties:

*Members of `QuantConnect.Data.Market.QuoteBar` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.Data.Market.QuoteBar&language=python*

Forex pairs have only quote data, so use a [`QuoteBarConsolidator`](https://www.quantconnect.com/docs/v2/writing-algorithms/consolidating-data/consolidator-types/time-period-consolidators.md#03-Consolidate-Quote-Bars) to consolidate their data. If you pass a `TradeBarConsolidator` for one of these securities to the `add_consolidator` method, LEAN throws an exception.

## Ticks

`Tick` objects represent a single trade or quote at a moment in time. A trade tick is a record of a transaction for the Forex pair. A quote tick is an offer to buy or sell the Forex pair at a specific price.

Trade ticks have a non-zero value for the `quantity` and `price` properties, but they have a zero value for the `bid_price`, `bid_size`, `ask_price`, and `ask_size` properties. Quote ticks have non-zero values for `bid_price` and `bid_size` properties or have non-zero values for `ask_price` and `ask_size` properties. To check if a tick is a trade or a quote, use the `ticktype` property.

In backtests, LEAN groups ticks into one millisecond buckets. In live trading, LEAN groups ticks into ~70-millisecond buckets. To get the `Tick` objects in the `Slice`, index the `Ticks` property of the `Slice` with a `symbol`. If the Forex pair doesn't actively trade or you are in the same time step as when you added the Forex pair subscription, the `Slice` may not contain data for your `symbol`. To avoid issues, check if the `Slice` contains data for your Forex pair before you index the `Slice` with the Forex pair `symbol`.

```python
def on_data(self, slice: Slice) -> None:
    ticks = slice.ticks.get(self._symbol, [])   # Empty if not found
    for tick in ticks:
        price = tick.price
```

You can also iterate through the `Ticks` dictionary. The keys of the dictionary are the `Symbol` objects and the values are the `list[Tick]` objects.

```python
def on_data(self, slice: Slice) -> None:
    for symbol, ticks in slice.ticks.items():
        for tick in ticks:
            price = tick.price
```

Tick data is raw and unfiltered, so it can contain bad ticks that skew your trade results. For example, some ticks come from dark pools, which aren't tradable. We recommend you only use tick data if you understand the risks and are able to perform your own online tick filtering.

`Tick` objects have the following properties:

*Members of `QuantConnect.Data.Market.Tick` (JSON): https://www.quantconnect.com/services/inspector?type=T:QuantConnect.Data.Market.Tick&language=python*

## Examples

The following examples demonstrate some common practices for handling Forex data.

### Example 1: Trade the USDJPY Spread

USDJPY is a Forex pair where the reserve banks of the base currency and quote currency act at different time, which results in a 12-hour period of seasonal peak volume. To reduce slippage costs, place orders when bid-ask spread is relatively low. The following algorithm demonstrates how to utilize the [Minimum indicator](https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/supported-indicators/minimum.md) to check for a narrow spread.

```python
class ForexExampleAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        # Add the USDJPY trading pair.
        self._forex = self.add_forex("USDJPY")
        # Create a Minimum indicator to track the lowest bid-ask spread for the past 12 hours.
        self._forex.spread_low = Minimum(12*60)
        # Warm up the indicator so it's immediately ready to use.
        self.warm_up_indicator(self._forex.symbol, self._forex.spread_low)

    def on_data(self, slice: Slice) -> None:
        # Ensure we have quote data for USDJPY in the current slice.
        if self._forex.symbol not in slice.quote_bars:
            return
        quote_bar = slice.quote_bars[self._forex.symbol]
        # Bid-ask spread = Ask price - Bid price
        bid_ask_spread = quote_bar.ask.close = quote_bar.bid.close
        # Update the spread minimum indicator to calculate the lowest bid-ask spread over the last 12 hours.
        self._forex.spread_low.update(quote_bar.end_time, bid_ask_spread)

        # Trade if the current spread is the lowest bid-ask spread, since it is the most efficient,
        # liquid price with lowest slippage.
        if not self.portfolio.invested and bid_ask_spread == self._forex.spread_low.current.value:
            self.market_order(self._forex.symbol, 1000)

        # Plot the bid-ask spread to validate the minimum bid-ask spread calculation.
        self.plot("Bid-Ask Spread", "spread", bid_ask_spread)
```

### Example 2: 5-Minute EURUSD Trend Reversal

The following algorithm applies a mean reveral strategy to EURUSD. To smooth out the noise of minute resolution data, it [consolidates the data into 5-minute bars](https://www.quantconnect.com/docs/v2/writing-algorithms/consolidating-data/consolidator-types/count-consolidators.md#03-Consolidate-Quote-Bars) and then calculates their 200-bar [Exponential Moving Average](https://www.quantconnect.com/docs/v2/writing-algorithms/indicators/supported-indicators/exponential-moving-average.md) (EMA). After every time the indicator updates, the algorithm checks for a trading opportuniity. When the current EURUSD price is below the EMA, it takes a long position. Otherwise, it takes a short position.

```python
class ForexExampleAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)

        # Add EURUSD minute data.
        self._forex = self.add_forex("EURUSD")
        # Create an EMA indicator to estimate the trend of EURUSD.
        self._forex.ema = ExponentialMovingAverage(200)
        # Attach an event handler to the indicator to track its updates.
        self._forex.ema.updated += self.on_updated
        # Create a consolidator that produces 5-minute quote bars to reduce reduce noise.
        consolidator = QuoteBarConsolidator(timedelta(minutes=5))
        # Define the consolidator handler so it updates the EMA with the consoildated bars.
        consolidator.data_consolidated += lambda _, bar: self._forex.ema.update(bar.end_time, bar.close)
        # Warm up the indicator with historical data.
        for bar in self.history[QuoteBar](self._forex.symbol, 1000):
            consolidator.update(bar)
        # Subscribe the consolidator for automatic updates.
        self.subscription_manager.add_consolidator(self._forex.symbol, consolidator)

    def on_updated(self, sender: object, point: IndicatorDataPoint) -> None:
        # Wait until the indicator is ready and the Forex pair has a price.
        if not self._forex.ema.is_ready or not self._forex.price:
            return
        # Rebalance the portfolio. If the current price is below the EMA, long. Otherwise, short.
        self.set_holdings(self._forex.symbol, 0.5 if self._forex.price <= point.value else -0.5)
```

### Example 3: New York Time Alignment

The following algorithm creates daily Forex bars that close at 4:30 PM Eastern Time (ET). This technique is helpful when you want to align the daily bars with the close of the New York.

```python
class ForexExampleAlgorithm(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2024, 9, 1)
        self.set_end_date(2024, 12, 31)
        # Add EURUSD minute data.
        self._forex = self.add_forex("EURUSD")
        # Create a daily bar consolidator with a custom period (4:30 PM-4:30 PM Eastern Time).
        consolidator = QuoteBarConsolidator(self._consolidation_period)
        # Attach a consolidation handler to plot the consolidated bars.
        consolidator.data_consolidated += lambda _, bar: self.plot("Consolidated Bar", "Close", bar.close)
        # Subscribe the consolidator for automatic updates.
        self.subscription_manager.add_consolidator(self._forex.symbol, consolidator)

    def _consolidation_period(self, dt: datetime) -> CalendarInfo:
        # Set the start time to be 4:30 PM of the previous day.
        start = dt.replace(hour=16, minute=30, second=0, microsecond=0)
        period = timedelta(1)
        if dt.hour < 17:
            start -= period
        # Set the period to 1 day to make daily bars.
        return CalendarInfo(start, period)
```
