Exemple #1
0
 def __init__(self, wrapper, window_size):
     """ window_size: 滚动窗口大小 """
     self.wrapper = wrapper
     self.window_size = window_size
     self.series = [[{}]]
     self.indicators = [[{}]]
     self.variables = [[{}]]
     data = self.wrapper.data
     if window_size == 0:
         self.window_size = len(data)
     self.open = NumberSeries(data.open.values, self.window_size, "open")
     self.close = NumberSeries(data.close.values, self.window_size, "close")
     self.high = NumberSeries(data.high.values, self.window_size, "high")
     self.low = NumberSeries(data.low.values, self.window_size, "low")
     self.volume = NumberSeries(data.volume.values, self.window_size, "volume")
     self.datetime = DateTimeSeries(data.index, self.window_size, "datetime")
     self.i = -1  # 第i个组合
     self.j = -1  # 第j个策略
     self.curbar = 0
     self.bar = Bar(None, None, None, None, None, None)
Exemple #2
0
class DataContext(object):
    def __init__(self, wrapper, window_size):
        """ window_size: 滚动窗口大小 """
        self.wrapper = wrapper
        self.window_size = window_size
        self.series = [[{}]]
        self.indicators = [[{}]]
        self.variables = [[{}]]
        data = self.wrapper.data
        if window_size == 0:
            self.window_size = len(data)
        self.open = NumberSeries(data.open.values, self.window_size, "open")
        self.close = NumberSeries(data.close.values, self.window_size, "close")
        self.high = NumberSeries(data.high.values, self.window_size, "high")
        self.low = NumberSeries(data.low.values, self.window_size, "low")
        self.volume = NumberSeries(data.volume.values, self.window_size, "volume")
        self.datetime = DateTimeSeries(data.index, self.window_size, "datetime")
        self.i = -1  # 第i个组合
        self.j = -1  # 第j个策略
        self.curbar = 0
        self.bar = Bar(None, None, None, None, None, None)

    @property
    def raw_data(self):
        return self.wrapper.data

    @property
    def pcontract(self):
        return self.wrapper.pcontract

    @property
    def contract(self):
        return self.wrapper.pcontract.contract

    def rolling_foward(self):
        ## @todo next
        """ 
        滚动读取下一步的数据。
        """
        new_row, self.curbar = self.wrapper.rolling_foward()
        if not new_row:
            self.curbar -= 1
            return False

        # 为当天数据预留空间, 改变g_window或者一次性分配
        # self.data = np.append(data, tracker.container_day)
        self.open.update_curbar(self.curbar)
        self.close.update_curbar(self.curbar)
        self.high.update_curbar(self.curbar)
        self.low.update_curbar(self.curbar)
        self.volume.update_curbar(self.curbar)
        self.datetime.update_curbar(self.curbar)
        # 更新数据源
        if series.g_rolling:
            self.datetime.update(new_row[0])
            self.open.update(new_row[1])
            self.close.update(new_row[2])
            self.high.update(new_row[3])
            self.low.update(new_row[4])
            self.volume.update(new_row[5])

        self.bar.datetime = self.datetime[0]
        self.bar.open = self.open[0]
        self.bar.close = self.close[0]
        self.bar.high = self.high[0]
        self.bar.low = self.low[0]
        self.bar.volume = self.volume[0]
        return True

    def update_user_vars(self):
        """ 更新用户定义的变量。 """
        try:
            siter = self.series[self.i][self.j].iteritems()
        except Exception:
            pass
        else:
            for key, s in siter:
                s.update_curbar(self.curbar)
                s.duplicate_last_element()
        try:
            indic_iter = self.indicators[self.i][self.j].iteritems()
        except Exception:
            pass
        else:
            for key, indic in indic_iter:
                if indic.multi_value:
                    for key, value in indic.series.iteritems():
                        value.update_curbar(self.curbar)
                else:
                    for s in indic.series:
                        s.update_curbar(self.curbar)

    def add_series(self, key, s):
        """ 添加on_init中初始化的序列变量    
        
        Args:
            key (str): 属性名

            s (Series): 序列变量 

        """
        s.reset_data([], self.window_size + series.g_window)
        if self.i < len(self.series):
            if self.j < len(self.series[self.i]):
                self.series[self.i][self.j][key] = s
            else:
                self.series[self.i].append({key: s})
        else:
            self.series.append([{key: s}])
        return

    def add_indicator(self, key, indic):
        if self.i < len(self.indicators):
            if self.j < len(self.indicators[self.i]):
                self.indicators[self.i][self.j][key] = indic
            else:
                self.indicators[self.i].append({key: indic})
        else:
            self.indicators.append([{key: indic}])

    def add_variable(self, key, var):
        if self.i < len(self.variables):
            if self.j < len(self.variables[self.i]):
                self.variables[self.i][self.j][key] = var
            else:
                self.variables[self.i].append({key: var})
        else:
            self.variables.append([{key: var}])

    def __len__(self):
        return len(self.wrapper)

    def get_item(self, name):
        """ 获取用户在策略on_init函数中初始化的变量
        
        Args:
            name (str): 变量名
        """
        try:
            return self.indicators[self.i][self.j][name]
        except KeyError:
            try:
                return self.series[self.i][self.j][name]
            except KeyError:
                return self.variables[self.i][self.j][name]

    def add_item(self, name, value):
        """
        添加用户初始化的变量。
        """
        if isinstance(value, series.SeriesBase):
            self.add_series(name, value)
        elif isinstance(value, IndicatorBase):
            self.add_indicator(name, value)
        else:
            self.add_variable(name, value)

    def __getattr__(self, name):
        return self.get_item(name)