mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-28 20:23:36 +00:00
Add sentiment command from stocktwits
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import config_bot as cfg
|
||||
import argparse
|
||||
from sentiment import reddit_api
|
||||
from sentiment import stocktwits_api
|
||||
|
||||
# -----------------------------------------------------------------------------------------------------------------------
|
||||
def print_sentiment():
|
||||
@@ -18,16 +19,21 @@ def print_sentiment():
|
||||
print(" spac show other users spacs announcements")
|
||||
print(" spac_c show other users spacs announcements from subreddit SPACs")
|
||||
print("")
|
||||
print("Stocktwits:")
|
||||
print(" sentiment estimate quick sentiment from last 30 messages on board")
|
||||
print("")
|
||||
|
||||
return
|
||||
|
||||
|
||||
# ---------------------------------------------------- MENU ----------------------------------------------------
|
||||
def sen_menu():
|
||||
def sen_menu(s_ticker):
|
||||
|
||||
# Add list of arguments that the discovery parser accepts
|
||||
sen_parser = argparse.ArgumentParser(prog='discovery', add_help=False)
|
||||
sen_parser.add_argument('cmd', choices=['help', 'q', 'quit',
|
||||
'watchlist', 'spac', 'spac_c', 'wsb', 'popular'])
|
||||
'watchlist', 'spac', 'spac_c', 'wsb', 'popular',
|
||||
'sentiment'])
|
||||
|
||||
print_sentiment()
|
||||
|
||||
@@ -71,6 +77,10 @@ def sen_menu():
|
||||
elif ns_known_args.cmd == 'popular':
|
||||
reddit_api.popular_tickers(l_args)
|
||||
|
||||
# ---------------------------------------------------- STOCKTWITS ---------------------------------------------------
|
||||
elif ns_known_args.cmd == 'sentiment':
|
||||
stocktwits_api.sentiment(l_args, s_ticker)
|
||||
|
||||
# ------------------------------------------------------------------------------------------------------------
|
||||
else:
|
||||
print("Command not recognized!")
|
||||
43
sentiment/stocktwits_api.py
Normal file
43
sentiment/stocktwits_api.py
Normal file
@@ -0,0 +1,43 @@
|
||||
import argparse
|
||||
import requests
|
||||
import pandas as pd
|
||||
|
||||
# -------------------------------------------------------------------------------------------------------------------
|
||||
def sentiment(l_args, s_ticker):
|
||||
parser = argparse.ArgumentParser(prog='sentiment',
|
||||
description="""Gather a stock sentiment based on last 30 messages on the board.
|
||||
Also prints the watchlist_count [stocktwits] """)
|
||||
|
||||
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", type=str, default=s_ticker, help='Ticker to gather sentiment')
|
||||
|
||||
try:
|
||||
(ns_parser, l_unknown_args) = parser.parse_known_args(l_args)
|
||||
|
||||
if l_unknown_args:
|
||||
print(f"The following args couldn't be interpreted: {l_unknown_args}\n")
|
||||
return
|
||||
|
||||
result = requests.get(f"https://api.stocktwits.com/api/2/streams/symbol/{ns_parser.s_ticker}.json")
|
||||
if result.status_code == 200:
|
||||
print(f"Watchlist count: {result.json()['symbol']['watchlist_count']}")
|
||||
n_cases = 0
|
||||
n_bull = 0
|
||||
n_bear = 0
|
||||
for message in result.json()['messages']:
|
||||
if (message['entities']['sentiment']):
|
||||
n_cases += 1
|
||||
n_bull += (message['entities']['sentiment']['basic'] == 'Bullish')
|
||||
n_bear += (message['entities']['sentiment']['basic'] == 'Bearish')
|
||||
|
||||
if n_cases > 0:
|
||||
print(f"\nOver {n_cases} sentiment messages:")
|
||||
print(f"Bullish {round(100*n_bull/n_cases, 2)}%")
|
||||
print(f"Bearish {round(100*n_bear/n_cases, 2)}%")
|
||||
else:
|
||||
print("Invalid symbol")
|
||||
|
||||
print("")
|
||||
|
||||
except:
|
||||
print("")
|
||||
|
||||
Reference in New Issue
Block a user