Take Profit / Stop Loss (Strategy Script)
Take Profit (Strategy)
To create a take profit for your strategy on TradingView, you need to define an input value for your parameter in percentage terms. In the sample code, the take profit is at 10%. Such an approach allows you to easily fine-tune your strategy.
long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
Next, specify the entry price of your position in order to start tracking the price increases or decreases relative to the entry point. If you would like to plot the take profit level on the chart, 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)
To create a stop loss, follow a similar procedure with a few changes. First, adjust the sign in your formula (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)
Then, update your strategy.exit function.
strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level)
At this point, 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
Once you have tested your strategy with a strategy script, you will need to convert it into a study script to automate your trading on WunderTrading.
However, Pine Script has certain limitations: functions like strategy.entry, strategy.exit, etc., are not allowed in study scripts. Therefore, you will need to work around this.
First, replace how you store the entry price of your position. In the study script, use the 'valuewhen' function. This 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)
Next, create the variables to identify your Take Profit (TP_long) and Stop Loss (SL_long), and insert them into 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)
Now create alertconditions and plot them on the chart. The full example code is presented 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 on 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)
