此示例代码旨在帮助您测试如何在 TradingView 上自动化运行您的第一个交易机器人。
该 TradingView 机器人指标 同时支持 多头(Long) 和 空头(Short) 仓位。
当资产价格在上一根 K 线上上涨时(即收盘价高于开盘价),该策略会开立多头仓位;当资产价格在上一根 K 线上下跌时(即收盘价低于开盘价),则会平掉多头仓位。
空头仓位的逻辑则相反——当多头平仓信号执行时,系统会自动触发空头入场。
该代码可应用于任何时间周期和任何交易资产。
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © WunderTrading
//@version=5
indicator("Signal Tester", overlay=true)
///////////////// LONG //////////////////
isEntry_Long = false
isEntry_Long := nz(isEntry_Long[1], false)
isExit_Long = false
isExit_Long := nz(isExit_Long[1], false)
entry_long = not isEntry_Long and close>open
exit_long = not isExit_Long and open>close
if (entry_long)
isEntry_Long := true
isExit_Long := false
if (exit_long)
isEntry_Long := false
isExit_Long := true
///////////// SHORT //////////////
isEntry_Short = false
isEntry_Short := nz(isEntry_Short[1], false)
isExit_Short = false
isExit_Short := nz(isExit_Short[1], false)
entry_short=not isEntry_Short and open>close
exit_short= not isExit_Short and close>open
if (entry_short)
isEntry_Short := true
isExit_Short := false
if (exit_short)
isEntry_Short := false
isExit_Short := true
alertcondition(entry_long, title="Enter Long")
alertcondition(exit_long, title="Exit Long")
alertcondition(entry_short, title="Enter Short")
alertcondition(exit_short, title="Exit Short")
plotshape(series=entry_long, text="Long", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.small)
plotshape(series=exit_long, text="EXIT Long",style=shape.triangledown, location=location.abovebar, color=color.purple, size=size.small)
plotshape(series=entry_short, text="Short", style=shape.triangledown, location=location.abovebar, color=color.red, size=size.small)
plotshape(series=exit_short, text="EXIT Short",style=shape.triangleup, location=location.belowbar, color=color.purple, size=size.small)
