Forex
Handling Data
Introduction
LEAN passes the data you request to the OnData
on_data
method so you can make trading decisions. The default OnData
on_data
method accepts a Slice
object, but you can define additional OnData
on_data
methods that accept different data types. For example, if you define an OnData
on_data
method that accepts a QuoteBar
argument, it only receives QuoteBar
objects. The Slice
object that the OnData
on_data
method receives groups all the data together at a single moment in time. To access the Slice
outside of the OnData
on_data
method, use the CurrentSlice
current_slice
property of your algorithm.
All the data formats use DataDictionary
objects to group data by Symbol
and provide easy access to information. The plural of the type denotes the collection of objects. For instance, the QuoteBars
DataDictionary
is made up of QuoteBar
objects. To access individual data points in the dictionary, you can index the dictionary with the Forex pair ticker or Symbol
symbol
, but we recommend you use the Symbol
symbol
.
To view the resolutions that are available for Forex data, see Resolutions.
Quotes
QuoteBar
objects are bars that consolidate NBBO quotes from the exchanges. They contain the open, high, low, and close prices of the bid and ask. The Open
open
, High
high
, Low
low
, and Close
close
properties of the QuoteBar
object are the mean of the respective bid and ask prices. If the bid or ask portion of the QuoteBar
has no data, the Open
open
, High
high
, Low
low
, and Close
close
properties of the QuoteBar
copy the values of either the Bid
bid
or Ask
ask
instead of taking their mean.
To get the QuoteBar
objects in the Slice
, index the QuoteBars
property of the Slice
with the Forex pair Symbol
symbol
. If the Forex pair doesn't actively get quotes or you are in the same time step as when you added the Forex pair subscription, the Slice
may not contain data for your Symbol
symbol
. To avoid issues, check if the Slice
contains data for your Forex pair before you index the Slice
with the Forex pair Symbol
symbol
.
public override void OnData(Slice slice) { // Check if the symbol is contained in QuoteBars object if (slice.QuoteBars.ContainsKey(_symbol)) { // Obtain the mapped QuoteBar of the symbol var quoteBar = slice.QuoteBars[_symbol]; } }
def on_data(self, slice: Slice) -> None: # Obtain the mapped QuoteBar of the symbol if any quote_bar = slice.quote_bars.get(self._symbol) # None if not found
You can also iterate through the QuoteBars
dictionary. The keys of the dictionary are the Symbol
objects and the values are the QuoteBar
objects.
public override void OnData(Slice slice) { // Iterate all received Symbol-QuoteBar key-value pairs foreach (var kvp in slice.QuoteBars) { var symbol = kvp.Key; var quoteBar = kvp.Value; var askPrice = quoteBar.Ask.Close; } }
def on_data(self, slice: Slice) -> None: # Iterate all received Symbol-QuoteBar key-value pairs for symbol, quote_bar in slice.quote_bars.items(): ask_price = quote_bar.ask.close
QuoteBar
objects let LEAN incorporate spread costs into your simulated trade fills to make backtest results more realistic.
QuoteBar
objects have the following properties:
Ticks
Tick
objects represent a single trade or quote at a moment in time. A trade tick is a record of a transaction for the Forex pair. A quote tick is an offer to buy or sell the Forex pair at a specific price.
Trade ticks have a non-zero value for the Quantity
quantity
and Price
price
properties, but they have a zero value for the BidPrice
bid_price
, BidSize
bid_size
, AskPrice
ask_price
, and AskSize
ask_size
properties. Quote ticks have non-zero values for BidPrice
bid_price
and BidSize
bid_size
properties or have non-zero values for AskPrice
ask_price
and AskSize
ask_size
properties. To check if a tick is a trade or a quote, use the TickType
ticktype
property.
In backtests, LEAN groups ticks into one millisecond buckets. In live trading, LEAN groups ticks into ~70-millisecond buckets. To get the Tick
objects in the Slice
, index the Ticks
property of the Slice
with a Symbol
symbol
. If the Forex pair doesn't actively trade or you are in the same time step as when you added the Forex pair subscription, the Slice
may not contain data for your Symbol
symbol
. To avoid issues, check if the Slice
contains data for your Forex pair before you index the Slice
with the Forex pair Symbol
symbol
.
public override void OnData(Slice slice) { if (slice.Ticks.ContainsKey(_symbol)) { var ticks = slice.Ticks[_symbol]; foreach (var tick in ticks) { var price = tick.Price; } } }
def on_data(self, slice: Slice) -> None: ticks = slice.ticks.get(self._symbol, []) # Empty if not found for tick in ticks: price = tick.price
You can also iterate through the Ticks
dictionary. The keys of the dictionary are the Symbol
objects and the values are the List<Tick>
list[Tick]
objects.
public override void OnData(Slice slice) { foreach (var kvp in slice.Ticks) { var symbol = kvp.Key; var ticks = kvp.Value; foreach (var tick in ticks) { var price = tick.Price; } } }
def on_data(self, slice: Slice) -> None: for symbol, ticks in slice.ticks.items(): for tick in ticks: price = tick.price
Tick data is raw and unfiltered, so it can contain bad ticks that skew your trade results. For example, some ticks come from dark pools, which aren't tradable. We recommend you only use tick data if you understand the risks and are able to perform your own online tick filtering.
Tick
objects have the following properties:
Examples
The following examples demonstrate some common practices for handling Forex data.
Example 1: Trade the USDJPY Spread
USDJPY is a Forex pair where the reserve banks of the base currency and quote currency act at different time, which results in a 12-hour period of seasonal peak volume. To reduce slippage costs, place orders when bid-ask spread is relatively low. The following algorithm demonstrates how to utilize the Minimum indicator to check for a narrow spread.
public class ForexExampleAlgorithm : QCAlgorithm { private dynamic _forex; public override void Initialize() { SetStartDate(2004, 1, 1); // Add the USDJPY trading pair. _forex = AddForex("USDJPY"); // Create a Minimum indicator to track the lowest bid-ask spread for the past 12 hours. _forex.SpreadLow = new Minimum(12*60); // Warm up the indicator so it's immediately ready to use. WarmUpIndicator<IndicatorDataPoint>(_forex.Symbol, _forex.SpreadLow); } public override void OnData(Slice slice) { // Ensure we have quote data for USDJPY in the current slice. if (!slice.QuoteBars.ContainsKey(_forex.Symbol)) { return; } var quoteBar = slice.QuoteBars[_forex.Symbol]; // Bid-ask spread = Ask price - Bid price var bidAskSpread = quoteBar.Ask.Close - quoteBar.Bid.Close; // Update the spread minimum indicator to calculate the lowest bid-ask spread over the last 12 hours. _forex.SpreadLow.Update(quoteBar.EndTime, bidAskSpread); // Trade if the current spread is the lowest bid-ask spread, since it is the most efficient, // liquid price with lowest slippage. if (!Portfolio.Invested && bidAskSpread == _forex.SpreadLow.Current.Value) { MarketOrder(_forex.Symbol, 1000); } // Plot the bid-ask spread to validate the minimum bid-ask spread calculation. Plot("Bid-Ask Spread", "spread", bidAskSpread); } }
class ForexExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2004, 1, 1) # Add the USDJPY trading pair. self._forex = self.add_forex("USDJPY") # Create a Minimum indicator to track the lowest bid-ask spread for the past 12 hours. self._forex.spread_low = Minimum(12*60) # Warm up the indicator so it's immediately ready to use. self.warm_up_indicator(self._forex.symbol, self._forex.spread_low) def on_data(self, slice: Slice) -> None: # Ensure we have quote data for USDJPY in the current slice. if self._forex.symbol not in slice.quote_bars: return quote_bar = slice.quote_bars[self._forex.symbol] # Bid-ask spread = Ask price - Bid price bid_ask_spread = quote_bar.ask.close = quote_bar.bid.close # Update the spread minimum indicator to calculate the lowest bid-ask spread over the last 12 hours. self._forex.spread_low.update(quote_bar.end_time, bid_ask_spread) # Trade if the current spread is the lowest bid-ask spread, since it is the most efficient, # liquid price with lowest slippage. if not self.portfolio.invested and bid_ask_spread == self._forex.spread_low.current.value: self.market_order(self._forex.symbol, 1000) # Plot the bid-ask spread to validate the minimum bid-ask spread calculation. self.plot("Bid-Ask Spread", "spread", bid_ask_spread)
Example 2: 5-Minute EURUSD Trend Reversal
The following algorithm applies a mean reveral strategy to EURUSD. To smooth out the noise of minute resolution data, it consolidates the data into 5-minute bars and then calculates their 200-bar Exponential Moving Average (EMA). After every time the indicator updates, the algorithm checks for a trading opportuniity. When the current EURUSD price is below the EMA, it takes a long position. Otherwise, it takes a short position.
public class ForexExampleAlgorithm : QCAlgorithm { private dynamic _forex; public override void Initialize() { SetStartDate(2003, 1, 1); SetEndDate(2005, 1, 1); // Add EURUSD minute data. _forex = AddForex("EURUSD"); // Create an EMA indicator to estimate the trend of EURUSD. _forex.ema = new ExponentialMovingAverage(200); // Attach an event handler to the indicator to track its updates. (_forex.ema as ExponentialMovingAverage).Updated += OnUpdated; // Create a consolidator that produces 5-minute quote bars to reduce reduce noise. var consolidator = new QuoteBarConsolidator(TimeSpan.FromMinutes(5)); // Define the consolidator handler so it updates the EMA with the consoildated bars. consolidator.DataConsolidated += (_, bar) => _forex.ema.Update(bar.EndTime, bar.Close); // Warm up the indicator with historical data. foreach (var bar in History<QuoteBar>(_forex.Symbol, 1000)) { consolidator.Update(bar); } // Subscribe the consolidator for automatic updates. SubscriptionManager.AddConsolidator(_forex.Symbol, consolidator); } private void OnUpdated(object sender, IndicatorDataPoint point) { // Wait until the indicator is ready. if (!_forex.ema.IsReady) { return; } // Rebalance the portfolio. If the current price is below the EMA, long. Otherwise, short. SetHoldings(_forex.Symbol, _forex.Price <= point.Value ? 0.5m : -0.5m); } }
class ForexExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: self.set_start_date(2003, 1, 1) self.set_end_date(2005, 1, 1) # Add EURUSD minute data. self._forex = self.add_forex("EURUSD") # Create an EMA indicator to estimate the trend of EURUSD. self._forex.ema = ExponentialMovingAverage(200) # Attach an event handler to the indicator to track its updates. self._forex.ema.updated += self.on_updated # Create a consolidator that produces 5-minute quote bars to reduce reduce noise. consolidator = QuoteBarConsolidator(timedelta(minutes=5)) # Define the consolidator handler so it updates the EMA with the consoildated bars. consolidator.DataConsolidated += lambda _, bar: self._forex.ema.update(bar.end_time, bar.close) # Warm up the indicator with historical data. for bar in self.history[QuoteBar](self._forex.symbol, 1000): consolidator.update(bar) # Subscribe the consolidator for automatic updates. self.subscription_manager.add_consolidator(self._forex.symbol, consolidator) def on_updated(self, sender: object, point: IndicatorDataPoint) -> None: # Wait until the indicator is ready. if not self._forex.ema.is_ready: return # Rebalance the portfolio. If the current price is below the EMA, long. Otherwise, short. self.set_holdings(self._forex.symbol, 0.5 if self._forex.price <= point.value else -0.5)
Example 3: New York Time Alignment
The following algorithm creates daily Forex bars that close at 4:30 PM Eastern Time (ET). This technique is helpful when you want to align the daily bars with the close of the New York.
public class ForexExampleAlgorithm : QCAlgorithm { private Symbol _forex; public override void Initialize() { // Add EURUSD minute data. _forex = AddForex("EURUSD", Resolution.Minute, Market.Oanda).Symbol; // Create a daily bar consolidator with a custom period (4:30 PM-4:30 PM Eastern Time). var consolidator = new QuoteBarConsolidator(ConsolidationPeriod); // Attach a consolidation handler to plot the consolidated bars. consolidator.DataConsolidated += (_, bar) => Plot("Consolidated Bar", "Close", bar.Close); // Subscribe the consolidator for automatic updates. SubscriptionManager.AddConsolidator(_forex, consolidator); } private CalendarInfo _ConsolidationPeriod(DateTime dt) { // Set the start time to be 4:30 PM of the previous day. var start = dt.Date.AddHours(16).AddMinutes(30); var period = TimeSpan.FromDays(1); if (dt.Hour < 17) { start = start - period; } // Set the period to 1 day to make daily bars. return new CalendarInfo(start, period); } }
class ForexExampleAlgorithm(QCAlgorithm): def initialize(self) -> None: # Add EURUSD minute data. self._forex = self.add_forex("EURUSD") # Create a daily bar consolidator with a custom period (4:30 PM-4:30 PM Eastern Time). consolidator = QuoteBarConsolidator(self._consolidation_period) # Attach a consolidation handler to plot the consolidated bars. consolidator.data_consolidated += lambda _, bar: self.plot("Consolidated Bar", "Close", bar.close) # Subscribe the consolidator for automatic updates. self.subscription_manager.add_consolidator(self._forex.symbol, consolidator) def _consolidation_period(self, dt: datetime) -> CalendarInfo: # Set the start time to be 4:30 PM of the previous day. start = dt.replace(hour=16, minute=30, second=0, microsecond=0) period = timedelta(1) if dt.hour < 17: start -= period # Set the period to 1 day to make daily bars. return CalendarInfo(start, period)