0
votes

I'm new to pinescript and I'm having difficulty writing conditions for stochastic crossovers

  • Enter short positions with crossover above 80
  • Enter long positions with crossover below 20

longCondition = crossover(sma(stoch(close, high, low, 13)), sma(15, 5) iff(close <=20))

*vice verse for shortCondotion

1
An example of how to code it can be found here: Strategy Stochastic Crossover Backtest - Bjorn Mistiaen

1 Answers

0
votes

This code example will get you on your way.

//@version=4
study("SO", overlay=true, scale=scale.left)

var loLevel = input(20, "Level Low",            minval=0, maxval=100)
var hiLevel = input(80, "Level High",           minval=0, maxval=100)
var stLen   = input(13, "Stochastic Length",    minval=1)

st      = stoch(close, high, low, 13)
hiCross = crossover(st, hiLevel)
loCross = crossunder(st, loLevel)

shortCondition  = hiCross
longCondition   = loCross

hline(loLevel)
hline(hiLevel)

plot(st)

bgcolor(hiCross ? color.red   : na, 80)
bgcolor(loCross ? color.green : na, 80)