止盈 / 止损(策略脚本)
止盈(策略)
要在 TradingView 策略中创建止盈,您需要以百分比形式为该参数定义一个输入值。在示例代码中,止盈设置为 10%。这种方式可以让您轻松微调策略。
long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
接下来,指定仓位的开仓价格,以便开始跟踪价格相对于开仓点的上涨或下跌。如果您想在图表上绘制止盈水平,请使用 plot 命令。
long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
plot(long_take_level, color=color.green)
将此条件插入到您的 strategy.exit 函数中。
strategy.exit("Take Profit","Long", limit=long_take_level)
止损(策略)
创建止损的流程类似,但需要做一些调整。首先,在公式中将符号从 + 改为 -。
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)
plot(long_stop_level, color=color.red)
然后,更新您的 strategy.exit 函数。
strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level)此时,包含止盈和止损的策略脚本如下所示:
/// Long Take Profit
long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
long_take_level = strategy.position_avg_price * (1 + long_tp_inp)
plot(long_take_level, color=color.green)
/// Long Stop Loss
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
long_stop_level = strategy.position_avg_price * (1 - long_sl_inp)
plot(long_stop_level, color=color.red)
/// Strategy Execution
entry_long = *** INSERT YOUR LONG CONDITIONS ***
exit_long = *** INSERT YOUR EXIT CONDITIONS ***
strategy.entry(id="Long", long=true, when=entry_long)
strategy.exit("Take Profit/ Stop Loss","Long", stop=long_stop_level, limit=long_take_level)
strategy.close(id="Long", when=exit_long, comment = "Exit")
Study 脚本和警报
在使用策略脚本测试完成后,您需要将其转换为 Study 脚本,以便在 WunderTrading 上实现自动化交易。
不过,Pine Script 存在一些限制:例如 strategy.entry、strategy.exit 等函数不能在 Study 脚本中使用。因此,您需要采用替代方式来实现相同逻辑。
首先,替换仓位开仓价格的保存方式。在 Study 脚本中,请使用 valuewhen 函数。该函数应放在仓位入场条件之后。
long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
entry_long = *** INSERT YOUR ENTRY CONDITIONS ***
entry_price_long=valuewhen(entry_long,close,0)
接下来,创建变量来识别止盈(TP_long)和止损(SL_long),并将它们加入 exit_long 条件中。
TP_long = entry_price_long * (1 + long_tp_inp)
plot(TP_long, color=color.green)
SL_long = entry_price_long * (1 - long_sl_inp)
plot(SL_long, color=color.red)
exit_long = *** INSERT YOUR ENTRY CONDITIONS ***, (high > TP_long or low < SL_long)
现在创建 alertcondition,并将其绘制到图表上。完整示例代码如下:
long_tp_inp = input(10, title='Long Take Profit %', step=0.1)/100
long_sl_inp = input(5, title='Long Stop Loss %', step=0.1)/100
entry_long = *** INSERT YOUR ENTRY CONDITIONS ***
entry_price_long=valuewhen(entry_long,close,0)
TP_long = entry_price_long * (1 + long_tp_inp)
plot(TP_long, color=color.green)
SL_long = entry_price_long * (1 - long_sl_inp)
plot(SL_long, color=color.red)
exit_long = *** INSERT YOUR ENTRY CONDITIONS ***, (high > TP_long or low < SL_long)
/// Alert Conditions
alertcondition(entry_long, title="Enter Long")
alertcondition(exit_long, title="Exit Long")
// Plot Alerts on the Graph
plotshape(series=entry_long, text="BUY", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=exit_long, text="EXIT BUY",style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)
