Exemple #1
0
    def _create_daily_symbol_bars(self, start_date, end_date):
        """
        Given a trade date range, query daily bars of all symbols in that range and return a list.
        
        Parameters
        ----------
        start_date : int
            Trade date.
        end_date : int
            Trade date.

        Returns
        -------
        res : list of tuples
            Two-element tuple: (trade_date, dict of quotes)

        """
        # query quotes data
        symbols_str = ','.join(self.ctx.universe)
        df_daily = self._get_df_daily(symbol=symbols_str, start_date=start_date, end_date=end_date)
        df_daily['date'] = df_daily['trade_date']
        if df_daily is None or df_daily.empty:
            return dict()

        # create nested dict
        df_daily = df_daily.sort_values(['trade_date', 'symbol'])
        res = []
        for date, df in df_daily.groupby(by='trade_date'):
            quotes_list = Bar.create_from_df(df)
            dic = {quote.symbol: quote for quote in quotes_list}
            res.append((date, dic))
    
        return res
Exemple #2
0
    def _run_daily(self):
        """Quotes of different symbols will be aligned into one dictionary."""
        from collections import defaultdict

        symbols_str = ','.join(self.ctx.universe)
        df_daily, msg = self.ctx.data_api.daily(symbol=symbols_str,
                                                start_date=self.start_date,
                                                end_date=self.end_date,
                                                adjust_mode='post')
        if msg != '0,':
            print(msg)
        if df_daily is None or df_daily.empty:
            return dict()

        # create nested dict
        quotes_list = Bar.create_from_df(df_daily)

        dic = defaultdict(dict)
        for quote in quotes_list:
            dic[quote.trade_date][quote.symbol] = quote

        dates = sorted(dic.keys())
        for i in range(len(dates) - 1):
            d1, d2 = dates[i], dates[i + 1]
            self.on_new_day(d2)

            quote1 = dic.get(d1)
            quote2 = dic.get(d2)
            self._process_quote_daily(quote1, quote2)

            self.on_after_market_close()
Exemple #3
0
    def _create_time_symbol_bars(self, date):
        """
        Given a trade date, query bars of all symbols on that day and return a nested dict.
        
        Parameters
        ----------
        date : int
            Trade date.

        Returns
        -------
        res : list of tuples
            Three-element tuple: (trade_date, time, dict of quote)

        """
        # query quotes data
        symbols_str = ','.join(self.ctx.universe)
        df_quotes = self._get_df_bar(symbols_str, date)
        if df_quotes is None or df_quotes.empty:
            return dict()
    
        # create nested dict
        df_quotes = df_quotes.sort_values(['date', 'time', 'symbol'])
        res = []
        for time, df in df_quotes.groupby(by=['time'], sort=False):
            quotes_list = Bar.create_from_df(df)
            dic = {quote.symbol: quote for quote in quotes_list}
            res.append((time, dic))
        return res
Exemple #4
0
    def _create_daily_symbol_bars(self, start_date, end_date):
        """
        Given a trade date range, query daily bars of all symbols in that range and return a list.
        
        Parameters
        ----------
        start_date : int
            Trade date.
        end_date : int
            Trade date.

        Returns
        -------
        res : list of tuples
            Two-element tuple: (trade_date, dict of quotes)

        """
        # query quotes data
        symbols_str = ','.join(self.ctx.universe)
        df_daily = self._get_df_daily(symbol=symbols_str, start_date=start_date, end_date=end_date)
        if df_daily is None or df_daily.empty:
            return dict()

        # create nested dict
        df_daily = df_daily.sort_values(['trade_date', 'symbol'])
        res = []
        for date, df in df_daily.groupby(by='trade_date'):
            quotes_list = Bar.create_from_df(df)
            dic = {quote.symbol: quote for quote in quotes_list}
            res.append((date, dic))
    
        return res
Exemple #5
0
    def _create_time_symbol_bars(self, date):
        """
        Given a trade date, query bars of all symbols on that day and return a nested dict.
        
        Parameters
        ----------
        date : int
            Trade date.

        Returns
        -------
        res : list of tuples
            Three-element tuple: (trade_date, time, dict of quote)

        """
        # query quotes data
        symbols_str = ','.join(self.ctx.universe)
        df_quotes = self._get_df_bar(symbols_str, date)
        if df_quotes is None or df_quotes.empty:
            return dict()
    
        # create nested dict
        df_quotes = df_quotes.sort_values(['trade_date', 'time', 'symbol'])
        res = []
        for (date, time), df in df_quotes.groupby(by=['trade_date', 'time']):
            quotes_list = Bar.create_from_df(df)
            dic = {quote.symbol: quote for quote in quotes_list}
            res.append((date, time, dic))
        
        return res
Exemple #6
0
def test_bar():
    from jaqs.data import RemoteDataService
    from jaqs.trade.common import QUOTE_TYPE
    ds = RemoteDataService()
    ds.init_from_config(data_config)
    
    df_quotes, msg = ds.bar(symbol='rb1710.SHF,hc1710.SHF', start_time=200000, end_time=160000,
                            trade_date=20170704, freq=QUOTE_TYPE.MIN)
    bar_list = Bar.create_from_df(df_quotes)
    bar = bar_list[0]
    print(str(bar))
Exemple #7
0
def test_bar():
    from jaqs.data import RemoteDataService
    from jaqs.trade.common import QUOTE_TYPE
    ds = RemoteDataService()
    ds.init_from_config(data_config)

    df_quotes, msg = ds.bar(symbol='rb1710.SHF,hc1710.SHF',
                            start_time=200000,
                            end_time=160000,
                            trade_date=20170704,
                            freq=QUOTE_TYPE.MIN)
    quotes_list = Bar.create_from_df(df_quotes)
Exemple #8
0
 def _create_time_symbol_bars(self, date):
     from collections import defaultdict
     
     # query quotes data
     symbols_str = ','.join(self.ctx.universe)
     df_quotes, msg = self.ctx.data_api.bar(symbol=symbols_str, start_time=200000, end_time=160000,
                                            trade_date=date, freq=self.bar_type)
     if msg != '0,':
         print(msg)
     if df_quotes is None or df_quotes.empty:
         return dict()
 
     # create nested dict
     quotes_list = Bar.create_from_df(df_quotes)
     
     dic = defaultdict(dict)
     for quote in quotes_list:
         dic[jutil.combine_date_time(quote.date, quote.time)][quote.symbol] = quote
     return dic