Skip to content

Instantly share code, notes, and snippets.

@sam-thecoder
Last active August 9, 2018 10:28
Show Gist options
  • Save sam-thecoder/bd081b675b4fe1988d7f3ff4bec03049 to your computer and use it in GitHub Desktop.
Save sam-thecoder/bd081b675b4fe1988d7f3ff4bec03049 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])
df.rename(columns={'Value': 'Value USD'}, inplace=True)
df['Value 2'] = df['Value USD'].shift(-1)
df['Max 7'] = df['Value USD'].rolling(7).max()
df['Min 7'] = df['Value USD'].rolling(7).min()
df['Change'] = df['Value USD'] - df['Value 2']
df['Mean Change 7'] = df['Change'].rolling(7).mean()
df['Drop 7'] = df['Change'].rolling(7).apply(count_drop, raw=True)
df['Up 7'] = df['Change'].rolling(7).apply(count_up, raw=True)
def decider(row):
if row['Drop 7'] < row['Up 7'] and row['Mean Change 7'] > 0 and row['Change'] > 0:
return 2
elif row['Drop 7'] == row['Up 7'] and row['Mean Change 7'] > 0 and row['Change'] >= 0:
return 1
elif row['Drop 7'] > row['Up 7'] and row['Mean Change 7'] < 0 and row['Change'] >= 0:
return 0
elif row['Drop 7'] < row['Up 7'] and row['Mean Change 7'] > 0 or row['Change'] >= 0:
return -1
elif row['Drop 7'] < row['Up 7'] and row['Mean Change 7'] < 0 or row['Change'] < 0:
return -2
elif row['Drop 7'] < row['Up 7'] and row['Mean Change 7'] > 0 and row['Change'] < 0:
return -3
elif row['Drop 7'] > row['Up 7'] and row['Mean Change 7'] < 0 and row['Change'] < 0:
return -4
df['Predict'] = df.apply(decider, axis=1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment