-
Notifications
You must be signed in to change notification settings - Fork 41
/
twitter_binance.py
156 lines (129 loc) · 4.39 KB
/
twitter_binance.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
import tweepy
import json
import time
from datetime import datetime
from binance_api import *
from stream_multiple import *
from query import *
import ast
import traceback
# Checks if a tweet from a user contains a particular trigger word
def tweepy_pull(api, user, pair, crypto, hold_time, volume, simulate, stream, wait_tweet=True, logfile=None, print_timer=False, full_ex=True):
exchange = binance_api(api_keys, logfile=logfile)
# Stream tweets
if stream:
while 1:
# Users specified with keywords
users = {user[0]:{'id':str(user[1]),'keywords':crypto['triggers']}}
# From steam_multiple.py file
try:
stream_tweets(api, users, pair[1].upper(), hold_time, volume, simulate, exchange, keywords=crypto['triggers'], buy_coin=pair[0].upper(), full_ex=full_ex)
except Exception as e:
print(e)
print('%s\n'%(datetime.now().strftime('%b %d - %H:%M:%S')))
# Query tweets
else:
# From query.py file
try:
query_tweets(api, exchange, user, pair, crypto, hold_time, volume, simulate, wait_tweet, print_timer, full_ex=full_ex)
except Exception as e:
print(traceback.print_exc())
print(e)
print('%s\n'%(datetime.now().strftime('%b %d - %H:%M:%S')))
# Loads a json file
def load_json(filepath):
with open(filepath) as json_file:
return json.load(json_file)
# Command line: python twitter_binance_futures.py l (save trade logs) p (print query intervals)
# Load keys, keywords and users
api_keys = load_json('../keys.json')
users = load_json('users.json')
cryptos = load_json('keywords.json')
if 'prev_trades' in os.listdir():
full_ex = False
else:
full_ex = True
twitter_keys = {'consumer_key':api_keys['twitter_keys']['consumer_key'],'consumer_secret':api_keys['twitter_keys']['consumer_secret'],'access_token_key':api_keys['twitter_keys']['access_token_key'],'access_token_secret': api_keys['twitter_keys']['access_token_secret']}
# Use second group of twitter api keys
if '2' in sys.argv:
api_keys2 = load_json('../twitter_keys2.json')
twitter_keys = {'consumer_key':api_keys2['twitter_keys']['consumer_key'],'consumer_secret':api_keys2['twitter_keys']['consumer_secret'],'access_token_key':api_keys2['twitter_keys']['access_token_key'],'access_token_secret': api_keys2['twitter_keys']['access_token_secret']}
# Get user inputs
print('\nEnter crypto to buy: '+'%s '* len(cryptos) % tuple(cryptos.keys()))
skip_input = False
# Buy currency
crypto = input()
if not crypto:
crypto = 'doge'
skip_input = True
buy_coin = cryptos[crypto]
# Sell currency
if not skip_input:
print('\nEnter currency to sell: '+'%s '* len(cryptos) % tuple(cryptos.keys()))
sell_coin = cryptos[input()]
else:
sell_coin = cryptos['btc']
pair = [buy_coin['symbol'], sell_coin['symbol']]
# User to track
if not skip_input:
print('\nUser: '+'%s '* len(users) % tuple(users.keys()))
username = input()
if username:
user = users[username]
else:
user = users['me']
skip_input = True
else:
user = users['me']
# Time after buying before selling
hold_time = [1]
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]
hold_time = ast.literal_eval('['+hold_time+']')
print(hold_time)
print('\nHodl time: '+'%.2fs '*len(hold_time) % tuple(hold_time))
# Amount of crypto to buy (Fastest if fewest queries before buying)
if not skip_input:
print('\nAmount to buy in $: ')
volume = input()
if not volume:
if crypto == 'doge':
volume = 100
elif crypto == 'btc':
volume = 0.0002
else:
volume = float(volume)
else:
volume = 50
print('\nVolume $%.8f' % (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 = True
if not skip_input:
print('\nStream or query s/q: ')
stream_input = input()
if stream_input != 'q':
stream = True
else:
stream = False
if simulate:
print('\nSIMULATION TRADING')
# Inintilizing a file of jsons to log trades
logfile = False
if 'l' in sys.argv:
logfile = 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, user, pair, buy_coin, hold_time, volume, simulate, stream, wait_tweet=not skip_input, logfile=logfile, full_ex=full_ex)