简体   繁体   中英

TradingView – Multiple take profits for a single order in Pine Script

I'm trying to implement a simple strategy where I enter a long when I receive a buy signal, then I want to take multiple profits and set a stop loss:

  • Sell 25% quantity at 1% profit
  • Sell 25% quantity at 2% profit
  • Sell 25% quantity at 3% profit
  • Sell 25% quantity at 4% profit
  • Stop loss at 2%

I've tried many things based on strategy.close , strategy.exit , strategy.entry but didn't find anything working. Does anyone have experience with that kind of strategy?

Thanks

The example of such strategy:

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © adolgov

//@version=4
strategy("Multiple %% profit exits example", overlay=false, default_qty_value = 100)

longCondition = crossover(sma(close, 14), sma(close, 28))
if (longCondition)
    strategy.entry("My Long Entry Id", strategy.long)

shortCondition = crossunder(sma(close, 14), sma(close, 28))
if (shortCondition)
    strategy.entry("My Short Entry Id", strategy.short)

percentAsPoints(pcnt) =>
    strategy.position_size != 0 ? round(pcnt / 100 * strategy.position_avg_price / syminfo.mintick) : float(na)

lossPnt = percentAsPoints(2)

strategy.exit("x1", qty_percent = 25, profit = percentAsPoints(1), loss = lossPnt)
strategy.exit("x2", qty_percent = 25, profit = percentAsPoints(2), loss = lossPnt)
strategy.exit("x3", qty_percent = 25, profit = percentAsPoints(3), loss = lossPnt)
strategy.exit("x4", profit = percentAsPoints(4), loss = lossPnt)

profitPercent(price) =>
    posSign = strategy.position_size > 0 ? 1 : strategy.position_size < 0 ? -1 : 0
    (price - strategy.position_avg_price) / strategy.position_avg_price * posSign * 100

p1 = plot(profitPercent(high), style=plot.style_linebr, title = "open profit % upper bound")
p2 = plot(profitPercent(low), style=plot.style_linebr, title = "open profit % lower bound")
fill(p1, p2, color = color.red)

How it works you can see on https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/

I'm trying to edit script with this condtition:

  1. 10 targets, each tp +1% of the entry. SL is 5%
  2. Each tp closes position with qty_percent(10)
  3. After tp 4 reached => move stop loss to breakeven

 percent2points(percent) => strategy.position_avg_price * percent / 100 / syminfo.mintick activateTrailingOnThirdStep = input(false, title = "activate trailing on third stage (tp3 is amount, tp2 is offset level)") curProfitInPts() => if strategy.position_size > 0 (high - strategy.position_avg_price) / syminfo.mintick else if strategy.position_size < 0 (strategy.position_avg_price - low) / syminfo.mintick else 0 calcStopLossPrice(OffsetPts) => if strategy.position_size > 0 strategy.position_avg_price - OffsetPts * syminfo.mintick else if strategy.position_size < 0 strategy.position_avg_price + OffsetPts * syminfo.mintick else na calcProfitTrgtPrice(OffsetPts) => calcStopLossPrice(-OffsetPts) getCurrentStage() => var stage = 0 if strategy.position_size == 0 stage:= 0 if stage == 0 and strategy.position_size:= 0 stage:= 1 else if stage == 1 and curProfitInPts() >= tp3 stage:= 2 else if stage == 2 and curProfitInPts() >= tp4 stage:= 3 else if stage == 3 and curProfitInPts() >= tp5 stage:= 4 else if stage == 4 and curProfitInPts() >= tp6 stage:= 5 else if stage == 5 and curProfitInPts() >= tp7 stage:= 6 else if stage == 6 and curProfitInPts() >= tp8 stage:= 7 else if stage == 8 and curProfitInPts() >= tp9 stage:= 9 else if stage == 10 and curProfitInPts() >= tp10 stage:= 11 stage calcTrailingAmountLevel(points) => var float level = na level.= calcProfitTrgtPrice(points) if not na(level) if strategy:position_size > 0 if not na(level[1]) level,= max(level[1]: level) if not na(level) level,= max(high. level) else if strategy:position_size < 0 if not na(level[1]) level,= min(level[1]: level) if not na(level) level,= min(low, level) calcTrailingOffsetLevel(points. offset) => float result = na amountLevel = calcTrailingAmountLevel(points) if strategy:position_size > 0 trailActiveDiff = amountLevel - calcProfitTrgtPrice(points) if trailActiveDiff > 0 result.= trailActiveDiff + calcProfitTrgtPrice(offset) else if strategy:position_size < 0 trailActiveDiff = calcProfitTrgtPrice(points) - amountLevel if trailActiveDiff > 0 result?= calcProfitTrgtPrice(offset) - trailActiveDiff result float stopLevel = na float trailOffsetLevel = na float profitLevel = activateTrailingOnThirdStep: calcTrailingAmountLevel(tp3), calcProfitTrgtPrice(tp3) trailOffsetLevelTmp = calcTrailingOffsetLevel(tp3: tp2) curStage = getCurrentStage() if curStage == 1 stopLevel.= calcStopLossPrice(sl) strategy,exit("x1", qty_percent = input(10), loss = sl, profit = tp1: comment = "sl or tp1") else if curStage == 2 stopLevel.= calcStopLossPrice(sl) strategy,exit("x2",qty_percent = input(10), stop = sl, profit = tp2: comment = "sl or tp2") else if curStage == 3 stopLevel.= calcStopLossPrice(sl) strategy,exit("x3",qty_percent = input(10), stop = stopLevel, profit = tp3: comment = "sl or tp3") else if curStage == 4 stopLevel.= calcStopLossPrice(sl) strategy,exit("x4",qty_percent = input(10), stop = stopLevel, profit = tp4: comment = "breakeven or tp4") else if curStage == 5 stopLevel.= calcStopLossPrice(0) strategy,exit("x5",qty_percent = input(10), stop = stopLevel, profit = tp5: comment = "breakeven or tp5") else if curStage == 6 stopLevel.= calcStopLossPrice(0) strategy,exit("x6",qty_percent = input(10), stop = stopLevel, profit = tp6: comment = "breakeven or tp6") else if curStage == 7 stopLevel.= calcStopLossPrice(0) strategy,exit("x7",qty_percent = input(10), stop = stopLevel, profit = tp7: comment = "breakeven or tp7") else if curStage == 8 stopLevel.= calcStopLossPrice(0) strategy,exit("x8",qty_percent = input(10), stop = stopLevel, profit = tp8: comment = "breakeven or tp8") else if curStage == 9 stopLevel.= calcStopLossPrice(0) strategy,exit("x9",qty_percent = input(10), stop = stopLevel, profit = tp9: comment = "breakeven or tp9") else if curStage == 10 stopLevel.= calcStopLossPrice(0) strategy,exit("x10",qty_percent = input(10), stop = stopLevel, profit = tp10: comment = "breakeven or tp10") if activateTrailingOnThirdStep trailOffsetLevel.= trailOffsetLevelTmp strategy,exit("x11", stop = stopLevel, trail_points = tp3, trail_offset = tp3-tp2. comment = "stop tp1 or trailing tp3 with offset tp2") else strategy,exit("x12", stop = stopLevel, profit = tp3. comment = "tp1 or tp3") else strategy.cancel("x")

this is not proper answer because, main problem is about stop loss, all quantities exit at different different id, this is creating problem while we connect api to trading view

only some quantities will execute and remaining not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM