首页 > 解决方案 > 有人可以帮我将以下策略转换为学习吗

问题描述

我基本上是在寻找为 Long Trail Stop 和 Short Trail Stop 设置警报。以下策略在第 3 版中。因此需要将其转换为第 4 版中的研究。另外,如果有人能分享关于追踪止损(工具价格的百分比)的类似研究,我将不胜感激。非常感谢您的帮助!

//@version=3
strategy(title="Trailing stop loss (% of instrument price)",
     overlay=true, pyramiding=3)

// STEP 1:
// 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:
// 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 for 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)

标签: pine-script

解决方案


我不是一个很好的编码器(还)来帮助你编写代码本身,但我找到了一个基于 % 的追踪止损的教程:

https://www.youtube.com/watch?v=azms_Kqq3EI

希望它至少有所帮助:)

编辑:哦,看看这个,它可能有用。

https://www.tradingview.com/support/solutions/43000530720-how-can-i-convert-v3-scripts-to-v4/


推荐阅读