Strategy
This strategy is backtested on daily OHLCV price data across major US equity indices and ETFs. Entry and exit signals are generated from the strategy's indicators with no lookahead bias; trades execute at the open of the bar following the signal.
Performance metrics are computed per symbol and shown individually. The parameter optimization section, where available, runs a grid search over the strategy's key parameters, optimizing for Sharpe ratio. Transaction costs and slippage are not modeled.
The signal logic and indicator code for this strategy is shown in the methodology code section below.
Mean-reversion using the Stochastic oscillator (%K and %D). Enters long when %K crosses above %D in oversold territory (below 20). Exits when %K crosses below %D in overbought territory (above 80).
class StochasticReversion(Strategy):
n = 14
d = 3
def init(self):
k_vals = _stoch_k(self.data.High, self.data.Low, self.data.Close, self.n)
self.stoch_k = self.I(lambda: k_vals, name="K")
self.stoch_d = self.I(_stoch_d, k_vals, self.d)
def next(self):
if self.stoch_k[-1] > self.stoch_d[-1] and self.stoch_k[-1] < 20 and not self.position:
self.buy()
elif self.stoch_k[-1] < self.stoch_d[-1] and self.stoch_k[-1] > 80 and self.position:
self.position.close()