---
title: "Requesting Data"
description: "Request Contract for Difference (CFD) data in your algorithm to receive a feed of contract prices in the OnData method."
canonical: https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/cfd/requesting-data
markdown: https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/cfd/requesting-data.md
language: python
source: https://github.com/QuantConnect/Documentation/tree/master/03%20Writing%20Algorithms/03%20Securities/99%20Asset%20Classes/10%20CFD/01%20Requesting%20Data
modified: 2026-10-28
index: https://www.quantconnect.com/llms.txt
---

# Requesting 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) > [CFD](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/cfd.md) > Requesting Data

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

## Introduction

Request Contract for Difference (CFD) data in your algorithm to receive a feed of contract prices in the `on_data` method. QuantConnect supports CFD trading with [OANDA](https://www.quantconnect.com/docs/v2/cloud-platform/live-trading/brokerages/cfd-and-forex-brokerages.md) and [Interactive Brokers](https://www.quantconnect.com/docs/v2/cloud-platform/live-trading/brokerages/interactive-brokers.md).

## OANDA Subscriptions

To create a CFD subscription, in the `initialize` method, call the `add_cfd` method. The `add_cfd` method returns a `Cfd` Security object, which contains a `symbol` property. Save a reference to the `symbol` so you can use it in `on_data` to access the security data in the `Slice`.

```python
self._symbol = self.add_cfd("XAUUSD").symbol
```

To view the supported CFD contracts, see [Supported Assets](https://www.quantconnect.com/docs/v2/writing-algorithms/datasets/quantconnect/cfd-data.md#09-Supported-Assets). For more information about the specific dataset we use for backtests, see the [OANDA CFD dataset listing](https://www.quantconnect.com/datasets/oanda-cfd-data). To trade OANDA CFDs live, use our [QuantConnect data provider](https://www.quantconnect.com/docs/v2/cloud-platform/datasets/quantconnect/cfd.md).

## Interactive Brokers Subscriptions

To create a CFD subscription for [Interactive Brokers (IB)](https://qnt.co/interactive-brokers), in the `initialize` method, call the `add_cfd` method and set the `market` parameter to `Market.INTERACTIVE_BROKERS`. The `add_cfd` method returns a `Cfd` Security object, which contains a `symbol` property. Save a reference to the `symbol` so you can use it in `on_data` to access the security data in the `Slice`.

```python
self._symbol = self.add_cfd("SPY", market=Market.INTERACTIVE_BROKERS).symbol
```

Historical data for backtesting IB CFDs is unavailable.

In live trading, include the [Interactive Brokers data provider](https://www.quantconnect.com/docs/v2/cloud-platform/datasets/interactive-brokers.md) when you deploy the algorithm to access IB CFD data. You can use this data provider for paper or real-money accounts. IB provides data for Indexes, Metals, Forex, and Global Stock CFDs. To view the Index and Forex CFD products that IB supports in live trading, see the [Interactive Brokers Supported Contracts](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/cfd/requesting-data.md#12-Interactive-Brokers-Supported-Contracts). To view the Equity CFD products, see the [Products Search](https://www.interactivebrokers.co.uk/en/trading/products-exchanges.php) page on the IB website.

If you live in the EU and can't trade US ETFs due to regulation, you can trade their [CFD equivalents](https://www.ibkrguides.com/kb/article-1912.htm) in live mode.

```python
security_type = SecurityType.CFD if self.live_mode else SecurityType.EQUITY
market=Market.INTERACTIVE_BROKERS if self.live_mode else Market.USA
self._symbol = Symbol.create("SPY", security_type, market)
self.add_security(self._symbol)
```

Market hours for IB CFDs are not modeled. You cannot use them for [scheduled events](https://www.quantconnect.com/docs/v2/writing-algorithms/scheduled-events.md) or to get [market open/close times](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/market-hours.md). To create a scheduled event, use a ticker of any [US Equity](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/us-equity/requesting-data.md) that you haven't added as a IB CFD or create a US Equity `Symbol`.

```python
def initialize(self):
    us_equity_symbol = Symbol.create("SPY", SecurityType.EQUITY, Market.USA)
    # Call the _before_market_open method 10 minutes before open every day US Equity stocks are trading.
    self.schedule.on(
        self.date_rules.every_day(us_equity_symbol),
        self.time_rules.before_market_open(us_equity_symbol, 10),
        self._before_market_open
    )
```

## Resolutions

The following table shows the available resolutions and data formats for CFD subscriptions:

| Resolution | TradeBar | QuoteBar | Trade Tick | Quote Tick |
| --- | --- | --- | --- | --- |
| `TICK` | Not supported | Not supported | Not supported | Supported✓ |
| `SECOND` | Not supported | Supported✓ | Not supported | Not supported |
| `MINUTE` | Not supported | Supported✓ | Not supported | Not supported |
| `HOUR` | Not supported | Supported✓ | Not supported | Not supported |
| `DAILY` | Not supported | Supported✓ | Not supported | Not supported |

The default resolution for CFD subscriptions is `Resolution.MINUTE`. To change the resolution, pass a `resolution` argument to the `add_cfd` method.

```python
self._symbol = self.add_cfd("XAUUSD", Resolution.DAILY).symbol
```

To create custom resolution periods, see [Consolidating Data](https://www.quantconnect.com/docs/v2/writing-algorithms/consolidating-data/getting-started.md).

## Supported Markets

The following `Market` enumeration members are available for CFDs:

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

Fields: `INTERACTIVE_BROKERS`, `InteractiveBrokers`, `OANDA`, `Oanda`

To set the market for a security, pass a `market` argument to the `add_cfd` method.

```python
self._symbol = self.add_cfd("XAUUSD", market=Market.OANDA).symbol
```

The [brokerage models](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/brokerages/key-concepts.md) have a default market for each asset class. If you set a brokerage model, you may not need to specify the market to use. In backtesting, set the market for CFDs to OANDA.

## Fill Forward

Fill forward means if there is no data point for the current [slice](https://www.quantconnect.com/docs/v2/writing-algorithms/key-concepts/time-modeling/timeslices.md), LEAN uses the previous data point. Fill forward is the default data setting. If you disable fill forward, you may get [stale fills](https://www.quantconnect.com/docs/v2/writing-algorithms/reality-modeling/trade-fills/key-concepts.md#06-Stale-Fills) or you may see trade volume as zero.

To disable fill forward for a security, set the `fill_forward` argument to false when you create the security subscription.

```python
self._symbol = self.add_cfd("XAUUSD", fill_forward=False).symbol
```

## Margin and Leverage

LEAN models buying power and margin calls to ensure your algorithm stays within the margin requirements. The specific margin available depends on your brokerage destination. To change the amount of leverage you can use for a CFD contract, pass a `leverage` argument to the `add_cfd` method.

```python
self._symbol = self.add_cfd("XAUUSD", leverage=35).symbol
```

## Data Normalization

The data normalization mode doesn't affect the data that LEAN passes to `on_data` or the data from [history request](https://www.quantconnect.com/docs/v2/writing-algorithms/historical-data/history-requests.md). If you change the data normalization mode, it won't change the outcome.

Interactive Brokers Stock CFDs apply corporate actions to the price of the asset, including paying dividends in cash to your account. This behavior is not modeled today and splits in live trading will result in price discontinuities. You should [monitor the underlying Equity](https://www.quantconnect.com/docs/v2/writing-algorithms/securities/asset-classes/us-equity/corporate-actions.md) for events and use the underlying Equity for indicators.

## Properties

The `add_cfd` method returns a `Cfd` object, which have the following properties:

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

## Interactive Brokers Supported Contracts

The following table shows the CFD contracts that IB supports:

| Contract Available (107) |  |  |  |  |  |
| --- | --- | --- | --- | --- | --- |
| AUDCAD | AUDCHF | AUDCNH | AUDHKD | AUDJPY | AUDNZD |
| AUDSGD | AUDUSD | AUDZAR | CADCHF | CADCNH | CADHKD |
| CADJPY | CHFCNH | CHFCZK | CHFDKK | CHFHUF | CHFJPY |
| CHFNOK | CHFPLN | CHFSEK | CHFZAR | CNHHKD | CNHJPY |
| DKKJPY | DKKNOK | DKKSEK | EURAUD | EURCAD | EURCHF |
| EURCNH | EURCZK | EURDKK | EURGBP | EURHKD | EURHUF |
| EURILS | EURJPY | EURMXN | EURNOK | EURNZD | EURPLN |
| EURRUB | EURSEK | EURSGD | EURUSD | EURZAR | GBPAUD |
| GBPCAD | GBPCHF | GBPCNH | GBPCZK | GBPDKK | GBPHKD |
| GBPHUF | GBPJPY | GBPMXN | GBPNOK | GBPNZD | GBPPLN |
| GBPSEK | GBPSGD | GBPUSD | GBPZAR | HKDJPY | IBAU200 |
| IBCH20 | IBDE40 | IBES35 | IBEU50 | IBFR40 | IBGB100 |
| IBHK50 | IBJP225 | IBNL25 | IBUS30 | IBUS500 | IBUST100 |
| MXNJPY | NOKJPY | NOKSEK | NZDCAD | NZDCHF | NZDJPY |
| NZDUSD | SEKJPY | SGDCNH | SGDJPY | USDCAD | USDCHF |
| USDCNH | USDCZK | USDDKK | USDHKD | USDHUF | USDILS |
| USDJPY | USDMXN | USDNOK | USDPLN | USDRUB | USDSEK |
| USDSGD | USDZAR | XAGUSD | XAUUSD | ZARJPY |  |

## Examples

The following examples demonstrate some common practices for requesting CFD data.

### Example 1: SMA Crossing

The following strategy purchases ETFs when their fast-moving average crosses over the slow-moving average. In live mode, the strategy purchases their CFD equivalents.

```python
class EtfCfdSwapMovingAverageCrossAlgorithm(QCAlgorithm):

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

        # Request the data to feed into indicators and trade.
        # In live mode, request CFD data for trading.
        # CFD is more useful in non-trading hours to reduce slippage when market prices jump at market opening.
        tickers = ["SPY", "BND", "GLD", "QQQ"]
        security_type = SecurityType.CFD if self.live_mode else SecurityType.EQUITY
        for ticker in tickers:
            # Create fast and slow-moving average indicators to trade SAM cross trend-following strategy.
            equity = self.add_equity(ticker, Resolution.DAILY)
            equity.sma_fast = self.sma(equity.symbol, 50)
            equity.sma_slow = self.sma(equity.symbol, 200)
            equity.target_vehicle = self.add_cfd(ticker, Resolution.DAILY, market=Market.INTERACTIVE_BROKERS).symbol if self.live_mode else equity.symbol

        # Create an schduled event to plot the prices.
        # We need to create a symbol representing the US Equity market
        # because market hours for IB CFDs are not modeled
        market = Symbol.create("SPY", SecurityType.EQUITY, Market.USA)
        self.schedule.on(self.date_rules.every_day(market), self.time_rules.midnight, self._plot_prices)

    def on_data(self, data: Slice) -> None:
        for security in self.securities.values():
            # Trade the SMA cross on the updated SMA indicator signals.
            # If fast MA is above slow MA, the price is in an uptrend, and we buy.
            if security.sma_fast > security.sma_slow:
                self.set_holdings(security.target_vehicle, 0.25)
            # Else, the price is in a downtrend, and we are short.
            else:
                self.set_holdings(security.target_vehicle, 0)

    def _plot_prices(self):
        for security in self.securities.values():
            self.plot(security.symbol.value, 'EOD', security.price)
```
