omega_client.communication package¶
Submodules¶
omega_client.communication.omega_connection module¶
Omega Connection class. Send and receive messages to and from Omega.
-
class
omega_client.communication.omega_connection.OmegaConnection(zmq_context: zmq.sugar.context.Context, omega_endpoint: str, request_sender_endpoint: str, response_receiver_endpoint: str, request_sender: omega_client.communication.request_sender.RequestSender, response_receiver: omega_client.communication.response_receiver.ResponseReceiver, omega_polling_timeout_milli: int = 1000, name: str = 'OmegaConnection', omega_socket_identity: bytes = None, server_zmq_encryption_key: str = None)[source]¶ Bases:
threading.ThreadBase OmegaConnection class that abstracts out ZMQ connection, capn-proto parsing, and communication with the Omega. Actions like Placing Orders, Requesting Account Balances and passing their associated responses from Omega as callbacks are handled by this class.
- Attributes:
_ZMQ_CONTEXT: (zmq.Context) Required to create sockets. It is recommended that one application use one shared zmq context for all sockets.
_OMEGA_ENDPOINT: (str) The zmq endpoint to connect to Omega, in the form of a zmq connection str ‘protocol://interface:port’, e.g. ‘tcp://0.0.0.0:9999’.
_REQUEST_SENDER_ENDPOINT: (str) The zmq endpoint used to connect to _request_sender. By default it is a local, inproc endpoint that lives in another thread of the same process.
_RESPONSE_RECEIVER_ENDPOINT: (str) The zmq endpoint used to connect to _response_receiver. By default it is a local, inproc endpoint that lives in another thread of the same process.
_OMEGA_POLLING_TIMEOUT_MILLI: (int) The polling timeout for _omega_connection_socket.
_OMEGA_SOCKET_IDENTITY: (bytes) The socket identity in bytes used for the ROUTER socket on the other side to identify the DEALER socket in this class. Optional since zmq DEALER socket generates a default identity.
_SERVER_ZMQ_ENCRYPTION_KEY: (str) The public key of the Omega server used to encrypt data flowing between the client and server.
_response_receiver: (ResponseReceiver) The response receiver object.
_request_sender: (RequestSender) The request sender object.
_is_running: (Event) An event to indicate if the connection is running.
-
cancel_all_orders(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, symbol: str = None, side: str = None)[source]¶ Sends a request to Omega to cancel an order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account on which to cancel order.
- symbol – (str) (optional)
- side – (str) (optional)
:return (capnp._DynamicStructBuilder) cancel_all_orders object.
-
cancel_order(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, order_id: str)[source]¶ Sends a request to Omega to cancel an order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account on which to cancel order.
- order_id – (str) order_id as returned from the ExecutionReport.
Returns: (capnp._DynamicStructBuilder) cancel_order object.
-
logoff(request_header: omega_client.messaging.common_types.RequestHeader)[source]¶ Logoff Omega for a specific client_id.
Parameters: request_header – Header parameter object for requests. Returns: (capnp._DynamicStructBuilder) Logoff capnp object.
-
logon(request_header: omega_client.messaging.common_types.RequestHeader, client_secret: str, credentials: List[omega_client.messaging.common_types.AccountCredentials])[source]¶ Logon to Omega for a specific client_id and set of credentials.
Parameters: - request_header – Header parameter object for requests.
- client_secret – (str) client_secret key assigned by Fund3.
- credentials – (List[AccountCredentials]) List of exchange
credentials in the form of AccountCredentials. :return: (capnp._DynamicStructBuilder) Logon capnp object.
-
place_order(request_header: omega_client.messaging.common_types.RequestHeader, order: omega_client.messaging.common_types.Order)[source]¶ Sends a request to Omega to place an order.
Parameters: - request_header – Header parameter object for requests.
- order – (Order) Python object containing all required fields.
Returns: (capnp._DynamicStructBuilder) place_order capnp object.
-
replace_order(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, order_id: str, order_type: str = 'undefined', quantity: float = 0.0, price: float = 0.0, stop_price: float = 0.0, time_in_force: str = 'gtc', expire_at: float = 0.0)[source]¶ Sends a request to Omega to replace an order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account on which to cancel order.
- order_id – (str) order_id as returned from the ExecutionReport.
- order_type – (OrderType) (optional)
- quantity – (float) (optional)
- price – (float) (optional)
- stop_price – (float) (optional)
- time_in_force – (TimeInForce) (optional)
- expire_at – (float) (optional)
Returns: (capnp._DynamicStructBuilder) replaceOrder capnp object.
-
request_account_balances(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for full account balances snapshot on an Account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) request_account_balances capnp
object.
-
request_account_data(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for full account snapshot including balances, open positions, and working orders on specified account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) request_account_data capnp
object.
Sends a request to Omega to refresh the session
Parameters: - request_header – Header parameter object for requests.
- auth_refresh – AuthorizationRefresh python object
Returns: (capnp._DynamicStructBuilder) authorization_refresh capnp
object.
-
request_completed_orders(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, count: int = None, since: float = None)[source]¶ Sends a request to Omega for all completed orders on specified account. If both ‘count’ and ‘from_unix’ are None, returns orders for last 24h.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
- count – (int) optional, number of returned orders (most recent
ones). :param since: (float) optional, returns all orders from provided unix timestamp to present. :return: (capnp._DynamicStructBuilder) request_completed_orders capnp object.
-
request_exchange_properties(request_header: omega_client.messaging.common_types.RequestHeader, exchange: str)[source]¶ Sends a request to Omega for supported currencies, symbols and their associated properties, timeInForces, and orderTypes on an exchange.
Parameters: - request_header – Header parameter object for requests.
- exchange – (str) The exchange of interest.
Returns: (capnp._DynamicStructBuilder) request_exchange_properties capnp
object.
-
request_open_positions(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for open positions on an Account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) request_open_positions capnp
object.
-
request_order_status(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, order_id: str)[source]¶ Sends a request to Omega to request status of a specific order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
- order_id – (str) The id of the order of interest.
Returns: (capnp._DynamicStructBuilder) request_order_status capnp
object.
-
request_server_time(request_header: omega_client.messaging.common_types.RequestHeader)[source]¶ Request Omega server time for syncing client and server timestamps.
Parameters: request_header – Header parameter object for requests. Returns: (capnp._DynamicStructBuilder) request_server_time capnp object.
-
request_working_orders(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for all working orders snapshot on an Account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) request_working_orders capnp
object.
-
run()[source]¶ Main loop for Omega connection. Set up 3 sockets: 1. omega_socket - the socket that sends and receives messages from Omega. 2. request_listener_socket - listens to requests from request sender
and forward them to omega_socket.- response_forwarding_socket - forwards responses to response_receiver
- when responses are received from Omega.
-
send_heartbeat(request_header: omega_client.messaging.common_types.RequestHeader)[source]¶ Sends a heartbeat to Omega for maintaining and verifying connection. Only clients that are logged on will receive heartbeat back from Omega.
Parameters: request_header – Header parameter object for requests. Returns: (capnp._DynamicStructBuilder) heartbeat capnp object.
-
omega_client.communication.omega_connection.configure_default_omega_connection(omega_endpoint: str, omega_server_key: str, response_handler: omega_client.messaging.response_handler.ResponseHandler)[source]¶ Set up a TesConnection that comes with request_sender and response_receiver.
Parameters: - omega_endpoint – (str) The zmq endpoint to connect to Omega.
- omega_server_key – (str) The public key of the Omega server.
- response_handler – (ResponseHandler) The handler object that will
be called in a callback function when omega_connection receives a message. :return: omega_connection, request_sender, response_receiver
-
omega_client.communication.omega_connection.configure_single_client_omega_connection(omega_endpoint: str, omega_server_key: str, client_id: int, sender_comp_id: str, response_handler: omega_client.messaging.response_handler.ResponseHandler)[source]¶ Set up a TesConnection that comes with request_sender and response_receiver. Sets the default client_id and sender_comp_id for the request sender. Note that each machine should be assigned a unique sender_comp_id even when the client_id is the same.
Parameters: - omega_endpoint – (str) The zmq endpoint to connect to Omega.
- omega_server_key – (str) The public key of the Omega server.
- client_id – (int) The client id assigned by Fund3.
- sender_comp_id – (str) str representation of a unique Python uuid.
- response_handler – (ResponseHandler) The handler object that will
be called in a callback function when omega_connection receives a message. :return: omega_connection, request_sender, response_receiver
omega_client.communication.request_sender module¶
-
class
omega_client.communication.request_sender.RequestSender(zmq_context: zmq.sugar.context.Context, zmq_endpoint: str, outgoing_message_queue: queue.Queue = None, queue_polling_timeout_seconds: int = 1, name: str = 'TesRequestSender')[source]¶ Bases:
threading.ThreadRuns as an individual thread to send requests to TesConnection, which then gets routed to Omega. The motivation of the design is different threads should not share zmq sockets, and that the TesConnection event loop should not be blocked.
When a request is “sent” from this class, it is placed into an internal thread-safe queue. The request sender loop checks if the queue has a message, and if there is one, sends it to TesConnection through an inproc connection.
- Attributes:
_ZMQ_CONTEXT: (zmq.Context) Required to create sockets. It is recommended that one application use one shared zmq context for all sockets.
_ZMQ_ENDPOINT: (str) The zmq endpoint to connect to.
_QUEUE_POLLING_TIMEOUT_SECONDS: (int) The polling timeout for the internal queue.
_outgoing_message_queue: (Queue) Internal message queue for outgoing Omega Messages.
_is_running: (Event) Event object that indicates on/ off behavior for the response handler loop.
-
cancel_all_orders(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, symbol: str = None, side: str = None)[source]¶ Sends a request to Omega to cancel all orders. Optionally including side and/or symbol
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account on which to cancel order.
- symbol – str (optional)
- side – str (optional)
Returns: (capnp._DynamicStructBuilder) cancel_all_orders object.
-
cancel_order(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, order_id: str)[source]¶ Sends a request to Omega to cancel an order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account on which to cancel order.
- order_id – (str) order_id as returned from the ExecutionReport.
Returns: (capnp._DynamicStructBuilder) cancel_order object.
-
logoff(request_header: omega_client.messaging.common_types.RequestHeader)[source]¶ Logoff Omega for a specific client_id.
Parameters: request_header – Header parameter object for requests. Returns: (capnp._DynamicStructBuilder) Logoff capnp object.
-
logon(request_header: omega_client.messaging.common_types.RequestHeader, client_secret: str, credentials: List[omega_client.messaging.common_types.AccountCredentials])[source]¶ Logon to Omega for a specific client_id and set of credentials.
Parameters: - request_header – Header parameter object for requests.
- client_secret – (str) client_secret key assigned by Fund3.
- credentials – (List[AccountCredentials]) List of exchange
credentials in the form of AccountCredentials. :return: (capnp._DynamicStructBuilder) Logon capnp object.
-
place_order(request_header: omega_client.messaging.common_types.RequestHeader, order: omega_client.messaging.common_types.Order)[source]¶ Sends a request to Omega to place an order.
Parameters: - request_header – Header parameter object for requests.
- order – (Order) Python object containing all required fields.
Returns: (capnp._DynamicStructBuilder) place_order capnp object.
-
replace_order(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, order_id: str, order_type: str = 'undefined', quantity: float = 0.0, price: float = 0.0, stop_price: float = 0.0, time_in_force: str = 'gtc', expire_at: float = 0.0)[source]¶ Sends a request to Omega to replace an order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account on which to cancel order.
- order_id – (str) order_id as returned from the ExecutionReport.
- order_type – (OrderType) (optional)
- quantity – (float) (optional)
- price – (float) (optional)
- stop_price – (float) (optional)
- time_in_force – (TimeInForce) (optional)
- expire_at – (float) (optional)
Returns: (capnp._DynamicStructBuilder) replaceOrder capnp object.
-
request_account_balances(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for full account balances snapshot on an Account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) get_account_balances capnp
object.
-
request_account_data(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for full account snapshot including balances, open positions, and working orders on specified account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) get_account_data capnp object.
Sends a request to Omega to refresh the session
Parameters: - request_header – Header parameter object for requests.
- auth_refresh – AuthorizationRefresh python object
Returns: (capnp._DynamicStructBuilder) authorization_refresh capnp
object.
-
request_completed_orders(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, count: int = None, since: float = None)[source]¶ Sends a request to Omega for all completed orders on specified account. If both ‘count’ and ‘from_unix’ are None, returns orders for last 24h.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
- count – (int) optional, number of returned orders (most recent
ones). :param since: (float) optional, returns all orders from provided unix timestamp to present. :return: (capnp._DynamicStructBuilder) get_completed_orders capnp object.
-
request_exchange_properties(request_header: omega_client.messaging.common_types.RequestHeader, exchange: str)[source]¶ Sends a request to Omega for supported currencies, symbols and their associated properties, timeInForces, and orderTypes on an exchange.
Parameters: - request_header – Header parameter object for requests.
- exchange – (str) The exchange of interest.
Returns: (capnp._DynamicStructBuilder) get_exchange_properties capnp
object.
-
request_open_positions(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for open positions on an Account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) get_open_positions capnp
object.
-
request_order_status(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo, order_id: str)[source]¶ Sends a request to Omega to request status of a specific order.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
- order_id – (str) The id of the order of interest.
Returns: (capnp._DynamicStructBuilder) get_order_status capnp object.
-
request_server_time(request_header: omega_client.messaging.common_types.RequestHeader)[source]¶ Request Omega server time for syncing client and server timestamps.
Parameters: request_header – Header parameter object for requests. Returns: (capnp._DynamicStructBuilder) heartbeat capnp object.
-
request_working_orders(request_header: omega_client.messaging.common_types.RequestHeader, account_info: omega_client.messaging.common_types.AccountInfo)[source]¶ Sends a request to Omega for all working orders snapshot on an Account.
Parameters: - request_header – Header parameter object for requests.
- account_info – (AccountInfo) Account from which to retrieve data.
Returns: (capnp._DynamicStructBuilder) get_working_orders capnp object.
-
run()[source]¶ Message sending loop. Create the request_socket as a zmq.DEALER socket and then connect to the provided _ZMQ_ENDPOINT.
Try to get a message for _QUEUE_POLLING_TIMEOUT_SECONDS and then send it out to TesConnection.
-
send_heartbeat(request_header: omega_client.messaging.common_types.RequestHeader)[source]¶ Sends a heartbeat to Omega for maintaining and verifying connection. Only clients that are logged on will receive heartbeat back from Omega.
Parameters: request_header – Header parameter object for requests. Returns: (capnp._DynamicStructBuilder) heartbeat capnp object.
omega_client.communication.response_receiver module¶
Omega Response Receiver class. Receive messages from a local TesConnection that is connected to Omega.
-
class
omega_client.communication.response_receiver.ResponseReceiver(zmq_context: zmq.sugar.context.Context, zmq_endpoint: str, response_handler: omega_client.messaging.response_handler.ResponseHandler, polling_timeout_milli: int = 1000, name: str = 'ResponseHandler', socket_identity: bytes = None)[source]¶ Bases:
threading.ThreadActs as a separate thread that processes the messages coming from Omega so that the message receive loop is not blocked. Only does unidirectional message receiving from TesConnection.
Future optimization plan: add this into a threadpool or processpool for sliced/ parallel processing of messages.
- Attributes:
_ZMQ_CONTEXT: (zmq.Context) Required to create sockets. It is recommended that one application use one shared zmq context for all sockets.
_ZMQ_ENDPOINT: (str) The zmq endpoint to connect to.
_RESPONSE_HANDLER: (ResponseHandler) The ResponseHandler object that holds the logic of handling each type of response.
_POLLING_TIMEOUT_MILLI: (int) The polling timeout for response_socket.
_SOCKET_IDENTITY: (bytes) The socket identity in bytes used for the ROUTER socket on the other side to identify the DEALER socket in this class. Optional since zmq DEALER socket generates a default identity.
_is_running: (Event) Event object that indicates on/off behavior for the response handler loop.
-
run()[source]¶ Message receiving loop. Create the response_socket as a zmq.DEALER socket and then connect to the provided _ZMQ_ENDPOINT. After that, set up the poller and handle received messages.
Normally zmq socket generates a default socket identity, but for testing or other purposes, the socket identity can be set by passing in a binary identity when creating the ResponseReceiver class.
The poller exists so that the response receiver can be stopped gracefully and not get blocked by socket.recv() or stuck in a loop.
omega_client.communication.single_client_request_sender module¶
-
class
omega_client.communication.single_client_request_sender.SingleClientRequestSender(zmq_context: zmq.sugar.context.Context, connection_string: str, client_id: int, sender_comp_id: str, outgoing_message_queue: queue.Queue = None)[source]¶ Bases:
objectWrapper around RequestSender with added boilerplate code support use cases with only 1 client_id.
-
cancel_all_orders(account_info: omega_client.messaging.common_types.AccountInfo, symbol: str = None, side: str = None)[source]¶
-
logon(client_secret: str, credentials: List[omega_client.messaging.common_types.AccountCredentials])[source]¶
-
replace_order(account_info: omega_client.messaging.common_types.AccountInfo, order_id: str, order_type: str = 'market', quantity: float = 0.0, price: float = 0.0, stop_price: float = 0.0, time_in_force: str = 'gtc', expire_at: float = 0.0)[source]¶
-
request_completed_orders(account_info: omega_client.messaging.common_types.AccountInfo, count: int = None, since: float = None)[source]¶
-
request_order_status(account_info: omega_client.messaging.common_types.AccountInfo, order_id: str)[source]¶
-