示例#1
0
 def compute(self):
     """
      构建时间序列变量,执行指标的向量算法。
     """
     if not hasattr(self, '_args'):
         raise Exception("每个指标都必须有_args属性,代表指标计算的参数!")
     self.data = self._args[0]
     # 数据转化成ta-lib能处理的格式
     #self._args[0] = ndarray(self._args[0])
     apply(self._vector_algo, tuple(self._args))
     if not hasattr(self, 'values'):
         raise Exception("每个指标都必须有value属性,代表指标计算结果!")
     if isinstance(self.values, dict):
         self.series = OrderedDict()
         for key, value in self.values.iteritems():
             self.series[key] = series.NumberSeries(
                 value, self.name, self, float('nan'))
         for key, value in self.series.iteritems():
             setattr(self, key, value)
         self.is_multiple = True
     else:
         self.series = [series.NumberSeries(
             self.values, self.name, self, float('nan'))]
         self.is_multiple = False
     self._init_bound()
示例#2
0
 def compute(self):
     """ 
      构建时间序列变量,执行指标的向量算法。
     """
     if not hasattr(self, '_args'):
         raise Exception("每个指标都必须有_args属性,代表指标计算的参数!")
     self.data = self._args[0]
     if not series.g_rolling:
         # 数据转化成ta-lib能处理的格式
         self._args[0] = transform2ndarray(self._args[0])
         apply(self._vector_algo, tuple(self._args))
     if not hasattr(self, 'values'):
         raise Exception("每个指标都必须有value属性,代表指标计算结果!")
     if isinstance(self.values, dict):
         self.series = OrderedDict()
         for key, value in self.values.iteritems():
             self.series[key] = series.NumberSeries(value, len(value),
                                                 self.name, self, float('nan'))
         for key, value in self.series.iteritems():
             setattr(self, key, value)
         self.multi_value = True
     else:
         self.series = [series.NumberSeries(self.values, len(self.values),
                         self.name, self, float('nan'))]
         self.multi_value = False
     if series.g_rolling:
         if self.multi_value:
             for key, value in self.series.iteritems():
                 value.reset_data([], 1+series.g_window)
         else:
             for s in self.series:
                 s.reset_data([], 1+series.g_window)
     self._init_bound()
示例#3
0
 def wrapper(self, *args, **kwargs):
     magic = inspect.getargspec(method)
     arg_names = magic.args[1:]
     # 默认参数
     default = dict(
         (x, y)
         for x, y in zip(magic.args[-len(magic.defaults):], magic.defaults))
     # 调用参数
     method_args = {}
     for i, arg in enumerate(args):
         method_args[arg_names[i]] = arg
     method_args.update(kwargs)
     #
     default.update(method_args)
     # 属性创建
     for key, value in default.iteritems():
         setattr(self, key, value)
     # 构造函数
     rst = method(self, *args, **kwargs)
     if not hasattr(self, 'value'):
         raise Exception("每个指标都必须有value属性,代表指标值!")
     else:
         # 序列变量
         if self.tracker:
             if isinstance(self.value, tuple):
                 self._series = [
                     series.NumberSeries(self.tracker, value)
                     for value in self.value
                 ]
             else:
                 self._series = series.NumberSeries(self.tracker,
                                                    self.value)
         # 绘图中的y轴范围未被设置,使用默认值。
         if not self.upper:
             upper = lower = []
             if isinstance(self.value, tuple):
                 # 多值指标
                 upper = [
                     max([value[i] for value in self.value])
                     for i in xrange(0, len(self.value[0]))
                 ]
                 lower = [
                     min([value[i] for value in self.value])
                     for i in xrange(0, len(self.value[0]))
                 ]
             else:
                 upper = self.value
                 lower = self.value
             self.set_yrange(lower, upper)
         return rst
示例#4
0
 def _init_by_subclass(self, data):
     # 数据转化成ta-lib能处理的格式
     # self.value为任何支持index的数据结构。
     # 在策略中,price变量可能为NumberSeries,需要用NUMBER_SERIES_SUPPORT处理,
     # 转化为numpy.ndarray等能被指标函数处理的参数。
     if not series.g_rolling:
         # 向量化运行的均值函数
         data = transform2ndarray(data)
         args =  (data, ) + self._args
         apply(self._vector_algo, args)
     #if not hasattr(self, 'value'):
         #raise Exception("每个指标都必须有value属性,代表指标值!")
     #else:
     if isinstance(self.value, dict):
         self.series = OrderedDict()
         for key, value in self.value.iteritems():
             self.series[key] = series.NumberSeries(value, len(value),
                                                 self.name, self, float('nan'))
         for key, value in self.series.iteritems():
             setattr(self, key, value)
         self.multi_value = True
     else:
         self.series = [series.NumberSeries(self.value, len(self.value),
                         self.name, self, float('nan'))]
         self.multi_value = False
     if series.g_rolling:
         if self.multi_value:
             for key, value in self.series.iteritems():
                 value.reset_data([], 1+series.g_window)
         else:
             for s in self.series:
                 s.reset_data([], 1+series.g_window)
     
     # 绘图中的y轴范围未被设置,使用默认值。
     if not self._upper:
         upper = lower = []
         if isinstance(self.value, tuple):
             # 多值指标
             upper = [ max([value[i] for value in self.value ]) 
                          for i in xrange(0, len(self.value[0]))]
             lower = [ min([value[i] for value in self.value ]) 
                           for i in xrange(0, len(self.value[0]))]
         else:
             upper = self.value
             lower = self.value
         self.set_yrange(lower, upper)