Strategy
This is a long-only daily strategy. The indicator is computed from the completed bar, and the order fills at the next bar’s open. Each fill pays 0.10% commission. There is no extra slippage, and the strategy does not sell short.
Metrics below are one index at a time, not a portfolio of them. Where a parameter search is available, it picks the Sharpe-maximizing grid point on the S&P 500. That search sees the same sample it reports, so the best cell is an in-sample peak.
The signal logic and indicator code for this strategy is shown in the methodology code section below.
Fractal Breakout uses Bill Williams' fractal pattern — a bar with two lower highs on each side. Enters long when price closes above a confirmed fractal high, exits when price falls to a fractal low.
class FractalBreakout(Strategy):
left = 2; right = 2
def init(self):
n = self.left + self.right + 1
h, l = pd.Series(self.data.High), pd.Series(self.data.Low)
fh = h.rolling(n, center=True).apply(lambda x: x.iloc[self.left] if x.iloc[self.left]==x.max() else np.nan, raw=False)
fl = l.rolling(n, center=True).apply(lambda x: x.iloc[self.left] if x.iloc[self.left]==x.min() else np.nan, raw=False)
self.fh = self.I(lambda: fh, name="FractalHigh")
self.fl = self.I(lambda: fl, name="FractalLow")
def next(self):
idx = self.left + self.right + 2
fh_val = self.fh[-idx]; fl_val = self.fl[-idx]
if not np.isnan(fh_val) and self.data.Close[-1] >= fh_val and not self.position: self.buy()
elif not np.isnan(fl_val) and self.data.Close[-1] <= fl_val and self.position: self.position.close()