Skip to main content

Sample TradingView Bot (Indicator Script)

A guide on using the free sample TradingView Bot based on the Indicator script.

Written by Ben Ross

This sample code is provided to help you test the automation of your first trading bot on TradingView.

The TradingView Bot Indicator includes both Long and Short positions.

This strategy enters a Long position if the price of the asset increased during the previous candle (i.e., the close price was higher than the open price), and exits the Long position if the price of the asset decreased during the previous candle (i.e., the close price was lower than the open price). The opposite is true for the Short position—when the Long exit signal is executed, it will automatically trigger the entry into a Short position.

This code can be applied to any timeframe and any asset.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © WunderTrading

//@version=5
indicator("Signal Tester", overlay=true)

///////////////// LONG //////////////////
isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)

isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)

entry_long = not isEntry_Long and close>open
exit_long = not isExit_Long and open>close

if (entry_long)
isEntry_Long := true
isExit_Long := false

if (exit_long)
isEntry_Long := false
isExit_Long := true

///////////// SHORT //////////////

isEntry_Short = false
isEntry_Short := nz(isEntry_Short[1], false)

isExit_Short = false
isExit_Short := nz(isExit_Short[1], false)


entry_short=not isEntry_Short and open>close
exit_short= not isExit_Short and close>open

if (entry_short)
isEntry_Short := true
isExit_Short := false

if (exit_short)
isEntry_Short := false
isExit_Short := true

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)
Did this answer your question?