def quotation_server(redis_url): """ [sys_stock_realtime] quotation service, download market data into redis Multiple RQAlpha instance can use single market data service. """ import redis import time import json from .utils import get_realtime_quotes redis_client = redis.from_url(redis_url) from rqalpha.data.data_proxy import DataProxy config = parse_config({}, verify_config=False) data_source = BaseDataSource(config.base.data_bundle_path) data_proxy = DataProxy(data_source) order_book_id_list = sorted(ins.order_book_id for ins in data_proxy.all_instruments("CS")) def record_market_data(total_df): for order_book_id, item in total_df.iterrows(): redis_client[order_book_id] = json.dumps(item.to_dict()) while True: try: total_df = get_realtime_quotes(order_book_id_list, include_limit=True) except Exception as e: system_log.exception("get_realtime_quotes fail. {}", e) continue system_log.info("Fetching snapshots, size {}", len(total_df)) record_market_data(total_df) time.sleep(1)
def quotation_server(redis_url): """ [stock_realtime] quotation service, download market data into redis Multiple RQAlpha instance can use single market data service. """ import redis import time import json from .utils import get_realtime_quotes redis_client = redis.from_url(redis_url) from rqalpha.environment import Environment from rqalpha.data.data_proxy import DataProxy # 通过 parse_config 函数 获取 默认 data_bundle 的配置 # 由于 parse_config 会进行配置检查,如果没有设置 account 会提示报错,因此随意设置一个股票初始资金,来保证正确获取默认参数配置 config = parse_config({ 'base': { 'accounts': { 'stock': 10000 } } }) Environment(config) data_source = BaseDataSource(config.base.data_bundle_path) data_proxy = DataProxy(data_source) order_book_id_list = sorted(ins.order_book_id for ins in data_proxy.all_instruments("CS")) def record_market_data(total_df): for order_book_id, item in total_df.iterrows(): redis_client[order_book_id] = json.dumps(item.to_dict()) retry_cnt = 0 while True: try: total_df = get_realtime_quotes(order_book_id_list, include_limit=True) except (OSError, IOError) as e: system_log.exception("get_realtime_quotes socket error. retry {} {}", retry_cnt, e) time.sleep(retry_cnt * 2) retry_cnt += 1 continue system_log.info("Fetching snapshots, size {}", len(total_df)) record_market_data(total_df) time.sleep(1) retry_cnt = 0
def quotation_server(redis_url): """ [sys_stock_realtime] quotation service, download market data into redis Multiple RQAlpha instance can use single market data service. """ import redis import time import json from .utils import get_realtime_quotes redis_client = redis.from_url(redis_url) from rqalpha.data.data_proxy import DataProxy config = parse_config({}, verify_config=False) data_source = BaseDataSource(config.base.data_bundle_path) data_proxy = DataProxy(data_source) order_book_id_list = sorted(ins.order_book_id for ins in data_proxy.all_instruments("CS")) def record_market_data(total_df): for order_book_id, item in total_df.iterrows(): redis_client[order_book_id] = json.dumps(item.to_dict()) while True: try: total_df = get_realtime_quotes(order_book_id_list, include_limit=True) except Exception as e: system_log.exception("get_realtime_quotes fail. {}", e) continue system_log.info("Fetching snapshots, size {}", len(total_df)) record_market_data(total_df) time.sleep(1)
class RQAlphaDataBackend(DataBackend): """ 目前仅支持日数据 """ skip_suspended = True def __init__(self, bundle_path="~/.rqalpha/bundle"): try: import rqalpha except ImportError: print("-" * 50) print("Run `pip install rqalpha` to install rqalpha first") print("-" * 50) raise # # FIXME # import warnings # warnings.simplefilter(action="ignore", category=FutureWarning) from rqalpha.data.base_data_source import BaseDataSource from rqalpha.data.data_proxy import DataProxy self.data_proxy = DataProxy( BaseDataSource(os.path.expanduser(bundle_path))) def get_price(self, order_book_id, start, end, freq): """ :param order_book_id: e.g. 000002.XSHE :param start: 20160101 :param end: 20160201 :returns: :rtype: numpy.rec.array """ assert freq == "1d" start = get_date_from_int(start) end = get_date_from_int(end) bar_count = (end - start).days bars = self.data_proxy.history_bars(order_book_id, bar_count, freq, field=None, dt=datetime.datetime.combine( end, datetime.time(23, 59, 59))) if bars is None or len(bars) == 0: raise KeyError("empty bars {}".format(order_book_id)) bars = bars.copy() return bars def get_order_book_id_list(self): """获取所有的 """ import pandas as pd insts = self.data_proxy.all_instruments("CS") if isinstance(insts, pd.DataFrame): # for old version of RQAlpha return sorted(insts.order_book_id.tolist()) else: # for new version fo RQAlpha return sorted([inst.order_book_id for inst in insts]) def symbol(self, order_book_id): """获取order_book_id对应的名字 :param order_book_id str: 股票代码 :returns: 名字 :rtype: str """ return self.data_proxy.instruments(order_book_id).symbol def get_trading_dates(self, start, end): """获取所有的交易日 :param start: 20160101 :param end: 20160201 """ start = get_date_from_int(start) end = get_date_from_int(end) trading_dates = self.data_proxy.get_trading_dates(start, end).tolist() trading_dates = [get_int_date(dt.date()) for dt in trading_dates] return trading_dates
class RQAlphaDataBackend(DataBackend): """ 目前仅支持日数据 """ skip_suspended = True def __init__(self, bundle_path="~/.rqalpha/bundle", start_date="2010-01-01"): try: import rqalpha except ImportError: print("-" * 50) print("Run `pip install rqalpha` to install rqalpha first") print("-" * 50) raise # FIXME import warnings warnings.simplefilter(action="ignore", category=FutureWarning) from rqalpha.data.base_data_source import BaseDataSource from rqalpha.data.data_proxy import DataProxy self.analyse_start_date = start_date self.data_proxy = DataProxy( BaseDataSource(os.path.expanduser(bundle_path))) def get_price(self, order_book_id, start, end): """ :param order_book_id: e.g. 000002.XSHE :param start: 20160101 :param end: 20160201 :returns: :rtype: numpy.rec.array """ start = get_date_from_int(start) end = get_date_from_int(end) bar_count = (end - start).days bars = self.data_proxy.history_bars(order_book_id, bar_count, "1d", field=None, dt=datetime.datetime.combine( end, datetime.time(23, 59, 59))) if bars is None or len(bars) == 0: raise KeyError("empty bars {}".format(order_book_id)) bars = bars.copy() origin_bars = bars = bars.astype([('datetime', '<u8'), ('open', '<f8'), ('close', '<f8'), ('high', '<f8'), ('low', '<f8'), ('volume', '<f8'), ('total_turnover', '<f8')]) dtype = copy.deepcopy(bars.dtype) names = list(dtype.names) names[0] = "date" dtype.names = names bars = np.array(bars, dtype=dtype) bars["date"] = origin_bars["datetime"] / 1000000 return bars def get_order_book_id_list(self): """获取所有的 """ return sorted( self.data_proxy.all_instruments("CS").order_book_id.tolist()) def get_start_date(self): """获取回溯开始时间 """ return str(self.analyse_start_date) def symbol(self, order_book_id): """获取order_book_id对应的名字 :param order_book_id str: 股票代码 :returns: 名字 :rtype: str """ return self.data_proxy.instruments(order_book_id).symbol def get_trading_dates(self, start, end): """获取所有的交易日 :param start: 20160101 :param end: 20160201 """ start = get_date_from_int(start) end = get_date_from_int(end) trading_dates = self.data_proxy.get_trading_dates(start, end).tolist() trading_dates = [get_int_date(dt.date()) for dt in trading_dates] return trading_dates