Share the state between iterations
-
related topic: Please advise on p settings. Thanks.
-
This feature has been implemented.
See: https://quantiacs.com/documentation/en/reference/evaluation.html#stateful-multi-pass-backtesting -
@support
Please advise what to do here. Thanks.--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-434dffafaefc> in <module> 50 analyze=True, 51 build_plots=True, ---> 52 collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states 53 ) TypeError: backtest() got an unexpected keyword argument 'collect_all_states'
-
@spancham
Helllo.Update the library, please. run this in you environmnent:
conda install 'quantiacs-source:qnt>=0.0.250'
Regards.
-
Hi @support
Thanks but I tried that and it still did not work.
Just so you understand, I do NOT work locally, I only work in the Quantiacs online cloud Jupyter env.
I ran the update you suggested in my Quantiacs online Jupyter and it still did not work. I'm guessing you guys keep the env updated though.
I'm still getting the same error:
TypeError: backtest() got an unexpected keyword argument 'collect_all_states'Any suggestions pls?
-
Hello. Sorry, there was a typo. Try this:
conda install 'quantiacs-source::qnt>=0.0.250' -n qntdev
(double ':' )
Regards.
-
@support
Sorry friend, that still does not work.
I am working in the Quantiacs online cloud Jupyter environment.
I am NOT working locally.
I copied your example template as is and just added load_data().
Below is what I am running:import xarray as xr import numpy as np import qnt.data as qndata import qnt.backtester as qnbt def load_data(period): data = qndata.futures_load_data(tail=period) return data def strategy(data, state): close = data.sel(field="close") last_close = close.ffill('time').isel(time=-1) # state may be null, so define a default value if state is None: state = { "ma": last_close, "ma_prev": last_close, "output": xr.zeros_like(last_close) } ma_prev = state['ma'] ma_prev_prev = state['ma_prev'] output_prev = state['output'] # align the arrays to prevent problems in case the asset list changes ma_prev, ma_prev_prev, last_close = xr.align(ma_prev, ma_prev_prev, last_close, join='right') ma = ma_prev.where(np.isfinite(ma_prev), last_close) * 0.97 + last_close * 0.03 buy_signal = np.logical_and(ma > ma_prev, ma_prev > ma_prev_prev) stop_signal = ma < ma_prev_prev output = xr.where(buy_signal, 1, output_prev) output = xr.where(stop_signal, 0, output) next_state = { "ma": ma, "ma_prev": ma_prev, "output": output } return output, next_state weights, state = qnbt.backtest( competition_type="futures", # Futures contest lookback_period=365, # lookback in calendar days start_date="2006-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 )
Error I'm getting is:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-434dffafaefc> in <module> 50 analyze=True, 51 build_plots=True, ---> 52 collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states 53 ) TypeError: backtest() got an unexpected keyword argument 'collect_all_states'
The reason I want to get the 'state' working is that I have a pairs trading strategy that needs it.
Any help would be greatly appreciated.
Thanks.In a related issue, when I run the macroeconomic example, I get the following error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-1-62eeefb4e65f> in <module> 76 strategy=strategy, 77 analyze=True, ---> 78 build_plots=True 79 ) ~/book/qnt/backtester.py in backtest(competition_type, strategy, load_data, lookback_period, test_period, start_date, window, step, analyze, build_plots) 66 data, time_series = extract_time_series(data) 67 print("Run pass...") ---> 68 result = strategy(data) 69 if result is None: 70 log_err("ERROR! Strategy output is None!") TypeError: strategy() missing 1 required positional argument: 'state'
-
Hello.
I guess there is a warning on the strategy "The strategy uses outdated libraries. Clone it to update the libraries."
Just clone this strategy, it will create a new environment with the latest library.
Regards.
-
@support
I am working in the Quantiacs online env in my account, why doesn't it catch the latest libraries?
Anyway, I created a new Jupyter notebook with the strategy as suggested and it still does NOT work, same error msg. -
@support
ok I did what @antinomy said below and the 'state' strategy worked.
https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/3?_=1619554449031However, it broke my old strategies. So he further suggested to:
https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/5?_=1619556376479
And my old strategies are running again.Ok, so looks like for now for all strategies without a state I have to output and pass None to weights for the state & pass a state variable to the strategy.
Will keep you updated. Thanks for looking into the issue. -
This post is deleted!