How to Set Up and Use the Gym CryptoTrading Environment

Apr 6, 2022 | Data Science

Welcome to our guide on how to navigate the thrilling world of cryptocurrency trading using the Gym CryptoTrading Environment! This article will walk you through the installation, usage, and key features of this Python-based Bitcoin trading simulator.

Installation

Getting started with the Gym CryptoTrading Environment is easy. Just follow these simple steps:

  • Clone the repository:
  • git clone https://github.com/samre12/gym-cryptotrading.git
  • Change directory to the cloned repository:
  • cd gym-cryptotrading
  • Install the environment:
  • pip install -e .

Usage

Once installed, importing the module is straightforward. Use the following line of code:

import gym_cryptotrading

After importing, you can start working with various trading environments:

  • RealizedPnLEnv-v0
  • UnRealizedPnLEnv-v0
  • WeightedPnLEnv-v0

To create an environment, simply execute:

env = gym.make("RealizedPnLEnv-v0")

For the first call, use env.reset() to initiate a new episode. Remember, you need to reset the environment before usage, or you’ll encounter the gym.error.ResetNeeded() error.

Basics of the Environment

Observation Space

Your agent will observe the price data of Bitcoin, including closing, lowest, highest prices, and trade volume within each minute. The observations are normalized based on previous prices, giving you a ratio that indicates whether the price has increased or decreased.

Action Space

At each time step, the agent has three possible actions:

  • NEUTRAL corresponds to 0
  • LONG corresponds to 1
  • SHORT corresponds to 2

Parameters

The environment has specific parameters that govern its behavior:

  • history_length: Determines the lag in observations returned by env.reset().
  • horizon: Defines the episode length – how many trades can be made.
  • unit: The fraction of Bitcoin traded in each step.

Simulator

The dataset for the simulator comprises per-minute Bitcoin prices, modified from the workflow found in this repository. It’s important to note that the dataset might have missing values due to exchange downtimes, but the model makes no assumptions about these gaps, filtering blocks with sufficient continuity for simulation.

Important Information

On your first run, the environment will automatically download the latest transactions and cache them. If you need to update your dataset at any time, you can execute:

from gym_cryptotrading.generator import Generator
Generator.update_gen()

This should be done prior to the first env.reset().

Recent Updates and Breaking Changes

The latest updates have introduced several improvements, including support for different trading environments and renaming files for clarity. However, note the breaking change in how to create environments and retrieve remaining trades.

Troubleshooting

If you encounter issues with the environment, consider the following:

  • Ensure you have the correct version of Python installed. The environment requires Python 2.7.
  • Verify your internet connection if the dataset can’t be downloaded.
  • If you see gym.error.ResetNeeded(), make sure you’ve called env.reset() before any other operations.
  • To systematically address any logging issues, adjust logging levels using gym.logger.set_level(level). You can track debug and info levels to understand the environment’s actions better.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Analogy to Simplify Understanding

Imagine you’re a trader on a crowded market floor. Each tick of the clock represents a minute, and you have to decide whether to buy Bitcoin, sell it, or just observe. The observation space represents what you see — the prices and volumes — just like you’d gauge the market movement end-of-day. Your action space allows you to choose your next move, akin to deciding whether to make a purchase, sell your assets, or stand still. Each decision you make affects your situation, similar to trading with the parameters that dictate how much you can invest at any moment.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox