-
Notifications
You must be signed in to change notification settings - Fork 41
/
twitter_kraken.py
139 lines (116 loc) · 3.9 KB
/
twitter_kraken.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
import tweepy
import json
import time
from datetime import datetime
from kraken_api import *
from stream import *
from query import *
import ast
# 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):
exchange = kraken_api(api_keys, logfile=logfile)
# Stream tweets
if stream:
while 1:
user_ids = [str(user[1])]
try:
stream_tweets(api, user_ids, set(user_ids), pair, hold_time, volume, simulate, exchange, keywords=crypto['triggers'])
except Exception as e:
print(e)
print('%s\n'%(datetime.now().strftime('%b %d - %H:%M:%S')))
# Query tweets
else:
twitter_q = Twitter_Query(api, exchange)
twitter_q.query(user, pair, crypto, hold_time, volume, simulate, wait_tweet, print_timer)
# Loads a json file
def load_json(filepath):
with open(filepath) as json_file:
return json.load(json_file)
# Load keys, keywords and users
api_keys = load_json('../keys.json')
users = load_json('users.json')
cryptos = load_json('keywords.json')
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('\nVolume in crypto: ')
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 %s' % (volume, buy_coin['symbol']))
# 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)