Gold Futures Signals Confirmed by IMF Gold Data
Summary
This example describes a simple long-only gold futures signal that combines futures prices with an IMF gold commodity series. It loads gold futures and commodity data, evaluates them in rolling windows, and returns a positive position when the latest commodity reading is above the previous reading and the futures close is above its close twenty observations earlier. Otherwise, it returns zero exposure. The example configures a futures backtest using a 365-day lookback and begins evaluation in 2006.
The document provides implementation details and backtest configuration, but no performance statistics, charts, or comparison against a benchmark. It does not describe short entries, position sizing, stop losses, transaction costs, or how the different data series’ timing and release schedules are handled. The signal is therefore best read as a basic demonstration of combining macroeconomic commodity data with a price trend condition, not as evidence that the approach is profitable or robust.
Key ideas
- The strategy takes a long position only when the latest IMF gold value rises and gold futures are above their level twenty observations earlier.
- It combines an external commodity series with futures market prices.
- The example uses rolling evaluation windows and a 365-day lookback.
- The document supplies no reported performance results or explicit risk controls.
Tags
Full text
# strategy-futures-commodity
# strategy-futures-commodity
## Source (MIT)
```python
# # Using IMF Commodity Data
#
# The [International Monetary Fund (IMF)](https://www.imf.org) publishes a range of time series data on IMF lending, exchange rates and other economic and financial indicators.
#
# In this template we show how to to use commodity data for developing a trading algorithm.
#
# **Need help?** Check the [**Documentation**](https://quantiacs.com/documentation/en/) and find solutions/report problems in the [**Forum**](https://quantiacs.com/community/categories) section.
#
# **More help with Jupyter?** Check the official [**Jupyter**](https://jupyter.org/) page.
#
# **Documentation on the IMF data** can be found [**here**](https://github.com/quantiacs/documentation/blob/master/en/source/user_guide/data.md).
#
# Once you are done, click on **Submit to the contest** and take part to our competitions.
#
# API reference:
#
# * **data**: check how to work with [data](https://quantiacs.com/documentation/en/reference/data_load_functions.html);
#
# * **backtesting**: read how to run the [simulation](https://quantiacs.com/documentation/en/reference/evaluation.html) and check the results.
#
# In this template we use the optimizer function described in:
#
# * **optimization**: read more on our [article](https://quantiacs.com/community/topic/29/optimizing-and-monitoring-a-trading-system-with-quantiacs).
%%javascript
window.IPython && (IPython.OutputArea.prototype._should_scroll = function(lines) { return false; })
// disable widget scrolling
import xarray as xr
import numpy as np
import pandas as pd
import qnt.backtester as qnbt
import qnt.data as qndata
# commodity listing
commodity_list = qndata.imf_load_commodity_list()
pd.DataFrame(commodity_list)
def load_data(period):
# load Futures Gold and Gold data:
futures = qndata.futures_load_data(assets=['F_GC'], tail=period, dims=('time','field','asset'))
commodity = qndata.imf_load_commodity_data(assets=['PGOLD'], tail=period).isel(asset=0)
return dict(commodity=commodity, futures=futures), futures.time.values
def window(data, max_date: np.datetime64, lookback_period: int):
# build sliding window for rolling evaluation:
min_date = max_date - np.timedelta64(lookback_period, 'D')
return dict(
futures = data['futures'].sel(time=slice(min_date, max_date)),
commodity = data['commodity'].sel(time=slice(min_date, max_date))
)
def strategy(data):
# strategy uses both Futures Gold and Gold data:
close = data['futures'].sel(field='close')
commodity = data['commodity']
if commodity.isel(time=-1) > commodity.isel(time=-2) and close.isel(time=-1) > close.isel(time=-20):
return xr.ones_like(close.isel(time=-1))
else:
return xr.zeros_like(close.isel(time=-1))
weights = qnbt.backtest(
competition_type='futures',
load_data=load_data,
window=window,
lookback_period=365,
start_date='2006-01-01',
strategy=strategy,
analyze=True,
build_plots=True
)
```Shown in full with attribution under the source's licence. Licence: MIT
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.