Skip to main content
All CollectionsTrading BotsSignal Bot
TradingView script code – Take Profit and Stop Loss
TradingView script code – Take Profit and Stop Loss

TradingView sample code for setting up Take Profit and Stop Loss levels.

Anna Smith avatar
Written by Anna Smith
Updated over 4 months ago

Take Profit / Stop Loss (Strategy Script)

Take Profit (Strategy)

In order to create a take profit for your strategy on TradingView you will need to create the input value for your parameter in percentage. In the example code, the take profit is at 10%. Such an approach will help you to easily fine-tune your strategy parameters.

long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100

After that, you will have to state the entry price of your position in order to start tracking the increase or decrease in the price from the entry point. If you would like to plot the "take profit" on the graph, use the plot command.

long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
plot(long_take_level, color=color.green)

Insert this condition into your strategy.exit function.

strategy.exit("Take Profit","Long", limit=long_take_level)

Stop-Loss (Strategy)

In order to create a stop loss, you will have to do the same procedure as in the previous section with some small changes. First of all, you will have to change the sign (from + to -).

long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)
plot(long_stop_level, color=color.red)

Secondly, in the strategy.exit function has to be changed as well.

strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level)

Overall your strategy script with both take profit and stop loss will look like this:

/// Long Take Profit
long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
plot(long_take_level, color=color.green)

/// Long Stop Loss
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)
plot(long_stop_level, color=color.red)

/// Strategy Execution
entry_long = *** INSERT YOUR LONG CONDITIONS ***
exit_long = *** INSERT YOUR EXIT CONDITIONS ***

strategy.entry(id="Long", long=true, when=entry_long)
strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level, limit=long_take_level)
strategy.close(id="Long", when=exit_long, comment = "Exit")

Study Script and Alerts

As you have tested your strategy using the strategy script you would now have to convert it into the study script in order to automate your trading in Wunderbit Trading. The problem with the PineScript language is that in the study script some of the functions are not allowed (such as strategy.entry, strategy.exit etc) Therefore, you will have to work around it.

First of all, you need to replace the way how you store the entry price of your position. In the study script, you will have to use the 'valuewhen' function. It should be inserted after your entry condition for the position.

long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100

entry_long = *** INSERT YOUR ENTRY CONDITIONS ***
entry_price_long=valuewhen(entry_long,close,0)

After that, you need to create the variables that would identify your Take Profit (TP_long / SL_long) and insert them in your exit_long conditions.

TP_long = entry_price_long * (1 + long_tp_inp)
plot(TP_long, color=color.green)
SL_long = entry_price_long * (1 - long_sl_inp)
plot(SL_long, color=color.red)

exit_long = *** INSERT YOUR ENTRY CONDITIONS ***, (high > TP_long or low < SL_long)

After that, you can create the alertconditions and plot them on the graph. The full example code is represented below.

long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100

entry_long = *** INSERT YOUR ENTRY CONDITIONS ***
entry_price_long=valuewhen(entry_long,close,0)

TP_long = entry_price_long * (1 + long_tp_inp)
plot(TP_long, color=color.green)
SL_long = entry_price_long * (1 - long_sl_inp)
plot(SL_long, color=color.red)

exit_long = *** INSERT YOUR ENTRY CONDITIONS ***, (high > TP_long or low < SL_long)
/// Alert Conditions
alertcondition(entry_long, title="Enter Long")
alertcondition(exit_long, title="Exit Long")

// Plot Alerts onf the graph
plotshape(series=entry_long, text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=exit_long, text="EXIT BUY",style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)

Did this answer your question?