Navigation

    Quantiacs Community

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

    Multi-pass Backtesting

    Strategy help
    5
    5
    414
    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.
    • C
      cyan.gloom last edited by

      Hello !

      I'd like to rewrite this code for Multi-pass Backtesting to pass the competition filter.
      What should I do ?

      def pair_trading(data):
          pair = ['NAS:FWLT', 'NAS:PCAR']
          close = data.sel(field='close')
          price = close.sel(asset=pair).transpose('time', 'asset').fillna(0) #欠損値処理
          modelCoint = VECM(price.values, k_ar_diff=0, deterministic='na')
          res = modelCoint.fit()
          pairs_value = res.beta[0]*price.sel(asset=pair[0])+ res.beta[1]*price.sel(asset=pair[1]) #ポートフォリオ価値の作成
          diff = pairs_value.shift(time=-1) - pairs_value
          threshold = diff.std(dim='time')*2 #閾値の設定
          signal = np.sign(diff)
          
          weights = xr.zeros_like(close) #ポートフォリオウェイトの初期化
      
          for t in data.time:
              if (weights.sel(time=t)==[0,0]).any() and pairs_value.sel(time=t) >= threshold:
                  weights.loc[dict(time=t)] = [1,-1]
                  
              if (weights.sel(time=t)==[0,0]).any() and pairs_value.sel(time=t) <= -threshold:
                  weights.loc[dict(time=t)] = [-1,1]
          
              if (weights.sel(time=t)==[1,-1]).any() and signal.sel(time=t) < 0:
                  weights.loc[dict(time=t)] = [0,0]
                  
              if (weights.sel(time=t)==[-1,1]).any() and signal.sel(time=t) >= 0:
                  weights.loc[dict(time=t)] = [0,0]
      
          return weights
      

      Best regards

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

        @cyan-gloom Hi, before that, are you sure that your code is not looking forward? There is a shift with a negative value "-1" for the variable "diff", which in turns is used via "signal" for defining "weights". Naively, we would say that the code is looking into the future, correct?

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

          @support is ".shift(time=-1)" really forward looking?
          In the Quantiacs Ridge Classifier example ".shift(time=-1)" is used, so I guess this operation is not forward looking. 🙂

          def get_target_classes(data):
              # define categorical targets
              price_current = data.sel(field="close").dropna("time") # rm NaN
              price_future = price_current.shift(time=-1).dropna("time")
          
              class_positive = 1
              class_negative = 0
          
              target_is_price_up = xr.where(price_future > price_current, class_positive, class_negative)
              return target_is_price_up.to_pandas()
          V 1 Reply Last reply Reply Quote 0
          • V
            Vyacheslav_B @EDDIEE last edited by

            @eddiee

            Hello.

            This code looks to the future.
            It is needed to train the model.
            Pay attention to the name of the variable.

            mr_johnseo 1 Reply Last reply Reply Quote 1
            • mr_johnseo
              mr_johnseo Banned @Vyacheslav_B last edited by mr_johnseo

              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