Crypto Trading Bot using TradingView — How to Guide

Excerpt. image url

First of all, let us define the main term of this article. What is a Trading Bot? Trading bot refers to using a computer program or system to trade on the market according to a predefined and specified set of rules. In other words, a trader builds a certain algorithm for his trading strategy, which would be automatically executed on the market when all the conditions are met. That is also the reason why we will also use the term algorithmic trading (a.k.a. Algo Trading). 

Algorithmic trading style makes a perfect combination with the cryptocurrency markets, which provide several advantages for algorithmic traders. First of all - volatility, cryptocurrency markets typically have much higher volatility than traditional markets, creating bigger swings in prices and opportunities for traders. Secondly, round the clock trading session as cryptocurrency markets are open for business 24/7. Thirdly, total market capitalization, which is smaller in relation to the traditional markets. Traditional markets are dominated by large multibillion-dollar funds while cryptocurrency markets are much younger, which means it’s relatively less saturated with massive funds and as a result, will not require large capital to start earning from cryptocurrency trading.

On the other hand, large volatility on the young market which never sleeps creates a perfect environment for speculations such as pump and dump strategies. And this should be definitely taken into the account if you decide to trade on the cryptocurrency markets.

A trading bot can trade according to technical indicators, momentum, and fundamentals. Or you can create a much more advanced trading algorithm which can help you to execute orders more efficiently, from routing orders across different exchanges (Arbitrage Trading) to slicing a large order into smaller pieces (automated Iceberg Orders). 

Most popular crypto trading strategies

The 3 most popular crypto trading strategies are Market Making, Arbitrage and Trend Following Strategies.

Market Making Strategy

This is the strategy in which continuous buying and selling occur to capture the spread between the buy and sell price. In order to do that, the trader will have to place limit orders on both sides of the order book to profit from the spread. However, this strategy will have its limitation in a low liquidity environment or during the previous extreme competition. 

Arbitrage Strategy

Arbitrage is the purchase and sale of an asset in order to profit from a difference in the asset's price between markets. Therefore this strategy takes advantage in the price difference of the same asset on different exchanges. The trader buys digital assets from one market and then sells them in another, earning a profit in the process. 

Trend Following Strategy

The aim of this strategy is to identify the trend of the asset and execute the deals based on the underlying trend.  The Trend Following strategy attempts to acquire gains through analyzing an asset’s momentum towards a given direction. Traders that execute this strategy will enter into a long position when a cryptocurrency trends upwards and sell the position when the trend is reversing.

Depending on the difficulty level of your trading algorithm you would need to use different tools. In its turn, the tools will depend a lot on the level of the technical skills that you currently possess. To keep it simple you can differentiate a bot building exercise in 3 main levels of difficulty: Beginner, Intermediary, Advance. In future reviews, we will go into details of each level and suggest the tools that can be used to create a trading algorithm and how to apply it to the market.

Example of a crypto trading bot using TradingView

For now, let us give an example of the crypto trading bot that could be built using TradingView (TV). First of all, it is important to construct the strategy script,  because the strategy script can be backtested and TradingView will provide the detailed results of your strategy.

The code of crypto trading bot below represents the simple EMA strategy in which the entry and exit occur at the crossing of the fast and slow Exponential Moving Average (EMA). This can be pasted into the Pine Editor of TradingView.

Example of the trading strategy:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Wunderbit Trading (WBT)

//@version=4
strategy("EMA cross", overlay=true)

//Inputs
fast_ema=ema(close, 9)
slow_ema=ema(close, 21)

plot(fast_ema, color=color.red)
plot(slow_ema, color=color.blue)

//Strategy Conditions 
entry_long =  crossover(fast_ema, slow_ema)
exit_long =  crossunder(fast_ema, slow_ema)

entry_short= crossover(slow_ema, fast_ema)
exit_short= crossunder(slow_ema, fast_ema)

//Strategy Execution
strategy.entry("Long", long=true, when=entry_long)
strategy.close("Long", when=exit_long)

strategy.entry("Short", long=false, when=entry_short)
strategy.close("Short", when=exit_short)

TradingView strategy result screen should look like this:

trading bot strategy in tradingview

This strategy can be modified by adding extra input variables such as volume or some stochastic indicators, or you could put a take profit and stop-loss targets. 

After the strategy was created and tested, you can create alerts in order not to miss your trading setups. However, in order to do, that you will have to change your script into a study script because it is not possible to apply the alert function to strategy.  

The example of the study script with the same strategy is represented below. 

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Wunderbit Trading (WBT)

//@version=4
study("EMA cross", overlay=true)

//Inputs
fast_ema=ema(close, 9)
slow_ema=ema(close, 21)

plot(fast_ema, color=color.red)
plot(slow_ema, color=color.blue)

//Strategy Conditions 
entry_long =  crossover(fast_ema, slow_ema)
exit_long =  crossunder(fast_ema, slow_ema)

entry_short= crossover(slow_ema, fast_ema)
exit_short= crossunder(slow_ema, fast_ema)

//Strategy Execution

alertcondition(entry_long, title="Enter Long")
alertcondition(exit_long, title="Exit Long")
alertcondition(entry_short, title="Enter Short")
alertcondition(exit_short, title="Exit Short")

plotshape(series=entry_long, text="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=exit_long, text="EXIT Long",style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)
plotshape(series=entry_short, text="Short", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plotshape(series=exit_short, text="EXIT Short",style=shape.triangleup, location=location.belowbar, color=color.purple, size=size.small)

TradingView study result screen should look like this:

crypto trading bot study script in tradingview

Now you can create alerts for your trading asset and select the alert conditions and actions. 

But what if the alert is triggered and you receive it while you are not next to the computer, and you do not have the opportunity to execute your strategy. There is a solution to automate your TradingView alerts through the third-party service provider such as Wunderbit Trading, which will make your bot automatically executed on the cryptocurrency exchange that you use. You can create a bot to enter and exit your strategy based on the TradingView alerts.