Introducing TMS: a Trading Market Simulator


This post was originally published by at Towards Data Science

An easy to use trading simulator to test trading (ML/AI) algorithms and strategies on Python

Image for post

Simulation of AAPL on September 9th, 2020, using TMS.

Sometime ago, I wrote an article on how to download stocks market data for free using Alpaca, a trading broker and API. I published this article because I had worked on projects that needed stocks market data before and I realized that finding these data for free is very hard. However, having these data is not enough to test trading algorithms, whether traditional or AI/ML-based. To do this, you still need to implement a testing or simulation environment that integrates these data.

In this post, I introduce the Trading Market Simulator (TMS): a simple simulator I have developed to get data from Alpaca and simulate a stream of live market data, so you can test your trading strategies on data from any day of your choice in no time.

TMS is a Python module that not only simulates the market but also a broker, so you can manage multiple accounts, opening and closing positions to test your trading strategies and algorithms. With TMS you can simulate long and short positions, customizing the spreads so they match with the ones used by your preferred broker. TMS uses data from Alpaca Broker, so you can simulate trades on U.S. stocks as long as they are included in Alpaca.

With TMS you can:

  • Automatically download market data for any U.S. stock of your choice (as long as it is included in Alpaca)
  • Simulate a stream of live market data for any day with a 1 minute granularity
  • Test your trading algorithms and strategies in a simple environment, but as close as possible to the real environment on Alpaca so you can simply copy-paste your code between TMS and Alpaca
  • Customize spreads for broker: you can simulate different spreads depending on the broker you want to use (more customizations such as commissions will come soon)

TMS is open-source software (MIT License) and its published on Github, so you can utilize it in any project you like and modify it for your needs. Note that TMS is not associated to Alpaca in any way.

Once you have created an Alpaca account, you can initialize a TMS broker with the following 3 lines of code:

This effectively downloads the data for AAPL and GOOG for the current day. From here on, you can open an account with some default available cash in it, and open and close positions for these stocks as you wish:

For a more detailed introduction, please follow the tutorial included in TMS’s GitHub.

You can start experimenting with TMS by choosing a day and testing simple strategies to detect buy opportunities such as the golden cross. Here you can also find a golden cross scalping strategy on Alpaca. Note that the Alpaca code only work when the market is open, but with some changes you can use the same strategy with TMS on any day of your choice. If you’re interested in more trading strategies, I recommend you this excellent article on Investopedia:

Beginner Trading Strategies

The key is to plan your trade, and then trade your plan. Sticking to the strategy will likely lead to better outcomes…

I have implemented TMS for testing machine learning-based strategies (most likely RL, but I’m also open to other methods). I will try to find the time to write about this on Medium, so stay tuned if you’re interested. In the meanwhile, you can start trying on your own, using one of these excellent frameworks:

5 Frameworks for Reinforcement Learning on Python

Programming your own Reinforcement Learning implementation from scratch can be a lot of work, but you don’t need to do…

Thank you for reading and I hope you find TMS useful! 🙂

Spread the word

This post was originally published by at Towards Data Science

Related posts