【发布时间】:2021-08-06 14:01:46
【问题描述】:
我在一个脚本中合并止盈和止损时遇到了严重问题。
我正在将此脚本用于 TP https://kodify.net/tradingview/orders/percentage-profit/
还有这个用于 SL https://kodify.net/tradingview/orders/percentage-stop/
我想出了以下不适合 SL 的脚本。因此,订单将保持打开状态,直到达到 TP %。我需要你的帮助来修复它并像 TP 一样激活 SL。
//@version=3
strategy(title="Take profit (% of instrument price)",
overlay=true, pyramiding=1)
// 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
longLossPerc = input(title="Long Stop Loss (%)",
type=float, minval=0.0, step=0.1, defval=1) * 0.01
// Calculate moving averages
fastSMA = sma(close, 20)
slowSMA = sma(close, 60)
// Calculate trading conditions
enterLong = crossover(fastSMA, slowSMA)
// Plot moving averages
plot(series=fastSMA, color=green)
plot(series=slowSMA, color=red)
// STEP 2:
// Figure out take profit price
longExitPrice = strategy.position_avg_price * (1 + longProfitPerc)
longStopPrice = strategy.position_avg_price * (1 - longLossPerc)
// 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) ? longStopPrice : na,
color=red, style=cross,
linewidth=2, title="Long Stop Loss")
// Submit entry orders
if (enterLong)
strategy.entry(id="EL", long=true)
// STEP 3:
// Submit exit orders based on take profit price
if (strategy.position_size > 0)
strategy.exit(id="TP", limit=longExitPrice)
if (strategy.position_size > 0)
strategy.exit(id="SL", stop=longStopPrice)
【问题讨论】:
标签: pine-script