def _load_data(self, strpcons, dt_start, dt_end, n, spec_date): all_data = OrderedDict() max_window = -1 log.info("loading data...") pcontracts = [PContract.from_string(s) for s in strpcons] pcontracts = sorted(pcontracts, key=PContract.__str__, reverse=True) for i, pcon in enumerate(pcontracts): strpcon = str(pcon) if strpcon in spec_date: dt_start = spec_date[strpcon][0] dt_end = spec_date[strpcon][1] assert (dt_start < dt_end) if n: raw_data = self._data_manager.get_last_bars(strpcon, n) else: raw_data = self._data_manager.get_bars(strpcon, dt_start, dt_end) if len(raw_data) == 0: continue all_data[strpcon] = raw_data max_window = max(max_window, len(raw_data)) if n: assert (max_window <= n) if len(all_data) == 0: assert (False) # @TODO raise return all_data, max_window
def _load_data(self, strpcons, dt_start, dt_end, n, spec_date): all_data = OrderedDict() max_window = -1 logger.info("loading data...") pbar = ProgressBar().start() pcontracts = [PContract.from_string(s) for s in strpcons] pcontracts = sorted(pcontracts, reverse=True) for i, pcon in enumerate(pcontracts): strpcon = str(pcon) if strpcon in spec_date: dt_start = spec_date[strpcon][0] dt_end = spec_date[strpcon][1] assert(dt_start < dt_end) if n: wrapper = self._data_manager.get_last_bars(strpcon, n) else: wrapper = self._data_manager.get_bars(strpcon, dt_start, dt_end) if len(wrapper) == 0: continue all_data[strpcon] = DataContext(wrapper) max_window = max(max_window, len(wrapper)) pbar.update(i*100.0/len(strpcons)) # progressbar.log('') if n: assert(max_window <= n) pbar.finish() if len(all_data) == 0: assert(False) # @TODO raise return all_data, max_window
def _load_data(self, strpcons, dt_start, dt_end, n, spec_date): all_data = OrderedDict() max_window = -1 logger.info("loading data...") pbar = ProgressBar().start() pcontracts = [PContract.from_string(s) for s in strpcons] pcontracts = sorted(pcontracts, reverse=True) for i, pcon in enumerate(pcontracts): strpcon = str(pcon) if strpcon in spec_date: dt_start = spec_date[strpcon][0] dt_end = spec_date[strpcon][1] assert (dt_start < dt_end) if n: wrapper = self._data_manager.get_last_bars(strpcon, n) else: wrapper = self._data_manager.get_bars(strpcon, dt_start, dt_end) if len(wrapper) == 0: continue all_data[strpcon] = DataContext(wrapper) max_window = max(max_window, len(wrapper)) pbar.update(i * 100.0 / len(strpcons)) # progressbar.log('') if n: assert (max_window <= n) pbar.finish() if len(all_data) == 0: assert (False) # @TODO raise return all_data, max_window
def import_bars(self, data_iter, strpcon): """ 导入交易数据 Args: data_iter (iteratorable object): 数据['datetime', 'open', 'close', 'high', 'low', 'volume'] strpcon (str): 周期合约字符串如, 'AA.SHFE-1.Minute' """ pcontract = PContract.from_string(strpcon) self._src.import_bars(data_iter, pcontract)
def __getitem__(self, strpcon): """ 获取跨品种合约 """ ## @TODO #if type(strpcon) == str: #pass #self._cur_data_context = self._data_contexts[pcon] ## @TODO 字典,str做key tt = PContract.from_string(strpcon) for key, value in self._data_contexts.iteritems(): if str(key) == str(tt): return value
def data(self, strpcon=None): """ 周期合约数据 Args: strpcon (str): 周期合约,如'BB.SHFE-1.Minute' Returns: pd.DataFrame. 数据 """ pcon = self._main_pcontract if strpcon: pcon = PContract.from_string(strpcon) return self._dcontexts[pcon].raw_data
def get_bars(self, strpcon, dt_start, dt_end): """ 获取本地历史数据 Args: strpcon (str): 周期合约 dt_start (datetime): 数据的开始时间 dt_end (datetime): 数据的结束时间 Returns: SourceWrapper. 数据 """ pcontract = PContract.from_string(strpcon) return self._src.get_bars(pcontract, dt_start, dt_end)
def get_bars(self, strpcon, dt_start, dt_end): """ 获取本地历史数 Args: strpcon (str): 周期合约 dt_start (datetime): 数据的开始时间 dt_end (datetime): 数据的结束时间 Returns: SourceWrapper. 数据 """ pcontract = PContract.from_string(strpcon) return self._src.get_bars(pcontract, dt_start, dt_end)
def data(self, strpcon=None): """ 周期合约数据, 只有向量运行才有意义。 Args: strpcon (str): 周期合约,如'BB.SHFE-1.Minute' Returns: pd.DataFrame. 数据 """ pcon = self._main_pcontract if strpcon: pcon = PContract.from_string(strpcon) return self._dcontexts[pcon].raw_data
def data(self, strpcon=None): """ 周期合约数据, 只有向量运行才有意义。 Args: strpcon (str): 周期合约,如'BB.SHFE-1.Minute' Returns: pd.DataFrame. 数据 """ if series.g_rolling: assert(False and '只有向量运行才存在数据') pcon = self._main_pcontract if strpcon: pcon = PContract.from_string(strpcon) return self._dcontexts[pcon].raw_data
def data(self, strpcon=None): """ 周期合约数据, 只有向量运行才有意义。 Args: strpcon (str): 周期合约,如'BB.SHFE-1.Minute' Returns: pd.DataFrame. 数据 """ if series.g_rolling: assert (False and '只有向量运行才存在数据') pcon = self._main_pcontract if strpcon: pcon = PContract.from_string(strpcon) return self._dcontexts[pcon].raw_data
def get_bars(self, strpcon, dt_start, dt_end): """ 获取本地历史数据 Args: strpcon (str): 周期合约 dt_start (datetime): 数据的开始时间 dt_end (datetime): 数据的结束时间 Returns: SourceWrapper. 数据 """ pcontract = PContract.from_string(strpcon) if pcontract.contract.exchange == 'stock': return [] else: return self._src.get_bars(pcontract, dt_start, dt_end)
def indicators(self, j=None, strpcon=None): """ 返回第j个策略的指标, 默认返回组合的所有指标。 Args: j (int): 第j个策略 strpcon (str): 周期合约 Returns: dict. {指标名:指标} """ pcon = PContract.from_string(strpcon) if strpcon else self._main_pcontract if j != None: return self._dcontexts[pcon].indicators[self.i][j] rst = { } for j in range(0, len(self._blts)): rst.update(self._dcontexts[pcon].indicators[self.i][j]) return rst
def _tranform_data(self, data: "Dict((strpcon, DataFrame))"): for s_pcontract, raw_data in six.iteritems(data): original = OriginalData(PContract.from_string(s_pcontract), raw_data) derived = DerivedData() pcontract_data = PContractData(s_pcontract, original, derived) self._all_pcontract_data[s_pcontract] = pcontract_data # PContract -- 'IF000.SHEF-10.Minutes' # 简化策略用户的合约输入。 symbol_exchange = s_pcontract.split('-')[0] same_contracts = list( filter(lambda x: x.startswith(symbol_exchange), data.keys())) if len(same_contracts) == 1: self._all_pcontract_data[symbol_exchange] = pcontract_data symbol = s_pcontract.split('.')[0] num_same_contract = list( filter(lambda x: x.startswith(symbol), data.keys())) if len(same_contracts) == 1: self._all_pcontract_data[symbol] = pcontract_data
def get_bars(self, strpcon, dt_start, dt_end, window_size): """ 获取本地历史数据 Args: strpcon (str): 周期合约 dt_start (datetime): 数据的开始时间 dt_end (datetime): 数据的结束时间 window_size (int): 窗口大小,0表示大小为数据长度。 Returns: SourceWrapper. 数据 """ pcontract = PContract.from_string(strpcon) if pcontract.contract.exchange == 'stock': return [] else: return self._src.get_bars(pcontract, dt_start, dt_end, window_size)
def load_bars(self, strpcon, dt_start, dt_end, window_size): """ 获取本地历史数据 Args: strpcon (str): 周期合约 dt_start (datetime): 数据的开始时间 dt_end (datetime): 数据的结束时间 window_size (int): 窗口大小,0表示大小为数据长度。 Returns: SourceWrapper. 数据 """ pcontract = PContract.from_string(strpcon) if pcontract.contract.exch_type == 'stock': return [] else: return self._src.load_bars(pcontract, dt_start, dt_end, window_size);
def set_symbols(pcons, window_size, dt_start=datetime(1980,1,1), dt_end=datetime(2100,1,1)): """ 添加数据 Args: pcons ([str,]): 周期合约数组 dt_start (str): 开始时间 dt_end (str): 结束 window_size (int): 序列数据的窗口大小 """ global g_simulator new_pcons = [] for pcon in pcons: new_pcons.append(PContract.from_string(pcon)) g_simulator = ExecuteUnit(new_pcons, window_size, dt_start, dt_end) return g_simulator
def get_bars(self, strpcon, dt_start, dt_end): pcontract = PContract.from_string(strpcon) if pcontract.contract.exchange == 'stock': return self.get_tushare_bars(pcontract, dt_start, dt_end) else: return []
def get_bars(self, strpcon, dt_start=DEFAULT_DT_START, dt_end=DEFAULT_DT_END): pcontract = PContract.from_string(strpcon) return self._src.get_bars(pcontract, dt_start, dt_end)
def get_all_pcontracts(self): # 模拟接口 data = ['CC.SHFE-1.MINUTE', 'BB.SHFE-1.MINUTE'] pcontracts = [PContract.from_string(d) for d in data] return serialize_all_pcontracts(pcontracts)
def setUp(self): cache = LocalFsCache(TestCache.CACHE_PATH) self.src = _MockSource() self.ds = CachedDatasource(self.src, cache) self.pcontract = PContract.from_string('000001.SH-1.DAY')
def get_last_bars(self, strpcon, n): pcontract = PContract.from_string(strpcon) return self._src.get_last_bars(pcontract, n)
def load_bars(self, strpcon, dt_start, dt_end): pcontract = PContract.from_string(strpcon) if pcontract.contract.exch_type == 'stock': return self.load_tushare_bars(pcontract, dt_start, dt_end) else: return []
def deserialize_all_pcontracts(pcontracts): return [PContract.from_string(strpcon) for strpcon in pcontracts]
def get_all_contracts(self): # 模拟接口 data = ['CC.SHFE-1.MINUTE', 'BB.SHFE-1.MINUTE'] pcons = [PContract.from_string(d) for d in data] contracts = [pcon.contract for pcon in pcons] return serialize_all_contracts(contracts)