About CFD Data

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.


About QuantConnect

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.


About QuantConnect

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.


Algorithm Example

from AlgorithmImports import *
from QuantConnect.DataSource import *

class SMAPairsTrading(QCAlgorithm):

    def initialize(self) -> None:
        self.set_start_date(2018, 7, 1)   
        self.set_end_date(2019, 3, 31)
        self.set_cash(100000)

        # Request gold and sliver spot CFDs for trading their spread difference, assuming their spread series is cointegrated
        self.add_cfd('XAUUSD', Resolution.HOUR)
        self.add_cfd('XAGUSD', Resolution.HOUR)

        # Use 500-step mean and SD indicator on determine the spread relative difference for trading signal generation
        self.pair = [ ]
        self.spread_mean = SimpleMovingAverage(500)
        self.spread_std = StandardDeviation(500)
        
    def on_data(self, slice: Slice) -> None:
        # Update the indicator with updated spread difference, such that the an updated cointegration threshold is calculated for trade inception
        spread = self.pair[1].price - self.pair[0].price
        self.spread_mean.update(self.time, spread)
        self.spread_std.update(self.time, spread) 
        
        spread_mean = self.spread_mean.current.value
        upperthreshold = spread_mean  + self.spread_std.current.value
        lowerthreshold = spread_mean  - self.spread_std.current.value

        # If the spread is higher than upper threshold, bet their spread series will revert to mean
        if spread > upperthreshold:
            self.set_holdings(self.pair[0].symbol, 1)
            self.set_holdings(self.pair[1].symbol, -1)
        elif spread < lowerthreshold:
            self.set_holdings(self.pair[0].symbol, -1)
            self.set_holdings(self.pair[1].symbol, 1)
        # Close positions if mean reverted
        elif (self.portfolio[self.pair[0].symbol].quantity > 0 and spread < spread_mean)\
        or (self.portfolio[self.pair[0].symbol].quantity < 0 and spread > spread_mean):
            self.liquidate()
    
    def on_securities_changed(self, changes: SecurityChanges) -> None:
        self.pair = [x for x in changes.added_securities]
        
        #1. Call for 500 bars of history data for each symbol in the pair and save to the variable history
        history = self.history([x.symbol for x in self.pair], 500)
        #2. Unstack the Pandas data frame to reduce it to the history close price
        history = history.close.unstack(level=0)
        #3. Iterate through the history tuple and update the mean and standard deviation with historical data 
        for tuple in history.itertuples():
            self.spread_mean.update(tuple[0], tuple[2]-tuple[1])
            self.spread_std.update(tuple[0], tuple[2]-tuple[1])

Example Applications

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