How to Build Your Own Python Trading Bot (2024)

Table of contents

In order to get you started with the Trality Bot Code Editor and your first Python trading bot, we’ll use this post to cover a fairly basic approach to building a simple trading algorithm. It consists of standard technical analysis (TA) but also includes some features of the Trality API that can help you to create more sophisticated trading bots as you proceed. Moreover, we’ll analyze a resulting backtest, which tests our algorithm on historical data and then we will use the Optimizer to optimize our strategy's parameters for maximum profit.

Creating Trading Bots With Trality’s Python Code Editor

Trality’s state-of-the-art Python Code Editor allows users to create highly innovative and intricate algorithms in the most efficient way possible.

Once you’re satisfied with your algorithm and backtesting results, you can deploy your bot for live trading or paper trading on some of the most trusted exchanges, including Binance, Binance.US, Bitpanda, Coinbase Pro, and Kraken.

How Do Trading Bots Work?

Trading bots make decisions on behalf of a trader based on information, such as price movements within the market, generating a reaction according to a predefined set of criteria. They can interpret and merge traditional market signals such as price, volume, orders and time with more sophisticated signals, such as machine learning models, and decide whether or not it’s a good opportunity to execute a trade.

Financial Data For Trading Bots

There are two main types of raw market data that come from centralized cryptocurrency exchanges (CEX). One is quote data from the limit order book (LOB) and the other is trade data. Quotes are aggregated snapshots of a set of limit orders at a given price level that have not been matched.

Trades occur whenever an order is placed that matches with another order resting in the LOB. Raw market data is streamed from the exchange via websockets. When any update to the exchange LOB occurs, the information regarding that change is pushed via the websocket in real-time.

Within the Trality engine, candlestick data is used to represent the prices of an asset over a specific period. There are 5 main components of a candlestick: 1) the open (first) price, 2) the highest price, 3) the lowest price, 4) the close (last) price, and 5) the volume.

Each candlestick is derived from the raw market data via the raw websocket rather than listening to the price bars from the exchange. It takes time for the exchange to build and publish these bars; by generating them it is possible to minimize the latency between a bar closing and it being available to a bot. Being able to react quicker improves the execution price the bot can achieve should it want to trade.

One of the most important parts of market data is that the backtests are consistent with the live environment. This ensures that the bot's behavior will be the same whether running in a backtest or live environment.

Trality achieves this by making sure the candlestick data that the bots receive is the same in the live, virtual, and backtest environments. As a result, whatever decisions the bot makes using market data will be consistent across all environments.

Candlestick data also needs to be consistent across multiple time frames. Each 1h candlestick should be consistent with the four 15m bars that make it up. And each 5m bar is consistent with the five 1m bars that make it up. This is achieved on Trality by deriving longer-term bars from the highest fidelity data we have, ensuring that candlesticks are not only accurate but also consistent across all time frames.

Creating a Python Trading Bot

So how do we use a simple cryptocurrency trading bot to protect our assets in a turbulent year like 2022? We will show you a beginner-level example strategy, which is based on a commonly-used TA indicator: an exponential moving average (EMA), or the crossover of two EMAs to be precise.

See the Trality Documentation for a detailed explanation of the EMA crossover strategy.

In short: An EMA is a type of moving average (MA) that places greater weight and significance on the most recent data points. An exponentially weighted moving average reacts more significantly to recent price changes than a simple moving average (SMA). A crossover strategy applies two moving averages to a chart, one longer and one shorter.

When the shorter-term MA crosses above the longer-term MA, it's a buy signal, as it indicates that the trend is shifting up. Meanwhile, when the shorter-term MA crosses below the longer-term MA, it's a sell signal, indicating that the trend is shifting down.

We, therefore, develop a strategy with two EMAs (20 and 50 candles look back period). The strategy trades on 6 hour candles, making it sensitive to mid- to short-term price movements. For this scenario, the strategy allocates 80% of the account balance when taking a position.

We use the Trality's Python Code Editor to code this algorithm. Let's go through each of the main building blocks one step at a time:

Step 0: Define handler function

To begin with, every function that is annotated with our schedule decorator is run on a specified time interval and receives symbol data. We call these annotated functions handlers, but you can name them however you want. They just have to take two arguments. We call the first one state and the second one data. The second argument will always receive the symbol data for the interval you specified. In this particular bot, we trade in 6 hour candle intervals and we specify a trading symbol, which is BTCUSDT. Of course multiple symbols are possible to trade on as well!

Step 1: Compute indicators from data

In the first step of our algorithm creation, we define two exponential moving averages (EMA), one with a shorter look-back period of 20 candles and one longer with a period of 50 candles.

