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

class EntryLine(bt.Indicator): lines = (‘entryprice’,) plotinfo = dict(subplot=False)…

  1. User

    class EntryLine(bt.Indicator): lines = (‘entryprice’,) plotinfo = dict(subplot=False) plotlines = dict(entryprice=dict(ls=‘-’, lw=1, color=‘red’))

    def init(self): self.addminperiod(self.data.size())

    def prenext(self): self.lines.entryprice[0] = float(‘nan’)

    def next(self): self.lines.entryprice[0] = float(‘nan’)

    def set_entryprice(self, price): for index in range(-1, -self.data.size() - 1, -1): self.lines.entryprice[index] = price改成任意直接,传入的是K线的时间截和收盘价

  2. Assistant

    class EntryLine(bt.Indicator): lines = ('entryprice',) plotinfo = dict(subplot=False) plotlines = dict(entryprice=dict(ls='-', lw=1, color='red'))

    def __init__(self, kline_time, close_price):
        self.kline_time = kline_time
        self.close_price = close_price
        super(EntryLine, self).__init__()
        
    def prenext(self):
        self.lines.entryprice[0] = float('nan')
    
    def next(self):
        self.lines.entryprice[0] = float('nan')
    
    def set_entryprice(self, kline_time, price):
        index = -self.data.close.get(size=0, ago=0).size
        while index < 0:
            if kline_time == self.kline_time[index]:
                self.lines.entryprice[index] = price
                break
            else:
                index += 1
    
  3. Assistant

    这个修改后的代码是为了让EntryLine指标在接收到k线的时间戳和收盘价之后,能够在该k线对应的位置上将entryprice设置为传入的收盘价。

    具体实现如下:

    1. __init__ 方法中增加了 kline_time 和 close_price 参数,用于保存传入的K线时间戳和收盘价。

    2. set_entryprice 方法中增加了 kline_time 参数,用于指定要更新 entryprice 的位置。该方法采用了循环遍历的方式,从最近的K线向前逐个比对时间戳,找到匹配的K线之后,将 entryprice 设置为传入的价格。由于是从最近的K线向前遍历,所以需要保证传入的 kline_time 是该K线对应的时间戳,否则可能会设置错误。

    3. prenext 和 next 方法没有做任何修改,仍然是将 entryprice 设置为 NaN。

    注意事项:

    该实现假设传入的K线时间戳和收盘价与传入的数据的时间戳和收盘价是一致的,如果不一致,就会导致找不到对应的位置而设置错误。同时,该实现没有对传入的数据进行任何处理,如时间戳是否按照时间顺序排列、是否有重复等,并假设传入的数据已经满足所有要求。

  4. User

    class EntryLine(bt.Indicator): lines = (‘entryprice’,) plotinfo = dict(subplot=False) plotlines = dict(entryprice=dict(ls=‘-’, lw=1, color=‘red’))

    def init(self): self.addminperiod(self.data.size())

    def prenext(self): self.lines.entryprice[0] = float(‘nan’)

    def next(self): self.lines.entryprice[0] = float(‘nan’)

    def set_entryprice(self, price): for index in range(-1, -self.data.size() - 1, -1): self.lines.entryprice[index] = price改成任意直接,传入的是K线的时间截和收盘价

  5. Assistant

    class EntryLine(bt.Indicator): lines = ('entryprice',) plotinfo = dict(subplot=False) plotlines = dict(entryprice=dict(ls='-', lw=1, color='red'))

    def __init__(self, kline_time, close_price):
        self.kline_time = kline_time
        self.close_price = close_price
        super(EntryLine, self).__init__()
        
    def prenext(self):
        self.lines.entryprice[0] = float('nan')
    
    def next(self):
        self.lines.entryprice[0] = float('nan')
    
    def set_entryprice(self, kline_time, price):
        index = -self.data.close.get(size=0, ago=0).size
        while index < 0:
            if kline_time == self.kline_time[index]:
                self.lines.entryprice[index] = price
                break
            else:
                index += 1
    
  6. User

    class EntryLine(bt.Indicator): lines = (‘entryprice’,) plotinfo = dict(subplot=False) plotlines = dict(entryprice=dict(ls=‘-’, lw=1, color=‘red’))

    def init(self): self.addminperiod(self.data.size())

    def prenext(self): self.lines.entryprice[0] = float(‘nan’)

    def next(self): self.lines.entryprice[0] = float(‘nan’)

    def set_entryprice(self, price): for index in range(-1, -self.data.size() - 1, -1): self.lines.entryprice[index] = price改成任意直接,传入的是K线的时间截和收盘价

  7. Assistant

    以下是修改后的代码,支持通过传入K线时间戳和收盘价的方式来设置 EntryLine 的值:

    class EntryLine(bt.Indicator):
        lines = ('entryprice',)
        plotinfo = dict(subplot=False)
        plotlines = dict(entryprice=dict(ls='-', lw=1, color='red'))
    
        def __init__(self, kline_time, close_price):
            self.kline_time = kline_time
            self.close_price = close_price
            super().__init__()
    
        def prenext(self):
            self.lines.entryprice[0] = float('nan')
    
        def next(self):
            self.lines.entryprice[0] = float('nan')
    
        def set_entryprice(self, kline_time, price):
            index = -self.data.close.get(size=0, ago=0).size
            while index < 0:
                if kline_time == self.kline_time[index]:
                    self.lines.entryprice[index] = price
                    break
                else:
                    index += 1
    

    在初始化 Indicator 时,需要传入两个参数 kline_time 和 close_price,这两个参数的长度应该与 Indicator 中的数据长度相同。在 set_entryprice 方法中,根据传入的 kline_time 和 close_price 来为 Indicator 对应的位置设置 entryprice 值,如果没有找到对应的位置,则不做任何操作。

    需要注意的是,由于使用了循环遍历的方式来查找对应位置,如果数据量较大,会比较耗时。另外,该实现假设数据已按照时间顺序排列,并且传入的数据不存在重复时间戳。如果数据写入存在问题,可能会导致结果不准确。

  8. User

    请解析plotlines = dict(entryprice=dict(ls=‘-’, lw=1, color=‘red’))此句是如何工作的

  9. Assistant

    这里的 plotlines 变量是一个字典,用于设置每个需要绘制的指标线条的颜色、样式等属性,以及是否需要在图表上显示。

    在这里,plotlines 字典中设置了 entryprice 这条线条的属性,它的值是另一个字典,其中 ls、lw、color 分别表示线条的样式、宽度和颜色。

    具体来说,ls=‘-’ 表示线条样式为实线,lw=1 表示线条宽度为 1,color=‘red’ 表示线条颜色为红色。这些属性设置的结果,就是在 Matplotlib 绘制图表时,可以根据这些属性对应的取值对线条进行样式设置,使其在图表上更加美观直观。

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