Forex
Requesting Data
Introduction
Request Forex data in your algorithm to receive a feed of exchange rates in the OnDataon_data method. For more information about the specific dataset we use for backtests, see the FOREX dataset listing. To trade Forex live, you can use the QuantConnect data provider.
Create Subscriptions
To create a Forex pair subscription, in the Initializeinitialize method, call the AddForexadd_forex method. The AddForexadd_forex method returns a Forex object, which contains a Symbolsymbol property. Save a reference to the Symbolsymbol so you can use it in OnDataon_data to access the security data in the Slice.
_symbol = AddForex("EURUSD").Symbol; self._symbol = self.add_forex("EURUSD").symbol
To view the supported Forex pairs, see Supported Assets.
Resolutions
The following table shows the available resolutions and data formats for Forex subscriptions:
| Resolution | TradeBar | QuoteBar | Trade Tick | Quote Tick |
|---|---|---|---|---|
TickTICK | ![]() | |||
SecondSECOND | ![]() | |||
MinuteMINUTE | ![]() | |||
HourHOUR | ![]() | |||
DailyDAILY | ![]() |
The default resolution for Forex subscriptions is Resolution.MinuteResolution.MINUTE. To change the resolution, pass a resolution argument to the AddForexadd_forex method.
_symbol = AddForex("EURUSD", Resolution.Daily).Symbol; self._symbol = self.add_forex("EURUSD", Resolution.DAILY).symbol
To create custom resolution periods, see Consolidating Data.
Supported Markets
The only market available for Forex pairs is Market.OandaMarket.OANDA, so you don't need to pass a market argument to the AddForexadd_forex method.
_symbol = AddForex("EURUSD", market: Market.Oanda).Symbol; self._symbol = self.add_forex("EURUSD", 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 fillForwardfill_forward argument to false when you create the security subscription.
_symbol = AddForex("EURUSD", fillForward: false).Symbol; self._symbol = self.add_forex("EURUSD", fill_forward=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 Forex pair, pass a leverage argument to the AddForexadd_forex method.
_symbol = AddForex("EURUSD", leverage: 35).Symbol; self._symbol = self.add_forex("EURUSD", leverage=35).symbol
Data Normalization
The data normalization mode doesn't affect the data that LEAN passes to OnDataon_data or the data from history request. If you change the data normalization mode, it won't change the outcome.
Examples
The following examples demonstrate some common practices for requesting Forex data.
Example 1: Add All Forex Pairs
The following algorithm adds all the Forex pairs that trade on the
OANDA exchange
.
To get all of the pairs from the
Symbol Properties Database
, call the
SymbolPropertiesDatabase.GetSymbolPropertiesList
symbol_properties_database.get_symbol_properties_list
method.
Adding all of the pairs is particularly useful for spotting arbitrage opportunities among the pairs.
public class ForexExampleAlgorithm : QCAlgorithm
{
private List<Forex> _forexPairs;
public override void Initialize()
{
SetStartDate(2024, 9, 1);
SetEndDate(2024, 12, 31);
// Get all of the OANDA Forex pairs from the Symbol Properties Database.
_forexPairs = SymbolPropertiesDatabase.GetSymbolPropertiesList(Market.Oanda, SecurityType.Forex)
// Add all of the pairs. You don't need to specify the market because the Symbol object already does.
.Select(x => AddForex(x.Key.Symbol, Resolution.Daily))
.ToList();
}
} class ForexExampleAlgorithm(QCAlgorithm):
def initialize(self) -> None:
self.set_start_date(2024, 9, 1)
self.set_end_date(2024, 12, 31)
self._forex_pairs = [
# Add all of the pairs. You don't need to specify the market because the Symbol object already does.
self.add_forex(x.key.symbol, Resolution.DAILY)
# Get all of the OANDA Forex pairs from the Symbol Properties Database.
for x in self.symbol_properties_database.get_symbol_properties_list(Market.OANDA, SecurityType.FOREX)
]
