btgym.dataserver module

class btgym.dataserver.BTgymDataFeedServer(dataset=None, network_address=None, log_level=None, task=0)[source]

Data provider server class. Enables efficient data sampling for asynchronous multiply BTgym environments execution. Manages global back-testing time and broadcast messages.

Configures data server instance.

Parameters:
  • dataset – data domain instance;
  • network_address – …to bind to.
  • log_level – int, logbook.level
  • task – id
get_data(sample_config=None)[source]

Get Trial sample according to parameters received. If no parameters being passed - makes sample with default parameters.

Parameters:sample_config – sampling parameters configuration dictionary
Returns:if sample_params arg has been passed and dataset is ready None: otherwise
Return type:sample
run()[source]

Server process runtime body.

btgym.server module

class btgym.server.BTgymServer(cerebro=None, render=None, network_address=None, data_network_address=None, connect_timeout=90, log_level=None, task=0)[source]

Backtrader server class.

Expects to receive dictionary, containing at least ‘action’ field.

Control mode IN:

dict(action=<control action, type=str>,), where control action is:
'_reset' - rewinds backtrader engine and runs new episode;
'_getstat' - retrieve episode results and statistics;
'_stop' - server shut-down.

Control mode OUT:

<string message> - reports current server status;
<statisic dict> - last run episode statisics.  NotImplemented.

Within-episode signals:
Episode mode IN:
dict(action=<agent_action, type=str>,), where agent_action is:
{'buy', 'sell', 'hold', 'close', '_done'} - agent or service actions; '_done' - stops current episode;

Episode mode OUT:

response  <tuple>: observation, <array> - observation of the current environment state,
                                         could be any tensor; default is:
                                         [4,m] array of <fl32>, where:
                                         m - num. of last datafeed values,
                                         4 - num. of data features (Lines);
                   reward, <any> - current portfolio statistics for environment reward estimation;
                   done, <bool> - episode termination flag;
                   info, <list> - auxiliary information.
Parameters:
  • cerebro – backtrader.cerebro engine class.
  • render – render class
  • network_address – environmnet communication, str
  • data_network_address – data communication, str
  • connect_timeout – seconds, int
  • log_level – int, logbook.level
get_trial(**reset_kwargs)[source]
Parameters:reset_kwargs – dictionary of args to pass to parent data iterator
Returns:trial_sample, trial_stat, dataset_stat
get_trial_message()[source]

Prepares message containing current trial instance, mimicking data_server message protocol. Intended for serving requests from data_slave environment.

Returns:dict containing trial instance, d_set statistic and origin key; dict containing ‘ctrl’ response if master d_set is not ready;
get_global_time()[source]

Asks dataserver for current dataset global_time.

Returns:POSIX timestamp
get_broadcast_message()[source]

Asks dataserver for current dataset global_time and broadcast message.

Returns:POSIX timestamp
run()[source]

Server process runtime body. This method is invoked by env._start_server().

btgym.spaces module

class btgym.spaces.DictSpace(spaces_dict)[source]

Wrapper for gym.spaces.Dict class. Adds support for .shape attribute. Defines space as nested dictionary of simpler gym spaces.

Parameters:spaces_dict – [nested] dictionary of core Gym spaces.
class btgym.spaces.ActionDictSpace(assets, base_actions=None)[source]

Extension of OpenAI Gym DictSpace providing additional domain-specific functionality. Action space for btgym environments as shallow dictionary of discrete or continuous spaces. Defines several handy attributes and encoding conversion methods.

Parameters:
  • base_actions – None or iterable of base asset discrete actions; if no actions provided - continuous 1D base action space is set in [0,1] interval.
  • assets – iterable of assets names
get_initial_action()[source]
Returns:‘do nothing’ action as OrderedDict (for discrete spaces) ‘put all in cash’ action as OrderedDict (for continuous actions)
encode(action)[source]

Given action returns it’s encoding. Encoding method depends on type of base actions: - if base actions defined are discrete (gym.spaces.Discrete), binary encoding is used; - if base actions defined are continuous(gym.spaces.Box),

encoding is translating shallow dictionary to vector of same values and back
Parameters:action – action from this space (shallow dictionary)
Returns:1D array of floats in [0, 1]
decode(code)[source]

Given code returns action. Encoding method depends on type of base actions: - if base actions defined are discrete (gym.spaces.Discrete), binary encoding is used; - if base actions defined are continuous(gym.spaces.Box),

encoding is translating shallow dictionary to vector of same values and back
Parameters:code – 1D array of floats in [0, 1]
Returns:action from this space (shallow dictionary)
one_hot_encode(action)[source]

Given action returns it’s encoding. Encoding method depends on type of base actions: - if base actions defined are discrete (gym.spaces.Discrete), one_hot encoding is used; - if base actions defined are continuous(gym.spaces.Box),

encoding is translating shallow dictionary to vector of same values and back
Parameters:action – action from this space (shallow dictionary)
Returns:1D array of floats in [0, 1]