Bulk Forex History Requests with QuoteBar Slices
Summary
This example shows how to configure a forex algorithm and request historical data for several currency pairs. It subscribes to EUR/USD, GBP/USD, and EUR/GBP at minute resolution, then requests recent daily, hourly, minute, and short-interval second history. The results are iterated in time order and logged by symbol; incoming data is also logged from the update callback.
The central data-handling point is that forex history is represented by QuoteBars, so the example requests history as slices that can contain data for multiple symbols. The code demonstrates setup and inspection of historical and live data, rather than a trading signal or tested strategy. It gives no performance evidence, execution logic, or guidance on handling missing data, spreads, or time alignment, so it is best read as an API usage example.
Key ideas
- Forex history can be requested in bulk for multiple currency pairs.
- The example requests history at daily, hourly, minute, and second resolutions.
- Forex history uses QuoteBars and is accessed through data slices.
- Sorting the returned history by time makes its contents easier to inspect.
- The example logs data but does not define a trading strategy.
Tags
Full text
# BasicTemplateForexAlgorithm
# BasicTemplateForexAlgorithm
Algorithm demonstrating FOREX asset types and requesting history on them in bulk. As FOREX uses QuoteBars you should request slices
## Source (Apache-2.0)
```python
# QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
# Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from AlgorithmImports import *
### <summary>
### Algorithm demonstrating FOREX asset types and requesting history on them in bulk. As FOREX uses
### QuoteBars you should request slices
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="history and warm up" />
### <meta name="tag" content="history" />
### <meta name="tag" content="forex" />
class BasicTemplateForexAlgorithm(QCAlgorithm):
def initialize(self):
# Set the cash we'd like to use for our backtest
self.set_cash(100000)
# Start and end dates for the backtest.
self.set_start_date(2013, 10, 7)
self.set_end_date(2013, 10, 11)
# Add FOREX contract you want to trade
# find available contracts here https://www.quantconnect.com/data#forex/oanda/cfd
self.add_forex("EURUSD", Resolution.MINUTE)
self.add_forex("GBPUSD", Resolution.MINUTE)
self.add_forex("EURGBP", Resolution.MINUTE)
self.history(5, Resolution.DAILY)
self.history(5, Resolution.HOUR)
self.history(5, Resolution.MINUTE)
history = self.history(TimeSpan.from_seconds(5), Resolution.SECOND)
for data in sorted(history, key=lambda x: x.time):
for key in data.keys():
self.log(str(key.value) + ": " + str(data.time) + " > " + str(data[key].value))
def on_data(self, data):
# Print to console to verify that data is coming in
for key in data.keys():
self.log(str(key.value) + ": " + str(data.time) + " > " + str(data[key].value))
```Shown in full with attribution under the source's licence. Licence: Apache-2.0
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.