Thanks @dark-yellowjacket for post your solution for the optimizer, it works fine for me but could you please go deeper explaining how do you implement the optimized obtained parameter values in the final strategy. I have tried several methods (even in several computers) , obtaining always errors.
For example using this template provided by Quantiacs (where i call to "Strategy" I use the same previouslly optimized, and the config.json is the output asset by asset obtained after optimization):
import json
import xarray as xr
import qnt.backtester as qnbk
from Strategy import *
def optmized_strategy(data, config):
results = []
for c in config:
results.append(strategy_long(data, **c))
# align and join results
results = xr.align(*results, join='outer')
results = [r.fillna(0) for r in results]
output = sum(results) / len(results)
return output
config = json.load(open('config.json', 'r'))
# multi-pass
# It may look slow, but it is ok. The evaluator will run only one iteration per day.
qnbk.backtest(
competition_type='stocks_nasdaq100',
lookback_period=365,
strategy=lambda d: optmized_strategy(d, config),
# strategy=strategy_long, # you can check the base strategy too
start_date='2006-01-01')
It rises the following error:
Reloaded modules: Estrategia
fetched chunk 1/5 0s
fetched chunk 2/5 0s
fetched chunk 3/5 0s
fetched chunk 4/5 0s
fetched chunk 5/5 0s
Data loaded 1s
Run last pass...
Load data...
fetched chunk 1/1 0s
Data loaded 0s
Run strategy...
Traceback (most recent call last):
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\spyder_kernels\py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "c:\users\luispc\desktop\quantiacs\q18\prueba.py", line 39, in <module>
start_date='2006-01-01')
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\qnt\backtester.py", line 291, in backtest
result = strategy_wrap(data, state)
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\qnt\backtester.py", line 270, in <lambda>
strategy_wrap = (lambda d, s: strategy(d)) if args_count < 2 else strategy
File "c:\users\luispc\desktop\quantiacs\q18\prueba.py", line 37, in <lambda>
strategy=lambda d: optmized_strategy(d, config),
File "c:\users\luispc\desktop\quantiacs\q18\prueba.py", line 22, in optmized_strategy
results.append(strategy_long(data, **c))
File "C:\Users\LuisPC\Desktop\quantiacs\Q18\Estrategia.py", line 16, in strategy_long
data = data.sel(asset=[asset])
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\xarray\core\dataarray.py", line 1337, in sel
**indexers_kwargs,
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\xarray\core\dataset.py", line 2505, in sel
self, indexers=indexers, method=method, tolerance=tolerance
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\xarray\core\coordinates.py", line 422, in remap_label_indexers
obj, v_indexers, method=method, tolerance=tolerance
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\xarray\core\indexing.py", line 120, in remap_label_indexers
idxr, new_idx = index.query(labels, method=method, tolerance=tolerance)
File "C:\Users\LuisPC\.conda\envs\qntdev\lib\site-packages\xarray\core\indexes.py", line 242, in query
raise KeyError(f"not all values found in index {coord_name!r}")
KeyError: "not all values found in index 'asset'"
I tried to explain this problem about the implementation in the Backtester in another forum thread time ago, without getting a valid answer, therefore I would appreciate any idea you can give me on this matter.
...Otherwise I am afraid that I will not present algorithms for this contest (Sadly 😢 )
Regards.
Luis G.