def list_exchanges(regions=None, sec_types=None): """ List exchanges by security type and country as found on the IB website. Parameters ---------- regions : list of str, optional limit to these regions. Possible choices: north_america, europe, asia, global sec_types : list of str, optional limit to these securitiy types. Possible choices: STK, ETF, FUT, CASH, IND Returns ------- dict """ params = {} if sec_types: params["sec_types"] = sec_types if regions: params["regions"] = regions response = houston.get("/master/exchanges", params=params, timeout=180) houston.raise_for_status_with_json(response) return response.json()
def s3_pull_databases(service, codes=None, force=False): """ Pull database(s) from Amazon S3 to the db service. Parameters ---------- serivce : str, required only pull databases for this service (specify 'all' to pull all services) codes: list of str, optional only pull databases identified by these codes (omit to pull all databases for service) force: bool overwrite existing database if one exists (default is to fail if one exists) Returns ------- json status message """ params = {} if codes: params["codes"] = codes if force: params["force"] = force response = houston.get("/db/s3/{0}".format(service), params=params) houston.raise_for_status_with_json(response) return response.json()
def translate_conids(conids, from_domain=None, to_domain=None): """ Translate conids (contract IDs) from one domain to another. Only translations to and from the "main" domain (that is, the IB domain) are supported. Parameters ---------- conids : list of int, required the conids to translate from_domain : str, optional the domain to translate from. This is the domain of the provided conids. Possible choices: main, sharadar to_domain : str, optional the domain to translate to. Possible choices: main, sharadar Returns ------- dict dict of <from_domain conid>:<to_domain conid> Examples -------- Translate a DataFrame with IB conids as columns to one with Sharadar conids as columns, and mask columns that can't be translated: >>> ib_conids = list(df_with_ib_cols.columns) >>> ib_to_sharadar = translate_conids(ib_conids, to_domain="sharadar") >>> df_with_sharadar_cols = df_with_ib_cols.rename(columns=ib_to_sharadar) >>> # Mask columns where no Sharadar conid was available >>> no_translations = set(ib_conids) - set(ib_to_sharadar) >>> df_with_sharadar_cols.loc[:, no_translations] = None Translate a DataFrame with Sharadar conids as columns to one with IB conids as columns, and drop columns that can't be translated: >>> sharadar_conids = list(df_with_sharadar_cols.columns) >>> sharadar_to_ib = translate_conids(sharadar_conids, from_domain="sharadar") >>> df_with_ib_cols = df_with_sharadar_cols.rename(columns=sharadar_to_ib) >>> # Drop columns where no IB conid was available >>> no_translations = set(sharadar_conids) - set(sharadar_to_ib) >>> df_with_ib_cols = df_with_ib_cols.drop(no_translations, axis=1) """ params = {} params["conids"] = conids if from_domain: params["from_domain"] = from_domain if to_domain: params["to_domain"] = to_domain response = houston.get("/master/translations", params=params) houston.raise_for_status_with_json(response) translations = response.json() # JSON requires dict keys to be strings, re-cast to int translations = dict([(int(k),v) for k,v in translations.items()]) return translations
def trade(strategies, accounts=None, review_date=None, output="csv", filepath_or_buffer=None): """ Run one or more strategies and generate orders. Allocations are read from configuration (quantrocket.moonshot.allocations.yml). Parameters ---------- strategies : list of str, required one or more strategy codes accounts : list of str, optional limit to these accounts review_date : str (YYYY-MM-DD), optional generate orders as if it were this date, rather than using today's date output : str, required the output format (choices are csv or json) filepath_or_buffer : str, optional the location to write the orders file (omit to write to stdout) Returns ------- None """ params = {} if strategies: params["strategies"] = strategies if accounts: params["accounts"] = accounts if review_date: params["review_date"] = review_date output = output or "csv" if output not in ("csv", "json"): raise ValueError( "invalid output: {0} (choices are csv or json".format(output)) response = houston.get("/moonshot/orders.{0}".format(output), params=params, timeout=60 * 5) houston.raise_for_status_with_json(response) # Don't write a null response to file if response.content[:4] == b"null": return filepath_or_buffer = filepath_or_buffer or sys.stdout write_response_to_filepath_or_buffer(filepath_or_buffer, response)
def list_universes(): """ List universes and their size. Returns ------- dict dict of universe:size """ response = houston.get("/master/universes") houston.raise_for_status_with_json(response) return response.json()
def get_license_profile(): """ Return the current license profile. Returns ------- dict license profile """ response = houston.get("/license-service/license") houston.raise_for_status_with_json(response) return response.json()
def download_executions(filepath_or_buffer=None, order_refs=None, accounts=None, conids=None, start_date=None, end_date=None): """ Query executions from the executions database. Parameters ---------- filepath_or_buffer : str or file-like object filepath to write the data to, or file-like object (defaults to stdout) order_refs : list of str, optional limit to these order refs accounts : list of str, optional limit to these accounts conids : list of int, optional limit to these conids start_date : str (YYYY-MM-DD), optional limit to executions on or after this date end_date : str (YYYY-MM-DD), optional limit to executions on or before this date Returns ------- None """ params = {} if order_refs: params["order_refs"] = order_refs if accounts: params["accounts"] = accounts if conids: params["conids"] = conids if start_date: params["start_date"] = start_date if end_date: params["end_date"] = end_date response = houston.get("/blotter/executions.csv", params=params) houston.raise_for_status_with_json(response) filepath_or_buffer = filepath_or_buffer or sys.stdout write_response_to_filepath_or_buffer(filepath_or_buffer, response)
def download_positions(filepath_or_buffer=None, output="csv", order_refs=None, accounts=None, conids=None): """ Query current positions and write results to file. To return positions as a Python list, see list_positions. Parameters ---------- filepath_or_buffer : str or file-like object filepath to write the data to, or file-like object (defaults to stdout) output : str output format (json, csv, txt, default is csv) order_refs : list of str, optional limit to these order refs accounts : list of str, optional limit to these accounts conids : list of int, optional limit to these conids Returns ------- None """ params = {} if order_refs: params["order_refs"] = order_refs if accounts: params["accounts"] = accounts if conids: params["conids"] = conids output = output or "csv" if output not in ("csv", "json", "txt"): raise ValueError("Invalid ouput: {0}".format(output)) response = houston.get("/blotter/positions.{0}".format(output), params=params) houston.raise_for_status_with_json(response) # Don't write a null response to file if response.content[:4] == b"null": return filepath_or_buffer = filepath_or_buffer or sys.stdout write_response_to_filepath_or_buffer(filepath_or_buffer, response)
def list_bundles(): """ List all of the available data bundles. Returns ------- dict data bundles and timestamps """ response = houston.get("/zipline/bundles") houston.raise_for_status_with_json(response) return response.json()
def list_databases(): """ List tick databases and associated aggregate databases. Returns ------- dict dict of {tick_db: [agg_dbs]} """ response = houston.get("/realtime/databases") houston.raise_for_status_with_json(response) return response.json()
def list_databases(): """ List history databases. Returns ------- list list of database codes """ response = houston.get("/history/databases") houston.raise_for_status_with_json(response) return response.json()
def get_history_queue(): """ Get the current queue of historical data collections. Returns ------- dict standard and priority queues """ response = houston.get("/history/queue") houston.raise_for_status_with_json(response) return response.json()
def list_calendar_statuses(exchanges, sec_type=None, in_=None, ago=None, outside_rth=False): """ Check whether exchanges are open or closed. Parameters ---------- exchanges : list of str, required the exchange(s) to check sec_type : str, optional the security type, if needed to disambiguate for exchanges that trade multiple security types. Possible choices: STK, FUT, CASH, OPT in_ : str, optional check whether exchanges will be open or closed at this point in the future (use Pandas timedelta string, e.g. 2h or 30min or 1d) ago : str, optional check whether exchanges were open or closed this long ago (use Pandas timedelta string, e.g. 2h or 30min or 1d) outside_rth : bool check extended hours calendar (default is to check regular trading hours calendar) Returns ------- dict exchange calendar status """ params = {} if exchanges: params["exchanges"] = exchanges if sec_type: params["sec_type"] = sec_type if in_: params["in"] = in_ if ago: params["ago"] = ago if outside_rth: params["outside_rth"] = outside_rth response = houston.get("/master/calendar", params=params) houston.raise_for_status_with_json(response) return response.json()
def list_order_statuses(order_ids=None, conids=None, strategies=None): """ List order status for one or more orders by order ID, conid, or strategy (order ref). Parameters ---------- order_ids : list of str, optional limit to these order IDs conids : list of int, optional limit to orders for these conids strategies: list of str, optional limit to orders for these strategy codes) Returns ------- dict order statuses Examples -------- List order status by order ID: >>> list_order_statuses(order_ids=['DU12345:7002:45','DU12345:7002:46']) List order status by conid: >>> list_order_statuses(conids=[123456]) List order status by strategy (order ref): >>> list_order_statuses(strategies=['my-strategy']) List order status for all open orders: >>> list_order_statuses() """ params = {} if order_ids: params["order_ids"] = order_ids if conids: params["conids"] = conids if strategies: params["strategies"] = strategies response = houston.get("/blotter/orders", params=params) houston.raise_for_status_with_json(response) return response.json()
def get_launchpad_config(): """ Returns the current config. Returns ------- dict the config as a dict """ response = houston.get("/launchpad/config") houston.raise_for_status_with_json(response) # It's possible to get a 204 empty response if not response.content: return {} return response.json()
def get_rollrules_config(): """ Returns the current rollover rules config. Returns ------- dict the config as a dict """ response = houston.get("/master/config/rollover") houston.raise_for_status_with_json(response) # It's possible to get a 204 empty response if not response.content: return {} return response.json()
def get_s3_config(): """ Return the current S3 configuration, if any. See http://qrok.it/h/dbs3 to learn more. Returns ------- dict configuration details """ response = houston.get("/db/s3config") houston.raise_for_status_with_json(response) # It's possible to get a 204 empty response if not response.content: return {} return response.json()
def get_crontab(service): """ Return the current crontab. Parameters ---------- service : str, required the name of the service, e.g. ``countdown-usa`` Returns ------- str string representation of crontab """ response = houston.get("/{0}/crontab".format(service)) houston.raise_for_status_with_json(response) return response.text
def get_timezone(service): """ Return the service timezone. Parameters ---------- service : str, required the name of the service, e.g. ``countdown-usa`` Returns ------- dict dict with key timezone """ response = houston.get("/{0}/timezone".format(service)) houston.raise_for_status_with_json(response) return response.json()
def list_databases(service=None, codes=None, detail=False, expand=False): """ List databases. Parameters ---------- service : str, optional only list databases for this service codes: list of str, optional only list databases identified by these codes (omit to list all databases for service) detail : bool return database statistics (default is to return a flat list of database names) expand : bool expand sharded databases to include individual shards (default is to list sharded databases as a single database) Returns ------- list list of databases Examples -------- Load database details in a pandas DataFrame: >>> from quantrocket.db import list_databases >>> databases = list_databases(detail=True) >>> databases = pd.DataFrame.from_records(databases) """ params = {} if service: params["service"] = service if codes: params["codes"] = codes if detail: params["detail"] = detail if expand: params["expand"] = expand response = houston.get("/db/databases", params=params) houston.raise_for_status_with_json(response) return response.json()
def get_timezone(service=None): """ Return the service timezone. Parameters ---------- service : str, optional the name of the countdown service (default 'countdown') Returns ------- dict dict with key timezone """ service = service or "countdown" response = houston.get("/{0}/timezone".format(service)) houston.raise_for_status_with_json(response) return response.json()
def get_db_config(code): """ Return the configuration for a history database. Parameters ---------- code : str, required the database code Returns ------- dict config """ response = houston.get("/history/databases/{0}".format(code)) houston.raise_for_status_with_json(response) return response.json()
def get_crontab(service=None): """ Return the current crontab. Parameters ---------- service : str, optional the name of the countdown service (default 'countdown') Returns ------- str string representation of crontab """ service = service or "countdown" response = houston.get("/{0}/crontab".format(service)) houston.raise_for_status_with_json(response) return response.text
def list_gateway_statuses(exchanges=None, sec_type=None, research_vendors=None, status=None, gateways=None): """ Query statuses of IB Gateway services. Parameters ---------- exchanges : list of str, optional limit to IB Gateway services with market data permission for these exchanges sec_type : str, optional limit to IB Gateway services with market data permission for this securitiy type (useful for disambiguating permissions for exchanges that trade multiple asset classes). Possible choices: STK, FUT, CASH, OPT research_vendors : list of str, optional limit to IB Gateway services with permission for these research vendors (choices: reuters, wsh) status : str, optional limit to IB Gateway services in this status. Possible choices: running, stopped, error gateways : list of str, optional limit to these IB Gateway services Returns ------- dict of gateway:status (if status arg not provided), or list of gateways (if status arg provided) """ params = {} if sec_type: params["sec_type"] = sec_type if exchanges: params["exchanges"] = exchanges if research_vendors: params["research_vendors"] = research_vendors if gateways: params["gateways"] = gateways if status: params["status"] = status response = houston.get("/launchpad/gateways", params=params) houston.raise_for_status_with_json(response) return response.json()
def list_databases(service=None): """ List databases. Parameters ---------- service : str, optional only list databases for this service Returns ------- list list of databases """ params = {} if service: params["service"] = service response = houston.get("/db/databases", params=params) houston.raise_for_status_with_json(response) return response.json()
def download_history_availability_file(code, filepath_or_buffer=None, output="csv"): """ Query historical market data availability from a history database and download to file. This function is normally called after running: quantrocket history collect mydb --availability Parameters ---------- code : str, required the code of the database to query filepath_or_buffer : str or file-like object filepath to write the data to, or file-like object (defaults to stdout) output : str output format (json, csv, default is csv) Returns ------- None See Also -------- get_history_availability : load historical availability into Series """ output = output or "csv" if output not in ("csv", "json", "txt"): raise ValueError("Invalid ouput: {0}".format(output)) response = houston.get("/history/availability/{0}.{1}".format( code, output)) houston.raise_for_status_with_json(response) filepath_or_buffer = filepath_or_buffer or sys.stdout write_response_to_filepath_or_buffer(filepath_or_buffer, response)
def download_database(database, outfile): """ Download a database from the db service and write to a local file. Parameters ---------- database : str, required the filename of the database (as returned by the list_databases) outfile: str, required filename to write the database to Returns ------- None """ response = houston.get("/db/databases/{0}".format(database), stream=True) houston.raise_for_status_with_json(response) with open(outfile, "wb") as f: for chunk in response.iter_content(chunk_size=1024): if chunk: f.write(chunk)
def get_credentials(gateway): """ Returns IB username and trading mode (paper/live) for IB Gateway. Parameters ---------- gateway : str, required name of IB Gateway service to get credentials for (for example, 'ibg1') Returns ------- dict credentials """ statuses = list_gateway_statuses(gateways=[gateway]) if not statuses: raise ValueError("no such IB Gateway: {0}".format(gateway)) response = houston.get("/{0}/credentials".format(gateway)) houston.raise_for_status_with_json(response) return response.json()
def list_universes(domain=None): """ List universes and their size. Parameters ---------- domain : str, optional the domain to list universes for (default is 'main', which runs against quantrocket.master.main.sqlite. Possible choices: main, sharadar) Returns ------- dict dict of universe:size """ url = "/master/{0}universes".format( "{0}/".format(domain) if domain else "") response = houston.get(url) houston.raise_for_status_with_json(response) return response.json()
def get_license_profile(force_refresh=False): """ Return the current license profile. Parameters ---------- force_refresh : bool refresh the license profile before returning it (default is to return the cached profile, which is refreshed every few minutes) Returns ------- dict license profile """ params = {} if force_refresh: params["force_refresh"] = force_refresh response = houston.get("/license-service/license", params=params) houston.raise_for_status_with_json(response) return response.json()