Step 2: Fetch position for symbol

In a second step we query for any open position by symbol. By calling this function we receive a boolean value indicating whether an open position for that symbol exists or not.

Step 3: Fetch position for symbol

In a third step we query for any open position by symbol. By calling this function we receive a boolean value indicating whether an open position for that symbol exists or not.

Step 3: Resolve buy or sell signals

In a third step, the heart and soul of our algorithm is defined: its trading strategy. We use the order API to create orders. Specifically, the algorithm places a market order going long if the shorter EMA crosses above the longer for 80% of the account balance.

We also define a sell logic, which closes the open position if the algorithm detects an open position and the shorter crosses below the longer EMA.

Putting the pieces together

If we put all these steps together, we get the following little code snippet, which we can subsequently put through our first backtest:

Backtesting the Python Bot on Historical Data

In order to evaluate our beginner-level cryptocurrency trading bot, we run the above code in the Trality backtester and obtain the following results:

How to Build Your Own Python Trading Bot (1)

How to Build Your Own Python Trading Bot (2)

The figure above shows the results of our Python trading bot from June 28 2022 to July 28 2022. Backtests on Trality always include exchange fees and can be modeled to account for slippage. As evidenced by the individual positions and stats, the bot executed 4 trades and the total return over the specified period is -6.61% while benchmark performance is 10.65%.

The next and final step is the optimization phase.

Optimizing Strategy Parameters

Creating a profitable Python-based bot can be challenging. Even when you have an algorithm idea you are satisfied with, optimizing its parameters can be frustrating and time-consuming. That’s why our research team built the Optimizer.

A new feature for the backtester when creating Python Code Bots, the Optimizer will allow you to automate the parameter optimization process. When writing your bot code, you simply define relevant parameters and their respective ranges that you want to be optimized to achieve the highest PnL, and let the Optimizer do its magic.

Trality’s Optimizer Walkthrough

We have made the process for the optimization of your bot very simple. Let's say we want to find the optimal period for ema_short and ema_long to achieve the highest possible return.

As you can see from the code below, we will need to add our new feature annotation @parameter on top of the initializer. Once that is done, to use the @parameter annotations we need to add the params object to the functions and to the indicators.

Now it’s ready to be optimized. Don’t forget to activate the optimizer under advanced settings!

How to Build Your Own Python Trading Bot (3)

How to Build Your Own Python Trading Bot (4)

Running the Optimizer, it was found that the optimal parameter for ema_short is 10 and for ema_long is 30. You can see the backtesting results in the image above. With the optimal parameters, the bot managed to increase total return from -6.61% to 3.21%.

Takeaways for Your Python Trading Bot

It is important to note that this is a fairly simple trading bot, which is meant as a starting point for your analysis. Trality offers many more possibilities to create bots that will help you to significantly outperform the market. In order to do so, more nuanced elements of your code might become necessary, such as trading on multiple intervals and with multiple coins or using sophisticated order management with multiple order types. And last but not least, leverage Trality’s state-of-the-art Optimizer to automatically optimize your strategy parameters to find the best settings for maximum profit.

However, if you find that you've created a profitable trading bot, then why take the next step by submitting it to the Trality Marketplace? It's the perfect way to earn passive income from investors around the world who are interested in crypto copy trading!

To find further information on our features please visit our documentation. And if you are looking to get started, we recommend taking a look at our Masterclass.

Disclaimer: None of what is found in this article should be considered investment advice. The above article is merely an opinion piece and does not represent any kind of trading advice or suggestions on how to invest, how to trade or in which assets to invest in or suggestions on how trading bots or trading algorithms can or should be used! Always do your own research before investing and always (!) only invest what you can afford to lose! Backtests are not indicative of future results.

How to Build Your Own Python Trading Bot (2024)

FAQs

How to build a trade bot in Python? ›

Building a Trading Bot in Python : Step-by-Step Guide with...
  1. Step 1: Set up your development environment. ...
  2. Step 2: Choose a trading platform and API. ...
  3. Step 3: Install necessary libraries. ...
  4. Step 4: Connect to the trading platform's API. ...
  5. Step 5: Fetch market data. ...
  6. Step 6: Implement your trading strategy.
Jun 11, 2023

Is Python good for trading bot? ›

Python, on the other hand, is popular for building trading bots that require data analysis and visualization capabilities. Python's libraries such as NumPy, Pandas, and Matplotlib are particularly useful for analyzing and visualizing financial data.

Can I make my own trading bot? ›

Trading bot development requires a combination of technical expertise and financial market apprehension. The best way to tackle this challenge is to partner with an experienced technology team possessing the expertise you need. That is what the process looks like step-by-step.

Is making a trading bot hard? ›

