<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[How to filter ticker futures by sharpe]]></title><description><![CDATA[<p dir="auto">Hello,</p>
<p dir="auto">I'm trying to apply ticker filters from stocks to futures, but it doesn't work. Below is my code</p>
<p dir="auto">For stock:</p>
<pre><code>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 &lt;= 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)
</code></pre>
<p dir="auto"><img src="/community/assets/uploads/files/1712221118512-747ae905-59df-4547-9c44-907349ed5784-image.png" alt="747ae905-59df-4547-9c44-907349ed5784-image.png" class="img-responsive img-markdown" /></p>
<p dir="auto">For futures</p>
<pre><code>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 &lt;= 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)
</code></pre>
<p dir="auto"><img src="/community/assets/uploads/files/1712221179054-a97f8908-4213-41d6-b519-de4b958e916f-image.png" alt="a97f8908-4213-41d6-b519-de4b958e916f-image.png" class="img-responsive img-markdown" /></p>
<p dir="auto">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</p>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/12">@support</a> <a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/5">@Vyacheslav_B</a></p>
]]></description><link>http://quantiacs.com/community/topic/554/how-to-filter-ticker-futures-by-sharpe</link><generator>RSS for Node</generator><lastBuildDate>Tue, 10 Mar 2026 10:31:44 GMT</lastBuildDate><atom:link href="http://quantiacs.com/community/topic/554.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 04 Apr 2024 09:00:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to How to filter ticker futures by sharpe on Tue, 09 Apr 2024 10:38:20 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/5">@vyacheslav_b</a> Thank you so much.</p>
<p dir="auto">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</p>
<p dir="auto"><img src="/community/assets/uploads/files/1712658870131-screenshot-2024-04-09-173002.png" alt="Screenshot 2024-04-09 173002.png" class="img-responsive img-markdown" /><br />
<img src="/community/assets/uploads/files/1712658870590-screenshot-2024-04-09-173012.png" alt="Screenshot 2024-04-09 173012.png" class="img-responsive img-markdown" /></p>
]]></description><link>http://quantiacs.com/community/post/1557</link><guid isPermaLink="true">http://quantiacs.com/community/post/1557</guid><dc:creator><![CDATA[newbiequant96]]></dc:creator><pubDate>Tue, 09 Apr 2024 10:38:20 GMT</pubDate></item><item><title><![CDATA[Reply to How to filter ticker futures by sharpe on Tue, 09 Apr 2024 00:55:05 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/3389">@newbiequant96</a> Hello. You have a problem with the variable <code>weight</code>.</p>
<p dir="auto">You are predicting portfolio weights for stocks with this variable, but you are trying to select the best assets among futures.</p>
<p dir="auto">If you want to filter by the best futures, then the variable should contain portfolio weights for futures. If you want to filter stocks, then the variable should contain portfolio weights for stocks.</p>
<p dir="auto">In your code, you are predicting stocks and trying to find the best futures among them.</p>
<p dir="auto">In this section, I provided an example of code on how to load stocks, futures, and cryptocurrencies together.<br />
<a href="https://quantiacs.com/community/topic/556/is-it-possible-to-combine-stocks-with-crypto/2" rel="nofollow ugc">https://quantiacs.com/community/topic/556/is-it-possible-to-combine-stocks-with-crypto/2</a></p>
]]></description><link>http://quantiacs.com/community/post/1556</link><guid isPermaLink="true">http://quantiacs.com/community/post/1556</guid><dc:creator><![CDATA[Vyacheslav_B]]></dc:creator><pubDate>Tue, 09 Apr 2024 00:55:05 GMT</pubDate></item><item><title><![CDATA[Reply to How to filter ticker futures by sharpe on Fri, 05 Apr 2024 14:40:14 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/5">@vyacheslav_b</a> said in <a href="/community/post/1550">How to filter ticker futures by sharpe</a>:</p>
<blockquote>
<p dir="auto">data = qndata.futures_load_data(min_date="2005-01-01")</p>
</blockquote>
<p dir="auto">Here is my full code</p>
<pre><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 &lt; 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 &lt;= 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
</code></pre>
<p dir="auto">I can filter tickers by stocks, but I cannot filter tickers by futures. Please take a look and help me</p>
]]></description><link>http://quantiacs.com/community/post/1551</link><guid isPermaLink="true">http://quantiacs.com/community/post/1551</guid><dc:creator><![CDATA[newbiequant96]]></dc:creator><pubDate>Fri, 05 Apr 2024 14:40:14 GMT</pubDate></item><item><title><![CDATA[Reply to How to filter ticker futures by sharpe on Fri, 05 Apr 2024 12:47:10 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="http://quantiacs.com/community/uid/3389">@newbiequant96</a> said in <a href="/community/post/1540">How to filter ticker futures by sharpe</a>:</p>
<blockquote>
<p dir="auto">import qnt.stats as qnstats</p>
<h1>data = qndata.stocks.load_ndx_data(tail = 17*365, dims = ("time", "field", "asset"))</h1>
<p dir="auto">data = qndata.futures_load_data(min_date="2005-01-01")<br />
def get_best_instruments(data, weights, top_size):<br />
# compute statistics:<br />
stats_per_asset = qnstats.calc_stat(data, weights, per_asset=True)<br />
# calculate ranks of assets by "sharpe_ratio":<br />
ranks = (-stats_per_asset.sel(field="sharpe_ratio")).rank("asset")<br />
# select top assets by rank "top_period" days ago:<br />
top_period = 1<br />
rank = ranks.isel(time=-top_period)<br />
top = rank.where(rank &lt;= top_size).dropna("asset").asset</p>
<pre><code># 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
</code></pre>
<p dir="auto">get_best_instruments(data, weight, 15)</p>
</blockquote>
<p dir="auto">Hello. I suppose there is an issue with the variable <code>weight</code> in your code.</p>
<p dir="auto">Here is a working example with futures selection.</p>
<pre><code class="language-python">import qnt.data as qndata
import qnt.stats as qnstats
import qnt.ta as qnta

data = qndata.futures_load_data(min_date="2005-01-01")


def strategy(data, params):
    s_ = qnta.trix(data.sel(field='high'), params[0])
    w_1 = s_.shift(time=params[1]) &gt; s_.shift(time=params[2])
    w_2 = s_.shift(time=params[3]) &gt; s_.shift(time=params[4])
    weights = (w_1 * w_2)
    return weights.fillna(0)


weights = strategy(data, [196, 125, 76, 12, 192])


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 &lt;= 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, 5)

</code></pre>
]]></description><link>http://quantiacs.com/community/post/1550</link><guid isPermaLink="true">http://quantiacs.com/community/post/1550</guid><dc:creator><![CDATA[Vyacheslav_B]]></dc:creator><pubDate>Fri, 05 Apr 2024 12:47:10 GMT</pubDate></item></channel></rss>