Crypto Trading Strategy Python

Crypto Trading Strategy Python

Crypto Trading Strategy Python – If you’ve never automated a trading strategy, you might think it’s an almost impossible endeavor that requires years of programming experience. In this tutorial, I will try to convey that it is not rocket science and I will show you step by step how to create a reliable trading bot.

In short, we will create a trading bot that trades multiple assets simultaneously based on 1 minute price bars. To be as broker-agnostic as possible, we’ll use Yahoo Finance as our data source and implement a tradelog that can be replaced with an interface to your preferred broker.

Crypto Trading Strategy Python

I recommend not just copying and pasting the code but also reading the description as it is equally important. Knowing the reasons behind some of the decisions I made is probably the most relevant paragraph in this article.

Backtesting Stock Trading Strategies In Python 🐍

Determining the logic behind the trading bot is one of the most critical aspects of implementing an algorithmic trading strategy. Also, you should extensively research and test any system you want to trade live in the market with.

As this tutorial deals with strategy implementation, we will assume that we have come up with a reasonable strategy that is likely to be profitable in the future. This is of course a big assumption and I am by no means trying to come up with a set of these trading rules.

We will create a long-term trading bot that buys a particular asset when the fast moving average crosses the slow moving average from below. Fast and slow,

We see the number of periods used by each indicator to calculate the average. A slower person will use more bars to average”

I Will Backtest Your Crypto Strategy In Python

You can see that this algorithm is not complicated at all. This is not a mistake as I want to keep things as straightforward as possible while simultaneously trying to convey as many good habits as possible.

Installed on your local computer. If not, you can go ahead and run the code in your browser using Google Colab. A has access to

The environment is beyond the scope of this article, but Google Colab requires no setup and is free for our purposes.

I want this tutorial to have as few dependencies as possible, but we still need to install a library or two, the first of which is yfinance (Yahoo Finance):

See also  Usb Gaming Headset With Microphone

How To Build A Simple Cryptocurrency Trading Bot In Python

YFinance is the most popular Python library for retrieving financial information. It not only provides access to historical price data, but also financial statement data and interesting datasets. If you want to learn more about this library, check out this tutorial I wrote.

Let’s create a simple snippet example to make sure the library is installed correctly and to demonstrate the basics:

(Open, High, Low, Close) data also has some extra columns that can be omitted, which is what we did in rows [5]-[7]. Finally, we print the last 10 bars to make sure everything is as expected:

The next step involves calculating technical indicators, which in this tutorial will be two simple moving averages. We can compute them using direct dataframe manipulation, instead I used the pandas_ta library. This library will allow you to take advantage of its functionality and quickly apply dozens of technical indicators without coding yourself. You can read more about the library in its GitHub repo.

Grid Trading · Github Topics · Github

Now that we know how to do all the necessary basic manipulations, we can start executing the strategy.

This section will establish the basic version of our trading bot, which we will continuously improve and refine in the next section. I highly recommend not skipping this part to better understand what is going on.

Remember: this strategy is not valid, but code. In other words, the key is to understand the code so that you can modify and implement your ideas.

Following the logic described in the previous section, we want to do something like this:

Build A Multi Asset Momentum Cryptocurrency Bot With Python And Binance

Although we are calculating a series of rolling moving averages, we need the most recent value of each one, which we use for that

The subscript indicates that we want to start from the bottom of the dataframe (the newest value).

We hope this simple if-else statement, in short, achieves our strategy. Of course, our bot has other essential features, such as:

We begin by declaring some variables that we will use throughout the strategy. ‘while true’ is nothing more than an infinite loop and the analog tells the code in the loop to repeat as long as one equals one. According to world-renowned mathematicians, this is true!

See also  Lg Flip Phone With Rotating Camera

Evaluate Real Trading Strategy With Python And Pandas

And do all the data manipulations we described earlier. The column deletion step is not required and can be deleted as well. You can go ahead if you want.

In addition to checking whether the moving average triggers a buy or sell signal, we also check whether we already own the asset or not. Also, whenever we buy or sell an asset, we update the ‘currently_holding’ variable and add the trade to the tradelog.

Finally, before stopping each iteration and starting the next one, we instruct our strategy to wait 60 seconds before doing so. Waiting 60 seconds is an oversimplification and is by all means useful. If we start the script at 12H:00M:20S, it will run for 20 seconds after each new bar. Also, it will move slightly from every 60 seconds + the time it takes to do one iteration.

At the end of this article, I’ll leave a function that suggests a strategy to wait until the next minute bar instead of once every 60 seconds.

Python: How I Used Simple Technical Analysis To Improve Bitcoin Price Prediction

Now that we are implementing a strategy for a single asset, we can make some changes to allow trading multiple assets simultaneously.

We do this by creating a dictionary list. Each asset will have its own dictionary and will contain the variables we use in the case of a single asset: the ticker, the yfinance object, and why we hold it.

The code snippet below contains some randomly selected assets. Feel free to customize the asset list to suit your needs!

Finally, we need to loop through each asset in each iteration. The code below is correct and as you can see, it requires little or no modification if you have implemented a single asset version!

Backtesting · Pypi

If you leave the code for a few minutes, hopefully it will generate some trades that can be seen in the tradelog:

Before moving on to the next section, I want to briefly mention the strong (and unrealistic) assumptions I introduced in the code. When logging a new trade, we use the latest closing price as the price at which our order was filled. This assumption has two drawbacks:

As I said in the previous section, we want our trading bot to return at the beginning of each new 1 minute bar, not the same as once every 60 seconds. If you don’t remember this issue, here it is:

See also  Update Samsung A50 Tempered Glass Price Review

If we start the script at 12H:00M:20S, it will run for 20 seconds after each new bar. Also, it will move a few extra seconds on each iteration since it takes 60 seconds (our delay) + the time it takes to do one iteration.

A Guide To Optimizing Crypto Trading Strategies

The result is the number of seconds we need to wait, starting from the next DateTime. Also, we use

(ceil ng) Round up the number instead of the nearest whole number (this will cause a delay of up to MM: 59S).

To take advantage of this function, you need to replace the current pause with this line of code:

After modifying this code to accommodate your custom indicator-based strategy, you can execute it and use the signal to manually place orders. This is a good approach as early versions of trading bots often have bugs that can trigger unwanted orders. Therefore, it is better to create orders manually to ensure that the bot works as expected.

Algorithmic Trading: Mean Reversion Using Python

Although this code adds a hypothetical trade to the tradelog, the script is sufficient to make requests to your broker (Interactive Broker, Alpaca, Binance, Coinbase, etc.) without much refactoring.

If you want to go ahead and contact a stockbroker or crypto exchange, you can check out some tutorials I created:

As you can see, getting a trading bot up and running isn’t rocket science. Coming up with a good and reliable trading strategy can prove to be a more challenging endeavor. Automating rule-based trading strategies is, in most cases, an almost trivial task once you know how to do it, and in this article, we’ve laid out the basics to get you started on the right track. Backtesting means working with history. Data to determine the future profitability of your current cryptocurrency trading

Python trading strategy, grid trading strategy crypto, day trading strategy crypto, best day trading strategy crypto, algorithmic trading strategy using python, mean reversion trading strategy python, crypto trading algorithm python, python crypto trading bot, python trading strategy backtesting, crypto arbitrage trading strategy, crypto swing trading strategy, crypto algo trading python

Originally posted 2023-08-28 16:58:37.

Leave a Reply

Your email address will not be published. Required fields are marked *