how to combine signals which triged in different days
-
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 hereI 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?
-
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 to0
. 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:
-
@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.