Skip to content

Instantly share code, notes, and snippets.

@samchaaa
Created December 20, 2020 08:28
Show Gist options
  • Save samchaaa/2b4311594d4020725ca812d46c259ac2 to your computer and use it in GitHub Desktop.
Save samchaaa/2b4311594d4020725ca812d46c259ac2 to your computer and use it in GitHub Desktop.
def hb_lb(ohlc, n, smoothing):
"""
Creates a "high band" and "low band", based on the absolute pct movement for a given period.
This will trigger the mean reversion trades.
'pct': percent change in the underlying, based on period n
'abs_vol': the absolute exponential moving average of the pct movement
"""
ohlc['pct'] = ((ohlc['C'] - ohlc['C'].shift(n)) / ohlc['C'])
# Gets the absolute pct change, with smoothing from ewm.
ohlc['abs_vol'] = abs(ohlc['pct']).ewm(span=smoothing, adjust=False).mean()
# Creates bands above/below price... one minute close outside of these triggers our trades.
ohlc['hb'] = (ohlc['C'] * (1 + ohlc['abs_vol'])).shift()
ohlc['lb'] = (ohlc['C'] * (1 - ohlc['abs_vol'])).shift()
return ohlc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment