Acess previous weights
-
Hello all,
I hope everyone is doing well.
I have a question regarding the access of previous weights during the backtesting/prediction phase. The following is my code:
I would like to include the last row (commented out) to improve the weights, but as my code predicts each time step I think the previous predictions are not saved.
Is there a better way to write the predictions or in general have the previous weights stored, in order to apply the lower slippage function?
thx a lot.
Regards -
@magenta-kabuto
Hello.https://github.com/quantiacs/strategy-ml_lstm_multiple_features/blob/master/strategy.ipynb
For this example, it would look like this.
weights = qnbt.backtest_ml( load_data=load_data, train=train_model, predict=predict, train_period=train_period, retrain_interval=360, retrain_interval_after_submit=1, predict_each_day=False, competition_type='stocks_nasdaq100', lookback_period=lookback_period, start_date='2006-01-01', build_plots=True ) import qnt.data as qndata import qnt.stats as qns import qnt.graph as qngraph data = qndata.stocks.load_ndx_data(min_date="2006-01-01") def get_lower_slippage(weights, rolling_time=6): return weights.rolling({"time": rolling_time}).max() improved_weights = get_lower_slippage(weights, rolling_time=6) stats = qns.calc_stat(data, improved_weights.sel(time=slice("2006-01-01", None))) display(stats.to_pandas().tail()) performance = stats.to_pandas()["equity"] qngraph.make_plot_filled(performance.index, performance, name="PnL (Equity)", type="log") qnout.write(improved_weights) # To participate in the competition, save this code in a separate cell.
-
Hi @vyacheslav_b,
thx for the solution.
Are you aware of a way to access previous positions taken, when using single pass, as the qnbt backtester leads to a runtime error?
Regards -
or if not. Is there code available of the competition backtest, so I may figure out a way.
Thx -
@magenta-kabuto Hello. I didn't understand your question. If you need portfolio weights for the previous day, you can use
weights.shift(time=1)
import qnt.data as qndata data = qndata.stocks.load_ndx_data(min_date="2005-01-01", assets=['NAS:GOOGL']) close = data.sel(field="close") weights = close - close.shift(time=1) weights_previous = weights.shift(time=1)
-
Hi @vyacheslav_b , thx for your reply.
Sry I expressed myself badly. What I mean is that as I understand if I predict each time step, my for example machine learning model, will take a position in t (lets say 0.5). Now in t+1 when the notebook is run again for prediction that information is lost ,isnt it? So if I want to apply the lower slippage, how can I do that?
An example is the screenshot I posted above, which makes a one step prediction, by assigning a weight for selected assets on the latest index at time step t. No tomorrow in t+1 it will assign a new weight for the selcted assets without knowledge what was assigned in t with the knowledge in t, as in t+1 I could take the value of the prediction for (which is part of the batch) but will be different from the weights I assigned in t because of forward looking.
I hope I didnt overcomplicate in my expression.
Regards -
Hey @support,
can you maybe help?
Is there a way to download or access weights like its done for data? (which is updated for each time step)
Thank you.
Regards -
@magenta-kabuto Hi, I am not sure I understand you correctly, maybe you can use the stateful version of the backtester as we described here:
https://quantiacs.com/documentation/en/reference/evaluation.html#stateful-multi-pass-backtesting
which will preserve in memory previous state.