Hello colleagues.

The solution in case of predicting one financial instrument can be the following (train_period changed)

def load_data(period):
return qndata.cryptodaily_load_data(tail=period, assets=['BTC'])
def train_model(data):
"""
train the LSTM network
"""
asset_name = 'BTC'
features_all = get_features(data)
target_all = get_target_classes(data)
model = get_model()
# drop missing values:
target_cur = target_all.sel(asset=asset_name).dropna('time', 'any')
features_cur = features_all.sel(asset=asset_name).dropna('time', 'any')
# align features and targets:
target_for_learn_df, feature_for_learn_df = xr.align(target_cur, features_cur, join='inner')
criterion = nn.MSELoss() # define loss function
optimiser = optim.LBFGS(model.parameters(), lr=0.08) # we use an LBFGS solver as optimiser
epochs = 1 # how many epochs
for i in range(epochs):
def closure(): # reevaluates the model and returns the loss (forward pass)
optimiser.zero_grad()
# input tensor
in_ = torch.zeros(1, len(feature_for_learn_df.values))
in_[0, :] = torch.tensor(np.array(feature_for_learn_df.values))
# output
out = model(in_)
# target tensor
target = torch.zeros(1, len(target_for_learn_df.values))
target[0, :] = torch.tensor(np.array(target_for_learn_df.values))
# evaluate loss
loss = criterion(out, target)
loss.backward()
return loss
optimiser.step(closure) # updates weights
return model
weights = qnbt.backtest_ml(
load_data=load_data,
train=train_model,
predict=predict,
train_period=1 * 365, # the data length for training in calendar days
retrain_interval=365, # how often we have to retrain models (calendar days)
retrain_interval_after_submit=1, # how often retrain models after submission during evaluation (calendar days)
predict_each_day=False, # Is it necessary to call prediction for every day during backtesting?
# Set it to true if you suspect that get_features is looking forward.
competition_type='crypto_daily_long_short', # competition type
lookback_period=365, # how many calendar days are needed by the predict function to generate the output
start_date='2014-01-01', # backtest start date
build_plots=True # do you need the chart?
)