otp.Session#

class Session(config=None, clean_up=utils.default, copy=True, override_env=False, redirect_logs=True)#

Bases: object

A class for setting up working OneTick session. It keeps configuration files during the session and allows to manage them. When instance is out of scope, then instance cleans up config files and configuration. You can leave the scope manually with method close(). Also, session is closed automatically if this object is used as a context manager.

Note

It is allowed to have only one alive session instance in the process.

If you don’t use Session’s instance, then ONE_TICK_CONFIG environment variable should be set to be able to work with OneTick.

If config file is not set then temporary is generated. Config includes locator and acl file, and if they are not set, then they are generated.

Parameters
  • config (str, onetick.py.session.Config, optional) – Path to an existing OneTick config file; if it is not set, then config will be generated. If config is not set, then temporary config is generated. Default is None.

  • clean_up (bool, optional) –

    A flag to control cleaning up process: if it is True then all temporary generated files will be automatically removed. It is helpful for debugging. The flag affects only generated files, but does not externally passed.

    By default, otp.config.clean_up_tmp_files is used.

  • copy (bool, optional) – A flag to control file copy process: if it is True then all externally passed files will be copied before usage, otherwise all modifications during an existing session happen directly with passed config files. NOTE: we suggest to set this flag only when you fully understand it’s effect. Default is True.

  • override_env (bool, optional) –

    If flag is True, then unconditionally ONE_TICK_CONFIG environment variable will be overridden with a config that belongs to a Session. Otherwise ONE_TICK_CONFIG will be defined in the scope of session only when it is not defined externally. For example, it is helpful when you test ascii_loader that uses ‘ONE_TICK_CONFIG’ only.

    Default is False ( default is False, because overriding external environment variable might be not obvious and desirable )

  • redirect_logs (bool, optional) – If flag is True, then OneTick logs will be redirected into a temporary log file. Otherwise logs will be mixed with output. Default is True.

Examples

If session is defined with environment, OneTick can be used right away:

>>> 'ONE_TICK_CONFIG' in os.environ
True
>>> list(otp.databases()) 
['COMMON', 'DEMO_L1', ..., 'SOME_DB']
>>> data = otp.DataSource('SOME_DB', symbol='S1', tick_type='TT')
>>> otp.run(data)
                     Time  X
0 2003-12-01 00:00:00.000  1
1 2003-12-01 00:00:00.001  2
2 2003-12-01 00:00:00.002  3
use(*items)#

Makes DB or TS available inside the session.

Parameters

items (DB or RemoteTS objects) – Items to be added to session.

Examples

(note that session is created before this example)

>>> list(otp.databases()) 
['COMMON', 'DEMO_L1', ..., 'SOME_DB']
>>> new_db = otp.DB('ZZZZ')
>>> session.use(new_db)
>>> list(otp.databases()) 
['COMMON', 'DEMO_L1', ..., 'SOME_DB', 'ZZZZ']
use_stub(stub_name)#

Adds stub-DB into the session. The shortcut for .use(otp.DB(stub_name))

Parameters

stub_name (str) – name of the stub

close()#

Close session

property config#

A reference to the underlying Config object that represents OneTick config file.

Return type

onetick.py.session.Config

property acl#

A reference to the underlying ACL object that represents OneTick access control list file.

Return type

onetick.py.session.ACL

property locator#

A reference to the underlying Locator that represents OneTick locator file.

Return type

onetick.py.session.Locator

property license#
property ts_databases#
property databases#