Ivan
Ivan

Reputation: 436

ta.crossover does not work as expected in pine script

I have the following script

//@version=5
strategy("Bollinger Bands Strategy", overlay=true)
// BB
source = close
length = input.int(20, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50, title="StdDev")
basis = ta.sma(source, length)
dev = mult * ta.stdev(source, length)
upper = basis + dev
lower = basis - dev

// Stoch
periodK = input.int(14, title="%K Length", minval=1)
periodD = input.int(3, title="%D Smoothing", minval=1)
smoothK = input.int(1, title="%K Smoothing", minval=1)
k = ta.sma(ta.stoch(close, high, low, periodK), smoothK)
d = ta.sma(k, periodD)
lcb = input.int(20, title="Long border cross", minval=1) // long condition bollinger
scb = input.int(80, title="Short border cross", minval=1) // short condition bollinger


long_condition = (k[2] < k[1]) and (k[1] < k) and ((k[2] + k[1] + k) / 3) < 20
plotchar(long_condition, "long?", "", location = location.top)
short_condition = (k[2] > k[1]) and (k[1] > k)  and ((k[2] + k[1] + k) / 3) > 80
plotchar(short_condition, "short?", "", location = location.top)
plotchar(source, "source", "", location = location.top)
plotchar(lower, "lower", "", location = location.top)
my_crossunder = (source[1] > upper[1]) and (source < upper) 
plotchar(my_crossunder, "my_crossunder", "", location = location.top)

if (my_crossunder)
    strategy.entry("BBS", strategy.short, stop=upper, oca_name="BollingerBands", oca_type=strategy.oca.cancel, comment="BBS")
else
    strategy.cancel(id="BBS")

and for some reason, it does not enter my if condition, while plotchar(my_crossunder, "my_crossunder", "", location = location.top) says the variable is true

Plot

Here at the screenshot, it has to enter the position, but it does not do, while the condition is true, why?

Upvotes: 1

Views: 2438

Answers (1)

Ivan
Ivan

Reputation: 436

Solved it, strategy.entry() can not open more, than one position with the current strategy. So it means I had previous order opened with the same strategy. More info here. So if you want to place order anyway you should use strategy.order()

Upvotes: 1

Related Questions