-
Notifications
You must be signed in to change notification settings - Fork 39
/
twitter_exchanges.py
217 lines (182 loc) · 6.77 KB
/
twitter_exchanges.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import tweepy
import json
import time
import ast
import os
from datetime import datetime
import traceback
from binance_api import *
from stream_multiple import *
from query_multiple import *
# Checks if a tweet from a user contains a particular trigger word
def tweepy_pull(api, users, sell_coin, hold_times, buy_volume, simulate, stream, wait_tweet=True, logfile=None, print_timer=False, full_ex=True, both=False, account_json=None):
# Create exchange object and start querying prices as a daemon (cancels when the main thread ends)
exchange = binance_api(api_keys, logfile=logfile, block=both, account_json=account_json)
exchange_data = exchange_pull(exchange, hold_times, base_coin=sell_coin)
daemon = threading.Thread(name='daemon', target=exchange_data.buy_sell_volumes, args=(volume,20*60))
daemon.setDaemon(True)
daemon.start()
time.sleep(3)
cancel = [False]
# Stream tweets
if not both:
if stream:
# From stream_multiple.py file
while 1:
try:
stream_tweets(api, users, sell_coin, hold_times, buy_volume, simulate, exchange, full_ex=full_ex, exchange_data=exchange_data, cancel=cancel)
except KeyboardInterrupt:
print('\nExiting main thread')
break
except Exception as e:
print('%s Error in streaming - %s' % (datetime.now().strftime('%m/%d %H:%M:%S'), e))
else:
# Query tweets from query.py file
query_tweets(api, users, sell_coin, hold_times, buy_volume, simulate, exchange, print_timer=print_timer, full_ex=full_ex, exchange_data=exchange_data, cancel=cancel)
# Sleeping wait for keyboard interrupt
while 1:
try:
time.sleep(2000)
except KeyboardInterrupt:
print('\nSetting cancel to true')
cancel[0] = True
time.sleep(2)
while threading.active_count() > 2:
time.sleep(1)
print('There are %d trades left to sell' % (threading.active_count() - 2))
print('\nExiting main thread')
exit()
else:
# Start the query as a thread with cancellling mechanism
t1 = threading.Thread(target=query_tweets, args=(api, users, sell_coin, hold_times, buy_volume, simulate, exchange), kwargs={'print_timer':print_timer, 'full_ex':full_ex, 'exchange_data':exchange_data, 'cancel':cancel})
t1.start()
# Stream tweets
while 1:
try:
stream_tweets(api, users, sell_coin, hold_times, buy_volume, simulate, exchange, full_ex=full_ex, exchange_data=exchange_data, cancel=cancel)
except KeyboardInterrupt:
break
except Exception as e:
print('%s Error in streaming - %s' % (datetime.now().strftime('%m/%d %H:%M:%S'), e))
print('\nSetting cancel to true')
cancel[0] = True
while threading.active_count() > 4 + len(users):
print('\nThere are %d trades left to clear' % (threading.active_count() - 4 - len(users)))
time.sleep(20)
print('\nExiting main thread')
exit()
# Loads a json file
def load_json(filepath):
with open(filepath) as json_file:
return json.load(json_file)
def read_twitter_keys(keys):
twitter_keys = {'consumer_key':keys['twitter_keys']['consumer_key'],'consumer_secret':keys['twitter_keys']['consumer_secret'],'access_token_key':keys['twitter_keys']['access_token_key'],'access_token_secret': keys['twitter_keys']['access_token_secret']}
return twitter_keys
# Command line: python twitter_binance_futures.py l (save trade logs) p (print query intervals) 2 (2nd set of twitter keys)
# Load keys, keywords and users
api_keys = load_json('../keys.json')
cryptos = load_json('keywords.json')
twitter_keys = read_twitter_keys(api_keys)
# Get command line user inputs
full_ex = True
if 'prev_trades' in os.listdir():
json_files = list(filter(lambda x : x.endswith('.json') and x not in ['keywords.json','users.json'],os.listdir()))
print('\nChoose accounts to follow: '+'%s ' * len(json_files) % tuple([file+' ('+str(i)+') ' for i, file in enumerate(json_files)]))
if 'query_futures.py' in os.listdir(): full_ex = False
accounts = input()
account_json_str = json_files[int(accounts)]
exchange_keywords = load_json(account_json_str)
else:
account_json_str = 'exchange_keywords.json'
exchange_keywords = load_json(account_json_str)
# Users to track
print('\nUsers: e.g. "coinbase,CoinbasePro,binance" or "all" from: '+'%s '* len(exchange_keywords) % tuple(list(exchange_keywords.keys())))
usernames = input()
skip_input = False
if not usernames:
users = ['ArbitrageDaddy']
skip_input = True
elif usernames == 'all':
users = list(filter(lambda x : x not in ['ArbitrageDaddy', 'elonmusk'],[i for i in exchange_keywords.keys()]))
else:
users = usernames.split(',')
print(users)
users = {key:exchange_keywords[key] for key in users}
# Sell currency
print('\nEnter currency to sell: btc, usdt')
sell_coin = 'BTC'
if not skip_input:
sell_input = input()
if not sell_input:
sell_coin = 'BTC'
skip_input = True
else:
sell_coin = cryptos[sell_input]['symbol']
skip_input = False
# Time after buying before selling
hold_time = [5]
if not skip_input:
print('\nHodl time(s) seconds e.g. 200 or 30,60,90: ')
hold_time = input()
if not hold_time:
hold_time = [30,60,90]
skip_input = True
else:
hold_time = ast.literal_eval('['+hold_time+']')
print(hold_time)
print('\nHodl time : '+'%.2fs '*len(hold_time) % tuple(hold_time))
# Amount in USD to buy
if not skip_input:
print('\nVolume in USD: ')
volume = input()
if not volume:
volume = 20
else:
volume = float(volume)
else:
volume = 20
print('\nVolume %.2f USD' % (volume))
# Simulation trade or real trade
simulate = True
if not skip_input:
print('\nTest y/n:')
test = input()
simulate = True
if test == 'n': simulate = False
# User to track, empty to skip tweet waiting
stream, both = True, False
if not skip_input:
print('\nStream or query s/q: ')
stream_input = input()
if stream_input == 'b':
both = True
elif stream_input != 'q':
stream = True
else:
stream = False
# Alternating use of twitter api keys
if '2' in sys.argv:
api_keys_2 = load_json('../twitter_keys2.json')
print('\nUsing twitter keys 2')
twitter_keys = read_twitter_keys(api_keys_2)
elif '3' in sys.argv:
api_keys_3 = load_json('../twitter_keys3.json')
print('\nUsing twitter keys 3')
twitter_keys = read_twitter_keys(api_keys_3)
if simulate:
print('\n'+'-'*10+' SIMULATION TRADING '+'-'*10+'\n')
else:
print('\n'+'-'*10+' LIVE TRADING '+'-'*10+'\n')
# Inintilizing a file of jsons to log trades
logfile = False
if 'l' in sys.argv:
logfile = True
print_timer = False
if 'p' in sys.argv:
print_timer = True
# Use twitter API
auth = tweepy.OAuthHandler(twitter_keys['consumer_key'], twitter_keys['consumer_secret'])
auth.set_access_token(twitter_keys['access_token_key'], twitter_keys['access_token_secret'])
api = tweepy.API(auth)
# Execute function
tweepy_pull(api, users, sell_coin, hold_time, volume, simulate, stream, wait_tweet=not skip_input, logfile=logfile, full_ex=full_ex, print_timer=print_timer, both=both, account_json=account_json_str[:-5])