Skip to main content
All CollectionsAutomated Trading Bots CenterCustom Strategies
How to run the TradingView bot using Strategies?
How to run the TradingView bot using Strategies?

Learn how to configure the TradingView bot using Pine Script and Strategies within Trading View.

Quad Team avatar
Written by Quad Team
Updated over a year ago

Latest Updates

  • You can now deposit QUAD, USDC, or USDT to run bots on external exchanges! For every trade filled by the bot, a small fee will be charged. For more information about the bot’s fees and how they are calculated, have a look at this article. - August 2022

  • If you are running the TradingView bot v2.0, make sure to add the "pair" argument in the alert message.


Overview

In this article, we will see two step-by-step guides to get you started on configuring the TradingView bot using Strategies and Pine Script, directly within TradingView.

Details

What is Pine Script?

Pine is a specialized language used to write scripts that can take two different forms: studies or strategies.

Strategies are used to run backtests. Meanwhile, studies, cannot be used to run backtests. Both strategies and studies can run in either overlay or pane mode, and plot information on your chart. Both can generate alert events. However, in order to generate alerts and messages sent to your bot, we will use strategies. More specifically we will use the strategy function to create dynamic alerts to send to your TradingView Bot.

Automate a strategy using Pine Script

We will guide you through a basic strategy based on the RSI indicator. It will buy when RSI crossover 30 and sell when RSI crossunder 60. We will use the BTC/USD pair on Binance.

1. Code the strategy

  • Select the right chart: BTCUSD on Binance - 1 hour TimeFrame

  • Open the Pine Editor and enter:

//@version=4 
strategy("Rsi cross")
r = rsi(close, 14)

Notes:

//@version=4 allows you to use the latest version of Pine

strategy("Rsi cross") creates a strategy and name it Rsi cross

r = rsi(close, 14) r is an identifier, a name used to define variables and functions, here r corresponds to the rsi values, close: we will track the value of RSI at each bar close. 14: is the lookback period

  • Code the triggers

The message below allows us to detect when the rsi crosses over 30 and crosses under (below 60):

xUp = crossover(r, 30)
xDn = crossunder(r, 60)

Notes:

xUp and xDn are the identifiers that we will reuse in the next step

crossover and crossunder are functions allowing us to detect when a cross occurs

(r, 30) r being the rsi - and 30 the value it has to cross over to trigger the alert

(r, 60) r being the rsi - and 60 the value it has to cross under to trigger the alert

Notes: you can find a full list of the built-in indicators in the "built-in function" section.

2. Placing orders

a. Enter a position

For this, you can use the strategy.entry command allowing us to enter a position whether it's long or short.

strategy.entry(id, long, qty, limit,)

id is a required parameter used as the order identifier

long is also a required parameter used to determine whether to buy or sell:

-- 'strategy.long' is for buy orders

-- 'strategy.short' is for sell orders.

qty is an optional parameter, it represents the "amount" to buy or sell. If you wish to use your entire starting capital, you do not need to use it.

limit is an optional parameter allowing to place limit orders, in quote currency

Buy market order - with entire capital

//@version=4
strategy("Rsi cross")
r = rsi(close, 14)

// Detect crosses.
xUp = crossover( r, 30)

// Place order on crosses using a custom alert message for each.
if xUp
strategy.entry("BUY", strategy.long)

Notes: here, when Rsi crosses over 30, the strategy will place a buy market order using the entire capital affected to the bot.

Sell Limit order - with entire capital

//@version=4
strategy("Rsi cross")
r = rsi(close, 14)

// Detect crosses.
xUp = crossbelow( r, 60)

// Place order on crosses using a custom alert message for each.
if xUp
strategy.entry("BUY", strategy.short, NaN, 39000)

Notes : using limit orders in combination with indicators might result in orders not being filled, and we would recommend using market orders.

Market order with a specified amount

//@version=4
strategy("Rsi cross")
r = rsi(close, 14)

// Detect crosses.
xUp = crossover( r, 30)

// Place order on crosses using a custom alert message for each.
if xUp
strategy.entry("SELL", strategy.short, 10)

You can find a full list of the supported parameters here.

b. Closing a position

Here you can use strategy.exit. It is a command to exit either a specific entry or a whole market position. It is very similar to strategy.entry. Except here we can make use of additional parameters including stop and loss

//@version=4
strategy("Rsi cross")
i = rsi(close, 14)

// Detect crosses.
xUp = crossover( i, 33)

// Place order on crosses using a custom alert message for each.
if xUp
strategy.entry("BUY", strategy.long, 10)
strategy.exit("close_position","BUY",profit = 3*close, loss = 9*close)

Notes: the strategy.exit will close the position "BUY" previously created with strategy.entry. Profit and loss are calculated using ticks close price of the market order's candle. In this case, the strategy is to take 3% profit and risk a 9% loss.

With strategy.exit you can use qty_percent in order to exit your position partially for a more extensive risk management.

There are more possibilities such as canceling order or combining multiple conditions to trigger a trade. You can find a full list of the supported parameters here.

3. Create the alert and configure the message

Once your strategy has been coded, you can create an alert and define the message sent to your TradingView Bot.

  1. Go to Strategy Tester and click 'Add Alert'

  2. In 'Condition', select the name of your Pine Script

  3. Select, open-ended

  4. Select an alert name

  5. Configure the message

Here, you can take advantage of the strategy configure in Pine and add dynamic fields according to your strategy.

  • Market orders - with the full starting capital

{
"action": "{{strategy.order.action}}",
"botId": "1234"
}
  • Limit orders - without a specified amount

{
"action": "{{strategy.order.action}}",
"price": {{strategy.order.price}},
"botId": "1234"
}
  • Orders with a specified amount

{
"action": "{{strategy.order.action}}",
"amount":{{strategy.order.contracts}},
"botId": "1234"
}

4. Run Backtests (Optional)

You can run backtest in order to see if your strategy is performing well or if it's not profitable. Before doing so, you will need to add some extra lines of code allowing you to run the backtest within specified timeframes.

Within your Pine Script Editor add these lines:

startDate = input(title="Start Date", type=input.integer,
defval=1, minval=1, maxval=31)
startMonth = input(title="Start Month", type=input.integer,
defval=1, minval=1, maxval=12)
startYear = input(title="Start Year", type=input.integer,
defval=2018, minval=1800, maxval=2100)

endDate = input(title="End Date", type=input.integer,
defval=1, minval=1, maxval=31)
endMonth = input(title="End Month", type=input.integer,
defval=7, minval=1, maxval=12)
endYear = input(title="End Year", type=input.integer,
defval=2019, minval=1800, maxval=2100)


Then add the script to your charts, and click on script Settings (on the chart). Once the pop-up appeared, click on Input and select the dates to run your backtest.


Important notes

  • Don't short and long using the same bot (have one strategy for long and one strategy for short )

  • If your strategy starts with a sell order you need to specify an amount

You might be interested in

Did this answer your question?