Skip to main content

Sample TradingView Bot (Strategy Script)

A guide on setting up a sample TradingView bot using a simple strategy script.

Written by Ben Ross

This sample code is provided to help you test a strategy script for your first trading bot on TradingView.

​The TradingView Bot Study 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 an 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=4
strategy("Signal Tester", overlay=true)

///////////////// LONG //////////////////

entry_long = close>open
exit_long = open>close

///////////// SHORT //////////////
entry_short=open>close
exit_short=close>open

strategy.entry("long", strategy.long, when=entry_long, comment="Insert Enter Long Comment Here")
strategy.close("long", when=exit_long, comment="Insert Exit Long Comment Here")

strategy.entry("short", strategy.short, when=entry_short, comment="Insert Enter Short Comment Here")
strategy.close("short", when=exit_short, comment="Insert Exit Short Comment Here")
Did this answer your question?