TradingView
adolgov
2020年4月29日午後12時29分

Multiple %% profit exits example 

Apple Inc.NASDAQ

詳細

This example show how to make multiple take profits in percent.
コメント
citizentraderx
Are you able to update this to have the StopLoss move to break even once the first take profit level is hit?
I can't find any example of this anywhere.

Note, I trade on the 1hr chart, so the StopLoss needs to work intra-candle, because a lot of price movement can happen in 1 hour.
Thanks.
cipher_phinx1988
@citizentraderx, have you figured how to move to breakeven if the tp1 has been hit?
citizentraderx
@cipher_phinx1988, I ended up having to work on a lower time frame to be able to capture TP1 level touches when back testing.
I was trying to avoid this because it severely limits back-testing data date range.
palotrading17
@citizentraderx, Hi, I combined this script and "Stepped trailing strategy example". For take profit I use strategy.exit() and for stoploss I use strategy.close(when=...) . The stop loss take place on candle close. But I didn't find any other solution. It looks like this ...

// random entry condition
if (crossover(sma(close, 14), sma(close, 28)))
strategy.entry("Long", strategy.long)

if (crossover(sma(close, 28), sma(close, 14)))
strategy.entry("Short", strategy.short)

...

strategy.exit("x1", qty_percent = 50, profit = tp1)
strategy.exit("x2", qty_percent = 25, profit = tp2)
strategy.exit("x3", profit = tp3)

// based on current stage set up exit
// note: we use same exit ids ("x") consciously, for MODIFY the exit's parameters
curStage = getCurrentStage()
if curStage == 1
stopLevel := calcStopLossPrice(sl)
strategy.close("Long", when=close < stopLevel)
strategy.close("Short", when=close > stopLevel)
else if curStage == 2
stopLevel := calcStopLossPrice(0)
strategy.close("Long", when=close < stopLevel)
strategy.close("Short", when=close > stopLevel)
else if curStage == 3
stopLevel := calcStopLossPrice(-tp1)
strategy.close("Long", when=close < stopLevel)
strategy.close("Short", when=close > stopLevel)
else
strategy.cancel("x")
PineCoders
This will be useful to many! Thx.
cipher_phinx1988
This is useful for newbie like me. It would be great if you put comments on each code so that it will understandable for beginners.
AlkiZaganiaris
Hi,

I noticed that for each exit statement, the qty_percentage applies to both the profit and loss (i.e "x1" profit would be 25% but also 25% if the stop loss was hit)

strategy.exit("x1", qty_percent = 25, profit = percentAsPoints(1), loss = lossPnt)

Is there a way to apply a different qty_percent for limit and stop. More specifically, can you always fix the stop to 100% of your position while changing the take profit percentage for different TP targets?
adolgov
@AlkiZaganiaris, yes this is possible. But we need "make" strategy.exit through `strategy.order` command.
myncrypto
@adolgov, Do you happen to have a fix or some example code that resolves the stop loss issue @AlkiZaganiaris is speaking of?
AlkiZaganiaris
I think I may have answered my own question. On your last exit you dont set qty_percent which would automatically close off any remaining position.

strategy.exit("x4", profit = percentAsPoints(4), loss = lossPnt)
詳細