TradingView
fpsd4ve
21 de Jul de 2022 16:59

2x take profit, move stop loss to entry 

BTCUSDT SPOTBybit

Descrição

Two take profit targets moving stop loss to entry after first take profit hit

This strategy shows a method to achieve the following trade management:

1) Open a position with two take profit targets
2) When first take profit is hit, move the stop loss to entry price
3) Position is closed when either second take profit is hit or position is stopped out at entry

Trading Conditions

The trading conditions used in this script are for illustrative purposes only and are not meant to be profitable.

Bollinger Bands
Used to show areas where price is moving outside of the normal range

Stochastic
Used to show overbought and oversold conditions.
Green dots are plotted when the K line crosses over its SMA. Red dots are plotted when the K line crosses under its SMA.

The option to use overbought/oversold thresholds is again illustrative.
Comentários
robotron2000
Thanks for the script.

If the TP1 is hit, then you call strategy.exit(TP1..) again on the next bar, why doesn't it re-enter?
Is the platform smart enough that it knows not to enter a duplicate (TP1, from_entry=Short) until that particular "Short" trade is done? I couldn't find this mentioned on the docs, just wanted to double check.
fpsd4ve
@robotron2000, are you asking about my code specificalyl or more generally?
robotron2000
@fpsd4ve, both
GoemonYae
Looks good! However, I don't see the `when` argument in the documentation of Pine Script v5. How come you have used it? This looks much cleaner than my solution (without `when`) where I have if-statement and cancel the order and create a new one to move the SL.
chrissteinert
Hi, thanks for the template. It was a "lifesaver" as a base for my code!
Axelito135790
Hi, the script was extremely usefull, but i found one problem with it while using it, if in the same candle that you open the trade, it touches the 1° Take Profit, the Stop Loss doesn't move to BE.
Did you encounter the same problem, if so, did you find any solutions?
fpsd4ve
@Axelito135790, Hi. I haven't encountered this issue but perhaps utilising the calconeverytick strategy parameter would help?
yuribor
Very helpful! Thank you so much!
CRabbit
Hi, thanks for your super script
I tired to use your strategy in a simple EMA50 crossover but seem to have an issue with the SL, any chance you could assist is pointing out where I'm going wrong

//@version=5

// Starting with $100 and using 10% of the account per trade
strategy("Take Profit", shorttitle="TP", overlay=true, initial_capital=100, default_qty_value=10, default_qty_type=strategy.percent_of_equity, max_bars_back=3000)

//Inputs
tp1 = input.float(1.0,"TP1-%",step=0.1, group="Strategy Settings", inline="1")
tp2 = input.float(2.0,"TP2-%",step=0.1, group="Strategy Settings", inline="1")
sl = input.float(1.0,"SL-%",step=0.1, group="Strategy Settings", inline="1")
tp1v = input.float(50,"TP1 Amount%",step=5, group="Strategy Settings")
tp2v = input.float(50,"TP2 Amount%",step=5, group="Strategy Settings", tooltip="% of Total not remaining amount") //% of total amount, not the remaining position

//EMA Indicator
ema50 = ta.ema(close, 50)

//open Long position if close is above EMA 50
if close > ema50
strategy.entry("Long Order", strategy.long, comment="ENTER-LONG")

//Stoploss and moving to Breakeven
var float longSL = 0.0

longSL := if strategy.position_size>0
(strategy.position_avg_price * (1 - (sl/100)))

else if strategy.position_size>0 and (open > strategy.position_avg_price * (1 + (tp1/100))) // Might need to latch this to keep this SL value when the price drops
strategy.position_avg_price

else
0

//Exit Order for Long
strategy.exit("L-TP1", from_entry="Long Order", qty_percent=tp1v, limit=(strategy.position_avg_price * (1 + (tp1/100))), stop=longSL)
strategy.exit("L-TP2", from_entry="Long Order", limit=(strategy.position_avg_price * (1 + (tp2/100))), stop=longSL)

// Plotting
plot(ema50, color=color.blue, linewidth=2)
fpsd4ve
@CRabbit, Hi. What is the SL issue you are having? that code doesn't appear to be based on my script so it will be difficult to assist without knowing what your specific issue is.
Mais