繁体   English   中英

TradingView – Pine Script 中单个订单的多个止盈

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

我正在尝试实施一个简单的策略,当我收到买入信号时我进入多头,然后我想获得多个利润并设置止损:

  • 以 1% 的利润出售 25% 的数量
  • 以 2% 的利润出售 25% 的数量
  • 以 3% 的利润出售 25% 的数量
  • 以 4% 的利润卖出 25% 的数量
  • 2% 止损

我已经尝试了很多基于strategy.closestrategy.exitstrategy.entry的事情,但没有发现任何工作。 有没有人有这种策略的经验?

谢谢

这种策略的例子:

// 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)

您可以在https://www.tradingview.com/script/kHhCik9f-Multiple-profit-exits-example/上看到它是如何工作的

我正在尝试使用此条件编辑脚本:

  1. 10 个目标,每个 tp + 1% 的条目。 止损为 5%
  2. 每个 tp 用 qty_percent(10) 关闭 position
  3. 达到 tp 4 后 => 将止损移至盈亏平衡

 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")

这不是正确的答案,因为主要问题是关于止损,所有数量都以不同的不同 id 退出,这在我们将 api 连接到交易视图时产生了问题

只有一些数量会执行,其余的不会

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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