Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    how to combine signals which triged in different days

    Indicators/Strategies/Analyzers
    2
    3
    130
    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.
    • D
      dindom999 last edited by

      class MyStrategy(bt.Strategy):
      
          def __init__(self):
              self.signalA = bt.indicators.SomeIndA()
              self.signalB = bt.indicators.SomeIndB()
              self.signalC = bt.indicators.SomeIndC()
      
      

      let's say, the following data of signals, 1 means long signal, -1 means short signal:
      signalA=[0,0,1,0,0,0,0,-1,0,0] # triggered at 3rd day,not yet
      signalB=[0,0,0,1,0,0,-1,0,0,0] # triggered at 4rd day,not yet
      signalC=[0,0,0,0,0,1,0,-1,0,0] # triggered at 6rd day, buy here

      I need to combine those 3 signals into which the 6rd day buy, but they are not triggered in a same day. how to do combine?

      1 Reply Last reply Reply Quote 0
      • run-out
        run-out last edited by

        Your logic is sufficiently complex I believe you will need a custom indicator. Look at the declarative approach in this article. Use next in an indicator to make adjustments to custom trackers for each of your signals. When the trackers sums to three, change your custom signal line to 1, otherwise set it to 0. Below is the idea sketched out, I haven't tried any of this code.

        class YourCustomIndicator(bt.Indicators):
        
            lines = (result_signal,)  # don't forget the trailing comma
        
            def __init__(self, signalA, signalB, signalC):
                self.signalA = signalA
                self.signalB = signalB
                self.signalC = signalC
        
                track_sigA = 0
                track_sigB = 0
                track_sigC = 0
                
            def next(self):
                if self.signalA[0] == 1 and track_sigA == 0:
                    track_sigA = 1
                if self.signalB[0] == 1 and track_sigB == 0:
                    track_sigB = 1
                if self.signalC[0] == 1 and track_sigC == 0:
                    track_sigC = 1
                
                if track_sigA == 1 and track_sigB == 1 and track_sigC == 1: 
        

        RunBacktest.com

        D 1 Reply Last reply Reply Quote 0
        • D
          dindom999 @run-out last edited by

          @run-out Thanks! I think your idea works, but that is what I want.

          before I start to try backtrader, I use talib+pandas, they are easy to transfer signals from :

          signalA=[0,0,1,0,0,0,0,-1,0,0] # triggered at 3rd day,not yet
          signalB=[0,0,0,1,0,0,-1,0,0,0] # triggered at 4rd day,not yet
          signalC=[0,0,0,0,0,1,0,-1,0,0] # triggered at 6rd day, buy here
          

          to

          signalA=[0,0,1,1,1,1,1,-1,-1,-1] # triggered at 3rd day,not yet
          signalB=[0,0,0,1,1,1,-1,-1,-1,-1] # triggered at 4rd day,not yet
          signalC=[0,0,0,0,0,1,1,-1,-1,-1] # triggered at 6rd day, buy here
          

          then , when signalA==1 and signalB==1 and signalC==1, it trigger the long signal.
          Since I am a newbie for backtrader, I don't know to datafeeds work that way.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors