0

I want to wait untill a condition is met before the strategy begins trading.

Trying to make code that waits for the 15 sma to cross below the 12 sma (the Start Trading condition), and then once that happens the strategy can begin trading, but only once the start trading condition is met.

How can I do this ?

--

I tryed to use a switch but this did not work

--

The code so far:

//@version=5
strategy("My script", overlay=true)


fifteensma = ta.sma(close, 15)

twelvesma = ta.sma(close, 12)

sevensma = ta.sma(close, 7)

fourteensma = ta.sma(close, 14)


starttrading =  (fifteensma[0]  < twelvesma[0] ) and (fifteensma[1]  > twelvesma[1] )

longcondition = (sevensma[0]  > fourteensma[0] ) and (sevensma[1]  < fourteensma[1] )



starttradingsignalsoccurred = 0.0


//Wait untill a start trading signal occurs

if (starttrading)
    
    //when a starttrading signal happens add 1 to the starttrading signal variable
    starttradingsignalsoccurred := starttradingsignalsoccurred[1] + 1


if starttradingsignalsoccurred > 0


    if ( longcondition)
        
        //Enter Trade Long
        strategy.entry("Long", strategy.long, qty=10)
    

plot(fifteensma, color=color.red)
plot(twelvesma, color=color.green)
plot(sevensma, color=color.orange)
plot(fourteensma, color=color.blue)
Roboman Robo
  • 599
  • 2
  • 5
  • 16

2 Answers2

0

I think you mess a bit with 'starttrading' and 'starttradingsignalsoccurred'.
Try :

var starttradingsignalsoccurred = 0

if (starttrading)
    starttradingsignalsoccurred := starttradingsignalsoccurred + 1

if starttradingsignalsoccurred > 0
    if longcondition
    ....
G.Lebret
  • 2,826
  • 2
  • 16
  • 27
  • Thanks updated post code but it does not work – Roboman Robo Dec 03 '22 at 18:35
  • @RobomanRobo you must pay attention : var starttradingsignalsoccurred = 0 is not the same at all than starttradingsignalsoccurred = 0. You should read https://www.tradingview.com/pine-script-docs/en/v5/language/Variable_declarations.html?highlight=var#var – G.Lebret Dec 04 '22 at 02:38
0

I think there is a different way of looking at it -

If I understand correctly, you are looking to start trading after the 12 SMA crossed over the 15 SMA, and stop trading if the 12 SMA crossed under the 15 SMA, and in this trading window you are looking for a crossover of the 7 SMA over the 14 SMA. If that's correct, your trading window is just whenever the 12 SMA is over the 15 SMA, because the trading window is between crossover and crossunder of those numbers.

If so, you can use:

//@version=5
strategy("My script", overlay=true)

fifteensma = ta.sma(close, 15)
twelvesma = ta.sma(close, 12)
sevensma = ta.sma(close, 7)
fourteensma = ta.sma(close, 14)

starttrading =  (fifteensma[0]  < twelvesma[0] )

longcondition = (sevensma[0]  > fourteensma[0] ) and (sevensma[1]  < fourteensma[1] ) and starttrading

if ( longcondition)   
    //Enter Trade Long
    strategy.entry("Long", strategy.long, qty=10)   

BTW - instead of your longcondition, you can use a built in function:

starttrading = fifteensma < twelvesma
longcondition = ta.crossover(sevensma, fourteensma) and starttrading

if ( longcondition)   
    //Enter Trade Long
    strategy.entry("Long", strategy.long, qty=10)
mr_statler
  • 1,913
  • 2
  • 3
  • 14