otp.Symbols#

class Symbols(db=None, tick_type='ANY', start=utils.adaptive, end=utils.adaptive, date=None, find_params=None, keep_db=False, pattern='%', for_tick_type=None, show_tick_type=False, symbology='', show_original_symbols=False, **kwargs)#

Bases: onetick.py.core.source.Source

Construct a source that returns ticks with information about symbols in a database. The SYMBOL_NAME field is populated with symbol names. The TICK_TYPE field contains corresponding tick type (enabled by the show_tick_type parameter).

Parameters
  • db (str) – Name of the database where to search symbols

  • tick_type (str) – Tick type to use. Default is ANY

  • start (datetime.datetime, otp.datetime, onetick.py.adaptive) – Time interval from which the data should be taken.

  • end (datetime.datetime, otp.datetime, onetick.py.adaptive) – Time interval from which the data should be taken.

  • date (datetime.date) – Alternative way of setting instead of start/end times

  • keep_db (bool) – Flag that indicates whether symbols should have a db prefix.

  • pattern (str) –

    Usual and special characters can be used to search for symbols. Special characters are:

    • % - any number of any characters (zero too)

    • _ - any single character

    • \ - used to escape special characters

    For example, if you want symbol name starting with NQ, you should write NQ%. If you want symbol name to contain literal % character, you should write NQ\%. \ is a special character too, so it need to be escaped too if you want symbol name to contain literal backslash, e.g. NQ\\M23. Default is %.

  • for_tick_type (str) – Fetch only symbols belong to this tick type, if specified.

  • show_tick_type (bool) – Add the TICK_TYPE column with the information about tick type

  • symbology (str) – The destination symbology for a symbol name translation. Translation is performed, if destination symbology is not empty and is different from that of the queried database.

  • show_original_symbols (bool) – Switches original symbol name propagation as a tick field ORIGINAL_SYMBOL_NAME if symbol name translation is performed (if symbology is set). Note that if this parameter is set to True, database symbols with missing translations are also propagated.

Note

Additional fields that can be added to Symbols will be converted to symbol parameters

Examples

This class can be used to get a list of all symbols in the database:

>>> symbols = otp.Symbols('NYSE_TAQ', date=otp.dt(2022, 3, 1))
>>> otp.run(symbols)
        Time  SYMBOL_NAME
0 2022-03-01          AAP
1 2022-03-01         AAPL

Also this class can be used to specify symbols for the main query:

>>> symbols = otp.Symbols('NYSE_TAQ', date=otp.dt(2022, 3, 1))
>>> data = otp.DataSource('NYSE_TAQ', tick_type='TRD', date=otp.dt(2022, 3, 1))
>>> result = otp.run(data, symbols=symbols)
>>> result['AAPL']
                     Time  PRICE  SIZE
0 2022-03-01 00:00:00.000    1.3   100
1 2022-03-01 00:00:00.001    1.4    10
2 2022-03-01 00:00:00.002    1.4    50
>>> result['AAP']
                     Time  PRICE
0 2022-03-01 00:00:00.000  45.37
1 2022-03-01 00:00:00.001  45.41

Additional fields of the otp.Symbols can be used in the main query as symbol parameters:

>>> symbols = otp.Symbols('SOME_DB', show_tick_type=True, keep_db=True)
>>> symbols['PARAM'] = symbols['SYMBOL_NAME'] + '__' + symbols['TICK_TYPE']
>>> data = otp.DataSource('SOME_DB')
>>> data['S_PARAM'] = data.Symbol['PARAM', str]
>>> data = otp.merge([data], symbols=symbols)
>>> otp.run(data)
                     Time   X          S_PARAM
0 2003-12-01 00:00:00.000   1  SOME_DB::S1__TT
1 2003-12-01 00:00:00.000  -3  SOME_DB::S2__TT
2 2003-12-01 00:00:00.001   2  SOME_DB::S1__TT
3 2003-12-01 00:00:00.001  -2  SOME_DB::S2__TT
4 2003-12-01 00:00:00.002   3  SOME_DB::S1__TT
5 2003-12-01 00:00:00.002  -1  SOME_DB::S2__TT

Escaping special characters in the pattern

When using patterns with special character, be aware that python strings \ is a special character too and need to be escaped as well:

>>> print('back\\slash')
back\slash

Pattern NQ\\M23 in python should be written as NQ\\\\M23:

>>> print('NQ\\\\23')
NQ\\23

Escaping character \ in python can be avoided with raw strings:

>>> print(r'NQ\\23')
NQ\\23

See also

FIND_DB_SYMBOLS OneTick event processor