简体   繁体   中英

Pinescript takes profit at next candle bar close

Have I have set levels for a time period and I'm trying to create strategy w/ RSI at these levels, but Pinescript takes profit at next candle bar close.

buyzone1 = ta.crossunder(close, b1)
buyzone2 = ta.crossunder(close, b2)
buyzone3 = ta.crossunder(close, b4)
sellzone1 = ta.crossover(close, s1)
sellzone2 = ta.crossover(close, s2)
sellzone3 = ta.crossover(close, s3)

length = input( 14 )
overSold = input( 30 )
overBought = input( 70 )
price = close
vrsi = ta.rsi(price, length)

co = ta.crossover(vrsi, overSold)
cu = ta.crossunder(vrsi, overBought)

short_tp = top_valuearea
long_tp = lower_valuearea

short_stp = bottom_tail_value
long_stop = top_tail_value

if (not na(vrsi))
    if (co) and (buyzone1) or (buyzone2) or (buyzone3)
        strategy.entry("Long", strategy.long, 1 ,comment="Long")
    if (cu) and (sellzone1) or (sellzone2) or (sellzone3)
        strategy.entry("Short", strategy.short, 1, comment="Short")

if (strategy.position_size > 0)
    strategy.exit("Long TP1", from_entry="Long", limit = long_tp, stop = long_stop)
    strategy.exit("Short TP1", from_entry="Short", limit = short_tp, stop = short_stp)

// Plot take profit values for confirmation
plot(series=(strategy.position_size > 0) ? long_tp : na, color=color.green, style=plot.style_circles, linewidth=1, title="Take Profit 1")
plot(series=(strategy.position_size > 0) ? short_tp : na, color=color.green, style=plot.style_circles, linewidth=1, title="Take Profit 1")
plot(series=(strategy.position_size > 0) ? long_stop : na, color=color.red, style=plot.style_circles, linewidth=1, title="Stop Loss")
plot(series=(strategy.position_size > 0) ? short_stp : na, color=color.red, style=plot.style_circles, linewidth=1, title="Stop Loss")

But the the the TP is always on the next candle close. Sometimes two Long positions open, and the shorts never take profit. 在此处输入图像描述

I'm not sure why it doesn't follow the RSI logic or take profit at its levels. The Green dots are the TP1 and the red dots are the stop losses.

Remove if check for strategy.exit :

...
strategy.exit("Long TP1", from_entry="Long", limit = long_tp, stop = long_stop)
strategy.exit("Short TP1", from_entry="Short", limit = short_tp, stop = short_stp)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM