【问题标题】:How to modify the code in TradingView Pine Realize trailing stop loss and profit?如何修改TradingView Pine中的代码实现追踪止损和止盈?
【发布时间】:2021-11-26 22:46:57
【问题描述】:

我在合并代码中遇到问题。我想同时实现追踪止损和获利。 Tradingview现在可以显示ES→EL、EL→XL TRL STP、ES→XS TP,但是我合并if语言后,还是不能显示EL→XS TP和ES→XS TRL STP,如果更改代码顺序,只能显示2种进入和退出方式。应该怎么修改?

[修改tradingview的详细视图] https://i.stack.imgur.com/IDiqO.jpg

整个松码:

//@version=3
strategy(title="Take profit (% of instrument price)",
     overlay=true, pyramiding=3)

// STEP 1:
// Make inputs that set the take profit % (optional)
longProfitPerc = input(title="Long Take Profit (%)",
     type=float, minval=0.0, step=0.1, defval=3) * 0.01

shortProfitPerc = input(title="Short Take Profit (%)",
     type=float, minval=0.0, step=0.1, defval=3) * 0.01

// Configure trail stop level with input options (optional)
longTrailPerc = input(title="Trail Long Loss (%)",
     type=float, minval=0.0, step=0.1, defval=3) * 0.01

shortTrailPerc = input(title="Trail Short Loss (%)",
     type=float, minval=0.0, step=0.1, defval=3) * 0.01

// Calculate moving averages
fastSMA = sma(close, 20)
slowSMA = sma(close, 60)

// Calculate trading conditions
enterLong  = crossover(fastSMA, slowSMA)
enterShort = crossunder(fastSMA, slowSMA)

// Plot moving averages
plot(series=fastSMA, color=teal)
plot(series=slowSMA, color=orange)

// STEP 2:
// Figure out take profit price
longExitPrice  = 

strategy.position_avg_price * (1 + longProfitPerc)
shortExitPrice = strategy.position_avg_price * (1 - shortProfitPerc)

// Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? longExitPrice : na,
     color=green, style=circles,
     linewidth=3, title="Long Take Profit")

plot(series=(strategy.position_size < 0) ? shortExitPrice : na,
     color=red, style=circles,
     linewidth=3, title="Short Take Profit")

// Determine trail stop loss prices
longStopPrice = 0.0, shortStopPrice = 0.0

longStopPrice := if (strategy.position_size > 0)
    stopValue = close * (1 - longTrailPerc)
    max(stopValue, longStopPrice[1])
else
    0

shortStopPrice := if (strategy.position_size < 0)
    stopValue = close * (1 + shortTrailPerc)
    min(stopValue, shortStopPrice[1])
else
    999999

// Plot stop loss values for confirmation
plot(series=(strategy.position_size > 0) ? longStopPrice : na,
     color=fuchsia, style=cross,
     linewidth=2, title="Long Trail Stop")
plot(series=(strategy.position_size < 0) ? shortStopPrice : na,
     color=fuchsia, style=cross,
     linewidth=2, title="Short Trail Stop")

// Submit entry orders
if (enterLong)
    strategy.entry(id="EL", long=true)

if (enterShort)
    strategy.entry(id="ES", long=false)

// STEP 3:
// Submit exit orders based on take profit price and  trail stop loss price

if (strategy.position_size > 0)
    strategy.exit(id="XL TRL STP", stop=longStopPrice)
    strategy.exit(id="XL TP", limit=longExitPrice)
    
if (strategy.position_size < 0)
    strategy.exit(id="XS TP", limit=shortExitPrice)
    strategy.exit(id="XS TRL STP", stop=shortStopPrice)

这是我要修改和合并的代码:

// STEP 3:
// Submit exit orders based on take profit price and  trail stop loss price

if (strategy.position_size > 0)
    strategy.exit(id="XL TRL STP", stop=longStopPrice)
if (strategy.position_size < 0)
    strategy.exit(id="XS TRL STP", stop=shortStopPrice)

if (strategy.position_size > 0)
    strategy.exit(id="XL TP", limit=longExitPrice)
if (strategy.position_size < 0)
    strategy.exit(id="XS TP", limit=shortExitPrice)

请帮我解决这个问题,我将不胜感激!

【问题讨论】:

    标签: pine-script


    【解决方案1】:
    // STEP 3:
    // Submit exit orders based on take profit price and  trail stop loss price
    
    if (strategy.position_size > 0)
        strategy.exit(id="XL", stop=longStopPrice, limit=longExitPrice)
        
    if (strategy.position_size < 0)
        strategy.exit(id="XS", limit=shortExitPrice, stop=shortStopPrice)
    

    【讨论】:

      猜你喜欢
      • 2022-12-09
      • 2022-09-23
      • 2020-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多