Trading bots offer many advantages, including speed, accuracy, and the ability to operate around the clock. However, building one can be a complex process, requiring knowledge of programming, data analysis, and market analysis.

Is it legal to make a trading bot? ›

Using a trading bot is perfectly legal. At this time, there are no rules or regulations that prohibit retail traders from using trading bots, even though there are some concerns about the effects of automated trading on the markets.

Can trading bots make money? ›

Conclusion. Trading bots have the potential to generate profits for traders by automating the trading process and capitalizing on market opportunities. However, their effectiveness depends on various factors, including market conditions, strategy effectiveness, risk management, and technology infrastructure.

Is Python too slow for trading? ›

Disadvantages of Python for Trading

Being a high-level programming language, Python is too slow for high-frequency trading applications. Current HFT implementations achieve latencies of only 40 microseconds or 0.04 milliseconds (the blink of an eye takes between 100 to 400 milliseconds).

How successful is a trading bot? ›

It depends on the bot! Some lower-risk crypto trading bots boast a 99% success rate, while others execute higher-risk strategies and have a lower success rate. The main thing most investors need to consider is whether the bot they're looking at can execute their specific investment strategy successfully.

What is the best bot for trading? ›

Breaking Down The Best Crypto Trading Bots:
  • Phemex. ...
  • Shrimpy. ...
  • Coinigy. ...
  • 3Commas. ...
  • CryptoHopper. ...
  • TradeSanta. ...
  • Kryll‍ When it comes to building your own trading strategies, Kryll.io leads the market with their outstanding UI. ...
  • Gunbot. Gunbot is a highly customizable trading bot for advanced traders.

Can you live off trading bots? ›

Making a living only through trading bots is obviously not easy, but it's not impossible either. While automated trading systems have helped some investors and traders earn money, it's far from easy to profit in the stock market due to the volatility of prices and market emotion.

How to create AI for trading? ›

How to Build a Trading Bot?
  1. 1 Selecting a programming language. ...
  2. 2 Choose your trading platform and the asset you want to trade. ...
  3. 3 Selecting the server to build your trading bot. ...
  4. 4 Define your strategy. ...
  5. 5 Integrate with the exchange API. ...
  6. 6 Backtesting your trading bot. ...
  7. 7 Optimizing your trading bot. ...
  8. 8 Forward testing.
Sep 22, 2023

Why do trading bots fail? ›

A smart auto trade bot can also fail because of technical glitches. Unstable connectivity, hardware crashes, and exchange outages can all disrupt a bot's trading. Even a brief disruption could allow major market movements during downtime. And complex bot strategies often rely on real-time market data.

Can you lose on a trading bot? ›

Traders can lose money in bot trading due to technical failures, market risks, programming errors, over-optimization, lack of adaptability, and human oversight. However, with proper risk management, oversight, and testing, traders can mitigate these risks and improve their chances of success in automated trading.

What is the best language for trading bots? ›

The choice of programming language for your trading bot largely depends on your specific requirements, trading strategy, and personal preferences. Python is an excellent choice for beginners and those focusing on data analysis. On the other hand, Java and C++ excel in high-frequency trading environments.

Can you build a bot using Python? ›

When you understand the basics of the ChatterBot library, you can build and train a self-learning chatbot with just a few lines of Python code. You'll get the basic chatbot up and running right away in step one, but the most interesting part is the learning phase, when you get to train your chatbot.

How to automate trading with Python? ›

Useful Packages/ Libraries in Python for Automated Trading

We'll have to import financial information, do a numerical analysis, create trading strategies, draw graphs, and backtest data. The following libraries are required: NumPy (short for NumericalPy) is a Python package for numerical data processing.

How to create an AI trading bot? ›

Here are the critical steps you must follow to create a bot that can autonomously execute trades based on predefined criteria:
  1. Choosing a Programming Language. ...
  2. Setting Up an API Connection. ...
  3. Designing Your Trading Strategy. ...
  4. Coding the Bot. ...
  5. Testing and Backtesting. ...
  6. Deploying on Cloud Infrastructure. ...
  7. Optimization and Monitoring.
Mar 27, 2024

How do I start coding a trading bot? ›

  1. Step 1: Programming Language. To write your bot, it is a good idea to choose a language that you are comfortable with. ...
  2. Step 2: Integrate Crypto Exchanges. ...
  3. Step 3: Create Accounts on these Exchanges. ...
  4. Step 4: Choose Type of Bot. ...
  5. Step 5: Confirm the Algorithm. ...
  6. Step 6: Encoding. ...
  7. Step 7: Product Testing. ...
  8. Step 8: Live Deployment.

Top Articles
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5770

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.