簡體   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