orderbookmdp.rl package¶
Submodules¶
orderbookmdp.rl.abstract_envs module¶
-
class
orderbookmdp.rl.abstract_envs.OrderTrackingEnv(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_env.MarketEnvAn abstract env that keeps track of all the orders the agent has in the order book.
-
orders_in_book_dict¶ dict – All the orders in the book
-
orders_in_book¶ PriceLevels
-
-
class
orderbookmdp.rl.abstract_envs.ExternalMarketEnv(max_episode_time='10days', order_paths='../../../data/feather/', snapshot_paths='../../../data/snap_json/', taker_fee=0.002, min_capital_pct=0.0, **kwargs)[source]¶ Bases:
orderbookmdp.rl.market_env.MarketEnvAn abstract env that handles orders from an external market.
By handling orders from an external market it is possible to simulate an external market or add artificial messages to an external market at any point in time.
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
__init__(max_episode_time='10days', order_paths='../../../data/feather/', snapshot_paths='../../../data/snap_json/', taker_fee=0.002, min_capital_pct=0.0, **kwargs)[source]¶ Parameters: - market_type (str) – The type of market to be used
- market_setup (dict) – Parameters for the market
- initial_funds (float) – The initial cash of the agent
- T_ID (int) – The agent’s trader id
-
orderbookmdp.rl.app module¶
orderbookmdp.rl.back_track_copy module¶
orderbookmdp.rl.backtrack module¶
-
class
orderbookmdp.rl.backtrack.CopyAbleMarketOrderEnvBuySell(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvBuySellAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
orderbookmdp.rl.backtracklib module¶
orderbookmdp.rl.dist_envs module¶
-
class
orderbookmdp.rl.dist_envs.SpreadEnv(**kwargs)[source]¶ Bases:
orderbookmdp.rl.abstract_envs.ExternalMarketEnv,orderbookmdp.rl.abstract_envs.OrderTrackingEnvAn environment that puts a buy and a sell limit order on a certain tick distance from the bid and the ask.
It keeps track of its orders and updates them accordingly.
-
min_order_capital¶ int – The minimum capital to put an order of
-
min_change_order_capital¶ int – The minimum capital to change update an order to
-
max_action¶ int – The maximum tick distance to ever use
-
default_action¶ np.array – The default action always to start from
-
match(side: int, size: float, price: int) -> (<class 'int'>, <class 'float'>)[source]¶ Matches previous trades with new trades.
The best buy orders (lowest price) are matched with the best sell orders (highest price). No match occurs if no trades of the other side (BUY/SELL) has occurred.
The returned volume weighted spread vws is calculated as: \(vws=(sell_{price} - buy_{price})*size\)
Parameters: - side (int) –
- size (float) –
- price (int) –
Returns: - volume_weighted_spread (float)
- size (float) – The remaining size of the trade that as occured.
-
send_messages(messages: tuple) -> (<class 'list'>, <class 'dict'>, <class 'bool'>)[source]¶ Sends all the messages to the market
Parameters: messages (tuple) – Returns: - trades (list)
- done (bool) – If the episode has finished or not.
- info (dict) – Extra information about the environment
-
get_messages(action: numpy.core.multiarray.array) → tuple[source]¶ Returns messages based on the action received.
Parameters: action (numpy.array) – Returns: messages Return type: tuple
-
adjust_orders(buy_sizes, buy_prices, sell_sizes, sell_prices)[source]¶ Creates messages so that the orders in book are updated accordingly to the prices and sizes wanted.
Creates limit orders, updates and cancellations based on the current order in books and the requested prices and sizes.
Parameters: - buy_sizes (np.array) – Wanted buy prizes
- buy_prices (np.array) – Wanted buy sizes
- sell_sizes (np.array) – Wanted sell prizes
- sell_prices (np.array) – Wanted sell sizes
Returns: messages
Return type: list
-
get_private_variables() → tuple[source]¶ No private variables implemented yet.
Returns: private_variables Return type: tuple
-
get_reward(trades: list, done=False) → float[source]¶ Calculates the reward based on the trades that has occured.
The reward is calculated based on previous trades and the current trades. If a buy trade has occurred, the volume_weighted_spread
match()is calculated from previous sell trades. Vice versa for a sell trade.Parameters: trades (list) – Returns: reward Return type: float
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
action_space¶ The action space is \(buy_{distance}, sell_{distance} = action\)
The action are distances in ticks:
\(buy_{distance}, sell_{distance} = action[0], action[1]\)
Sets the buy limit order with a distance from the bid:
\(buy_{price} = bid - buy_{distance}\)
And the sell limit order with a distance from the ask:
\(sell_{price} = ask + sell_{distance}\)
-
-
class
orderbookmdp.rl.dist_envs.DistEnv(pdf_type='beta', **kwargs)[source]¶ Bases:
orderbookmdp.rl.dist_envs.SpreadEnvA extension of the
SpreadEnvthat has a full distribution of orders.Has one distribution of buy orders from bid and a certain amount of tick sizes downwards. Vice versa for the sell orders. One example distribution is to place the orders according to a beta distribution.
-
n_tick_levels¶ int – The number of tick levels to put orders away from the anchor price, bid in buy case.
-
n_price_levels¶ int – The number of price levels to put orders in. Must be smaller than the number of tick levels.
-
get_messages(action: numpy.core.multiarray.array) → tuple[source]¶ Returns messages based on the action received.
Parameters: action (numpy.array) – Returns: messages Return type: tuple
-
action_space¶ The action space is the buy and sell distributions beta and alpha.
\(buy_{\alpha}, buy_{\beta}, sell_{\alpha}, sell_{\beta} = action\)
-
orderbookmdp.rl.dp_env module¶
-
class
orderbookmdp.rl.dp_env.ForwardDpEnv(max_episode_time='10days', order_paths='../../../data/feather/', snapshot_paths='../../../data/snap_json/', taker_fee=0.002, min_capital_pct=0.0, **kwargs)[source]¶ Bases:
orderbookmdp.rl.abstract_envs.ExternalMarketEnvAn abstract env that handles orders from an external market.
By handling orders from an external market it is possible to simulate an external market or add artificial messages to an external market at any point in time.
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
get_messages(action: numpy.core.multiarray.array) → tuple[source]¶ Returns messages based on the action received.
Parameters: action (numpy.array) – Returns: messages Return type: tuple
-
get_reward(trades: list) → tuple[source]¶ Returns a reward based on trades
Parameters: trades (list) – Returns: reward Return type: float
-
send_messages(messages: tuple) -> (<class 'list'>, <class 'bool'>, <class 'dict'>)[source]¶ Sends all the messages to the market
Parameters: messages (tuple) – Returns: - trades (list)
- done (bool) – If the episode has finished or not.
- info (dict) – Extra information about the environment
-
get_private_variables() → tuple[source]¶ Returns private variables of for the agent to observe. For example the possession of the agent.
Returns: private_variables Return type: tuple
-
action_space¶ gym.spaces – The action space of the environment.
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
orderbookmdp.rl.env_utils module¶
-
orderbookmdp.rl.env_utils.quote_differs[source]¶ Numba Jitted version of if quote a and b differs.
Parameters: - a (quote) –
- b (quote) –
Returns: True of quote and and b differs, otherwise false
Return type: bool
-
orderbookmdp.rl.env_utils.quote_differs_pct[source]¶ Numba Jitted version of if quote a and b differs.
Parameters: - a (quote) –
- b (quote) –
Returns: True of quote and and b differs, otherwise false
Return type: bool
-
orderbookmdp.rl.env_utils.pct_change[source]¶ Percentage change of new and old value.
Parameters: - new –
- old –
orderbookmdp.rl.gdax_envs module¶
orderbookmdp.rl.level_2_data module¶
-
orderbookmdp.rl.level_2_data.get_level_2_sequence(T=10, initial_funds=100, order_path='../../../data/feather/', snapshot_path='../../../data/snap_json/')[source]¶
-
orderbookmdp.rl.level_2_data.match(level_2_sequences, capital, funds, possession, t, action, takerfee)[source]¶
-
orderbookmdp.rl.level_2_data.back_track_opt_paths(init_funds, capital, funds, possession, level_2_sequences, t, T, takerfee, path=())[source]¶
orderbookmdp.rl.market_env module¶
-
orderbookmdp.rl.market_env.get_market(market_type, **kwargs)[source]¶ Returns a market based on the parameter. :param market_type: The type of market to return :type market_type: str :param kwargs: Additional settings for the market
Returns: market Return type: Market
-
class
orderbookmdp.rl.market_env.MarketEnv(market_type='cyext', tick_size=0.01, ob_type='cy', price_level_type='cydeque', price_levels_type='cylist', initial_funds=10000, T_ID=1, **kwargs)[source]¶ Bases:
objectA market environment extending the OpenAI gym class .
The main functions are the step and reset functions. In the step function the agent sends orders and receives a reward based on the orders/trades.
A basic rendering function using Dash is implemented. This opens a Flask server in another thread that renders different information about the market.
-
capital¶ float – The capital of the agent
-
funds¶ float – The cash/funds of the agent
-
possession¶ float – The size of the asset the agent possesses
-
price_n¶ int – Number of price points to render back in time.
-
trades_list¶ list – All trades
-
metadata= {'render.modes': []}¶
-
reward_range= (-inf, inf)¶
-
spec= None¶
-
__init__(market_type='cyext', tick_size=0.01, ob_type='cy', price_level_type='cydeque', price_levels_type='cylist', initial_funds=10000, T_ID=1, **kwargs)[source]¶ Parameters: - market_type (str) – The type of market to be used
- market_setup (dict) – Parameters for the market
- initial_funds (float) – The initial cash of the agent
- T_ID (int) – The agent’s trader id
-
step(action)[source]¶ Sends orders based on the agents action and receives a reward based on trades that have occurred.
The action is first converted to messages to the market. Then the messages are sent and trades are received. The reward is then calculated based on the trades.
Parameters: action (numpy.array) – Returns: - obs (list[tuple, tuple]) – The current quotes of the market and private information about the agents portfolio
- reward (float)
- done (bool) – If the episode has finished
- info (dict) – Additional information
-
get_messages(action: numpy.core.multiarray.array) → tuple[source]¶ Returns messages based on the action received.
Parameters: action (numpy.array) – Returns: messages Return type: tuple
-
get_reward(trades: list) → tuple[source]¶ Returns a reward based on trades
Parameters: trades (list) – Returns: reward Return type: float
-
send_messages(messages: tuple) -> (<class 'list'>, <class 'bool'>, <class 'dict'>)[source]¶ Sends all the messages to the market
Parameters: messages (tuple) – Returns: - trades (list)
- done (bool) – If the episode has finished or not.
- info (dict) – Extra information about the environment
-
get_private_variables() → tuple[source]¶ Returns private variables of for the agent to observe. For example the possession of the agent. :returns: private_variables :rtype: tuple
-
get_obs() → tuple[source]¶ Returns an observation of the market. Currently the quotes. :returns: observation :rtype: tuple
-
reset(market=None)[source]¶ Resets the environment with a market or creates a new one. Also resets the render app if needed. :param market: If to use a specific market instead of a newly created one. :type market: Market
Returns: observation Return type: tuple
-
action_space¶ gym.spaces – The action space of the environment.
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
unwrapped¶ Completely unwrap this env. :returns: The base non-wrapped gym.MarketEnv instance :rtype: gym.Env
-
orderbookmdp.rl.market_order_envs module¶
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnv(**kwargs)[source]¶ Bases:
orderbookmdp.rl.abstract_envs.ExternalMarketEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
__init__(**kwargs)[source]¶ Parameters: - market_type (str) – The type of market to be used
- market_setup (dict) – Parameters for the market
- initial_funds (float) – The initial cash of the agent
- T_ID (int) – The agent’s trader id
-
get_messages(action: numpy.core.multiarray.array) → tuple[source]¶ Returns a market order if possible. Actions are mapped 0=BUY, 1=SELL and 2=HOLD.
Parameters: action (int) – Returns: market_order Return type: list
-
get_reward(trades: list, done) → tuple[source]¶ Returns the reward as the percentage change in capital.
\(capital = funds + possession*theoretical\_sell\_price\)
Where the theoretical sell price is the current bid.
Parameters: trades (list) – Returns: reward Return type: float
-
send_messages(messages: tuple) -> (<class 'list'>, <class 'dict'>, <class 'bool'>)[source]¶ Sends all the messages to the market
Parameters: messages (tuple) – Returns: - trades (list)
- done (bool) – If the episode has finished or not.
- info (dict) – Extra information about the environment
-
get_private_variables() → tuple[source]¶ Returns the agents possession as private variable
Returns: private_variables Return type: tuple
-
reset(market=None)[source]¶ Resets the environment.
Also resets the render app with zero portfolio.
Parameters: market (Market) – If to use a specific market instead of a newly created one. Returns: observation Return type: tuple
-
action_space¶ The action space is 0=BUY, 1==SELL, 0=HOLD
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvBuySell(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
get_messages(action: numpy.core.multiarray.array) → tuple[source]¶ Returns a market order if possible. Actions are mapped 0=BUY, 1=SELL and 2=HOLD.
Parameters: action (int) – Returns: market_order Return type: list
-
action_space¶ The action space is 0=BUY, 1==SELL
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvCumReturn(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvExtends the Market Order Enviroment because it uses the cumulative return instead of the return as the reward.
-
cum_return¶ float – The cumulative return
-
__init__(**kwargs)[source]¶ Parameters: - market_type (str) – The type of market to be used
- market_setup (dict) – Parameters for the market
- initial_funds (float) – The initial cash of the agent
- T_ID (int) – The agent’s trader id
-
get_reward(trades: list, done)[source]¶ The reward is the cumulative return.
\(reward_t = cum\_return_t - 1\)
Where cum_return is:
\(cum\_return_t = 1*\prod_{i=1}^{t} 1+return_t\)
Parameters: trades (list) – Returns: reward Return type: float
-
reset(market=None)[source]¶ Resets the environment.
Also resets the render app with zero portfolio.
Parameters: market (Market) – If to use a specific market instead of a newly created one. Returns: observation Return type: tuple
-
get_private_variables()[source]¶ Returns the agents possession as private variable
Returns: - possession (float)
- cum_return (float)
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvAdjustment(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvExtends the Market Order Enviroment because it uses a adjusted return instead of the return as the reward.
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
__init__(**kwargs)[source]¶ Parameters: - market_type (str) – The type of market to be used
- market_setup (dict) – Parameters for the market
- initial_funds (float) – The initial cash of the agent
- T_ID (int) – The agent’s trader id
-
get_reward(trades: list, done)[source]¶ The reward is the adjusted return.
\(reward_t = return_t * rac{cap_t}{cap_0}\)
Parameters: trades (list) – Returns: reward Return type: float
-
get_private_variables()[source]¶ Returns the agents possession as private variable
Returns: - possession (float)
- cum_return (float)
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvBuyingPower(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
get_reward(trades: list, done) → tuple[source]¶ Returns the reward as the percentage change in capital.
\(capital = funds + possession*theoretical\_sell\_price\)
Where the theoretical sell price is the current bid.
Parameters: trades (list) – Returns: reward Return type: float
-
get_private_variables()[source]¶ Returns the agents possession as private variable
Returns: - possession (float)
- cum_return (float)
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvEndReward(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvFunds(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
get_reward(trades: list, done) → tuple[source]¶ Returns the reward as the percentage change in capital.
\(capital = funds + possession*theoretical\_sell\_price\)
Where the theoretical sell price is the current bid.
Parameters: trades (list) – Returns: reward Return type: float
-
get_private_variables() → tuple[source]¶ Returns the agents possession as private variable
Returns: private_variables Return type: tuple
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvCritic(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
-
class
orderbookmdp.rl.market_order_envs.MarketOrderEnvOpt(**kwargs)[source]¶ Bases:
orderbookmdp.rl.market_order_envs.MarketOrderEnvAn environment that only sends a market order of its full funds (BUY) or possession (SELL).
-
os¶ orderstream – The orderstream that gives messages from the external market
-
filled¶ bool – If the env has been filled by a snapshot
-
get_reward(trades: list, done)[source]¶ Returns the reward as the percentage change in capital.
\(capital = funds + possession*theoretical\_sell\_price\)
Where the theoretical sell price is the current bid.
Parameters: trades (list) – Returns: reward Return type: float
-
get_private_variables()[source]¶ Returns the agents possession as private variable
Returns: - possession (float)
- cum_return (float)
-
observation_space¶ gym.spaces – The obervation space of the environment.
-
orderbookmdp.rl.multi_agent_envs module¶
-
class
orderbookmdp.rl.multi_agent_envs.MultiAgentOrderEnv(agent_list, random_agent_list=[], episode_seconds=60, **kwargs)[source]¶ Bases:
orderbookmdp.rl.market_env.MarketEnvAn environment that hosts multiple independent agents. Agents are identified by (string) agent ids. Note that these “agents” here are not to be confused with RLlib agents. .. rubric:: Examples
>>> env = MyMultiAgentEnv() >>> obs = env.reset() >>> print(obs) { "car_0": [2.4, 1.6], "car_1": [3.4, -3.2], "traffic_light_1": [0, 3, 5, 1], } >>> obs, rewards, dones, infos = env.step( action_dict={ "car_0": 1, "car_1": 0, "traffic_light_1": 2, }) >>> print(rewards) { "car_0": 3, "car_1": -1, "traffic_light_1": 0, } >>> print(dones) { "car_0": False, "car_1": True, "__all__": False, }
-
capital¶ float – The capital of the agent
-
funds¶ float – The cash/funds of the agent
-
possession¶ float – The size of the asset the agent possesses
-
price_n¶ int – Number of price points to render back in time.
-
trades_list¶ list – All trades
-
__init__(agent_list, random_agent_list=[], episode_seconds=60, **kwargs)[source]¶ Parameters: - market_type (str) – The type of market to be used
- market_setup (dict) – Parameters for the market
- initial_funds (float) – The initial cash of the agent
- T_ID (int) – The agent’s trader id
-
step(action_dict)[source]¶ Run one timestep of the environment’s dynamics. When end of episode is reached, you are responsible for calling reset() to reset this environment’s state. Accepts an action and returns a tuple (observation, reward, done, info). :param action: an action provided by the environment :type action: object
Returns: agent’s observation of the current environment reward (float) : amount of reward returned after previous action done (boolean): whether the episode has ended, in which case further step() calls will return undefined results info (dict): contains auxiliary diagnostic information (helpful for debugging, and sometimes learning) Return type: observation (object) Parameters: action_dict – Returns: - obs (list[tuple, tuple]) – The current quotes of the market and private information about the agents portfolio
- reward (float)
- done (bool) – If the episode has finished
- info (dict) – Additional information
-
send_messages(messages: tuple) -> (<class 'list'>, <class 'dict'>, <class 'bool'>)[source]¶ Sends all the messages to the market
Parameters: messages (tuple) – Returns: - trades (list)
- done (bool) – If the episode has finished or not.
- info (dict) – Extra information about the environment
-
reset(market=None)[source]¶ Resets the environment with a market or creates a new one. Also resets the render app if needed.
Parameters: market (Market) – If to use a specific market instead of a newly created one. Returns: observation Return type: tuple
-
get_messages(action_dict: dict) → tuple[source]¶ Returns messages based on the action received.
Parameters: action (numpy.array) – Returns: messages Return type: tuple
-
get_reward(trades: list, reward_dict=None) → tuple[source]¶ Returns a reward based on trades
Parameters: trades (list) – Returns: reward Return type: float
-
get_obs() → dict[source]¶ Returns an observation of the market. Currently the quotes.
Returns: observation Return type: tuple
-
get_private_variables() → tuple[source]¶ Returns private variables of for the agent to observe. For example the possession of the agent.
Returns: private_variables Return type: tuple
-
action_space¶ gym.spaces – The action space of the environment.
-
observation_space¶ gym.spaces – The obervation space of the environment.
-