简单说明dtype使用方法:
Rule为规则类,其中有3个字段,1为类型,2为计算规则,3为保留位数
如 :'close': Rule(float64, 1 / 10000.0, 2),
表示收盘价,Rule有三个字段,首为浮点类型,次为计算规则,末为保留小数位数
Converter类则是Rule字典集合,这里表示的为open,close等为key的集合
# /usr/bin/python3# -*- encoding: utf-8 -*-from collections import namedtuple
import numpy as np
float64 = np.dtype('float64')Rule = namedtuple('Rule', ['dtype', 'multiplier', 'round'])class Converter(object):
def __init__(self, rules):
self._rules = rulesdef convert(self, name, data):try:r = self._rules[name]
except KeyError:return dataresult = data * r.multiplier
if r.round:
result = np.round(result, r.round)return result
def field_type(self, name):try:return self._rules[name].dtypeexcept KeyError:return self._rules['open'].dtypeif __name__ == '__main__':StockBarConverter = Converter({'open': Rule(float64, 1 / 10000.0, 4),
'close': Rule(float64, 1 / 10000.0, 2),
'high': Rule(float64, 1 / 10000.0, 2),'low': Rule(float64, 1 / 10000.0, 2),'limit_up': Rule(float64, 1/10000.0, 2),'limit_down': Rule(float64, 1/10000.0, 2),'volume': Rule(float64, 1, 0),})_converter = StockBarConverterdata = np.array([1001, 2103])
result = _converter.convert('open', data)open_type = _converter.field_type("open")
print("open_type: ", open_type) #open_type: float64