How to make a cryptocurrency trading bot using TradingView

No technical knowledge and coding experience — Cloud-based — Part 1

Strategst
Coinmonks
Published in
8 min readJul 22, 2020

--

First, some caveats:

  • Not trading advice.
  • Some others know botbuilding/Pine/etc. better than I.
  • I personally have no affiliation with TradingView, Alertatron, exchanges, or any mentioned services.
  • Most products I'm showing here I use for several years already.
  • You can help this project by registering with the referral link in the post.

Do not let the length of the post to scare you. Here you have lots of screenshots and descriptions, but really it was harder to write this tutorial than to do it.

Help and chat here: My Own Crypto Bots chat

Why I am doing this, part zero.

What we are doing

We are making cloud-based cryptocurrency trading bot based on open-source strategy, so we can see bots trading logic, past performance, tune parameters, and fully control its execution. This will be a very simple trend-following strategy, but a provenly profitable one.

Bot structure

Generally, every automatic trading system has three major parts:

1. Signal generator. This is about price tracking software, information processing in the defined way to generate signals, and a signal sender. There are more ways to do it: python scripts to pull the price from the exchanges through API, MQL 4/5 that runs on the local computer, thousands of them. Here we will put it simply and as automatic as possible, so we’ll choose Tradingview. This part is about producing signals with Tradingview.

2. Risk management. This is about to trade the position that will not bankrupt you and let you fight another day. We will use a very simple but rigid system, 3% per trade, 9% in all trades (though it is topic for discussion in later parts). Speaking about stop losses, we will code them in the strategy for backtesting, and I will show how to set up them automatically on exchanges in the following parts.

3. Execution. Execution is the second crucial part of your bot. The execution could make your strategy as much profitable as on backtests or leave your strategy in losses. This is about commissions, slippage, limit/market orders, and many other very small but hugely important things. This is the topic for the second tutorial.

Generating signals.

So, for signals generating we have to prepare two things.

First, for sending server-side alerts to exchanges, we need at least a PRO subscription (this is my reflink) from TradingView. It gives us an ability to make up to 10 alerts, so we can monitor up to 5 coins with our strategies.

TradingView plans

The second and most important thing is that we have to find a profitable strategy. As we know most of the profitable strategies keep in secret. Thankfully, on TradingView we have a huge open source community and generous programmers that publish their strategies free, so we can study them, change some parts and also publish them openly. So, you can go here and find some.

Choose “Strategies only“

Here I want to draw your attention to one thing. On TradingView there are two types of scripts: “study” and “strategy” scripts. Shortly, “strategies” give us an ability to backtest results, and with “studies” we can create alerts that will be sent to the execution service. Docs. We can convert strategies to studies, as we can convert studies to strategies, but there are details.

So, for now, I recommend choosing strategies and making research among them.

For this tutorial, I’ve found a backtests-profitable strategy and study for you from the trader Dreadblitz.

“Follow Line Indicator” is a very simple and very effective trend-following strategy. Best for trending and volatile assets like Bitcoin. So, let's see strategy real performance.

Adding the strategy to the chart

First, open the chart window, go to TradingView and choose “Chart”

We are here. You can see price movements, timeframe, candles, settings. All in one place.

Now, let’s choose Bitcoin on Bitmex, just write XBTUSD and choose Bitmex Perpetual futures in the window.

Then choose 1H timeframe (for now) and click on “Pine Editor” tab.

Ok, we are here in Pine script coding language editor and free to write any strategy we want to add to the chart.

In TradingView we have two ways to add the script to the chart.

First is pasting the code and second is choosing the script from the public library.

a. First, let’s see what will the public library show us.

Choose “Indicator & Strategies” at the top of the window.

Type “Follow Line Indicator” in the search bar. Find “Strategy” version and click on it.

Okay, now we have a strategy applied to our chart. Let's look at its parameters. Click on the “gear” button.

Parameters have a huge impact on strategy profitability. You can adjust it as you wish, but it is important to keep in mind that as a result of parameters tuning you have the backtests, and they are the result of past trades. The relationship of past performance though somehow correlated, but highly uncertain.

By tuning parameters, you can adjust profit curve smoothing, profit/drawdown ratio, total number of closed trades, etc.

Let’s change “BB Period” from 21 to 12 and ATR Period form 5 to 3. It will give us a more responsive strategy for the latest Bitcoin volatile market.

What do we have now? A simple profitable strategy that makes 215% profit in two years, 409 closed trades, and almost 12.9% maximum drawdown. As for comparison, we have a thin gray line down there. This line shows us our profits if we just hold Bitcoin for these two years. In precise numbers, this backtest shows us two ways for capital allocation: using this strategy, with a start capital of 1BTC/$14500 on 2018/01/01 making $31277.50 on 2020/07/22 using this strategy, or staying with $9300 by just holding $BTC.

Caution: backtesting results are best-possible-results, there are lots of ways for losing money if something goes wrong or something was not calculated properly, but generally the difference between trading with profitable strategy or simply hodling is here. Also, adjust the strategy so it can be profitable on as many assets and timeframes as possible, avoid over-fitting.

b. Or we can add our own code

Here we have our first detail that is not so obvious. The coder of this strategy left the strategy without stop losses. So in the code, we see only buy and sell functions. But obviously, our risk-management system wouldn’t allow us to trade without stop losses. Don’t worry, I’ve already added this for you in the code. Go to pastebin and copy the code there. (My Own Crypto Bots chat for help)

Open Pine Editor

Delete everything first, paste the code and add the script to the chart.

Ok, now we have more realistic backtests with stop loss of 3%. This gives us less net profits and less drawdown, but generally, this didn’t change our profit curve.

Setting up the alerts with TradingView for automatic trading

Let's suppose that we have reliable backtests now, checked everything, and satisfied with our strategy. Now we need a “study” version of this script for alerts.

  • Go to “Indicators & Strategies” and search for “Follow Line Indicator” one more time.
  • As before, we see two versions of “Follow Line Indicator”, now we choose “study” version (without arrows), study version has about 1500 likes.
  • Add this study on the chart, click to gear button, and input the same parameters as you had in the strategy. To get the same results in the backtests and real trading, every parameter must be identical.

Now let’s set alerts for trading. We have to set separate alerts for buying and selling for that.

First, go to “alarm” button.

Then click on “New alert” button

Here in “Condition” choose our study “FLI 12, 1, 3”

For setting “sell” alert, choose “Sell” and Once Per Bar Close option. Nothing else for now, just click “Close”.

For “buy” alerts choose the study “FLI 12, 1, 3”, then “Buy” and Once Per Bar Close option.

You will see the list of alerts you’ve settled. Hover on one to pause, open settings, or delete.

So, we’ve finished our first part of bot building, so we can auto-generate signals, see backtests and adjust it by our profit/risk appetite. These signals we can send to our executing software to trade automatically on crypto exchanges.

See you on My Own Crypto Bot discussions

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--