vegaprotocol / vega-market-sim

Build and interact with a self-contained Vega chain with controllable passage of time
MIT License
19 stars 10 forks source link

Vega Market Simulator

Build and interact with a self-contained Vega chain, with controllable passage of time.

The market simulator aims to:

Market Simulator is still under active development and so may change notably over time.

Setup

Linux: You should be good to go.

MacOS: Ensure that you have the command line developer tools installed. You can install them by running xcode-select --install in the terminal.

Windows: Please follow the Windows setup instructions.

For the most part the package is fairly self-contained Python, however there are some utility functions which will automatically download the requisite Vega services for you. You can choose to either build the Vega binaries from source (allowing you to investigate or change the internals if you desire) or download prebuilt binaries for your platform.

Download binaries

Build Vega from Source

UI

Building the console UI is not a necessary component, but can be useful for visualising the state of the Vega service. In order to do this:

Basic use

Examples can be found within the /examples folder. A good one to start is /examples/nullchain.py which spins up a simple service and proposes a market.

To run the example, start by running poetry shell if you're using poetry to activate the environment (If you're using another package manager, a similar command likely exists).

Ensure you are running these commands in your WSL terminal if using a Windows machine.

Then, to run the example mentioned above call: python -m examples.nullchain

If you've installed the console with make ui you can also extend this to visualise what is happening with the --console flag:

  1. First you have to - Set the run_with_console flag to True when creating the VegaServiceNull class
  2. Run the command

    python -m examples.nullchain --console

Alternatively, to run a longer and more complex scenario, run:

  python -m vega_sim.scenario.adhoc -s historic_ideal_market_maker_v2 --pause

If you've installed the console with make ui you can also extend this to visualise what is happening with the --console flag:

  python -m vega_sim.scenario.adhoc -s historic_ideal_market_maker_v2 --pause --console

Decimal Handling

For the most part the simulator aims to shield you from the specifics of running the market on a blockchain, however decimal handling is one place where you may have to be (hopefully only slightly) aware of what's going on. In order to avoid the imprecision issues with floating point numbers, along with aligning with how Ethereum handles the issue, stored numbers in the Vega chain are generally saved as ints, padded with zeros to represent the number of decimal places the number has. For example, 10 with two decimal places would be saved as 1000, whilst 10.52 would become 1052. Whilst this obviously limits precision to a set level (there can be no 10.521 in the two decimal place world) it allows the numbers to be stored exactly.

Alongside these, there is also a field for the number of decimal places a given asset's price, or market's price and position, is stored in. This allows conversion back to a floating point number when desired.

Many fields, especially those returning a single number, run this conversion for you. As do all those taking a number as input. Where one has to be more careful is in more complex objects which may have such numbers nested inside them in unknown locations.

To help with this differentiation, all functions which may return such an object should be carved out into the data_raw module within the api path, as opposed to the standard data module. If you're just using the Service interface (as hopefully is generally sufficient) then there is an additional layer of defence, the warn_on_raw_data_access flag. This flag will trigger the module to warn log every time a method which returns something which could/does contain these integers is called. It's then up to the external code to handle this information (or ignore it if you know you are safe).

Once code has been written, this flag can be turned off for production runs so as not to flag erroneously.

Reinforcement Learning

The nascent framework for reinforcement learning applications of the Vega Market Sim can be found within vega_sim.reinforcement, this includes the beginnings of a 'background market' within the folder vega_sim.reinforcement.full_market_sim alongside an agent in vega_sim.reinforcement.learning_agent which learns from a currently very simple set of inputs (alongside an input of a future price, to make learning on a random walk possible at all!) Usage is new, and the code strucure and functionality is liable to change significantly in the future, however input is welcomed.

Parameter Testing

One use of this framework is for testing the effect on Vega core itself of varying the various specified network and market parameters, allowing analysis and comparison and hopefully a more optimal set of parameters. To dig into this use case, the configuration for various existing tests lives in vega_sim.parameter_test.parameter.configs and these can be run by finding the parameter test name in that file and calling:

python -m vega_sim.parameter_test.run -c YOUR_TEST_NAME_HERE

This will run the configured scenario several times with each configured parameter value before outputting them into the /parameter_results folder in the root of the repository.

Scenarios

To support the above mentioned parameter tests, and to allow for more complex capabilities, a set of scenarios (and agents who act within them) live in vega_sim.scenario. We aim here to provide a set of agent primitives which allow one to construct complex interactions and trading scenarios.

Momentum Agent

One of the agents provided in the scenarios is momentum agent that can currently follow APO/ RSI/ STOCHRSI/ CMO/ MACD momentum strategies. A comprehensive scenario for momentum agent tests is in vega_sim.scenario.comprehensive_market.scenario, where mutileple momentum agents with different momentum strategies can be tested.

To run momentum agents, you must first install the TA-Lib libraries. The instructions here are likely a good start. Follow this by running poetry install -E agents. Then, run python examples.agent_market.MomentumAgent.py, and the performance of momentum agents are shown in the notebook examples.notebooks.MomentumAgentPerformance. The agents can be configured in ComprehensiveMarket. For example, to run 2 momentum agents with APO and MACD strategies:

ComprehensiveMarket(
  num_momentum_agents=2,
  momentum_trader_strategies=['APO', 'MACD'],
  momentum_trader_strategy_args=[
    {
      "fastperiod": 12,
      "slowperiod": 26,
    },
    {
      "fastperiod": 14,
      "slowperiod": 26,
      "signalperiod": 9,
    }
  ],  
)

Release Process

Releases are aligned to Vega core releases, to provide a snapshot of working code for each release cutoff. To build and release a Vega Market Sim package, follow these steps:

Windows Setup

1. Install [WSL]

  1. Install Windows Subsystem for Linux (WSL) on Windows
  2. Install Ubuntu distribution inside WSL.

2. Install 'Make' to your WSL

3. Install 'Poetry' to your WSL

4. Clone the Repository inside your WSL

5. Run make to automatically pull install dependencies

  1. Run the command:

    make

    6. Install the package into your local environment.

  2. Run the command:

    poetry shell
  3. Run the command:

    poetry install

    [OPTIONAL]

    To include all optional extras use:

    poetry install --all-extras

7. Run initial tests

  1. Run the command:

    make test

    This will check all the python environment + vega imports are set up correctly, doesn't run Vega yet.

8. Run integration tests

  1. Run the command:

    make test_integration

    This will check that everything is set up correctly. Takes about 5 minutes.

    9. Set up Development environment

    1. Install "Remote - WSL" extension

  2. Install ms-vscode-remote.remote-wsl extension

  3. Open your WSL terminal (ensure you are in the vega-market-sim directory). Run the command:

    code .

    This will open up a visual studio window connected to your linux environment.

OPTIONAL Setup for the UI

1. Install the following to your WSL

NVM Node 16 Yarn

2. Make UI

Run the command:

   make ui