forked from OpenBB-finance/OpenBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocktwits_api.py
138 lines (110 loc) · 5.68 KB
/
stocktwits_api.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import argparse
import requests
import pandas as pd
from helper_funcs import *
# -------------------------------------------------------------------------------------------------------------------
def bullbear(l_args, s_ticker):
parser = argparse.ArgumentParser(prog='bullbear',
description="""Print bullbear sentiment based on last 30 messages on the board.
Also prints the watchlist_count. [Source: Stocktwits]""")
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", type=str, default=s_ticker,
help='ticker to gather sentiment from.')
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("")
# -------------------------------------------------------------------------------------------------------------------
def messages(l_args, s_ticker):
parser = argparse.ArgumentParser(prog='messages',
description="""Print up to 30 of the last messages on the board. [Source: Stocktwits]""")
parser.add_argument('-t', "--ticker", action="store", dest="s_ticker", type=str, default=s_ticker,
help='get board messages from this ticker.')
parser.add_argument('-l', "--limit", action="store", dest="n_lim", type=check_positive, default=30,
help='limit messages shown.')
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:
for idx, message in enumerate(result.json()['messages']):
print("------------------------------------------------------------------------------------------")
print(message['body'])
if idx > ns_parser.n_lim-1:
break
else:
print("Invalid symbol")
print("")
except:
print("")
# -------------------------------------------------------------------------------------------------------------------
def trending(l_args):
parser = argparse.ArgumentParser(prog='trending',
description="""Stocks trending. [Source: Stocktwits]""")
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/trending/symbols.json")
if result.status_code == 200:
l_symbols = list()
for symbol in result.json()['symbols']:
l_symbols.append([symbol['symbol'], symbol['watchlist_count'], symbol['title']])
pd.set_option('display.max_colwidth', -1)
df_trending = pd.DataFrame(l_symbols, columns=['Ticker', 'Watchlist Count', 'Name'])
print(df_trending.to_string(index=False))
else:
print("Error!")
print("")
except:
print("")
# -------------------------------------------------------------------------------------------------------------------
def stalker(l_args):
parser = argparse.ArgumentParser(prog='stalker',
description="""Print up to the last 30 messages of a user. [Source: Stocktwits]""")
parser.add_argument('-u', "--user", action="store", dest="s_user", type=str, default='Newsfilter',
help='username.')
parser.add_argument('-l', "--limit", action="store", dest="n_lim", type=check_positive, default=30,
help='limit messages shown.')
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/user/{ns_parser.s_user}.json")
if result.status_code == 200:
for idx, message in enumerate(result.json()['messages']):
print("------------------------------------------------------------------------------------------")
print(message['created_at'].replace('T', ' ').replace('Z', ''))
print(message['body'])
print("")
if idx > ns_parser.n_lim-1:
break
else:
print("Invalid user")
print("")
except:
print("")