Requesting Data


Request Contract for Difference (CFD) data in your algorithm to receive a feed of contract prices in the OnData method. For more information about the specific dataset we use for backtests, see the CFD dataset listing. To trade CFDs live, you can use our CFD data feed.

Create Subscriptions

To create a CFD subscription, in the Initialize method, call the AddCfd method. The AddCfd method returns a Cfd object, which contains a Symbol. Save a reference to the Symbol so you can use it in OnData to access the security data in the Slice.

_symbol = AddCfd("XAUUSD").Symbol;
self.symbol = self.AddCfd("XAUUSD").Symbol

To view the supported CFD contracts, see Supported Assets.


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

ResolutionTradeBarQuoteBarTrade TickQuote Tick
Tickgreen check
green check
green check
green check
green check

The default resolution for CFD subscriptions is Resolution.Minute. To change the resolution, pass a resolution argument to the AddCfd method.

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

To create custom resolution periods, see Consolidating Data.

Supported Markets

The only market available for CFD contracts is Market.Oanda, so you don't need to pass a market argument to the AddCfd method.

_symbol = AddCfd("XAUUSD", market: Market.Oanda).Symbol;
self.symbol = self.AddCfd("XAUUSD", market=Market.Oanda).Symbol

The brokerage models have a default market for each asset class. If you set a brokerage model, you may not need to specify the market to use.

Fill Forward

Fill forward means if there is no data point for the current slice, LEAN uses the previous data point. Fill forward is the default data setting. If you disable fill forward, you may get stale fills or you may see trade volume as zero.

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

_symbol = AddCfd("XAUUSD", fillForward: false).Symbol;
self.symbol = self.AddCfd("XAUUSD", fillForward=False).Symbol

Margin and Leverage

LEAN models buying power and margin calls to ensure your algorithm stays within the margin requirements. The Oanda brokerage let's you use up to 50x leverage on margin accounts. To change the amount of leverage you can use for a CFD contract, pass a leverage argument to the AddCfd method.

_symbol = AddCfd("XAUUSD", leverage: 35).Symbol;
self.symbol = self.AddCfd("XAUUSD", leverage=35).Symbol

Data Normalization

The data normalization mode doesn't affect the data that LEAN passes to OnData or the data from history request. If you change the data normalization mode, it won't change the outcome.


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

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: