QuantConnect

CFD Data

Introduction

The CFD Data by QuantConnect serves 51 contracts for differences (CFD). The data starts as early as May 2002 and is delivered on any frequency from tick to daily. This dataset is created by QuantConnect processing raw tick data from OANDA.

For more information about the CFD Data dataset, including CLI commands and pricing, see the dataset listing.

About the Provider

QuantConnect was founded in 2012 to serve quants everywhere with the best possible algorithmic trading technology. Seeking to disrupt a notoriously closed-source industry, QuantConnect takes a radically open-source approach to algorithmic trading. Through the QuantConnect web platform, more than 50,000 quants are served every month.

Getting Started

The following snippet demonstrates how to request data from the CFD dataset:

self.xauusd = self.add_cfd("XAUUSD", Resolution.DAILY).symbol
_symbol = AddCfd("XAUUSD", Resolution.Daily).Symbol;

Data Summary

The following table describes the dataset properties:

PropertyValue
Start DateMixed, earliest starts May 2002
Asset Coverage51 Contracts
Data DensityDense
ResolutionTick, Second, Minute, Hour, & Daily
TimezoneMixed, in which the contract is listed*
Market HoursAlways Open, except from Friday 5 PM EST to Sunday 5 PM EST.
Index CFDs depends on the underlying market hour*

* E.g.: DE30EUR tracks DAX30 Index, which is listed in Europe/Berlin timezone.

Example Applications

The CFD price data enables you to trade CFD assets in your algorithm. Examples include the following strategies:

  • Exploring the daily worldwide news cycles with CFDs that track international indices.
  • Trading price movements of commodities with no delivery of physical goods. For example, pairs trading between gold and silver, corn and wheat, brent and crude oil, etc.

For more example algorithms, see Examples.

Data Point Attributes

The CFD dataset provides QuoteBar and Tick objects.

QuoteBar Attributes

QuoteBar objects have the following attributes:

Tick Attributes

Tick objects have the following attributes:

Supported Assets

The following table shows the available contracts:

Requesting Data

To add CFD data to your algorithm, call the AddCfdadd_cfd method. Save a reference to the CFD Symbol so you can access the data later in your algorithm.

class CfdAlgorithm (QCAlgorithm):
    def initialize(self) -> None:
        self.set_account_currency('EUR');

        self.set_start_date(2019, 2, 20)
        self.set_end_date(2019, 2, 21)
        self.set_cash('EUR', 100000)

        self.de30eur = self.add_cfd('DE30EUR').symbol

        self.set_benchmark(self.de30eur)
namespace QuantConnect.Algorithm.CSharp
{
    public class CfdAlgorithm : QCAlgorithm
    {
        private Symbol _symbol;

        public override void Initialize()
        {
            SetAccountCurrency("EUR");

            SetStartDate(2019, 2, 20);
            SetEndDate(2019, 2, 21);
            SetCash("EUR", 100000);

            _symbol = AddCfd("DE30EUR").Symbol;

            SetBenchmark(_symbol);
        }
    }
}

For more information about creating CFD subscriptions, see Requesting Data.

Accessing Data

To get the current CFD data, index the QuoteBarsquote_bars, or Ticksticks properties of the current Slice with the CFD Symbol. Slice objects deliver unique events to your algorithm as they happen, but the Slice may not contain data for your security at every time step. To avoid issues, check if the Slice contains the data you want before you index it.

def on_data(self, slice: Slice) -> None:
    if self.de30eur in slice.quote_bars:
        quote_bar = slice.quote_bars[self.de30eur]
        self.log(f"{self.de30eur} bid at {slice.time}: {quote_bar.bid.close}")

    if self.de30eur in slice.ticks:
        ticks = slice.ticks[self.de30eur]
        for tick in ticks:
            self.log(f"{self.de30eur} price at {slice.time}: {tick.price}")
public override void OnData(Slice slice)
{
    if (slice.QuoteBars.ContainsKey(_symbol))
    {
        var quoteBar = slice.QuoteBars[_symbol];
        Log($"{_symbol} bid at {slice.Time}: {quoteBar.Bid.Close}");
    }

    if (slice.Ticks.ContainsKey(_symbol))
    {
        var ticks = slice.Ticks[_symbol];
        foreach (var tick in ticks)
        {
            Log($"{_symbol} price at {slice.Time}: {tick.Price}");
        }
    }
}

You can also iterate through all of the data objects in the current Slice.

def on_data(self, slice: Slice) -> None:
    for symbol, quote_bar in slice.quote_bars.items():
        self.log(f"{symbol} bid at {slice.time}: {quote_bar.bid.close}")

    for symbol, ticks in slice.ticks.items():
        for tick in ticks:
            self.log(f"{symbol} price at {slice.time}: {tick.price}")
public override void OnData(Slice slice)
{
    foreach (var kvp in slice.QuoteBars)
    {
        var symbol = kvp.Key;
        var quoteBar = kvp.Value;
        Log($"{symbol} bid at {slice.Time}: {quoteBar.Bid.Close}");
    }

    foreach (var kvp in slice.Ticks)
    {
        var symbol = kvp.Key;
        var ticks = kvp.Value;
        foreach (var tick in ticks)
        {
            Log($"{symbol} price at {slice.Time}: {tick.Price}");
        }
    }
}

For more information about accessing CFD data, see Handling Data.

Historical Data

To get historical CFD data, call the Historyhistory method with the CFD Symbol. If there is no data in the period you request, the history result is empty.

# DataFrame
history_df = self.history(self.de30eur, 100, Resolution.MINUTE)

# QuoteBar objects
history_quote_bars = self.history[QuoteBar](self.de30eur, 100, Resolution.MINUTE)

# Tick objects
history_ticks = self.history[Tick](self.de30eur, timedelta(seconds=10), Resolution.TICK)
// QuoteBar objects 
var historyQuoteBars = History<QuoteBar>(_symbol, 100, Resolution.Minute);

// Tick objects 
var historyTicks = History<Tick>(_symbol, TimeSpan.FromSeconds(10), Resolution.Tick);

For more information about historical data, see History Requests.

Remove Subscriptions

To remove a CFD subscription, call the RemoveSecurityremove_security method.

self.remove_security(self.de30eur)
RemoveSecurity(_symbol);

The RemoveSecurityremove_security method cancels your open orders for the security and liquidates your holdings.

Example Applications

The CFD price data enables you to trade CFD assets in your algorithm. Examples include the following strategies:

  • Exploring the daily worldwide news cycles with CFDs that track international indices.
  • Trading price movements of commodities with no delivery of physical goods. For example, pairs trading between gold and silver, corn and wheat, brent and crude oil, etc.

For more example algorithms, see Examples.

You can also see our Videos. You can also get in touch with us via Discord.

Did you find this page helpful?

Contribute to the documentation: