Skip to content

Instantly share code, notes, and snippets.

@tomas-rampas
Created June 3, 2015 16:30
Show Gist options
  • Save tomas-rampas/3cfdda04ae90d42f7ca9 to your computer and use it in GitHub Desktop.
Save tomas-rampas/3cfdda04ae90d42f7ca9 to your computer and use it in GitHub Desktop.
# Donchian Channel
def donchian(df, n):
i = df.index[-1] - 1
dcuarr = [float('NaN')] * df.index[-1]
dclarr = [float('NaN')] * df.index[-1]
while i - n >= 0:
dcupper = max(df['High'].ix[i - n:i])
dclower = min(df['Low'].ix[i-n:i])
dcuarr[i] = dcupper
dclarr[i] = dclower
i -= 1
donchian_upper = pd.Series(dcuarr, name='DonchianUpper')
donchian_lower = pd.Series(dclarr, name='DonchianLower')
df = df.join(donchian_upper)
df = df.join(donchian_lower)
return df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment