mohsen taherinia
mohsen taherinia

Reputation: 1

How to trigger the position only once, after occurring one signal, (in Pine Script)

please Help me: my strategy is very simple.

long position: signal=> crossing the lower bound of "Bollinger Bound" position trigger=> candle is closed higher than the"Donchian Channels(13)" - after signal was occurred

short position: signal=> crossing the upper bound of "Bollinger Bound" position trigger=> candle is closed lower than the"Donchian Channels(13)" - after signal was occurred

After the trade signal(signalLong or signalLong) is given, I wait for the trigger(close>DC_basis or close>DC_basis). I have written this using ta.barsince function.

signalLong :=   low<lower 
signalShort :=  high>upper 

since_signalLong = 0
since_signalShort = 0
since_signalLong := ta.barssince(ta.change(signalLong))
since_signalShort := ta.barssince(ta.change(signalShort))

enterLong = false
enterShort = false
enterLong := signalLong[since_signalLong+1] and close > DC_basis
enterShort :=  signalShort[since_signalShort+1]  and  close < DC_basis

But after the trigger is activated, it trades on all of the subsequent candles.

if close > DC_basis then execute Long position (enterLong) //it checks the signal on since_signalShort+1 candle ago (signal remains true to end, after the first crossing) if close < DC_basis then execute Short position (enterShort) //it checks the signal on since_signalShort+1 candle ago (signal remains true to end, after the first crossing)

In my opinion, this problem is the signal remaining "true" after triggerring.

I don't know how to handle the Entry signal(enterLong or enterShort) after opening the trade.

/@version=5
strategy(title="myStrategy", overlay=true, pyramiding=10)

///////////////BB
length = input.int(20, minval=1)
src = input(close, title="Source")
mult = input.float(3.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(src, length)
dev = mult * ta.stdev(src, length)
upper = basis + dev
lower = basis - dev
offset = input.int(0, "Offset", minval = -500, maxval = 500)
//plot(basis, "Basis", color=#FF6D00, offset = offset)
p1 = plot(upper, "Upper", color=#2962FF, offset = offset)
p2 = plot(lower, "Lower", color=#2962FF, offset = offset)
//fill(p1, p2, title = "Background", color=color.rgb(33, 150, 243, 95))
///////////////BB

///////////////DC
DC_length = input.int(13, minval=1)
DC_lower = ta.lowest(DC_length)
DC_upper = ta.highest(DC_length)
DC_basis = math.avg(DC_upper, lower)
plot(DC_basis, "Basis", color=#FF6D00,linewidth = 2)
///////////////DC

signalLong= false
signalShort = false
signalLong :=   low<lower 
signalShort :=  high>upper 

plot(signalLong  ? low :na , color=color.green,style=plot.style_cross, linewidth  = 5)
plot(signalShort   ? high :na , color=color.red,style=plot.style_cross, linewidth  = 5)

since_signalLong = 0
since_signalShort = 0
since_signalLong := ta.barssince(ta.change(signalLong))
since_signalShort := ta.barssince(ta.change(signalShort))

enterLong = false
enterShort = false
enterLong := signalLong[since_signalLong+1] and close > DC_basis
enterShort :=  signalShort[since_signalShort+1]  and  close < DC_basis

plot(enterLong  ? low :na , color=color.green,style=plot.style_circles, linewidth  = 5)
plot(enterShort ? high :na , color=color.red,style=plot.style_circles, linewidth  = 5)

Correct

incorrect

incorrect after modification

Upvotes: 0

Views: 316

Answers (1)

mr_statler
mr_statler

Reputation: 2161

Sounds like you're looking for the ta.crossover() and ta.crossunder() function which will trigger the condition only when crossed:

signalLong := ta.crossunder(low, lower)
signalShort := ta.crossover(high, upper)

Upvotes: 0

Related Questions