-2

Hi all hope ur doing well,

I’m trying to code a strategy that go long when k is in the overbought area and crossed d and the opposite for shorting

But I’m new to this and I don’t now how to write the long entry if the cross happen in the overbought area.

Thanks all,

Bjorn Mistiaen
  • 6,459
  • 3
  • 18
  • 42
Salem
  • 1
  • 1
  • 2

1 Answers1

1

I have coded the following to get strategy results that go long when k is in the overbought area and crossed d and the opposite for shorting. More details added within the code comment.

//@version=5
strategy("Stochastic Strategy", overlay=true)
//Stochastic Inputs
length = input.int(14, minval=1)
OverBought = input(80)
OverSold = input(20)
smoothK = 3
smoothD = 3
k = ta.sma(ta.stoch(close, high, low, length), smoothK)
d = ta.sma(k, smoothD)
//Rule to define crossover /crossunder
co = ta.crossover(k,d)
cu = ta.crossunder(k,d)

if (not na(k) and not na(d))
    //code to define if k is in overbought zone and k crossover d and enter a long trade
    if (co and k < OverSold)
        strategy.entry("StochLE", strategy.long, comment="StochLE")
    //code to define if k is in oversold zone and k crossunder d and enters a short trade
    if (cu and k > OverBought)
        strategy.entry("StochSE", strategy.short, comment="StochSE")
TJalam
  • 308
  • 4
  • 16