Pine script 策略退出未正确触发

Pine script 策略退出未正确触发,pine-script,Pine Script,谢谢你检查这个,希望你能帮助我 我已经创建了一个代码,该代码同时考虑了布林格带和RSI指标,并创建了一个策略,当收盘位于BB的较低带下方,且RSI显示超卖时,信号是买入,当收盘回到布林格带的基准线上方时应退出 此外,当收盘价高于布林格带的上带且RSI显示超买时,信号是卖出,当收盘价回到布林格带的基准线以下时应退出 然而,退出有一些问题,因为它有时正确触发,有时不正确。请注意,我补充说,如果有一个200点的移动在相反的方向,它应该结束交易 下面您将找到代码。请让我知道我做错了什么。谢谢 // ©

谢谢你检查这个,希望你能帮助我

我已经创建了一个代码,该代码同时考虑了布林格带和RSI指标,并创建了一个策略,当收盘位于BB的较低带下方,且RSI显示超卖时,信号是买入,当收盘回到布林格带的基准线上方时应退出

此外,当收盘价高于布林格带的上带且RSI显示超买时,信号是卖出,当收盘价回到布林格带的基准线以下时应退出

然而,退出有一些问题,因为它有时正确触发,有时不正确。请注意,我补充说,如果有一个200点的移动在相反的方向,它应该结束交易

下面您将找到代码。请让我知道我做错了什么。谢谢

// © hkanaan0

//@version=3
// 1. Define strategy settings
strategy(title="Bollinger Breakout", overlay=true,
     pyramiding=0, initial_capital=100000,
     commission_type=strategy.commission.cash_per_order,
     commission_value=4, slippage=2)

smaLength = input(title="SMA Length", type=integer, defval=50)
stdLength = input(title="StdDev Length", type=integer, defval=50)

ubOffset = input(title="Upper Band Offset", type=float, defval=2, step=0.5)
lbOffset = input(title="Lower Band Offset", type=float, defval=2, step=0.5)

usePosSize = input(title="Use Position Sizing?", type=bool, defval=true)
riskPerc   = input(title="Risk %", type=float, defval=0.5, step=0.25)

rsiSource = input(title = "RSI Source", type = source, defval = hlc3)
rsiLength = input(title = "RSI Length", type = integer, defval = 14)
rsiOverbought = input(title = "RSI Overbought Level", type = integer, defval = 70)
rsiOversold = input(title = "RSI Oversold Level", type = integer, defval = 30)

// 2. Calculate strategy values
rsiValue = rsi(rsiSource, rsiLength)
isRSIOB = rsiValue >= rsiOverbought
isRSIOS = rsiValue <= rsiOversold
isBullish = crossover(rsiValue, 55)
isBearish = crossunder(rsiValue, 45)

smaValue = sma(hlc3, smaLength)
stdDev   = stdev(hlc3, stdLength)

upperBand = smaValue + (stdDev * ubOffset)
lowerBand = smaValue - (stdDev * lbOffset)

riskEquity  = (riskPerc / 100) * strategy.equity
atrCurrency = (atr(20) * syminfo.pointvalue)
posSize     = usePosSize ? floor(riskEquity / atrCurrency) : 1

// 3. Output strategy data
plot(series=smaValue, title="SMA", color=blue, linewidth=2)

plot(series=upperBand, title="UB", color=green,
     linewidth=4)
plot(series=lowerBand, title="LB", color=red,
     linewidth=4)
     
//plotshape(isRSIOB, title="Overbought" , location=location.abovebar , color=red , transp=0 , style=shape.triangledown , text="Sell")
//plotshape(isRSIOS, title = "Oversold", location = location.belowbar, color =lime, transp = 0, style = shape.triangleup, text = "Buy")
plotshape(isBullish, title = "Bullish Momentum", location=location.abovebar, color=lime, transp = 0, style = shape.arrowup, text = "Bullish")
plotshape(isBearish, title = "Bearish Momentum", location=location.belowbar, color=red, transp = 0, style = shape.arrowdown, text = "Bearish")


// 4. Determine long trading conditions
enterLong = crossunder(close, lowerBand) and isRSIOS
exitLong  = crossover(close, smaValue)

// 5. Code short trading conditions
enterShort = crossover(close, upperBand) and isRSIOB
exitShort  = crossunder(close, smaValue)

// 6. Submit entry orders
if (enterLong)
    strategy.order(id="Enter Long", long=true, qty=posSize)

if (enterShort)
    strategy.order(id="Enter Short", long=false, qty=posSize)

