0

Here is my code

class TestStrategy(bt.Strategy):

    params = dict(
        stop_loss=0.02,  # price is 2% less than the entry point
        trail=False,
    )

    def log(self, txt, dt=None):
        ''' Logging function fot this strategy'''
        dt = dt or self.datas[0].datetime.date(0)
        print('%s, %s' % (dt.isoformat(), txt))

    def __init__(self):
        # Keep a reference to the "close" line in the data[0] dataseries
        self.dataclose = self.datas[0].close
        self.bearish = pd.read_csv('bearish_comments.csv')
        self.bullish = pd.read_csv('bullish_comments.csv')
        self.bearish['dt'] = pd.to_datetime(self.bearish['dt'])
        self.bullish['dt'] = pd.to_datetime(self.bullish['dt'])
        self.bullish['tt'] = np.where(self.bullish['TSLA'] > (2 * self.bearish['TSLA']), True, False)
        self.order = None
        self.buyprice = None
        self.buycomm = None

    def notify_order(self, order):
        self.order = None

    def next(self):
        dt = self.datas[0].datetime.date(0)
        # Simply log the closing price of the series from the reference
        buy = self.bullish[self.bullish['dt'].isin(
            [datetime.datetime(dt.year, dt.month, dt.day, 0, 0, 0, 0),
             datetime.datetime(dt.year, dt.month, dt.day, 23, 59, 59, 99)])].iloc[0].tt
        self.log('Close, %.2f' % self.dataclose[0])
        print(buy)
        if self.order:
            return
        if not self.position:
            if buy:
                # BUY, BUY, BUY!!! (with all possible default parameters)
                self.log('BUY CREATE, %.2f' % self.dataclose[0])
                self.order = self.buy()
                print(self.order)
        else:
            if buy == False:
                if self.dataclose[0] >= ((self.order * 0.01) + self.order):
                    # TAKE PROFIT!!! (with all possible default parameters)
                    self.log('TAKE PROFIT, %.2f' % self.dataclose[0])
                    self.order = self.sell()

So for line

if self.dataclose[0] >= ((self.order * 0.01) + self.order):

when I use self.order it gives an error TypeError: unsupported operand type(s) for *: 'NoneType' and 'float' So what I want to do is to get price at what the order was bought and then take profit when the bought price increase with 1%

YanRemes
  • 347
  • 2
  • 10

1 Answers1

1

The error is self-explanatory: the variable self.order is None, so it has not been assigned a value yet.

From what I see, that must happen when you sell before having bought.

Miquel Escobar
  • 380
  • 1
  • 6