0

I am trying to build a simple strategy for a stochastic entry/exit. I've pretty much copy-pasted the stochastic formula from the inbuilt indicator. The problem I'm having is that the long and short boolean signals seem to be having some issues.

While a cross-under with k >= 75 using a 'goshort' boolean works perfectly fine for me, when I try to evaluate a cross-over with k <= 25 using a 'golong' boolean, it causes the short signals to vanish. I am not using the 'golong' boolean in the sell entry strategy at all.

I've kept the 'golong' boolean as cross-over with k <= 75 in the below code which is not a real stochastic strategy at all but if I do that, the short signals appear fine. If I change this to k <= 25, somehow it impacts the short signals.

//@version=4
strategy("Stochastic Slow Strategy", overlay=true)
periodK = input(8, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(3, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
OverSold = 20
OverBought = 80
co = k >= d and k[1] <= d[1] ? true : false
cu = k <= d and k[1] >= d[1] ? true : false

golong=co and k <= 75? true: false  // <--This is the line where I have a problem
goshort=cu and k >= 75? true: false

strategy.entry("Sell",  strategy.short, comment="Sell", when=goshort==true)

strategy.entry("Buy", strategy.long, comment="Buy", when=golong==true)
Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
Sriram V
  • 1
  • 1

2 Answers2

0

I suggest to debug this problem with plots:

//@version=4
strategy("Stochastic Slow Strategy", overlay=true)
periodK = input(8, title="K", minval=1)
periodD = input(3, title="D", minval=1)
smoothK = input(3, title="Smooth", minval=1)
k = sma(stoch(close, high, low, periodK), smoothK)
d = sma(k, periodD)
OverSold = 20
OverBought = 80
co = k >= d and k[1] <= d[1] ? true : false
cu = k <= d and k[1] >= d[1] ? true : false

golong=co and k <= 75? true: false  // <--This is the line where I have a problem
goshort=cu and k >= 75? true: false

strategy.entry("Sell",  strategy.short, comment="Sell", when=goshort==true)

strategy.entry("Buy", strategy.long, comment="Buy", when=golong==true)

plot(golong ? 1 : 0)
plot(goshort ? 1 : 0)
plot(co)
plot(cu)
plot(k)

I suppose the problem is on the bars where k == d and k[1] == d[1] is true

Andrey D
  • 1,534
  • 1
  • 5
  • 7
0

Thanks Andrey D, I used the plot commands you provided to analyze the data.

Further to that, after a bit of studying, I added these two lines before the strategy entries and it is working fine now. I guess, I am supposed to close out an existing buy or sell signal before the next buy is generated.

strategy.close("Buy", when=goshort==true or golong==true)
strategy.close("Sell", when=golong==true or goshort==true)

A beginner's mistake I suppose.

Sriram V
  • 1
  • 1