Search code examples
pine-scriptpine-script-v5

Pine Script v5 Strategy not giving entries


I've tried to create a Pine Script strategy and I've reached the point where there are no errors prompted by the IDE but the strategy didn't give any entries. I've tried to understand how to fix the issue, I've searched for other similar questions, I've checked if I was missing something with the Pine Script User Manual and at some point I've even tried asking ChatGPT how to fix that but couldn't have any better results. I would really appreciate any help as I'm learning everyday how to deal with the Pine Script language and couldn't improve if it was not for all the usefull contents gathered on platform like this one.

//@version=5
strategy("Custom Strategy", overlay=true)

// First Indicator - v5i
length = input(20, title='Length')
mult = input(2, title='Multiplier')

atr_ = ta.atr(length)

var float max1 = 0.0
var float min1 = 0.0
var bool is_uptrend_prev = false
var float stop = 0.0
var float vstop_prev = 0.0
var float vstop1 = 0.0
var bool is_uptrend = false
var bool is_trend_changed = false
var float max_ = 0.0
var float min_ = 0.0
var float vstop = 0.0

max1 := math.max(nz(max1[1]), close)
min1 := math.min(nz(min1[1]), close)
is_uptrend_prev := nz(is_uptrend_prev[1], true)
stop := is_uptrend_prev ? max1 - mult * atr_ : min1 + mult * atr_
vstop_prev := nz(vstop[1])
vstop1 := is_uptrend_prev ? math.max(vstop_prev, stop) : math.min(vstop_prev, stop)
is_uptrend := close - vstop1 >= 0
is_trend_changed := is_uptrend != is_uptrend_prev
max_ := is_trend_changed ? close : max1
min_ := is_trend_changed ? close : min1
vstop := is_trend_changed ? is_uptrend ? max_ - mult * atr_ : min_ + mult * atr_ : vstop1

buysignal = is_trend_changed and close - vstop1 > 0
sellsignal = is_trend_changed and close - vstop1 < 0

// Debug labels
label.new(bar_index, buysignal ? close : na, text="buysignal: " + str.tostring(buysignal), color=color.green, textcolor=color.white, style=label.style_label_left)
label.new(bar_index, sellsignal ? close : na, text="sellsignal: " + str.tostring(sellsignal), color=color.red, textcolor=color.white, style=label.style_label_left)
label.new(bar_index, vstop, text="vstop: " + str.tostring(vstop), color=color.yellow, textcolor=color.black, style=label.style_label_left)

// Second Indicator - Swing Points and Liquidity
swingSizeR = input.int(10, 'Bars Right-Left', inline='brl')
swingSizeL = input.int(15, '-', inline='brl')

// Pivot calculations
var int prevHighIndex = na
var int prevLowIndex = na
var bool highActive = false
var bool lowActive = false
var bool h = false
var bool l = false
pivHi = ta.pivothigh(high, swingSizeL, swingSizeR)
pivLo = ta.pivotlow(low, swingSizeL, swingSizeR)

// Variables for managing positions and stop loss
var bool longPosition = false
var bool shortPosition = false
var float stopLossLevel = na

lastPivHiHigh = ta.valuewhen(pivHi != 0, high[pivHi], 0)
previousPivHiHigh = ta.valuewhen(pivHi != 0, high[pivHi], 1)
isLastPivHiHigher = lastPivHiHigh > previousPivHiHigh
lastPivLoLow = ta.valuewhen(pivLo != 0, low[pivLo], 0)
previousPivLoLow = ta.valuewhen(pivLo != 0, low[pivLo], 1)
isLastPivLoLow = lastPivLoLow < previousPivLoLow

// Entry conditions and management
if buysignal and not longPosition
    longPosition := true
    stopLossLevel := ta.valuewhen(pivLo != 0, low[pivLo], 1)
    strategy.entry("Long", strategy.long)
    strategy.exit("Long TP", "Long", limit=vstop)
    strategy.exit("Long SL", "Long", stop=isLastPivHiHigher ? (ta.valuewhen(pivHi != 0, high[pivHi], 0)) : stopLossLevel)

if sellsignal and not shortPosition
    shortPosition := true
    stopLossLevel := ta.valuewhen(pivHi != 0, high[pivHi], 1)
    strategy.entry("Short", strategy.short)
    strategy.exit("Short TP", "Short", limit=vstop)
    strategy.exit("Short SL", "Short", stop=isLastPivLoLow ? (ta.valuewhen(pivLo != 0, low[pivLo], 0)) : stopLossLevel)

plot(pivHi != 0 ? high[pivHi] : na, title="Pivot High", color=color.red, style=plot.style_circles, linewidth=2, transp=0)
plot(pivLo != 0 ? low[pivLo] : na, title="Pivot Low", color=color.green, style=plot.style_circles, linewidth=2, transp=0)

Solution

  • the script has many errors. fix it step by step

    1. replace longPosition and shortPosition with strategy.position_size
    2. remove SL and TP (temporarily). first fix long and short

    something like this

    if buysignal and strategy.position_size <= 0
        strategy.entry("Long", strategy.long)
    
    if sellsignal and strategy.position_size >= 0
        strategy.entry("Short", strategy.short)
    

    at this point, there are only short entries
    fix long

    1. for debug, plot vstop1

    plot (vstop1)

    now you can see why there is no long trigger enter image description here

    it seems that this rule is wrong

    buysignal = is_trend_changed and close - vstop1 > 0