otp.by_symbol#
- by_symbol(src, symbol_field, single_invocation=False, db=utils.adaptive_to_default, tick_type=utils.adaptive, start=utils.adaptive, end=utils.adaptive)#
Create a separate data series for each unique value of
symbol_fieldin the output ofsrc.srcmust specify enough parameters to be run (e.g., symbols, query range). A typical use case is to split a single data series (e.g., from a CSV file) into separate data series by symbol. This method is a source.- Parameters
src (Source) – a query which output is to be split by
symbol_fieldsymbol_field (str) – the name of the field carrying symbol name in the
srcquerysingle_invocation (bool, optional) –
Truemeans that thesrcquery is run once and the result stored in memory speeding up the execution.Falsemeans that thesrcquery is run for every symbol of the query saving memory but slowing down query execution. Default:Falsedb (str, optional) – Database for running the query. Doesn’t affect the
srcquery. The default value isotp.config['default_db'].tick_type (str, optional) – Tick type for the query. Doesn’t affect the
srcquery.start (otp.dt, optional) – By default it is taken from the
srcstart timeend (otp.dt, optional) – By default it is taken from the
srcend time
- Return type
Examples
>>> executions = otp.CSV( ... otp.utils.file(os.path.join(cur_dir, 'data', 'example_events.csv')), ... converters={"time_number": lambda c: c.apply(otp.nsectime)}, ... timestamp_name="time_number", ... start=otp.dt(2022, 7, 1), ... end=otp.dt(2022, 7, 2), ... order_ticks=True ... )[['stock', 'px']] >>> csv = otp.by_symbol(executions, 'stock') >>> trd = otp.DataSource( ... db='NYSE_TAQ', ... tick_type='TRD', ... start=otp.dt(2022, 7, 1), ... end=otp.dt(2022, 7, 2) ... )[['PRICE', 'SIZE']] >>> data = otp.join_by_time([csv, trd]) >>> result = otp.run(data, symbols=executions.distinct(keys='stock')[['stock']], concurrency=8) >>> result['THG'] Time stock px PRICE SIZE 0 2022-07-01 11:37:56.432947200 THG 148.02 146.48 1 >>> result['TFX'] Time stock px PRICE SIZE 0 2022-07-01 11:39:45.882808576 TFX 255.61 251.97 1 >>> result['BURL'] Time stock px PRICE SIZE 0 2022-07-01 11:42:35.125718016 BURL 137.53 135.41 2
See also
SPLIT_QUERY_OUTPUT_BY_SYMBOL OneTick event processor