Navigation

    Quantiacs Community

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

    Can't apply optimizer to another simple strategy!

    Strategy help
    2
    3
    181
    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.
    • magenta.grimer
      magenta.grimer last edited by magenta.grimer

      Hi!
      I'm trying to use the optimizer to optimize the very simple Blank Templeate for development strategy. It optimizer runs but I don't get sharpe ratios and always 0 as mean return.

      Where's the error?

      Cell1

      import xarray as xr
      
      import qnt.ta as qnta
      import qnt.backtester as qnbt
      import qnt.data as qndata
      
      
      
      def single_pass_strategy1(data, parameter1=20, parameter2=10):
          wma = qnta.lwma(data.sel(field='close'), parameter1)
          sroc = qnta.roc(wma, parameter2)
          weights = xr.where(sroc > 0, 1, 0)
          weights = weights / len(data.asset) # normalize weights so that sum=1, fully invested
          with qnlog.Settings(info=False, err=False): # suppress log messages
              weights = qnout.clean(weights, data, debug=False) # check for problems
          return weights
      

      Cell 2

      def single_pass_strategy2(data,parameter1=10, parameter2=1):
          close = data.sel(field='close')
          ma_slow = close.rolling(time=parameter1).mean().isel(time=-1) # qnta.sma(close, 200).isel(time=-1)
          ma_fast = close.rolling(time=parameter2).mean().isel(time=-1) # qnta.sma(close, 20).isel(time=-1)
          
          weights= xr.where(ma_fast > ma_slow, 1, -1)
          
          weights = weights / len(data.asset) # normalize weights so that sum=1, fully invested
          
          with qnlog.Settings(info=False, err=False): # suppress log messages
              weights = qnout.clean(weights, data, debug=True) # check for problems
          
          
          return weights
      

      Cell 3

      import qnt.data as qndata
      import qnt.ta as qnta
      import qnt.output as qnout
      import qnt.stats as qns
      import qnt.log as qnlog
      import qnt.optimizer as qnop
      import qnt.backtester as qnbt
      
      import xarray as xr
      
      
      #DEBUG#
      # evaluator will remove all cells with this tag before evaluation
      
      data = qndata.futures.load_data(min_date='2004-01-01') # indicators need warmup, so prepend data
      
      result = qnop.optimize_strategy(
          data,
          single_pass_strategy2,
          qnop.full_range_args_generator(
              parameter1=range(1, 200, 20), # min, max, step
              parameter2=range(1, 200, 20)   # min, max, step
          ),
          workers=1 # you can set more workers when you run this code on your local PC to speed it up
      )
      
      qnop.build_plot(result) # interactive chart in the notebook
      
      print("---")
      print("Best iteration:")
      display(result['best_iteration']) # as a reference, display the iteration with the highest Sharpe ratio
      

      single pass strategy 1 works (should be exactly the same used in the optimizer example) whereas single pass strategy 2 gives no sharpe and flat returns

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

        @magenta-grimer @support
        I coded this way so they look the most possible the same and to test one or another I just change the 1 with the 2. With the 1 it seems to work, with 2 no 😞

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

          @magenta-grimer

          Hello.

          Remove .isel(time=-1).

          ma_slow = close.rolling(time=parameter1).mean() #.isel(time=-1) 
          ma_fast = close.rolling(time=parameter2).mean()#.isel(time=-1) 
          

          It selects the last day, you need an entire series.

          Regards.

          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