// 7. Submit exit orders
strategy.exit(id="Exit Long", when=exitLong, loss = 200)
strategy.exit(id="Exit Short", when=exitShort, loss = 200)```
/©HKANANAN0
//@版本=3
// 1. 定义策略设置
策略(title=“Bollinger Breakout”,overlay=true,
聚合=0,初始资本=100000,
佣金类型=每个订单的策略.佣金.现金,
佣金(U值=4,滑移=2)
smaLength=输入(title=“SMA Length”,type=整数,deffal=50)
stdleength=输入(title=“StdDev Length”,type=integer,deffal=50)
ubOffset=输入(title=“上频带偏移”,类型=浮动,defval=2,步长=0.5)
lbOffset=输入(title=“下频带偏移”,类型=浮动,defval=2,步长=0.5)
usePosSize=input(title=“使用职位大小?”,type=bool,deffal=true)
riskPerc=输入(title=“Risk%”,type=float,deffal=0.5,step=0.25)
rsiSource=输入(title=“RSI Source”,type=源,deffal=hlc3)
RSI长度=输入(title=“RSI长度”,类型=整数,定义=14)
RSI超买=输入(title=“RSI超买水平”,类型=整数,定义=70)
RSI超卖=输入(title=“RSI超卖级别”,类型=整数,定义=30)
// 2. 计算战略价值
rsiValue=rsi(rsiSource,rsiLength)
isRSIOB=rsiValue>=RSIoverbund

isRSIOS=rsiValue发生的情况是,当exit_short或exit_long触发时,计算出200点子。因此,如果在12000点触发一个exit_short触发器,那么价格指令将只在11800点发生,即使你在13000点做空

为了解决这个问题,我计算了您进入市场时的止损价:

opened_order = strategy.position_size[0] != 0 and strategy.position_size[1] == 0
如果你当时没有职位,但现在是,那么
策略。职位大小会发生变化。因此,我使用
valuewhen(opened\u order,close,0)
来获取打开订单时的收盘价。我把这个价格和+200滴答从它,取决于哪个方向的订单打开

最后,如果当前的低点/高点已触及止损价,则该头寸关闭。我也发表了不同的评论,所以你可以看看交易是否已经停止,或者你是否获利回吐。要做到这一点,我必须将PineScript更新为v4,tho,因此,如果您不需要这些注释,可以将它们删除

代码如下:

// Strategy settings
strategy(title="Bollinger Breakout", overlay=true,
     pyramiding=0, initial_capital=100000,
     calc_on_order_fills = true,
     commission_type=strategy.commission.cash_per_order,
     commission_value=4, slippage=2)

smaLength = input(title="SMA Length", defval=50)
stdLength = input(title="StdDev Length", defval=50)

ubOffset = input(title="Upper Band Offset", defval=2, step=0.5)
lbOffset = input(title="Lower Band Offset", defval=2, step=0.5)

usePosSize = input(title="Use Position Sizing?", defval=false)
riskPerc   = input(title="Risk %", defval=2.5, step=0.25)

rsiSource = input(title = "RSI Source", defval = hlc3)
rsiLength = input(title = "RSI Length", defval = 14)
rsiOverbought = input(title = "RSI Overbought Level", defval = 70)
rsiOversold = input(title = "RSI Oversold Level", defval = 30)

// Calculate strategy values
rsiValue = rsi(rsiSource, rsiLength)
isRSIOB = rsiValue >= rsiOverbought
isRSIOS = rsiValue <= rsiOversold
isBullish = crossover(rsiValue, 55)
isBearish = crossunder(rsiValue, 45)

smaValue = sma(hlc3, smaLength)
stdDev   = stdev(hlc3, stdLength)

upperBand = smaValue + (stdDev * ubOffset)
lowerBand = smaValue - (stdDev * lbOffset)

riskEquity  = (riskPerc / 100) * strategy.equity
atrCurrency = (atr(20) * syminfo.pointvalue)
posSize     = usePosSize ? floor(riskEquity / atrCurrency) : 1

in_market = strategy.opentrades != 0
opened_order = strategy.position_size[0] != 0 and strategy.position_size[1] == 0
is_long = strategy.position_size > 0
is_short = strategy.position_size < 0
bought = is_long[0] and not is_long[1]
sold = is_short[0] and not is_short[1]

// Output strategy data

plot(series=smaValue, title="SMA", color=color.blue, linewidth=2)
plot(series=upperBand, title="UB", color=color.green, linewidth=4)
plot(series=lowerBand, title="LB", color=color.red, linewidth=4)

// Determine long trading conditions

enterLong = crossunder(close, lowerBand) and isRSIOS
plotshape(enterLong, title="Entry long signal", location=location.abovebar, color=color.lime, style=shape.triangledown)
exitLong  = crossover(close, smaValue)

// Determine short trading conditions

enterShort = crossover(close, upperBand) and isRSIOB
plotshape(enterShort, title="Entry short signal", location=location.belowbar, color=color.red, style = shape.triangleup)
exitShort  = crossunder(close, smaValue)

// Stop loss calculations

moving_stop_price_long = not in_market ? close - (200 * syminfo.mintick) : na
moving_stop_price_short = not in_market ? close + (200 * syminfo.mintick) : na
stop_price = is_long ? valuewhen(bought, close[1], 0) - (200 * syminfo.mintick): is_short ? valuewhen(sold, close[1], 0) + (200 * syminfo.mintick) : na
plot(stop_price ? stop_price : stop_price[1], "Stop price", color.red, 2, plot.style_linebr)

// Submit entry orders

if enterLong and not in_market
    strategy.order(id="Long", long=true, qty=posSize)

if enterShort and not in_market
    strategy.order(id="Short", long=false, qty=posSize)

// Submit exit orders
if is_long
    strategy.exit("Long", limit=exitLong ? close : na, stop=stop_price)
if is_short
    strategy.exit("Short", limit=exitShort ? close : na, stop=stop_price)
//策略设置
策略(title=“Bollinger Breakout”,overlay=true,
聚合=0,初始资本=100000,
计算顺序填充=真,
佣金类型=每个订单的策略.佣金.现金,
佣金(U值=4,滑移=2)
smaLength=输入(title=“SMA Length”,deffal=50)
stdleength=输入(title=“StdDev Length”,deffal=50)
ubOffset=输入(title=“上频带偏移”,defval=2,步长=0.5)
lbOffset=输入(title=“下频带偏移”,defval=2,步长=0.5)
usePosSize=input(title=“使用职位大小?”,deffal=false)
riskPerc=输入(title=“Risk%”,defval=2.5,step=0.25)
rsiSource=输入(title=“RSI Source”,deffal=hlc3)
RSI长度=输入(title=“RSI长度”,定义=14)
RSI超买=输入(title=“RSI超买水平”,defval=70)
RSI超卖=输入(title=“RSI超卖级别”,defval=30)
//计算战略价值
rsiValue=rsi(rsiSource,rsiLength)
isRSIOB=rsiValue>=RSIoverbund
isRSIOS=RSI值0
_short=strategy.position_size<0
买来的=很长[0]而不是很长[1]
卖出=空头[0]和不空头[1]
//输出策略数据
绘图(系列=smaValue,title=“SMA”,颜色=color.blue,线宽=2)
绘图(系列=上带,标题=“UB”,颜色=颜色。绿色,线宽=4)
绘图(系列=下边,title=“LB”,颜色=颜色。红色,线宽=4)
//确定长期交易条件
enterLong=交叉(关闭、下带)和isRSIOS
plotshape(enterLong,title=“Entry long signal”,location=location.overbar,color=color.lime,style=shape.triangledown)
exitLong=交叉(闭合,SMA值)
//确定空头交易条件
enterShort=交叉(闭合,上带)和isRSIOB
plotshape(enterShort,title=“Entry short signal”,location=location.belowbar,color=color.red,style=shape.triangleup)
exitShort=crossunder(关闭,smaValue)
//止损计算
移动停止价格长=不在市场中?关闭-(200*symininfo.mintick):不适用
移动停止价格短=不在市场中?关闭+(200*symininfo.mintick):不适用
停止价格=长吗?valuewhen(买入,收盘[1],0)-(200*syminfo.mintick):是否短?valuewhen(售出,关闭[1],0)+(200*syminfo.mintick):不适用
绘图(停止价格?停止价格:停止价格[1],“停止价格”,颜色.red,2,绘图.style\u linebr)
//提交入境令
如果进入长期且不在市场
策略.订单(id=“Long”,Long=true,qty=posSize)
如果输入较短且不在_市场中
策略.订单(id=“Short”,long=false,qty=posSize)
//提交出境令
如果你很长
策略.退出(“长”,限制=退出长?关闭:不适用,停止=停止价格)
如果你短
策略.退出(“短”,限制=退出短?关闭:不适用,停止=停止价格)

发生的事情是,在触发exit_short或exit_long时计算出200点子。因此,如果在12000点触发一个exit_short触发器,那么价格指令将只在11800点发生,即使你在13000点做空

为了解决这个问题,我计算了您进入市场时的止损价:

opened_order = strategy.position_size[0] != 0 and strategy.position_size[1] == 0
如果您当时没有处于某个位置,但现在处于该位置,
strategy.positio