Navigation

    Quantiacs Community

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

    Backtesting

    Strategy help
    4
    16
    1165
    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.
    • M
      magenta.kabuto last edited by

      Bildschirmfoto 2023-03-26 um 15.39.29.png

      Hello,
      I have a question regarding my strategy. I defined a trading strategy and in [10] for each Asset listed in the Nasdaq, applied that strategy for the close values for a given time period.
      The signals of each asset are saved in a pandas dataframe with datetime index, which are the saved in a dictionary.
      Now it is not clear to me, from the examples given on the page, how to pass this into the qnt backtest.
      I will be thankful, if anyone can help me out on this. Thx

      support S 2 Replies Last reply Reply Quote 1
      • support
        support @magenta.kabuto last edited by

        @magenta-kabuto Hello, are you using a dictionary where asset names are keys and values are dataframes? If you let us know the dictionary, we can help you in converting that to an xarray DataArray structure

        M 1 Reply Last reply Reply Quote 0
        • M
          magenta.kabuto @support last edited by

          @support Hello, thx for your reply!Bildschirmfoto 2023-03-27 um 19.19.56.png This is the dicitonary containing each asset. My strategy algorithm is created for inputing a pandas series instead of xArray (it outputs 1,0,-1 for the close price of each date of one asset). In the previous screenshot is an example of the output for a one year period, for each asset(Trades Dictionary). Is it possible to convert my strategy output to xArray or will I have to redefine the Algorithm.
          And since each asset and the positions taken for each asset (1,0,-1) are one dictionary entry per asset, it is also not clear to me, how to backtest all assets at once instead of backtesting each at a time.
          Thank you and sorry if my description is a bit unclear.

          V 1 Reply Last reply Reply Quote 0
          • V
            Vyacheslav_B @magenta.kabuto last edited by

            @magenta-kabuto

            Hello

            An example using pandas
            One can work with pandas DataFrames at intermediate steps and at the end convert them to xarray data structures:

            def get_price_pct_change(prices):
                prices_pandas = prices.to_pandas()
                assets = data.coords["asset"].values
                for asset in assets:
                    prices_pandas[asset] = prices_pandas[asset].pct_change()
                return prices_pandas
            
            prices = data.sel(field="close") * 1.0
            prices_pct_change = get_price_pct_change(prices).unstack().to_xarray()
            
            M 1 Reply Last reply Reply Quote 1
            • M
              magenta.kabuto @Vyacheslav_B last edited by

              @vyacheslav_b Thank you 🙂

              1 Reply Last reply Reply Quote 0
              • S
                stefanm @magenta.kabuto last edited by

                @magenta-kabuto Hi, maybe this can help with your strategy also:
                In your function regime_trade() before returning signals_df, convert it to pandas.Series structure, and name it to corresponding asset (e.g. "NAS: AAPL").

                series = signals_df.squeeze().rename(asset_name)
                return series
                

                After putting it to trades = dict(), you can concatenate trades.values(), which will create pandas.DataFrame of signals by assets:

                pd_signals = pd.concat(trades.values(), axis=1)
                

                Then, convert it to xarray.DataArray and pass it as weights to backtester.

                import xarray as xr
                
                xr_signals = xr.DataArray(pd_signals, dims=('time', 'asset'))
                
                M 1 Reply Last reply Reply Quote 2
                • M
                  magenta.kabuto @stefanm last edited by

                  @stefanm Thank you very much, that was extremely helpful. Without your help I would probably be still trying to figure out a way 👏

                  1 Reply Last reply Reply Quote 0
                  • M
                    magenta.kabuto last edited by

                    @magenta-kabuto Bildschirmfoto 2023-03-29 um 17.33.56.png Bildschirmfoto 2023-03-29 um 17.32.54.png
                    First of all thx again for the help. Single-Pass Backtesting works fine and I am pretty happy with the result. Multiple-Pass Backtesting however returns the error shown in the screenshot. I assume that this problem occurs because load data, an xArray DataArray is passed into the function that inputs pandas. IS this right ?And if yes, can I avoid it without having to rewrite the algo or can I skip Multiple-Pass backtest as my algo doesnt look into the future? Very grateful, if anyone can help. Regards

                    M S 2 Replies Last reply Reply Quote 0
                    • M
                      magenta.kabuto @magenta.kabuto last edited by

                      @magenta-kabuto And what is the exact competition name ? 😅

                      1 Reply Last reply Reply Quote 0
                      • S
                        stefanm @magenta.kabuto last edited by

                        @magenta-kabuto Hi, yes that's right, it seems that xarray DataArray is passed to function which expects pandas DataFrame (your entire algorithm is not visible, which is ok, so this is an assumption), but maybe you can try with this:

                        ### use your regime_trade(Stockdata, param_2=0.15) as helper function
                        
                        
                        def strategy(data):
                        # param data: the data returned from load_data function, xarray.DataArray structure. 
                            Stockdata = ... # prepare data for regime_trade input, like you did for single pass
                            trades = {}
                            for j in logopenmod.keys():
                                trades[j] = regime_trade(Stockdata[j].iloc[:,3], 0.15)
                            pd_signals = pd.concat(trades.values(), axis=1)
                            xr_signals = xr.DataArray(pd_signals, dims=('time', 'asset'))
                            is_liquid = data.sel(field="is_liquid") # assume that "stocks" is exactly the same as data is
                            return xr_signals * is_liquid
                            
                        

                        Try it with Multi-pass, and change the name in competition_type. Set the start date as below:

                        weights = qnbk.backtest(
                          competition_type = "stocks_nasdaq100",
                          load_data = load_data, # if omitted it loads data by competition_type
                          lookback_period = 365,
                          start_date = "2006-01-01", # set start_date 
                          strategy = strategy,
                          analyze = True,
                        )
                        

                        Regards

                        M 1 Reply Last reply Reply Quote 1
                        • M
                          magenta.kabuto @stefanm last edited by

                          @stefanm I cant thank you enough. A very elegant way. The backtest atleast gets started but after approx. 20 seconds: the following occurs:
                          Sry for being so annoying, the conversion is an extreme blindspot to me and if it takes too much time, pls focus on your strategy instead of mineBildschirmfoto 2023-03-29 um 20.42.45.png Bildschirmfoto 2023-03-29 um 20.42.50.png

                          S 1 Reply Last reply Reply Quote 0
                          • S
                            stefanm @magenta.kabuto last edited by

                            @magenta-kabuto No problem, you're welcome. Can you please change the start_date to '2006-01-01' when running backtester, and let us know if it worked?

                            M support 2 Replies Last reply Reply Quote 1
                            • M
                              magenta.kabuto @stefanm last edited by

                              @stefanm Finally worked. For whatever reason I had to write the trade library and the loop outside the strategy function, as the output was otherwise empty. (Tried all sorts of spacing to get it inside the stratety function but didnt work). Good luck with your strategy if you are taking part in the contest too! 🙂 Bildschirmfoto 2023-03-30 um 02.14.55.png

                              M 1 Reply Last reply Reply Quote 0
                              • M
                                magenta.kabuto @magenta.kabuto last edited by

                                This post is deleted!
                                M 1 Reply Last reply Reply Quote 0
                                • M
                                  magenta.kabuto @magenta.kabuto last edited by

                                  This post is deleted!
                                  1 Reply Last reply Reply Quote 0
                                  • support
                                    support @stefanm last edited by

                                    @stefanm Thank you!

                                    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