-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis_tools.py
77 lines (65 loc) · 2.41 KB
/
analysis_tools.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import time
from firebase_db import db
ahi_targets = db().collection('targets').document('ahi').get().to_dict()
datatypes = ["tweet", "reddit_comment", "reddit_post", "stocktwits_post", "yahoo_finance_comment"]
def calculate_ahi(document):
ahi = 0
total_weight = 0
for dt in datatypes:
mentions = document.get(dt + "_mentions", 0)
weight = ahi_targets.get(dt + "_weight", 0)
benchmark = ahi_targets.get(dt + "_benchmark")
if not benchmark:
print(f"no ahi benchmark found for datatype {dt}")
ahi += weight * mentions / benchmark
total_weight += weight
if total_weight != 0:
return ahi / total_weight
else:
return 0
def calculate_sentiment(document):
total_sentiment = 0
total_weight = 0
for dt in datatypes:
sentiment = document.get(dt + "_sentiment", 0)
weight = ahi_targets.get(dt + "_weight", 0)
total_sentiment += weight * sentiment
total_weight += weight
if total_weight != 0:
return total_sentiment / total_weight
else:
return 0
def calculate_rhi(ticker):
ahi_history = db()\
.collection('tickers')\
.document(ticker)\
.collection('history')\
.document('AHI')\
.get()\
.to_dict()["history"]
day_ago = time.time() - 3600 * 24
week_ago = time.time() - 3600 * 24 * 7
last_day = [dp["data"] for dp in ahi_history if dp["timestamp"] > day_ago]
last_week = [dp["data"] for dp in ahi_history if dp["timestamp"] > week_ago]
if sum(last_day) == 0 or sum(last_week) == 0:
return None
day_average = sum(last_day) / len(last_day)
week_average = sum(last_week) / len(last_week)
return day_average / week_average
def calculate_sgp(ticker):
sentiment_history = db() \
.collection('tickers') \
.document(ticker) \
.collection('history') \
.document('sentiment') \
.get() \
.to_dict()["history"]
day_ago = time.time() - 3600 * 24
week_ago = time.time() - 3600 * 24 * 7
last_day = [dp["data"] for dp in sentiment_history if dp["timestamp"] > day_ago]
last_week = [dp["data"] for dp in sentiment_history if dp["timestamp"] > week_ago]
if sum(last_day) == 0 or sum(last_week) == 0:
return None
day_average = sum(last_day) / len(last_day)
week_average = sum(last_week) / len(last_week)
return day_average / week_average