Skip to content

Instantly share code, notes, and snippets.

@sam-thecoder
Created August 24, 2018 11:43
Show Gist options
  • Save sam-thecoder/c4de2e1657332417ae539dc16af1d2f6 to your computer and use it in GitHub Desktop.
Save sam-thecoder/c4de2e1657332417ae539dc16af1d2f6 to your computer and use it in GitHub Desktop.
def count_drop(numbers):
return len([x for x in numbers if x < 0])
def count_up(numbers):
return len([x for x in numbers if x > 0])
def actual_calc(row):
if row['Open'] > row['Adj Close']:
return 0
return 1
df['Open 2'] = df['Open'].shift(-1)
df['Max 7'] = df['Open'].rolling(7).max()
df['Min 7'] = df['Open'].rolling(7).min()
df['Change'] = df['Open'] - df['Open 2']
df['Mean Change 7'] = df['Change'].rolling(7).mean()
df['Drop 7'] = df['Change'].rolling(7).apply(count_drop)
df['Up 7'] = df['Change'].rolling(7).apply(count_up)
df['Actual'] = df.apply(actual_calc, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment