我在将止盈和止损合并到一个脚本中时遇到严重问题。
我正在使用此脚本进行 TP https://kodify.net/tradingview/orders/percentage-profit/
这是 SL 的 https://kodify.net/tradingview/orders/percentage-stop/
我想出了下面的脚本,它不适合 SL。因此,订单将保持开放状态,直到达到目标价%。我需要你的帮助来修复它并像 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)
Run Code Online (Sandbox Code Playgroud)
小智 14
到这里,还有一些补充
\n// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/\n// \xc2\xa9 SafetyHammer\n\n//@version=4\nstrategy(title="Take profit (% of instrument price)", overlay=true, pyramiding=1)\n\n// STEP 1:\n// Make inputs that set the take profit % (optional)\n\n\n\nFastPeriod = input(title="Fast MA Period", type=input.integer, defval=20, minval=1, group="Moving Average")\nSlowPeriod = input(title="Slow MA Period", type=input.integer, defval=60, minval=1, group="Moving Average")\n\n\n\nTP1Perc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=2, group="TP & SL") \nTP2Perc = input(title="Long Take Profit (%)", type=input.float, minval=0.0, step=0.1, defval=4, group="TP & SL") \nSLPerc = input(title="Long Stop Loss (%)", type=input.float, minval=0.0, step=0.1, defval=2, group="TP & SL")\n\nTP1_Ratio = input(title="Sell Postion Size % @ TP1", type=input.float, defval=50, step=1, group="TP & SL", tooltip="Example: 50 closing 50% of the position once TP1 is reached")/100\n\n\n// Calculate moving averages\nfastSMA = sma(close, FastPeriod)\nslowSMA = sma(close, SlowPeriod)\n\n// Calculate trading conditions\nenterLong = crossover(fastSMA, slowSMA)\n\n\n// Plot moving averages\nplot(series=fastSMA, color=color.green, title="Fase MA")\nplot(series=slowSMA, color=color.red, title="Slow MA")\n\n// STEP 2:\n// Figure out take profit price\npercentAsPoints(pcnt) =>\n strategy.position_size != 0 ? round(pcnt / 100.0 * strategy.position_avg_price / syminfo.mintick) : float(na)\n\npercentAsPrice(pcnt) =>\n strategy.position_size != 0 ? ((pcnt / 100.0) + 1.0) * strategy.position_avg_price : float(na)\n \ncurrent_position_size = abs(strategy.position_size)\ninitial_position_size = abs(valuewhen(strategy.position_size[1] == 0.0, strategy.position_size, 0)) \n \nTP1 = strategy.position_avg_price + percentAsPoints(TP1Perc) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)\nTP2 = strategy.position_avg_price + percentAsPoints(TP2Perc) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)\nSL = strategy.position_avg_price - percentAsPoints(SLPerc) * syminfo.mintick * strategy.position_size / abs(strategy.position_size)\n\n\n\n// Submit entry orders\nif (enterLong) \n strategy.entry(id="Long", long=true) \n\n\n\n// STEP 3:\n// Submit exit orders based on take profit price\n\nif strategy.position_size > 0 \n strategy.exit("TP1", from_entry="Long", qty = initial_position_size * TP1_Ratio, limit = TP1, stop = SL)\n strategy.exit("TP2", from_entry="Long", limit = TP2, stop = SL) \n \n \n// Plot take profit values for confirmation\nplot(series=(strategy.position_size > 0) ? TP1 : na, color=color.green, style=plot.style_circles, linewidth=1, title="Take Profit 1")\nplot(series=(strategy.position_size > 0) ? TP2 : na, color=color.green, style=plot.style_circles, linewidth=1, title=" Take Profit 2")\nplot(series=(strategy.position_size > 0) ? SL : na, color=color.red, style=plot.style_circles, linewidth=1, title="Stop Loss")\n\n\nRun Code Online (Sandbox Code Playgroud)\n