async_exchange
is lightweight python library for concurrent multi-agent stock exchange simulation.
async_exchange
is designed in way that the simulations can be run in a single thread, and the agents can interact with the exchange in a asynchronous way.
The basic usage of the async_exchange
does not require any additional dependencies.
Install the package in your preferred environment, and try the demo.
Create your own trader agents in two steps:
-
Define a subclass of the
Trader
that implements an asynchronouscycle()
method. Thecycle
method should emulate the agent's thinking and decision making process. The agent can get information about the existing order book, own standing orders via theexchange_api
methods.In the demo example, the
cycle
consists of a "sleep" phase (the agent is inactive) and a random order submission.class RandomTrader(Trader): async def cycle(self): while True: await self.sleep() self.place_random_order()
Note that if any actual CPU-bound computations happen inside the
cycle
method of an agent, the whole event loop will be blocked. Consider delegating heavy computations to an external process that can be awaited. -
Create an environment of agents (not necessarily of the same type), and run the simulation.
Users are welcome to implement their own post-processing and analysis tools. In the following sections, we introduce a possible way to store logs and visualize the trading history.
Users can implement their own logger and pass it to the Exchange
instance.
The logger must implement a send_event
method with two arguments: an event_type
string, and a message
.
There exists a built-in logging infrastructure.
The InfluxDBLogger
stores the information about all successful exchange operations from the Exchange
to a local InfluxDB.
First, install necessary influxdb
requirements from the setup/influxdb/requirements.txt
.
Second, the logger expects an InfluxDB service available on http://localhost:8086
.
Use the docker-compose.yml file to start required services via docker.
Important: the docker-compose
expect that the /tmp/exchange/influxdb
directory exists on the host machine.
$ cd setup/influxdb
$ mkdir -p /tmp/exchange/influxdb
$ docker-compose -f docker-compose.yml up -d
If the docker-compose
method from the previous section was used to set up logging, a Grafana service is a suggested way to visualize trading sessions.
- Login to Grafana at
http://localhost:3031
using the standardadmin
/admin
username and password. - A visualization dashboard should have already been created. Start the trading session, and the candlestick chart should appear shortly (the dashboard updates every 5 seconds).