Navigation

    Quantiacs Community

    • Register
    • Login
    • Search
    • Categories
    • News
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    1. Home
    2. Popular
    Log in to post
    • All categories
    • Support
    •      Request New Features
    • Strategy help
    • General Discussion
    • News and Feature Releases
    • All Topics
    • New Topics
    • Watched Topics
    • Unreplied Topics
    • All Time
    • Day
    • Week
    • Month
    • magenta.grimer

      Optimizer for simple MA crypto strategy
      Strategy help • • magenta.grimer

      4
      0
      Votes
      4
      Posts
      585
      Views

      A

      There is a way to use the optimizer with a (stateful) mulit pass algo, but depending on the total number of changed parameters it can take a very long time. However, if it runs on a local computer with many workers this can still be useful.

      We could run the backtester with the multi pass algo to get all the weights for the test period and pass these weights to the optimizer.
      There's just one problem with this: you can't pass changed parameters to the strategy using the backtester.
      In order to solve this I created a nested function where the outer function takes the changed parameters from the optimizer. The inner function is the actual multi pass strategy and doesn't define the params but just uses the ones from the outer function. Still within the outer function we run the backtester with one set of params, get the weights it returns and return them to the optimizer.

      The time it takes to run the optimization would roughly be
      (time for 1 multi pass backtest) x (total number of parameter changes) / (number of workers that are able to run)
      So if one multi pass takes 1 minute, you want to optimize 10 parameter changes and can run 5 workers it would take about 2 minutes.

      Here's an example based on the one above with 2 parameter changes and 2 workers:

      import qnt.data as qndata import qnt.ta as qnta import qnt.optimizer as qnop import qnt.backtester as qnbt import xarray as xr def load_data(period): """Loads the BTC Futures data for the BTC Futures contest""" return qndata.cryptofutures.load_data(tail=period, dims=("time", "field", "asset")) def multi_pass_strategy(data, ma_slow_param=50, ma_fast_param=10): """The outer function gets called by the optimizer with changed params, the inner function gets passed to the backtester.""" def strategy(data, state): # The state isn't used in this example, this is just to show that it can be used while optimizing. if state is None: state = 0 state += 1 close = data.sel(field="close") ma_slow = qnta.lwma(close, ma_slow_param).isel(time=-1) ma_fast = qnta.lwma(close, ma_fast_param).isel(time=-1) weights = xr.zeros_like(close.isel(time=-1)) weights[:] = 1 if ma_fast > ma_slow else -1 return weights, state """The backtester returns all weights for the test period which will then be returned to the optimizer""" weights, state = qnbt.backtest( strategy=strategy, competition_type="cryptofutures", load_data=load_data, lookback_period=700, start_date='2014-01-01', build_plots=False, ) return weights data = qndata.cryptofutures.load_data(min_date='2014-01-01') result = qnop.optimize_strategy( data, multi_pass_strategy, qnop.full_range_args_generator( ma_slow_param=range(50, 60, 5), # min, max, step # ma_fast_param=range(5, 100, 5) # min, max, step ), workers=2 # you can set more workers on your PC ) print("---") print("Best iteration:") print(result['best_iteration']) qnop.build_plot(result)

      There might be more efficient ways to do this, so if anyone has one feel free to post it here.

    • S

      Pairs trading with states iterations
      Strategy help • • spancham

      4
      0
      Votes
      4
      Posts
      661
      Views

      S

      @support
      Cool, thanks very much! 👍

    • O

      Can I use astronomical data as features for my machine learning model?
      Support • • omohyoid

      4
      0
      Votes
      4
      Posts
      592
      Views

      O

      @support Thx for ur reply

    • T

      Calculation time exceeded on submission
      Support • • TheFlyingDutchman

      4
      1
      Votes
      4
      Posts
      633
      Views

      V

      @theflyingdutchman Hello,

      Another option is to rewrite your strategy for a single-pass version before submitting it. This approach will significantly speed up the calculations. However, it's important to note that the actual statistical values can only be tracked after submitting the strategy to the competition.

      For example:
      https://github.com/quantiacs/strategy-ml-crypto-long-short/blob/master/strategy.ipynb

      To adapt this strategy for a single-pass version, follow these steps:

      Comment out or delete the line where qnbt.backtest_ml is used. Insert the following code: import xarray as xr import qnt.ta as qnta import qnt.data as qndata import qnt.output as qnout import qnt.stats as qnstats retrain_interval = 3*365 + 1 data = qndata.stocks.load_ndx_data(tail=retrain_interval) models = train_model(data) weights = predict(models, data) In a new cell, insert code to save the weights: qnout.write(weights)

      To view the strategy's statistics, use the following code in a new cell:

      # 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 qnbt.backtest_ml function is a unique tool for evaluating machine learning strategies, which stands out from what is offered on other platforms. It allows users to set retraining intervals and analyze statistical metrics of the strategy, as opposed to the traditional evaluation of the machine learning model. This provides a deeper understanding of the strategy's effectiveness under various market conditions.

    • magenta.grimer

      Importing external data
      General Discussion • • magenta.grimer

      4
      1
      Votes
      4
      Posts
      629
      Views

      support

      @penrose-moore Thank you for the idea. For the Bitcoin Futures contest we are indeed patching the Bitcoin Futures data with the BTC spot price to build a meaningful time series. For the other Futures contracts, for the moment we will keep the futures histories only, but add spot prices + patching with spot prices to increase the length of the time series to our to-do list.

    • news-quantiacs

      New futures data and next-to-front contracts
      News and Feature Releases • • news-quantiacs

      4
      1
      Votes
      4
      Posts
      569
      Views

      support

      @magenta-grimer Hello, we updated the documentation.

      Now there are 78 futures contracts. Yes, we allow allocating to only 1 asset. If you trade more assets, then you can go long on some of them and short others.

      Using more assets helps in increasing the Sharpe ratio, as the mean return grows linearly with the number of assets, and the volatility in the denominator with the square root of the number of assets if there are no correlation terms.

      Using uncorrelated assets would then lead to a scaling of the Sharpe ratio with the square root of the number of assets. In practice, however, correlation terms are decreasing this growth.

      Stated more simply, it is a good idea to avoid putting all your eggs in the same basket...

    • A

      Jupyter/Jupyter Lab are not working for code editing/running
      Support • • AlgoQuant

      4
      0
      Votes
      4
      Posts
      835
      Views

      support

      @captain-nidoran Fixed, sorry for issue

    • M

      Why we need to limit the time to process the strategy ?
      Support • • multi_byte.wildebeest

      4
      0
      Votes
      4
      Posts
      444
      Views

      support

      @multi_byte-wildebeest Hi, these limitations refer to the processing time per point in time, not for the full strategy.

      If it takes 10 minutes per historical day, and the simulation has to take into account 250 days for let us say 10 years, the multi-pass simulation would process 6 days per hour, 144 days per real day, that means 2 weeks of processing time for the full submission, it is a lot of time.

    • A

      Submission Logic Questions
      Support • • auxiliary.snail

      4
      0
      Votes
      4
      Posts
      1005
      Views

      support

      @auxiliary-snail Hi,

      unfortunately, this is not allowed and in accordance with the rules. Using hard-coded time periods in which trading algorithm will work differently, is not a quantitative method (just like manual asset selection, e.g. "trade only Apple or Microsoft"). We still haven't implemented a mechanism for automatic recognition of such behaviors in trading strategies, and even though a strategy could be successfully submitted, it will not be eligible for prize winning.
      What we are searching for, is well performing strategy over entire in_sample period (SR>0.7), robust to all market movements 2006-2025, so we can expect it will perform well in future, too.

    • A

      Unable to see 15/16 of myu strategies
      Support • • anshul96go

      4
      0
      Votes
      4
      Posts
      676
      Views

      support

      @captain-nidoran hi, all your strategies which will take part to the contest should be under the "In Contest" tab in the "Competition" section.

      The migration "Candidates" -> "In Contest" was not immediate as we released minor improvements to the front-end side once the submission phase was over.

    • L

      Error message when enter JupyterLab
      Support • • lemonpie

      3
      0
      Votes
      3
      Posts
      208
      Views

      L

      @support Thanks. Works fine now.

    • S

      Error for importing quantiacs module
      Support • • steel.camel

      3
      0
      Votes
      3
      Posts
      912
      Views

      support

      @steel-camel Sorry for the issue, it has been fixed.

    • M

      How can we have the estimation of Sharpe submitted ?
      Support • • multi_byte.wildebeest

      3
      2
      Votes
      3
      Posts
      822
      Views

      V

      @multi_byte-wildebeest Hello.

      How to get the Sharpe Ratio is in the Quick Start template.
      https://github.com/quantiacs/strategy-q20-nasdaq100-quick-start/blob/master/strategy.ipynb

      import qnt.stats as qnstats def get_sharpe(market_data, weights): rr = qnstats.calc_relative_return(market_data, weights) sharpe = qnstats.calc_sharpe_ratio_annualized(rr).values[-1] return sharpe sharpe = get_sharpe(data, weights) # weights.sel(time=slice("2006-01-01",None))

      or

      import qnt.output as qnout qnout.check(weights, data, "stocks_nasdaq100")

      or

      stat = qnstats.calc_stat(data, weights) display(stat.to_pandas().tail())

      or

      import qnt.graph as qngraph statistics = qnstats.calc_stat(data, weights) display(statistics.to_pandas().tail()) performance = statistics.to_pandas()["equity"] qngraph.make_plot_filled(performance.index, performance, name="PnL (Equity)", type="log") display(statistics[-1:].sel(field=["sharpe_ratio"]).transpose().to_pandas()) qnstats.print_correlation(weights, data)

      Please look at this post
      https://quantiacs.com/community/topic/515/what-is-forward-looking-and-why-it-s-effective-badly-to-strategy/6?_=1711712434795

    • A

      Saving and recalling a dictionary of trained models
      Support • • alfredaita

      3
      0
      Votes
      3
      Posts
      530
      Views

      A

      @alfredaita
      In case you don't want to run init.py every time in order to install external libraries, I came up with a solution for this. You basically install the library in a folder in your home directory and let the strategy create symlinks to the module path at runtime. More details in this post.

    • N

      Use of Technical indicators
      Support • • noka'sworld

      3
      0
      Votes
      3
      Posts
      261
      Views

      N

      @support that is really useful! thank you very much!

    • O

      I can't find why the submission failed
      Support • • omohyoid

      3
      0
      Votes
      3
      Posts
      325
      Views

      O

      @support
      Actually, I've write the weights to the output function.
      螢幕擷取畫面 2024-04-24 235034.png
      I think the reason might be that the data was out-of-date when the strategy received at the weekend. After the data update in the next day, it failed to pass the test.

    • A

      Erroneous Data?
      Support • • antinomy

      3
      1
      Votes
      3
      Posts
      664
      Views

      support

      @antinomy Hello, sorry for delay again. We found a problem with the data provider, sorry.

    • A

      notebook for googlecolab not working
      Support • • alfredaita

      3
      1
      Votes
      3
      Posts
      298
      Views

      A

      @support Thanks seems fine

    • cespadilla

      Q17 ML Example not running on Local Development
      Support • • cespadilla

      3
      1
      Votes
      3
      Posts
      403
      Views

      cespadilla

      @support thanks, I deleted the old environment, installed it again according to the documentation, and now it is working 👌

    • A

      Issues with the Legacy Website
      Support • • antinomy

      3
      1
      Votes
      3
      Posts
      984
      Views

      A

      @jeppe_and Ok, thanks for the quick reply!

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