Navigation

    Quantiacs Community

    • Register
    • Login
    • Search
    • Categories
    • News
    • Recent
    • Tags
    • Popular
    • Users
    • Groups

    Share the state between iterations

    Request New Features
    4
    13
    1148
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • support
      support last edited by support

      The current implementation of the backtester is stateless and there is no way to share context (variables) between iterations. But some strategies need it. We will provide a solution.

      support 1 Reply Last reply Reply Quote 1
      • support
        support @support last edited by support

        related topic: Different Sharpe ratios in backtest and competition filter

        1 Reply Last reply Reply Quote 0
        • support
          support last edited by

          related topic: Please advise on p settings. Thanks.

          support 1 Reply Last reply Reply Quote 0
          • support
            support @support last edited by

            @support

            This feature has been implemented.
            See: https://quantiacs.com/documentation/en/reference/evaluation.html#stateful-multi-pass-backtesting

            S 1 Reply Last reply Reply Quote 0
            • S
              spancham @support last edited by

              @support
              Please advise what to do here. Thanks.

              ---------------------------------------------------------------------------
              TypeError                                 Traceback (most recent call last)
              <ipython-input-1-434dffafaefc> in <module>
                   50     analyze=True,
                   51     build_plots=True,
              ---> 52     collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
                   53 )
              
              TypeError: backtest() got an unexpected keyword argument 'collect_all_states'
              
              support 1 Reply Last reply Reply Quote 0
              • support
                support @spancham last edited by

                @spancham
                Helllo.

                Update the library, please. run this in you environmnent:

                conda install 'quantiacs-source:qnt>=0.0.250'
                

                Regards.

                S 1 Reply Last reply Reply Quote 0
                • S
                  spancham @support last edited by spancham

                  Hi @support
                  Thanks but I tried that and it still did not work.
                  Just so you understand, I do NOT work locally, I only work in the Quantiacs online cloud Jupyter env.
                  I ran the update you suggested in my Quantiacs online Jupyter and it still did not work. I'm guessing you guys keep the env updated though.
                  I'm still getting the same error:
                  TypeError: backtest() got an unexpected keyword argument 'collect_all_states'

                  Any suggestions pls?

                  support 1 Reply Last reply Reply Quote 1
                  • support
                    support @spancham last edited by

                    @spancham

                    Hello. Sorry, there was a typo. Try this:

                    conda install 'quantiacs-source::qnt>=0.0.250' -n qntdev
                    

                    (double ':' )

                    Regards.

                    S 1 Reply Last reply Reply Quote 0
                    • S
                      spancham @support last edited by spancham

                      @support
                      Sorry friend, that still does not work.
                      I am working in the Quantiacs online cloud Jupyter environment.
                      I am NOT working locally.
                      I copied your example template as is and just added load_data().
                      Below is what I am running:

                      import xarray as xr
                      import numpy as np
                      import qnt.data as qndata
                      import qnt.backtester as qnbt
                      
                      def load_data(period):
                          data = qndata.futures_load_data(tail=period)
                          return data
                      
                      def strategy(data, state):
                          close = data.sel(field="close")
                          last_close = close.ffill('time').isel(time=-1)
                      
                          # state may be null, so define a default value
                          if state is None:
                              state = {
                                  "ma": last_close,
                                  "ma_prev": last_close,
                                  "output": xr.zeros_like(last_close)
                              }
                      
                          ma_prev = state['ma']
                          ma_prev_prev = state['ma_prev']
                          output_prev = state['output']
                      
                          # align the arrays to prevent problems in case the asset list changes
                          ma_prev, ma_prev_prev, last_close = xr.align(ma_prev, ma_prev_prev, last_close, join='right') 
                      
                          ma = ma_prev.where(np.isfinite(ma_prev), last_close) * 0.97 + last_close * 0.03
                      
                          buy_signal = np.logical_and(ma > ma_prev, ma_prev > ma_prev_prev)
                          stop_signal = ma < ma_prev_prev
                      
                          output = xr.where(buy_signal, 1, output_prev)
                          output = xr.where(stop_signal, 0, output)
                      
                          next_state = {
                              "ma": ma,
                              "ma_prev": ma_prev,
                              "output": output
                          }
                          return output, next_state
                      
                      
                      weights, state = qnbt.backtest(
                          competition_type="futures",  # Futures contest
                          lookback_period=365,  # lookback in calendar days
                          start_date="2006-01-01",
                          strategy=strategy,
                          analyze=True,
                          build_plots=True,
                          collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
                      )
                      

                      Error I'm getting is:

                      ---------------------------------------------------------------------------
                      TypeError                                 Traceback (most recent call last)
                      <ipython-input-1-434dffafaefc> in <module>
                           50     analyze=True,
                           51     build_plots=True,
                      ---> 52     collect_all_states=False # if it is False, then the function returns the last state, otherwise - all states
                           53 )
                      
                      TypeError: backtest() got an unexpected keyword argument 'collect_all_states'
                      

                      The reason I want to get the 'state' working is that I have a pairs trading strategy that needs it.
                      Any help would be greatly appreciated.
                      Thanks.

                      In a related issue, when I run the macroeconomic example, I get the following error:

                      ---------------------------------------------------------------------------
                      TypeError                                 Traceback (most recent call last)
                      <ipython-input-1-62eeefb4e65f> in <module>
                           76     strategy=strategy,
                           77     analyze=True,
                      ---> 78     build_plots=True
                           79 )
                      
                      ~/book/qnt/backtester.py in backtest(competition_type, strategy, load_data, lookback_period, test_period, start_date, window, step, analyze, build_plots)
                           66     data, time_series = extract_time_series(data)
                           67     print("Run pass...")
                      ---> 68     result = strategy(data)
                           69     if result is None:
                           70         log_err("ERROR! Strategy output is None!")
                      
                      TypeError: strategy() missing 1 required positional argument: 'state'
                      
                      1 Reply Last reply Reply Quote 0
                      • support
                        support last edited by

                        Hello.

                        I guess there is a warning on the strategy "The strategy uses outdated libraries. Clone it to update the libraries."

                        Just clone this strategy, it will create a new environment with the latest library.

                        Regards.

                        S 1 Reply Last reply Reply Quote 0
                        • S
                          spancham @support last edited by

                          @support
                          I am working in the Quantiacs online env in my account, why doesn't it catch the latest libraries?
                          Anyway, I created a new Jupyter notebook with the strategy as suggested and it still does NOT work, same error msg.

                          S 1 Reply Last reply Reply Quote 0
                          • S
                            spancham @spancham last edited by spancham

                            @support
                            ok I did what @antinomy said below and the 'state' strategy worked.
                            https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/3?_=1619554449031

                            However, it broke my old strategies. So he further suggested to:
                            https://quantiacs.com/community/topic/46/macroeconomic-data-with-quantiacs/5?_=1619556376479
                            And my old strategies are running again.

                            Ok, so looks like for now for all strategies without a state I have to output and pass None to weights for the state & pass a state variable to the strategy.
                            Will keep you updated. Thanks for looking into the issue.

                            1 Reply Last reply Reply Quote 0
                            • G
                              green.sheep last edited by

                              This post is deleted!
                              1 Reply Last reply Reply Quote 0
                              • First post
                                Last post
                              Powered by NodeBB | Contributors
                              • Documentation
                              • About
                              • Career
                              • My account
                              • Privacy policy
                              • Terms and Conditions
                              • Cookies policy
                              Home
                              Copyright © 2014 - 2021 Quantiacs LLC.
                              Powered by NodeBB | Contributors