Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· OpenAIAi2 WildChat-4.8M (AI2)

在历史数据回测中,当运行至某K线时,用self.stoporder = self.buy(price=self.stopprice,…

  1. User

    在历史数据回测中,当运行至某K线时,用self.stop_order = self.buy(price=self.stop_price, exectype=bt.Order.Stop)设置了止损单,self.stop_price的取值若不包含在K线中就不能设置吗?

  2. Assistant

    如果self.stop_price取值不包含在当前K线中,就不能设置止损单。因为止损单的执行是基于当前K线的价格,如果当前K线的价格与设置的止损价格不符,那么止损单就不会被执行,无法达到止损的目的。

  3. User

    但止损价不应该是高于当前的开仓价吗

  4. Assistant

    通常情况下,止损价应该是高于当前的开仓价,这是为了保证止损的效果。但是,在历史数据回测中,由于我们只能对已经发生的历史K线进行分析,所以止损价不一定能够高于当前的开仓价,可能会有错位的情况发生,这时就需要根据实际情况进行调整,确保止损价能够在回测中起到应有的作用。比如可以设置一个间隔,将止损价设定为开仓价加上一定比例的间隔,这样就可以保证止损价相对于开仓价有所调整。

  5. User

    但若加上间隔后,价格就不在K线内,那如何能成交呢?

  6. Assistant

    如果设置的止损价加上间隔后导致价格跨出了当前K线的范围,就不能成交。在这种情况下,可以考虑将止损单挂在接下来的K线上,并设置适当的价格间隔。比如,如果当前K线的最高价为X,开仓价为Y,止损价设定为Y+Z,但是止损价加上Z后已经超出了当前K线范围,那么可以将止损单挂在下一根K线上,价格设定为X+Z,这样就可以保证止损单能够在合适的价格触发,达到止损的目的。不过,需要注意的是,在历史数据回测中,这种处理方式有可能会对结果产生一定的影响,需要谨慎处理。

  7. User

    请用代码说明一下

  8. Assistant

    这里以backtrader为例演示如何在历史数据回测中设置止损单,当设置的止损价超出当前K线范围时,将止损单挂在接下来的K线上:

    import backtrader as bt
    
    class MyStrategy(bt.Strategy):
        
        def __init__(self):
            self.order = None
            self.stop_order = None
            self.stop_price = None
        
        def next(self):
            # 如果当前没有持仓,以开盘价买入
            if not self.position:
                self.order = self.buy(price=self.data.open[0], exectype=bt.Order.Market)
                # 设置止损价为开仓价加上一定比例的间隔,如果超出当前K线范围,挂在下一根K线上
                stop_price = self.data.open[0] * 0.9
                if stop_price < self.data.low[0]:
                    self.stop_price = self.data.low[0] + (stop_price - self.data.low[0])
                else:
                    self.stop_price = stop_price
                self.stop_order = self.sell(price=self.stop_price, exectype=bt.Order.Stop)
                self.log('Create stop order, price: %.2f' % self.stop_order.price)
            # 如果当前持仓,检查是否需要止损
            else:
                if self.data.close[0] < self.stop_price:
                    self.log('Execute stop order, price: %.2f' % self.stop_order.price)
                    self.close()
    

    这里的代码逻辑比较简单,首先在开仓时设置止损价,如果止损价超出了当前K线范围,则挂在接下来的K线上。然后在持仓时,如果价格达到止损价,就执行止损单并平仓。

    需要注意的是,这里的止损逻辑只是一个简单的示例,实际操作中需要根据具体的需求进行修改和完善。

  9. User

    用self.stop_order = self.buy(price=self.stop_price, exectype=bt.Order.Stop)设置的止损单,如何查看具体的成交信息

  10. Assistant

    可以使用回测框架backtrader提供的内置函数来查看当前策略的交易记录和交易细节信息。在backtrader的官方文档中,有关交易记录和交易细节的相关内容分别被归为Transactions和Broker classes。

    其中,Transactions类提供了一个回测完成后所有交易数据的访问接口,我们可以使用它来查看策略的成交记录。

    假设我们的策略实例为strategy,可以使用以下代码查看所有交易的细节:

    for t in strategy.trades:
        print('---')
        print('Open date:', t.open_datetime)
        print('Open price:', t.open_price)
        print('Size:', t.size)
        print('Commission:', t.commission)
        print('PNL:', t.pnl)
    

    这个代码片段中,循环遍历了所有交易,然后输出了每一笔交易的成交时间、开仓价格、交易数量、佣金和盈亏。

    如果需要查看某一笔具体的交易信息,可以使用backtrader的内置函数getbroker().getexecutioninfo(),这个函数返回一个由交易信息组成的序列,在序列中可以找到与当前订单对应的交易信息,然后获取具体信息。如下所示:

    exec_info = strategy.broker.getbroker().getexecutioninfo(strategy.order)
    print('Price:', exec_info.price)
    print('Size:', exec_info.size)
    print('Commission:', exec_info.comm)
    print('DateTime:', exec_info.datetime)
    

    这个代码片段中,我们调用getbroker()获取当前策略的经纪人对象,然后通过getexecutioninfo()方法获取与当前订单对应的交易信息,最后输出交易价格、数量、佣金以及成交时间等信息。需要注意的是,如果订单类型为止损单,那么需要调用getbroker().getfillinfo()函数来获取相关的成交信息。

  11. User

    参照这个stop_details=(f'{order.getordername()} Price: {order.executed.price}, size: {round(abs(order.size),2)}'),获取订单是买单还是卖单

  12. Assistant

    可以通过查询订单的 size 属性来判断订单是买单还是卖单。

    如果订单的 size 属性为正数,则表示买单;如果 size 属性为负数,则表示卖单。具体的代码如下:

    if order.size > 0:
        print('Order %d is buy order, executed price: %.2f, size: %.2f' % (order.ref, order.executed.price, abs(order.size)))
    elif order.size < 0:
        print('Order %d is sell order, executed price: %.2f, size: %.2f' % (order.ref, order.executed.price, abs(order.size)))
    else:
        print('Order %d is not buy or sell order' % order.ref)
    

    这个代码片段中,我们判断订单的 size 是否大于零,如果是,则说明是买单;否则,就是卖单。需要注意的是,如果订单既不是买单也不是卖单,比如某些特殊订单类型,就不能用这种方式来判断,需要根据具体的订单类型进行判断。

  13. User

    用self.stop_order = self.buy(price=self.stop_price, exectype=bt.Order.Stop)设置了止损单后;这个单就变成了买单吗?

  14. Assistant

    在backtrader中,当使用Order.Stop类型创建止损单时,实际上创建的是一种特殊类型的订单,称为"Stop Limit Order"。它将以限价单的形式提交到市场,并在触及止损价格时激活,变为真正的卖单。

    在使用Order.Stop类型创建止损单时,需要设置price参数来指定止损价格,这个价格应该是低于当前市场价格的。

    因此,在使用self.stop_order = self.buy(price=self.stop_price, exectype=bt.Order.Stop)创建止损单时并不是一个买单,而是一个“Stop Limit Order”,属于一种特殊的订单类型,在触及止损价位时才会在市场上转化为真正的卖单。换句话说,在创建止损单时,我们调用了buy方法,但实际上创建的是一种等待触发的卖单。

  15. User

    如何防止if order.isbuy()的条件读取到设置的止损单

  16. Assistant

    在backtrader中,可以通过设置订单名称来区分不同类型的订单,这样就可以在代码中对不同类型的订单进行分类处理。

    具体来说,在创建订单时,可以使用bt.Order()函数的name参数来设置订单的名称。例如,可以为买单设置一个名称为"Buy Order",为卖单设置一个名称为"Sell Order",为止损单设置一个名称为"Stop Loss Order",然后在代码中就可以根据订单名称来判断所处理的订单类型了。

    下面是一个示例:

    class MyStrategy(bt.Strategy):
        def __init__(self):
            self.order = None
            self.stop_order = None
            self.stop_price = None
    
        def next(self):
            # 如果当前没有持仓,以开盘价买入
            if not self.position:
                self.order = self.buy(price=self.data.open[0], exectype=bt.Order.Market, name='Buy Order')
                # 设置止损价为开仓价减去一定比例的间隔
                stop_price = self.data.open[0] * 0.9
                if stop_price < self.data.low[0]:
                    self.stop_price = self.data.low[0] + (stop_price - self.data.low[0])
                else:
                    self.stop_price = stop_price
                # 为止损单设置名称
                self.stop_order = self.sell(price=self.stop_price, exectype=bt.Order.Stop, name='Stop Loss Order')
                self.log('Create stop order, price: %.2f' % self.stop_order.price)
            # 如果当前持仓,检查是否需要止损
            else:
                # 获取当前订单
                order = self.order
                # 如果当前订单不是止损单且成交价格低于止损价,就需要触发止损单
                if order is not None and order.executed.price < self.stop_price and order.getordername() != 'Stop Loss Order':
                    self.log('Trigger stop order, price: %.2f' % self.stop_price)
                    self.stop_order.activate()
                    self.order = None
                    # 为当前订单设置名称
                    order.setname('Sell Order')
                # 如果当前订单已经是止损单了,就执行止损
                elif order is not None and order.getordername() == 'Stop Loss Order':
                    self.log('Execute stop order, price: %.2f' % self.stop_order.price)
                    self.close(order=self.stop_order)
                    self.stop_order = None
                # 如果当前订单未成交,就等待下一根K线
                else:
                    pass
    

    这个代码中,我们使用 setname() 方法来修改当前订单的名称,在下一次循环时,就可以使用 getordername() 方法获取名字来判断当前订单属于哪个类型。

    这样,就可以在if语句中使用isbuy()或issell()等方法来判断当前订单的类型,而不会将止损单误判为买单。

  17. User

    AttributeError: 'NoneType' object has no attribute 'getordername'

  18. Assistant

    这个错误通常是因为 order 对象为空值(NoneType),导致在调用 getordername() 方法时出现了错误。具体来说,可能是 order 对象尚未被创建,或者已经被撤销或者执行完毕,因此在下一次循环中无法正常使用。

    解决这个问题的方法是,在使用 order 对象前,先对其是否为 None 进行检查,如果是空值则不进行任何处理,否则再调用对应的方法。下面是一个示例:

    class MyStrategy(bt.Strategy):
        def __init__(self):
            self.order = None
            self.stop_order = None
            self.stop_price = None
    
        def next(self):
            # 如果当前没有持仓,以开盘价买入
            if not self.position:
                self.order = self.buy(price=self.data.open[0], exectype=bt.Order.Market)
                # 设置止损价为开仓价减去一定比例的间隔
                stop_price = self.data.open[0] * 0.9
                if stop_price < self.data.low[0]:
                    self.stop_price = self.data.low[0] + (stop_price - self.data.low[0])
                else:
                    self.stop_price = stop_price
                # 为止损单设置名称
                self.stop_order = self.sell(price=self.stop_price, exectype=bt.Order.Stop)
                self.stop_order.setname('Stop Loss Order')
                self.log('Create stop order, price: %.2f' % self.stop_order.price)
            # 如果当前持仓,检查是否需要止损
            else:
                # 获取当前订单
                order = self.order
                if order is not None:
                    # 如果当前订单是买单,将其修改为卖单,并设置名称
                    if order.isbuy():
                        order.setname('Sell Order')
                    # 如果当前订单是止损单,执行止损
                    elif order.getordername() == 'Stop Loss Order':
                        self.log('Execute stop order, price: %.2f' % self.stop_order.price)
                        self.close(order=self.stop_order)
                        self.stop_order = None
                    else:
                        pass  # 其他订单类型
                # 如果当前订单为空,等待下一根K线
                else:
                    pass
    

    这个代码中,在获取当前订单对象后,我们先检查它是否为 None,如果不是,则进一步判断订单是否为买单或者止损单。如果是买单,则修改为卖单,并设置名称;如果是止损单,则执行止损。如果当前订单是其他类型,则不做任何处理。如果当前订单为空值,则跳过当前K线,等待下一根K线再进行处理。

  19. User

    5个3.3uf,50V的电容并联后能耐250V的高压吗?

  20. Assistant

    在计算多个电容并联时,需要考虑它们的电压分配情况。在并联电路中,位于电压较高的电容器上受到的电压较高,而位于电压较低的电容器上受到的电压较低。因此,如果需要将多个电容并联,需要确保每个电容器上的电压都小于其额定电压。

    在这个具体的情况下,假设每个3.3uF,50V的电容器都能够耐受50V的电压,如果将5个电容并联,其总容值为3.3uF*5=16.5uF。如果在并联电路中,该电容器组合所受到的电压为250V,则每个电容器上的电压为250V/5=50V,刚好等于它们的额定电压。因此,这种情况下,可以将这5个电容并联在一起,不会导致电容器烧毁或损坏。

    需要注意的是,上述计算是在假定每个

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.