mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 12:40:35 +00:00
Add queries and rise queries from google to sentiment
This commit is contained in:
@@ -15,8 +15,7 @@ def mentions(l_args, s_ticker, s_start):
|
||||
parser.add_argument('-s', "--start", type=valid_date, dest="s_start", default=s_start,
|
||||
help="starting date (format YYYY-MM-DD) from when we are interested in stock's mentions.")
|
||||
|
||||
#try:
|
||||
if 1:
|
||||
try:
|
||||
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
|
||||
|
||||
if l_unknown_args:
|
||||
@@ -42,16 +41,17 @@ def mentions(l_args, s_ticker, s_start):
|
||||
plt.show()
|
||||
print("")
|
||||
|
||||
#except:
|
||||
# print("")
|
||||
except:
|
||||
print("")
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def regions(l_args):
|
||||
def regions(l_args, s_ticker):
|
||||
parser = argparse.ArgumentParser(prog='regions',
|
||||
description="""Print other users watchlist. [Source: Reddit]""")
|
||||
parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=5,
|
||||
help='limit of posts with watchlists retrieved.')
|
||||
description="""Plot bars of regions based on stock's interest. [Source: Google]""")
|
||||
|
||||
parser.add_argument('-n', "--num", action="store", dest="n_num", type=check_positive, default=10,
|
||||
help='number of regions to plot that show highest interest.')
|
||||
|
||||
try:
|
||||
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
|
||||
@@ -60,16 +60,31 @@ def regions(l_args):
|
||||
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
|
||||
return
|
||||
|
||||
pytrend = TrendReq()
|
||||
pytrend.build_payload(kw_list=[s_ticker])
|
||||
df_interest_region = pytrend.interest_by_region()
|
||||
df_interest_region = df_interest_region.sort_values([s_ticker], ascending=False).head(ns_parser.n_num)
|
||||
|
||||
plt.figure(figsize=(25,5))
|
||||
plt.title(f"Top's regions interest on {s_ticker}")
|
||||
plt.bar(df_interest_region.index, df_interest_region[s_ticker], width=0.8)
|
||||
plt.grid(b=True, which='major', color='#666666', linestyle='-')
|
||||
plt.ylabel('Interest [%]')
|
||||
plt.xlabel("Time")
|
||||
plt.show()
|
||||
print("")
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def queries(l_args):
|
||||
def queries(l_args, s_ticker):
|
||||
parser = argparse.ArgumentParser(prog='queries',
|
||||
description="""Print other users watchlist. [Source: Reddit]""")
|
||||
parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=5,
|
||||
help='limit of posts with watchlists retrieved.')
|
||||
description="""Print top related queries with this stock's query. [Source: Google]""")
|
||||
|
||||
parser.add_argument('-n', "--num", action="store", dest="n_num", type=check_positive, default=10,
|
||||
help='number of top related queries to print.')
|
||||
|
||||
try:
|
||||
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
|
||||
@@ -78,16 +93,25 @@ def queries(l_args):
|
||||
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
|
||||
return
|
||||
|
||||
pytrend = TrendReq()
|
||||
pytrend.build_payload(kw_list=[s_ticker])
|
||||
df_related_queries = pytrend.related_queries()
|
||||
df_related_queries = df_related_queries[s_ticker]['top'].head(ns_parser.n_num)
|
||||
df_related_queries['value'] = df_related_queries['value'].apply(lambda x: str(x)+'%')
|
||||
print(df_related_queries.to_string(index=False))
|
||||
print("")
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def rise(l_args):
|
||||
def rise(l_args, s_ticker):
|
||||
parser = argparse.ArgumentParser(prog='rise',
|
||||
description="""Print other users watchlist. [Source: Reddit]""")
|
||||
parser.add_argument('-l', "--limit", action="store", dest="n_limit", type=check_positive, default=5,
|
||||
help='limit of posts with watchlists retrieved.')
|
||||
description="""Print top rising related queries with this stock's query.
|
||||
[Source: Google]""")
|
||||
parser.add_argument('-n', "--num", action="store", dest="n_num", type=check_positive, default=10,
|
||||
help='number of top rising related queries to print.')
|
||||
|
||||
try:
|
||||
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
|
||||
@@ -96,6 +120,13 @@ def rise(l_args):
|
||||
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
|
||||
return
|
||||
|
||||
pytrend = TrendReq()
|
||||
pytrend.build_payload(kw_list=[s_ticker])
|
||||
df_related_queries = pytrend.related_queries()
|
||||
df_related_queries = df_related_queries[s_ticker]['rising'].head(ns_parser.n_num)
|
||||
print(df_related_queries.to_string(index=False))
|
||||
print("")
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
|
||||
@@ -34,6 +34,9 @@ def print_sentiment():
|
||||
print("")
|
||||
print("Google:")
|
||||
print(" mentions interest over time based on stock's mentions")
|
||||
print(" regions regions that show highest interest in stock")
|
||||
print(" queries top related queries with this stock")
|
||||
print(" rise top rising related queries with stock")
|
||||
print("")
|
||||
|
||||
return
|
||||
@@ -47,7 +50,8 @@ def sen_menu(s_ticker, s_start):
|
||||
sen_parser.add_argument('cmd', choices=['help', 'q', 'quit',
|
||||
'watchlist', 'spac', 'spac_c', 'wsb', 'popular',
|
||||
'bullbear', 'messages', 'trending', 'stalker',
|
||||
'infer', 'sentiment', 'mentions'])
|
||||
'infer', 'sentiment', 'mentions', 'regions',
|
||||
'queries', 'rise'])
|
||||
|
||||
print_sentiment()
|
||||
|
||||
@@ -115,6 +119,15 @@ def sen_menu(s_ticker, s_start):
|
||||
elif ns_known_args.cmd == 'mentions':
|
||||
google_api.mentions(l_args, s_ticker, s_start)
|
||||
|
||||
elif ns_known_args.cmd == 'regions':
|
||||
google_api.regions(l_args, s_ticker)
|
||||
|
||||
elif ns_known_args.cmd == 'queries':
|
||||
google_api.queries(l_args, s_ticker)
|
||||
|
||||
elif ns_known_args.cmd == 'rise':
|
||||
google_api.rise(l_args, s_ticker)
|
||||
|
||||
# ------------------------------------------------------------------------------------------------------------
|
||||
else:
|
||||
print("Command not recognized!")
|
||||
Reference in New Issue
Block a user