Navigation

    Quantiacs Community

    • Register
    • Login
    • Search
    • Categories
    • News
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    1. Home
    2. newbiequant96
    N
    • Profile
    • Following 0
    • Followers 0
    • Topics 4
    • Posts 10
    • Best 2
    • Groups 0
    • Blog

    newbiequant96

    @newbiequant96

    2
    Reputation
    4
    Profile views
    10
    Posts
    0
    Followers
    0
    Following
    Joined Last Online

    newbiequant96 Follow

    Best posts made by newbiequant96

    • 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
      )
      

      2f2a9656-20d0-46e7-9a67-e3bd0d46a75f-image.png

      posted in Support
      N
      newbiequant96
    • 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)
      

      747ae905-59df-4547-9c44-907349ed5784-image.png

      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)
      

      a97f8908-4213-41d6-b519-de4b958e916f-image.png

      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

      @support @Vyacheslav_B

      posted in Support
      N
      newbiequant96

    Latest posts made by newbiequant96

    • How to submit stateful long short

      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.

      @support @Vyacheslav_B

      posted in Strategy help
      N
      newbiequant96
    • 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.

      posted in General Discussion
      N
      newbiequant96
    • 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

      469fc971-7907-4a18-875b-71f8ba467669-image.png
      bf6144b2-1cd1-4ae4-8786-ce0ae4f36a72-image.png
      16bdea24-da19-4e24-a5ce-d2aae7480bbc-image.png
      10d2812d-f309-4140-8b03-945e1816b404-image.png

      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

      @support @Vyacheslav_B

      posted in Support
      N
      newbiequant96
    • 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

      Screenshot 2024-04-09 173002.png
      Screenshot 2024-04-09 173012.png

      posted in Support
      N
      newbiequant96
    • 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

      posted in Support
      N
      newbiequant96
    • 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)
      

      19ae499c-71f3-4702-bba3-81d20fb6c5ac-image.png

      posted in Support
      N
      newbiequant96
    • 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
      )
      

      2f2a9656-20d0-46e7-9a67-e3bd0d46a75f-image.png

      posted in Support
      N
      newbiequant96
    • 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)
      

      747ae905-59df-4547-9c44-907349ed5784-image.png

      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)
      

      a97f8908-4213-41d6-b519-de4b958e916f-image.png

      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

      @support @Vyacheslav_B

      posted in Support
      N
      newbiequant96
    • Documentation
    • About
    • Career
    • My account
    • Privacy policy
    • Terms and Conditions
    • Cookies policy
    Home
    Copyright © 2014 - 2021 Quantiacs LLC.
    Powered by NodeBB | Contributors