Hello @support,
I was wondering if I can request your assistance in converting the code below to work on the Futures dataset. (I omitted & changed a few things to not reveal the true strategy. But this is the basic idea.)
It currently works on the Crypto dataset.
I'm posting the crypto version as the attempts I made to convert it to work on Futures are a big mess.
Basically, I changed the load_data and competition type to futures and removed the markets = ['BTC'] line.
I also removed from the output: dims=['asset'], coords=dict(asset=markets)
But still no luck getting it to run on Futures.
Can you take a look pls when you have a moment.
Thanks.
import xarray as xr
import numpy as np
import qnt.data as qndata
import qnt.backtester as qnbt
import qnt.ta as qnta
def load_data(period):
data = qndata.cryptofutures_load_data(tail=period)
return data
def strategy(data, state):
markets = ['BTC']
zthreshold = 2.0
lookback_trading_days = 20
close = data.sel(field='close')
last_close = close.isel(time=-1)
high = data.sel(field='high')
low = data.sel(field='low')
# state may be null, so define a default value
prev_output = state
if prev_output is None:
prev_output = xr.zeros_like(last_close)
s1 = close[-lookback_trading_days:]
# Compute mean of the spread up to now
mvavg = np.mean(np.log(s1))
# Compute stdev of the spread up to now
stdev = np.std(np.log(s1))
# Compute current spread
current_spread = np.log(s1[-1])
# Compute z-score
zscore = (current_spread - mvavg) / stdev if stdev > 0 else 0
output = prev_output
if prev_output == [1] and zscore[-1] <= -zthreshold:
output = [0]
elif prev_output == [-1] and zscore[-1] >= -zthreshold:
output = [0]
elif zscore[-1] <= zthreshold:
output = [1]
elif zscore[-1] >= -zthreshold:
output = [-1]
else:
output = prev_output
return xr.DataArray(output, dims=['asset'], coords=dict(asset=markets)), output
weights, state = qnbt.backtest(
competition_type="cryptofutures",
lookback_period=200, # lookback in calendar days
start_date= "2014-01-01",
strategy=strategy,
analyze=True,
build_plots=True,
collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
)