Ejemplo n.º 1
0
 def get_bars(self, pcontract, dt_start, dt_end, window_size):
     fname = os.path.join(self._root, str(pcontract) + ".csv")
     if not series.g_rolling:
         try:
             data = pd.read_csv(fname, index_col=0, parse_dates=True)
         except IOError:
             raise FileDoesNotExist(file=fname)
         else:
             dt_start = pd.to_datetime(dt_start)
             dt_end = pd.to_datetime(dt_end)
             data = data[(dt_start <= data.index) & (data.index <= dt_end)]
             #data.index = map(lambda x : int(time.mktime(x.timetuple())*1000), data.index)
             assert data.index.is_unique
             return CsvSourceWrapper(pcontract, data, None, len(data))
     else:
         data = pd.DataFrame({
             'open': [],
             'close': [],
             'high': [],
             'low': [],
             'volume': []
         })
         data.index = []
         cursor = csv.reader(open(fname, 'rb'))
         fmt = ['datetime', 'open', 'close', 'high', 'low', 'volume']
         header = cursor.next()
         if header[0:6] != fmt:
             raise DataFieldError(error_fields=header, right_fields=fmt)
         return CsvSourceWrapper(pcontract, data, cursor, window_size)
Ejemplo n.º 2
0
    def load_data(self, pcontract, dt_start=None, dt_end=None):
        """ 加载本地周期合约数据.
        
        Args:
            pcontract (PContract): 周期合约
        
        Returns:
            DataFrame. 

        Raises:
            FileDoesNotExist
        """
        if pcontract.contract.exch_type == 'stock':
            import tushare as ts
            # 使用tushare接口
            print "load stock data with tushare..."
            data = ts.get_hist_data(pcontract.contract.code)
            return process_tushare_data(data)
        else:
            # 期货数据
            fname = ''.join([str(pcontract), ".csv"])
            try:
                data = pd.read_csv(fname, index_col=0, parse_dates=True)
                assert data.index.is_unique
            except Exception:
                #print u"**Warning: File \"%s\" doesn't exist!"%fname
                raise FileDoesNotExist(file=fname)
            else:
                return data
Ejemplo n.º 3
0
 def _load_bars(self, pcontract):
     strpcon = str(pcontract).upper()
     contract, period = tuple(strpcon.split('-'))
     code, exch = tuple(contract.split('.'))
     period = period.replace('.', '')
     fname = os.path.join(self._root, period, exch, code + ".csv")
     try:
         data = pd.read_csv(fname, index_col=0, parse_dates=True)
     except IOError:
         raise FileDoesNotExist(file=fname)
     else:
         return data
Ejemplo n.º 4
0
 def set_source(self, settings):
     if settings['source'] == 'sqlite':
         try:
             import sqlite3
             path = os.path.join(settings['data_path'], 'digger.db')
             self._src = SqlLiteSource(path)
         except sqlite3.OperationalError:
             raise FileDoesNotExist(file=path)
     elif settings['source'] == 'csv':
         self._src = CsvSource(settings['data_path'])
     try:
         Contract.info = self.get_contracts()
     except Exception, e:
         ## @TODO
         pass
Ejemplo n.º 5
0
 def load_bars(self, pcontract, dt_start, dt_end):
     fname = ''.join([self._root, str(pcontract), ".csv"])
     try:
         data = pd.read_csv(fname, index_col=0, parse_dates=True)
         dt_start = pd.to_datetime(dt_start)
         dt_end = pd.to_datetime(dt_end)
         data = data[(dt_start <= data.index) & (data.index <= dt_end)]
         data.index = map(lambda x: int(time.mktime(x.timetuple()) * 1000),
                          data.index)
         assert data.index.is_unique
     except IOError:
         #print u"**Warning: File \"%s\" doesn't exist!"%fname
         raise FileDoesNotExist(file=fname)
     else:
         return data
Ejemplo n.º 6
0
    def load_data(self, pcontract, dt_start=None, dt_end=None):
        """ 加载本地周期合约数据.
        
        Args:
            pcontract (PContract): 周期合约
        
        Returns:
            DataFrame. 

        Raises:
            FileDoesNotExist
        """
        if pcontract.contract.exch_type == 'stock':
            import tushare as ts
            # 使用tushare接口
            print "load stock data with tushare... (start=%s,end=%s)" % (
                dt_start, dt_end)
            if (pcontract.period._type == 'Minute'):
                data = tick2period(pcontract.contract.code,
                                   str(pcontract.period)[:-3].replace('.', ''),
                                   start=dt_start,
                                   end=dt_end)
            elif (pcontract.period._type == 'Second'):
                data = tick2period(pcontract.contract.code,
                                   str(pcontract.period)[:-5].replace('.', ''),
                                   start=dt_start,
                                   end=dt_end)
            else:
                #日线直接调用
                data = ts.get_hist_data(pcontract.contract.code,
                                        start=dt_start,
                                        end=dt_end)

            return process_tushare_data(data)
        else:
            # 期货数据
            fname = ''.join([str(pcontract), ".csv"])
            try:
                data = pd.read_csv(fname, index_col=0, parse_dates=True)
                data = _filter_by_datetime_range(data, dt_start, dt_end)
                assert data.index.is_unique
            except Exception:
                #print u"**Warning: File \"%s\" doesn't exist!"%fname
                raise FileDoesNotExist(file=fname)
            else:
                return data