所以我试图用 pine 脚本写出这个策略,它的目的是用于 1 分钟图表,但使用来自 5 分钟级别的随机 rsi 的信息。
我遇到的问题是空头头寸的追踪止损锁定了整个加密货币,从某种意义上说,如果保证金足够高,任何空头头寸要么获得保证金,要么永远保持开放,似乎price_stop_short
没有被计算和我不明白为什么,如果您注释掉短条目,只要长条目,一切都可以正常if (inShortTrade)
工作,它正在工作,只是里面没有什么,而且,奇怪的是,当将smoothK
随机 rsi 的输入设置为时,脚本将解锁某些值(例如 5 或 6),但在这些情况下,追踪止损仍然不适用于空头入场,而那些都只是在初始设置止损处关闭......
我一直在研究这个并改变一些东西来测试打开和关闭几个小时,但似乎无法找到它为什么不起作用。
//@version=5
strategy("My Strategy", overlay=false, margin_long=100, margin_short=100)
Per = input.timeframe(title="resolution", defval="5")
smoothK = input.int(3, minval=1)
smoothD = input.int(3, minval=1)
lengthRSI = input.int(14, minval=1)
lengthStoch = input.int(14, minval=1)
src = input(close, title="RSI Source")
rsi1 = request.security(syminfo.tickerid, Per, ta.rsi(src, lengthRSI))
K = request.security(syminfo.tickerid, Per, ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK))
D = request.security(syminfo.tickerid, Per, ta.sma(K, smoothD))
atr = ta.atr(14)
//trailing stop variables
trailMult = input.float(1.9, "trail multiplier", minval = 0, step = 0.1)
price_stop_long = 0.0
price_stop_short = 999999.9
//conditions
longCondition = ta.crossover(K, D) or D < 3
shortCondition = ta.crossunder(K,D) or D > 97
inLongTrade = strategy.position_size > 0
notInTrade = strategy.position_size == 0
inShortTrade = strategy.position_size < 0
if (notInTrade and longCondition)
strategy.entry("Long", strategy.long)
if (notInTrade and shortCondition)
strategy.entry("short", strategy.short)
//trailing stop
if (inLongTrade)
stopValue = close-(atr * trailMult)
price_stop_long := math.max(stopValue, price_stop_long[1])
strategy.exit(id="trailing stoploss", stop=price_stop_long)
else if (inShortTrade)
stopValue = open+(atr * trailMult)
price_stop_short := math.min(stopValue, price_stop_short[1])
strategy.exit(id="trailing stoploss", stop=price_stop_short)
else
price_stop_short := 99999999
price_stop_long := 0.0
//plot(K)
//plot(D, color=color.red)
plot(inLongTrade ? price_stop_long:na, color=color.green)
plot(inShortTrade ? price_stop_short:na, color=color.orange)