otp.DB#
- class DB(name=None, src=None, date=None, symbol=None, tick_type=None, kind='archive', db_properties=None, db_locations=None, db_raw_data=None, db_feed=None, write=True, clean_up=True, destroy_access=False)[source]#
Creates database object. Used to configure databases locator and access list properties and to write data to the database. By default creates temporary database. This object can then be
used
inonetick.py.Session
. Note: already presented ticks can’t be updated. Data can only be added during db creation or withadd()
method.- Parameters
name (str) – Database name In case you want to specify derived db, you should specify in “parent//derived” format. Derived database inherits parent database’s properties.
src (optional) – Data to add to database
clean_up (bool, optional) – Flag that controls temporary database cleanup
date (datetime.datetime, optional) –
src
will be added to this datesymbol (str, optional) – symbol name to add data
tick_type (str, optional) – tick type to add data
db_properties (
dict
, optional) – Properties of database to add to locatordb_locations (
list
ofdict
, optional) – Locations of database to add to locator. This parameter is list, because database in locator can have several location sections. If not specified, some temporary directory is used as database location.db_raw_data (
list
ofdict
, optional) – Raw databases’ configuration.db_feed (dict, optional) – Feed configuration.
write (bool, optional) – Flag that controls access to write to database
destroy_access (bool, optional) – Flag that controls access to destroy to database
Examples
Database can be initialized along with data:
>>> data = otp.Ticks(X=['hello', 'world!']) >>> db = otp.DB('MYDB', data)
You can specify derived db by using
//
as a separator:>>> data = otp.Ticks(X=['parent1', 'parent2']) >>> db = otp.DB('DB_A', data) >>> db.add(data)
>>> data_derived = otp.Ticks(X=['derived1', 'derived2']) >>> db_derived = otp.DB('DB_A//DB_D') >>> session.use(db_derived) >>> db_derived.add(data_derived)
- add(src, date=None, start=None, end=None, symbol=None, tick_type=None, timezone=None, **kwargs)#
Add data to database. If ticks with the same timestamp are already presented in database old values won’t be updated.
- Parameters
src (
otp.Source
) – source that will be written to the database.date (datetime or None) – date of the day in which the data will be saved. The timestamps of the ticks should be between the start and the end of the day. Be default, it is set to otp.config.default_date.
start (datetime or None) – start day of period in which the data will be saved. The timestamps of the ticks should be between start and end dates. Cannot be used with date parameter. Be default, None.
end (datetime or None) – end day of period in which the data will be saved. The timestamps of the ticks should be between start and end dates. Cannot be used with date parameter. Be default, None.
symbol (str or Column) – resulting symbol name string or column to get symbol name from. Be default, it is set to otp.config.default_db_symbol.
tick_type (str or Column) – resulting tick type string or column to get tick type from. If tick type is None then an attempt will be taken to get tick type name automatically based on the
src
source’s schema. (ORDER, QTE, TRD and NBBO tick types are supported).timezone (str) – This timezone will be used for running the query. By default, it is set to otp.config.tz.
kwargs – other arguments that will be passed to
onetick.py.Source.write()
function.
Examples
Data is saved to the specified date, symbol and tick type: (note that
session
is created before this example)>>> db = otp.DB('MYDB2') >>> db.add(otp.Ticks(A=[4, 5, 6]), date=otp.dt(2003, 1, 1), symbol='SMB', tick_type='TT') >>> session.use(db)
We can get the same data by specifying the same parameters:
>>> data = otp.DataSource(db, date=otp.dt(2003, 1, 1), symbols='SMB', tick_type='TT') >>> otp.run(data) Time A 0 2003-01-01 00:00:00.000 4 1 2003-01-01 00:00:00.001 5 2 2003-01-01 00:00:00.002 6
- property properties#
Get dict of database properties.
- Return type
Examples
>>> otp.DB('X').properties {'symbology': 'BZX', 'archive_compression_type': 'NATIVE_PLUS_GZIP', 'tick_timestamp_type': 'NANOS'}
- property locations#
Get list of database locations.
- Return type
list of dict
Examples
>>> otp.DB('X').locations [{'access_method': 'file', 'start_time': '20021230000000', 'end_time': '21000101000000', ...}]
- property raw_data#
Get dict of database raw configurations.
- Return type
dict of dict
Examples
>>> db = otp.DB('RAW_EXAMPLE', ... db_raw_data=[{ ... 'id': 'PRIMARY_A', ... 'prefix': 'DATA.', ... 'locations': [ ... {'mount': 'mount1'} ... ] ... }] ... ) >>> db.raw_data [{'id': 'PRIMARY_A', 'prefix': 'DATA.', 'locations': [{'mount': 'mount1', 'access_method': 'file', ...}]}]
- property feed#
Get dict of database feed configuration.
- Return type
Examples
>>> db = otp.DB('RAW_EXAMPLE', ... db_raw_data=[{ ... 'id': 'PRIMARY_A', ... 'prefix': 'DATA.', ... 'locations': [ ... {'mount': 'mount1'} ... ] ... }], ... db_feed={'type': 'rawdb', 'raw_source': 'PRIMARY_A'}, ... ) >>> db.feed {'type': 'rawdb', 'raw_source': 'PRIMARY_A', 'format': 'native'}