-
Notifications
You must be signed in to change notification settings - Fork 2
/
momentum.py
50 lines (41 loc) · 1.9 KB
/
momentum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
""" Momentum Indicator
"""
import math
import pandas
from talib import abstract
from analyzers.utils import IndicatorUtils
class Momentum(IndicatorUtils):
def analyze(self, historical_data, period_count=10,
signal=['momentum'], hot_thresh=None, cold_thresh=None):
"""Performs momentum analysis on the historical data
Args:
historical_data (list): A matrix of historical OHCLV data.
period_count (int, optional): Defaults to 10. The number of data points to consider for
our momentum.
signal (list, optional): Defaults to momentum. The indicator line to check hot/cold
against.
hot_thresh (float, optional): Defaults to None. The threshold at which this might be
good to purchase.
cold_thresh (float, optional): Defaults to None. The threshold at which this might be
good to sell.
Returns:
pandas.DataFrame: A dataframe containing the indicators and hot/cold values.
"""
dataframe = self.convert_to_dataframe(historical_data)
mom_values = abstract.MOM(dataframe, period_count).to_frame()
mom_values.dropna(how='all', inplace=True)
mom_values.rename(
columns={mom_values.columns[0]: 'momentum'}, inplace=True)
last_item = mom_values.shape[0]-1
item1 = mom_values.momentum[last_item]
item2 = mom_values.momentum[last_item-1]
if(item1>item2):
print("momentum went up")
elif(item1<item2):
print("momentum went down")
else:
print("Nothing happned to momentum")
if mom_values[signal[0]].shape[0]:
mom_values['is_hot'] = mom_values[signal[0]] > hot_thresh
mom_values['is_cold'] = mom_values[signal[0]] < cold_thresh
return mom_values