0

I started to study "backtesting" and during testing I encountered one incomprehensible situation.

class MyCandlesStrat(Strategy):
    def init(self):
        super().init()
        self.signal1 = self.I(SIGNAL)

    def next(self):
        super().next()
        if self.signal1==1:
            self.buy()
        elif self.signal1==-1:
            self.sell()

above is the strategy test code, but I can't figure out how to indicate that the signal = 0, did it close the order?

UPD : Signal column contains signals. in the code above, only trades are opened and not closed.

0
0
0
1
1
1
1
1
0
-1
-1
-1
-1
-1
-1
0
0
0
1
1
1
1

after a little digging on the net, I added the code class MyCandlesStrat(Strategy): def init(self): super().init() self.signal1 = self.I(SIGNAL)

def next(self):
    super().next()
    if self.signal1 ==1:
        self.position.close()
        self.buy()

    elif self.signal1 ==-1:
        self.position.close()
        self.sell()

    else:
        self.signal1 == 0
        self.position.close()

But it's still not what I want. This code closes deals with the same signals and does not

scrin

I want to understand how to implement "buy and hold until the signal is the same" or "sell and hold while the signal is the same" ??

xxxHEKETOSxxx
  • 57
  • 1
  • 9

1 Answers1

0

Try this, it would probably work for you:

if len(self.trades) > 0:
    if self.trades[-1].is_long and <your logic>:
        self.trades[-1].close()
    elif self.trades[-1].is_short and <your logic>:
        self.trades[-1].close()

PD: The index for self.trades doesn't have to be the last trade, it's your call which trade you want to close