Build your own trading robot with Trading Gym

June 19, 2017

Quickstart to trading with Reinforcement Learning.

Have you ever wanted to build your own trading robot? In this blog post, we will walk through how to write a simple reinforcement learning algorithm using Trading Gym, an open source project by Prediction Machines. Trading Gym provides a trading scenario for the algorithm to learn from sample market data with trading fees, time fees and spread coefficients designed to mimic the real trading world.

In this article, we will explain how to teach an automated agent how to trade a single financial asset A (In this case of a single asset, the spread coefficient is simply (1)). Going long on the asset A would involve buying 1 unit of A while going short on the spread would involve selling 1 unit of A. The environment also allows to consider a more general spread composed of several products. In the case of two products for instance, the spread coefficient tuple could be (1, -1) meaning buying the spread would translate in buying 1 lot of A and selling 1 lot of B.

The environment can take as input three possible actions: Buy, Sell and Hold. When our agent submits an action, the Trading Gym environment would return a state composed of:

  • 1) Current prices
  • 2) The position (‘flat’: no position taken, ‘long’: bought 1 unit of A, ‘short’: sold 1 unit of A),
  • 3) The entry price if long or short (0 if flat) and
  • 4) The realised PnL.

Getting started

To get started, make sure Python 2.7 and the python package manager pip are installed on your machine.

Step 1: Install Trading Gym

pip install tgym

Step 2: Install Keras

pip install keras tensorflow gym

Step 3: Clone the Prediction Machine’s Trading Gym Repo

git clone

Step 4: Run the example in the examples folder.



For a more rich sample use case of Trading Gym, we have a proposed architecture on Github that can be found here Trading Brain