Backtesting
-
Hello
An example using pandas
One can work with pandas DataFrames at intermediate steps and at the end convert them to xarray data structures:def get_price_pct_change(prices): prices_pandas = prices.to_pandas() assets = data.coords["asset"].values for asset in assets: prices_pandas[asset] = prices_pandas[asset].pct_change() return prices_pandas prices = data.sel(field="close") * 1.0 prices_pct_change = get_price_pct_change(prices).unstack().to_xarray()
-
@vyacheslav_b Thank you
-
@magenta-kabuto Hi, maybe this can help with your strategy also:
In your function regime_trade() before returning signals_df, convert it to pandas.Series structure, and name it to corresponding asset (e.g. "NAS: AAPL").series = signals_df.squeeze().rename(asset_name) return series
After putting it to trades = dict(), you can concatenate trades.values(), which will create pandas.DataFrame of signals by assets:
pd_signals = pd.concat(trades.values(), axis=1)
Then, convert it to xarray.DataArray and pass it as weights to backtester.
import xarray as xr xr_signals = xr.DataArray(pd_signals, dims=('time', 'asset'))
-
@stefanm Thank you very much, that was extremely helpful. Without your help I would probably be still trying to figure out a way
-
@magenta-kabuto
First of all thx again for the help. Single-Pass Backtesting works fine and I am pretty happy with the result. Multiple-Pass Backtesting however returns the error shown in the screenshot. I assume that this problem occurs because load data, an xArray DataArray is passed into the function that inputs pandas. IS this right ?And if yes, can I avoid it without having to rewrite the algo or can I skip Multiple-Pass backtest as my algo doesnt look into the future? Very grateful, if anyone can help. Regards -
@magenta-kabuto And what is the exact competition name ?
-
@magenta-kabuto Hi, yes that's right, it seems that xarray DataArray is passed to function which expects pandas DataFrame (your entire algorithm is not visible, which is ok, so this is an assumption), but maybe you can try with this:
### use your regime_trade(Stockdata, param_2=0.15) as helper function def strategy(data): # param data: the data returned from load_data function, xarray.DataArray structure. Stockdata = ... # prepare data for regime_trade input, like you did for single pass trades = {} for j in logopenmod.keys(): trades[j] = regime_trade(Stockdata[j].iloc[:,3], 0.15) pd_signals = pd.concat(trades.values(), axis=1) xr_signals = xr.DataArray(pd_signals, dims=('time', 'asset')) is_liquid = data.sel(field="is_liquid") # assume that "stocks" is exactly the same as data is return xr_signals * is_liquid
Try it with Multi-pass, and change the name in competition_type. Set the start date as below:
weights = qnbk.backtest( competition_type = "stocks_nasdaq100", load_data = load_data, # if omitted it loads data by competition_type lookback_period = 365, start_date = "2006-01-01", # set start_date strategy = strategy, analyze = True, )
Regards
-
@stefanm I cant thank you enough. A very elegant way. The backtest atleast gets started but after approx. 20 seconds: the following occurs:
Sry for being so annoying, the conversion is an extreme blindspot to me and if it takes too much time, pls focus on your strategy instead of mine -
@magenta-kabuto No problem, you're welcome. Can you please change the start_date to '2006-01-01' when running backtester, and let us know if it worked?
-
@stefanm Finally worked. For whatever reason I had to write the trade library and the loop outside the strategy function, as the output was otherwise empty. (Tried all sorts of spacing to get it inside the stratety function but didnt work). Good luck with your strategy if you are taking part in the contest too!
-
This post is deleted! -
This post is deleted! -
@stefanm Thank you!