Пример #1
0
def test_remote_data_service_fin_indicator():
    ds = RemoteDataService()
    
    symbol = '000008.SZ'
    filter_argument = ds._dic2url({'symbol': symbol})
    
    df_raw, msg = ds.query("lb.finIndicator", fields="",
                           filter=filter_argument, orderby="symbol")
    print
Пример #2
0
def test_remote_data_service_daily_ind_performance():
    ds = RemoteDataService()
    
    hs300 = ds.get_index_comp('000300.SH', 20140101, 20170101)
    hs300_str = ','.join(hs300)
    
    fields = "pb,pe,share_float_free,net_assets,limit_status"
    res, msg = ds.query("lb.secDailyIndicator", fields=fields,
                          filter=("symbol=" + hs300_str
                                  + "&start_date=20160907&end_date=20170907"),
                          orderby="trade_date")
    assert msg == '0,'
Пример #3
0
def test_remote_data_service_lb():
    ds = RemoteDataService()
    
    # test lb.secDailyIndicator
    fields = "pb,pe,share_float_free,net_assets,limit_status"
    for res3, msg3 in [ds.query("lb.secDailyIndicator", fields=fields,
                                filter="symbol=600030.SH&start_date=20170907&end_date=20170907",
                                orderby="trade_date"),
                       ds.query_lb_dailyindicator('600030.SH', 20170907, 20170907, fields)]:
        assert msg3 == '0,'
        assert abs(res3.loc[0, 'pb'] - 1.5135) < 1e-4
        assert abs(res3.loc[0, 'share_float_free'] - 781496.5954) < 1e-4
        assert abs(res3.loc[0, 'net_assets'] - 1.437e11) < 1e8
        assert res3.loc[0, 'limit_status'] == 0
    
    # test lb.income
    for res4, msg4 in [ds.query("lb.income", fields="",
                                filter="symbol=600000.SH&start_date=20150101&end_date=20170101&report_type=408001000",
                                order_by="report_date"),
                       ds.query_lb_fin_stat('income', '600000.SH', 20150101, 20170101, fields="")]:
        assert msg4 == '0,'
        assert res4.shape == (8, 12)
        assert abs(res4.loc[4, 'oper_rev'] - 120928000000) < 1
Пример #4
0
class Calendar(object):
    """
    A calendar for manage trade date.
    
    Attributes
    ----------
    data_api :

    """
    def __init__(self, data_api=None):
        if data_api is None:
            self.data_api = RemoteDataService()
        else:
            self.data_api = data_api

    def get_trade_date_range(self, begin, end):
        """
        Get array of trade dates within given range.
        Return zero size array if no trade dates within range.
        
        Parameters
        ----------
        begin : int
            YYmmdd
        end : int

        Returns
        -------
        trade_dates_arr : np.ndarray
            dtype = int

        """
        filter_argument = self.data_api._dic2url({
            'start_date': begin,
            'end_date': end
        })

        df_raw, msg = self.data_api.query("jz.secTradeCal",
                                          fields="trade_date",
                                          filter=filter_argument,
                                          orderby="")
        if df_raw.empty:
            return np.array([], dtype=int)

        trade_dates_arr = df_raw['trade_date'].values.astype(int)
        return trade_dates_arr

    def get_last_trade_date(self, date):
        """
        
        Parameters
        ----------
        date : int

        Returns
        -------
        res : int

        """
        dt = dtutil.convert_int_to_datetime(date)
        delta = pd.Timedelta(weeks=2)
        dt_old = dt - delta
        date_old = dtutil.convert_datetime_to_int(dt_old)

        dates = self.get_trade_date_range(date_old, date)
        mask = dates < date
        res = dates[mask][-1]

        return res

    def is_trade_date(self, date):
        """
        Check whether date is a trade date.

        Parameters
        ----------
        date : int

        Returns
        -------
        bool

        """
        dates = self.get_trade_date_range(date, date)
        return len(dates) > 0

    def get_next_trade_date(self, date):
        """
        
        Parameters
        ----------
        date : int

        Returns
        -------
        res : int

        """
        dt = dtutil.convert_int_to_datetime(date)
        delta = pd.Timedelta(weeks=2)
        dt_new = dt + delta
        date_new = dtutil.convert_datetime_to_int(dt_new)

        dates = self.get_trade_date_range(date, date_new)
        mask = dates > date
        res = dates[mask][0]

        return res