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







