0

这是我的代码

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()

所以对于线

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

当我使用 self.order 时,它会给出错误 TypeError: unsupported operand type(s) for *: 'NoneType' and 'float' 所以我想要做的是以购买订单的价格获取价格,然后在购买价格上涨 1%

4

1 回答 1

1

错误是不言自明的:变量self.orderNone,所以它还没有被赋值。

据我所知,这必须发生在你在买之前卖掉。

于 2021-09-02T11:04:33.960 回答