Hello, I just found a new example about stateful long short. Link here:
https://github.com/quantiacs/strategy-stateful_long_short_with_exits/blob/master/strategy.ipynb
I would like to ask how to convert from multi backtest to single backtest and submit to the contest? Besides, if we keep the multi backtest, how to submit? Thank you.
Posts made by newbiequant96
-
How to submit stateful long short
-
RE: Improving Quantiacs: Aligning Developer Objectives with the ones of Quantiacs
@eddiee Hi, Mr. Eddie.
I am new to building strategies using ML/DL on Quantiacs and am very impressed with the OS performance of your ML strategies. I hope you can give me your contact (mail, limkedin,...) so I can learn from your experience in building an ML/DL strategy.
Sincerely thank.
-
KeyError: "cannot represent labeled-based slice indexer for coordinate 'time' with a slice over integer positions; the index is unsorted or non-unique"
Hello, I am currently using the following backtest to develop a strategy using neural network
import qnt.data as qndata import qnt.stats as qnstats retrain_interval = 19*365 + 1 data = qndata.stocks.load_ndx_data(min_date ="2005-01-01") models = train_model(data) weights = predict(models, data) is_liquid = data.sel(field="is_liquid") weights = weights * is_liquid def get_enough_bid_for(data_, weights_): time_traded = weights_.time[abs(weights_).fillna(0).sum('asset') > 0] is_strategy_traded = len(time_traded) if is_strategy_traded: return xr.where(weights_.time < time_traded.min(), data_.sel(field="is_liquid"), weights_) return weights_ weights = get_enough_bid_for(data, weights) weights = weights.fillna(0) # Calculate stats stats = qnstats.calc_stat(data, weights) display(stats.to_pandas().tail()) # Graph performance = stats.to_pandas()["equity"] import qnt.graph as qngraph #qngraph.make_plot_filled(performance.index, performance, name="PnL (Equity)", type="log")
The results of checking the strategy file are normal, however when I run precheck the following error occurs
And when I submit the strategy, it is put into the filter with the error that there is no weight. Please help me fix the error. Thank you.
My strategy ID is 16900804
-
RE: How to filter ticker futures by sharpe
@vyacheslav_b Thank you so much.
I have one more question for you to answer. I ran the precheck and the result was nan value the first time, but I set the min_date to 2005 - 01 - 01. I would like to ask, why is there a nan value problem? Is it because the ticker I chose had some companies that weren't listed at that time? My strategy id code is # 16767242. Thank you so much
-
RE: How to filter ticker futures by sharpe
@vyacheslav_b said in How to filter ticker futures by sharpe:
data = qndata.futures_load_data(min_date="2005-01-01")
Here is my full code
# Import basic libraries. import xarray as xr import pandas as pd import numpy as np # Import Quantiacs libraries. import qnt.data as qndata # load and manipulate data import qnt.output as qnout # manage output import qnt.backtester as qnbt # backtester import qnt.stats as qnstats # statistical functions for analysis import qnt.graph as qngraph # graphical tools import qnt.ta as qnta # indicators library import qnt.xr_talib as xr_talib # indicators library def load_data(period): futures = qndata.futures_load_data(tail=period) crypto = qndata.cryptofutures_load_data(tail=period) stocks = qndata.stocks.load_ndx_data(tail=period) return {"futures": futures, "crypto": crypto, "stocks": stocks}, futures.time.values def window(data, max_date: np.datetime64, lookback_period: int): min_date = max_date - np.timedelta64(lookback_period, 'D') return { "futures": data['futures'].sel(time=slice(min_date, max_date)), "crypto": data['crypto'].sel(time=slice(min_date, max_date)), "stocks": data['stocks'].sel(time=slice(min_date, max_date)), } def strategy(data): close = data['futures'].sel(field='close') close_prev = data['futures'].sel(field='close').shift(time=1) close_change = (close - close_prev)/close_prev close_stocks = data['stocks'].sel(field='close') close_stocks_prev = data['stocks'].sel(field='close').shift(time=1) close_change_stocks = (close_stocks - close_stocks_prev)/close_stocks_prev sma200 = qnta.lwma(close_change, 20).fillna(0).mean('asset').isel(time=-1) sma200_crypto = qnta.dema(close_change_stocks, 20).isel(time=-1) return xr.where(sma200 < sma200_crypto, 1, 0) weight = qnbt.backtest( competition_type="stocks_nasdaq100", load_data=load_data, lookback_period=90, start_date='2006-01-01', strategy=strategy, window=window ) import qnt.stats as qnstats # data = qndata.stocks.load_ndx_data(tail = 17*365, dims = ("time", "field", "asset")) data = qndata.futures_load_data(min_date="2005-01-01") def get_best_instruments(data, weights, top_size): # compute statistics: stats_per_asset = qnstats.calc_stat(data, weights, per_asset=True) # calculate ranks of assets by "sharpe_ratio": ranks = (-stats_per_asset.sel(field="sharpe_ratio")).rank("asset") # select top assets by rank "top_period" days ago: top_period = 1 rank = ranks.isel(time=-top_period) top = rank.where(rank <= top_size).dropna("asset").asset # select top stats: top_stats = stats_per_asset.sel(asset=top.values) # print results: print("SR tail of the top assets:") display(top_stats.sel(field="sharpe_ratio").to_pandas().tail()) print("avg SR = ", top_stats[-top_period:].sel(field="sharpe_ratio").mean("asset")[-1].item()) display(top_stats) return top_stats.coords["asset"].values get_best_instruments(data, weight, 15) weights = weight.sel(time=slice("2006-01-01",None)) qnout.check(weights, data, "stocks_nasdaq100") qnout.write(weights) # to participate in the competition
I can filter tickers by stocks, but I cannot filter tickers by futures. Please take a look and help me
-
RE: Is it possible to combine stocks with crypto?
@vyacheslav_b Thank you very much for your support.
I would like to ask, if I want to filter out the crypto codes with the highest sharpness, what should I do? Thank you. I tried using the get_best_instruments function but it didn't work
import qnt.stats as qnstats # data = qndata.stocks.load_ndx_data(tail = 17*365, dims = ("time", "field", "asset")) data = qndata.stocks.load_ndx_data(min_date="2005-01-01") def get_best_instruments(data, weights, top_size): # compute statistics: stats_per_asset = qnstats.calc_stat(data, weights, per_asset=True) # calculate ranks of assets by "sharpe_ratio": ranks = (-stats_per_asset.sel(field="sharpe_ratio")).rank("asset") # select top assets by rank "top_period" days ago: top_period = 1 rank = ranks.isel(time=-top_period) top = rank.where(rank <= top_size).dropna("asset").asset # select top stats: top_stats = stats_per_asset.sel(asset=top.values) # print results: print("SR tail of the top assets:") display(top_stats.sel(field="sharpe_ratio").to_pandas().tail()) print("avg SR = ", top_stats[-top_period:].sel(field="sharpe_ratio").mean("asset")[-1].item()) display(top_stats) return top_stats.coords["asset"].values get_best_instruments(data, weights, 10)
-
Is it possible to combine stocks with crypto?
Hello,
I'm testing combining crypto with stocks for Q21 but I'm getting an error. We hope to help.
Thank you.
Below is my code
# Import basic libraries. import xarray as xr import pandas as pd import numpy as np # Import Quantiacs libraries. import qnt.data as qndata # load and manipulate data import qnt.output as qnout # manage output import qnt.backtester as qnbt # backtester import qnt.stats as qnstats # statistical functions for analysis import qnt.graph as qngraph # graphical tools import qnt.ta as qnta # indicators library import qnt.xr_talib as xr_talib # indicators library def load_data(period): futures = qndata.futures.load_data(tail=period).isel(asset=0) stocks = qndata.stocks.load_ndx_data(tail=period) crypto= qndata.crypto.load_data(tail=period) return {"futures": futures, "stocks": stocks, "crypto": crypto}, futures.time.values def window(data, max_date: np.datetime64, lookback_period: int): min_date = max_date - np.timedelta64(lookback_period, "D") return { "futures": data["futures"].sel(time=slice(min_date, max_date)), "stocks": data["stocks"].sel(time=slice(min_date, max_date)), "crypto": data["crypto"].sel(time=slice(min_date, max_date)), } def strategy(data): close_futures = data["crypto"].sel(field="close") close_stocks = data["stocks"].sel(field="close") sma20 = qnta.sma(close_futures, 20).isel(time=-1) sma20_stocks = qnta.sma(close_stocks, 20).isel(time=-1) is_liquid = data["stocks"].sel(field="is_liquid").isel(time=-1) weights = xr.where(sma20 < sma20_stocks, 1, -1) weights = weights * is_liquid weights = weights / 100.0 return weights qnbt.backtest( competition_type= "stocks_nasdaq100", load_data= load_data, lookback_period= 90, start_date= "2006-01-01", strategy= strategy, window= window )
-
How to filter ticker futures by sharpe
Hello,
I'm trying to apply ticker filters from stocks to futures, but it doesn't work. Below is my code
For stock:
import qnt.stats as qnstats # data = qndata.stocks.load_ndx_data(tail = 17*365, dims = ("time", "field", "asset")) data = qndata.stocks.load_ndx_data(min_date="2005-01-01") def get_best_instruments(data, weights, top_size): # compute statistics: stats_per_asset = qnstats.calc_stat(data, weights, per_asset=True) # calculate ranks of assets by "sharpe_ratio": ranks = (-stats_per_asset.sel(field="sharpe_ratio")).rank("asset") # select top assets by rank "top_period" days ago: top_period = 1 rank = ranks.isel(time=-top_period) top = rank.where(rank <= top_size).dropna("asset").asset # select top stats: top_stats = stats_per_asset.sel(asset=top.values) # print results: print("SR tail of the top assets:") display(top_stats.sel(field="sharpe_ratio").to_pandas().tail()) print("avg SR = ", top_stats[-top_period:].sel(field="sharpe_ratio").mean("asset")[-1].item()) display(top_stats) return top_stats.coords["asset"].values get_best_instruments(data, weight, 15)
For futures
import qnt.stats as qnstats # data = qndata.stocks.load_ndx_data(tail = 17*365, dims = ("time", "field", "asset")) data = qndata.futures_load_data(min_date="2005-01-01") def get_best_instruments(data, weights, top_size): # compute statistics: stats_per_asset = qnstats.calc_stat(data, weights, per_asset=True) # calculate ranks of assets by "sharpe_ratio": ranks = (-stats_per_asset.sel(field="sharpe_ratio")).rank("asset") # select top assets by rank "top_period" days ago: top_period = 1 rank = ranks.isel(time=-top_period) top = rank.where(rank <= top_size).dropna("asset").asset # select top stats: top_stats = stats_per_asset.sel(asset=top.values) # print results: print("SR tail of the top assets:") display(top_stats.sel(field="sharpe_ratio").to_pandas().tail()) print("avg SR = ", top_stats[-top_period:].sel(field="sharpe_ratio").mean("asset")[-1].item()) display(top_stats) return top_stats.coords["asset"].values get_best_instruments(data, weight, 15)
Please help me. I hope you can provide an example on how to filter ticker futures by sharpe similar to the get_best_instruments function. Thank you