-
Notifications
You must be signed in to change notification settings - Fork 11
/
gekkoBots.py
263 lines (243 loc) · 12.1 KB
/
gekkoBots.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import json, codecs
import requests, urllib3
import time
import click
from short_utm import UniversalTableMethods as utm
#when running self-signed SSL certs on a VPS, ignore warnings in console:
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
class gekkoServer(object):
def __init__(self, server):
self.get_server_info(server)
self.gekkos = self.get_gekkos()
self.scansets = []
def get_scansets(self):
response = self.api_calls(type='post', call='scansets')
if response.status_code == 200:
data = response.json()['datasets']
return data
#NOT WORKING AT ALL
def start_imports_for_gaps(self):
self.scansets = self.get_scansets()
for d in self.scansets:
print(d['asset'])
print(d)
from_values = [r['from'] for r in d['ranges']]
to_values = [r['to'] for r in d['ranges']]
print('from values: ', from_values)
print('to values: ', to_values)
print('Max: ', max([r['to'] for r in d['ranges']]))
def import_data(self, exchange, currency, asset, from_date, to_date):
conf = {"watch":{
"exchange":exchange,
"currency":currency,
"asset":asset},
"importer":{
"daterange":{
"from": from_date, #"2018-03-19 04:35"
"to": to_date }}, #"2018-04-05 04:38"
"candleWriter":{"enabled":True}}
response = self.api_calls(type='post', call='import', data=conf)
def start_from_file(self, file_name):
with open(file_name, 'r') as f:
d = json.load(f)
f.close()
#start all watchers first
[self.start_watcher(x['exchange'], x['currency'], x['asset']) for x in d if x['type'] == 'watcher']
#start all tradebots now
[self.start_trader(x['exchange'], x['currency'], x['asset'], x['candleSize'], x['historySize'], x['strat_name'], x['strat_settings']) for x in d if x['type'] == 'tradebot']
#start all paper traders
[self.start_papertrader(x['exchange'], x['currency'], x['asset'], x['candleSize'], x['historySize'], x['strat_name'], x['strat_settings']) for x in d if x['type'] == 'paper trader']
def save_gekkos(self, gekko_list_id):
watchers = [
{'type': w['type'], 'exchange': w['watch']['exchange'], 'asset': w['watch']['asset'], 'currency': w['watch']['currency']} for w in self.gekkos if w['type'] == 'watcher']
tradebots = [
{'type': w['trader'], 'exchange': w['watch']['exchange'], 'asset': w['watch']['asset'], 'currency': w['watch']['currency'],
'strat_name': w['strat']['name'], 'candleSize': w['strat']['tradingAdvisor']['candleSize'], 'historySize': w['strat']['tradingAdvisor']['historySize'],
'strat_settings': w['strat']['params']} for w in self.gekkos if w['type'] == 'leech' and w['trader'] == 'tradebot']
papertraders = [
{'type': w['paper trader'], 'exchange': w['watch']['exchange'], 'asset': w['watch']['asset'], 'currency': w['watch']['currency'],
'strat_name': w['strat']['name'], 'candleSize': w['strat']['tradingAdvisor']['candleSize'], 'historySize': w['strat']['tradingAdvisor']['historySize'],
'strat_settings': w['strat']['params']} for w in self.gekkos if w['type'] == 'leech' and w['trader'] == 'paper trader']
# ****** ADD PAPER TRADERS HERE TO SAVE
watchers.extend(tradebots)
watchers.extend(papertraders)
with open('saved_bots-' + gekko_list_id + '.txt', 'wb') as f:
json.dump(watchers, codecs.getwriter('utf-8')(f), sort_keys = True, indent = 4, ensure_ascii=False)
f.close()
return 'saved_bots-' + gekko_list_id + '.txt'
def get_server_info(self, server):
# ########################
# server_credentials_api.json file structure:
# (for protecting your website with login credentials)
# [{
# "server_addr" : "https://123.456.789/api/",
# "username" : "myuser",
# "password" : "mypassword"
# }]
############################
if server == 'VPS':
data = json.load(open('server_credentials_api.json', 'r'))
self.api_url = data[0]['server_addr']
self.u_name = data[0]['username']
self.p_word = data[0]['password']
elif server == 'localhost':
self.api_url = 'http://localhost:3000/api/'
def api_calls(self, type='get', call='gekkos', data={}):
data = json.dumps(data)
header = {
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json'
}
if type == 'get':
if 'localhost' in self.api_url:
response = requests.get(self.api_url + call, data=data, headers=header)
else:
response = requests.get(self.api_url + call, data=data, headers=header, verify=False, auth=(self.u_name, self.p_word))
elif type == 'post':
if 'localhost' in self.api_url:
response = requests.post(self.api_url + call, data=data, headers=header)
#print(response.request.body)
else:
response = requests.post(self.api_url + call, data=data, headers=header, verify=False, auth=(self.u_name, self.p_word))
return response
def get_gekkos(self):
response = self.api_calls('get', 'gekkos')
if response.status_code == 200:
data = response.json()
return data
def kill_gekko(self, id_to_kill):
gekko_ids = [d['id'] for d in self.gekkos]
def killer(x):
if x in gekko_ids:
response = self.api_calls('post','killGekko', {'id':x})
if response.status_code == 200:
print('************* ', x, ' Gekko killed', response.json())
else:
print('************* kill failed', response)
else:
print('******* ' + id_to_kill, ' not in list of gekko ids ' , gekko_ids , '*******' )
if id_to_kill == "all":
[killer(d['id']) for d in self.gekkos]
else:
killer(id_to_kill)
self.gekkos = self.get_gekkos()
def start_all(self):
self.scansets = self.get_scansets()
[self.start_watcher(i['exchange'], i['currency'], i['asset']) for i in self.scansets]
def start_papertrader(self, exchange, currency, asset, candleSize, historySize, strat_name, strat_settings):
#create conf file
conf= {"market":{"type":"leech"},
"mode":"realtime",
"watch":{
"exchange":exchange,
"currency":currency,
"asset":asset
},
"tradingAdvisor":{
"enabled":True,
"method":strat_name,
"candleSize":candleSize,
"historySize":historySize
},
strat_name: strat_settings,
"candleWriter":{"enabled":True,"adapter":"sqlite"},
"type":"paper trader",
"performanceAnalyzer":{"riskFreeReturn":2,"enabled":True},
"paperTrader":{"feeMaker":0.05,"feeTaker":0.05,"feeUsing":"maker","slippage":0,"simulationBalance":{"asset":0,"currency":1},"reportRoundtrips":True,"enabled":True}}
#check if watcher already exists:
# flattened_gekkos = [utm.flatten_json(x) for x in self.gekkos]
# matching_leeches = [w for w in flattened_gekkos if w['type'] == 'leech' and w['watch.exchange'] == exchange and w['watch.currency'] == currency and w['watch.asset'] == asset]
# if matching_leeches:
# utm.print_lod('Paper trader or trader already active: ', utm.filter_lod_keys(['trader', 'type', 'watch.exchange', 'watch.currency', 'watch.asset', 'id'],matching_leeches))
# else:
self.start_config(conf)
def start_watcher(self, exchange, currency, asset):
conf = {
"type":"market watcher",
"mode":"realtime",
"watch":{
"exchange":exchange,
"currency":currency,
"asset":asset
},
"candleWriter":{"enabled":True, "adapter":"sqlite"}
}
matching_watchers=False
if self.gekkos:
flattened_gekkos = [utm.flatten_json(x) for x in self.gekkos]
matching_watchers = [w for w in flattened_gekkos if w['type'] == 'watcher' and w['watch.exchange'] == exchange and w['watch.currency'] == currency and w['watch.asset'] == asset]
if matching_watchers:
utm.print_lod('watcher already active: ', utm.filter_lod_keys(['trader', 'type', 'watch.exchange', 'watch.currency', 'watch.asset', 'id'],matching_watchers))
else:
self.start_config(conf)
def start_trader(self, exchange, currency, asset, candleSize, historySize, strat_name, strat_settings):
#create conf file
conf= {"market":{"type":"leech"},
"mode":"realtime",
"watch":{
"exchange":exchange,
"currency":currency,
"asset":asset
},
"tradingAdvisor":{
"enabled":True,
"method":strat_name,
"candleSize":candleSize,
"historySize":historySize
},
strat_name: strat_settings,
"candleWriter":{"enabled":True,"adapter":"sqlite"},
"type":"tradebot",
"performanceAnalyzer":{"riskFreeReturn":2,"enabled":True},
"trader":{"enabled":True},"valid":True}
#check if trader already exists:
flattened_gekkos = [utm.flatten_json(x) for x in self.gekkos]
matching_leeches = [w for w in flattened_gekkos if w['type'] == 'leech' and w['watch.exchange'] == exchange and w['watch.currency'] == currency and w['watch.asset'] == asset]
if matching_leeches:
utm.print_lod('Trader or paper trader already active: ', utm.filter_lod_keys(['trader', 'type', 'watch.exchange', 'watch.currency', 'watch.asset', 'id'],matching_leeches))
else:
self.start_config(conf)
def start_config(self, config):
header = {
'Accept-Encoding': 'gzip, deflate, br',
'Content-Type': 'application/json'
}
try:
self.api_calls(type='post', call='startGekko', data=config)
time.sleep(2)
self.gekkos = self.get_gekkos()
except Exception as e:
print('EXCEPTION:', e)
@click.command()
@click.option('--server', help='"localhost" or "VPS"')
@click.option('--kill', help='id of gekko to kill, or "all"')
@click.option('--start', help='"filename.txt" starts all saved gekkos in file. "all" starts watchers for imports ("all" NOT WORKING).')
@click.option('--save', help='Save current gekkos for later. Specify gekko file identifier to distinguish from other saves.')
@click.option('--import_all', help='set to "true" to import all missing data in databases')
def init_Gekko(server='localhost' ,kill=False, start=False, save=False, import_all=False):
if server == 'VPS' or server == 'localhost':
srv = gekkoServer(server)
else:
srv = gekkoServer('localhost')
if kill:
srv.kill_gekko(kill)
if import_all:
srv.start_imports_for_gaps()
if start:
srv.scansets = srv.get_scansets()
if start == 'all':
srv.start_all()
else:
srv.start_from_file(start)
#print gekko info:
if srv.gekkos and len(srv.gekkos) > 0:
gekkos = [utm.flatten_json(x) for x in srv.gekkos]
gekkos = utm.filter_lod_keys(['trader', 'type', 'watch.exchange', 'watch.currency', 'watch.asset', 'id', 'startAt'], gekkos)
gekkos = utm.fill_in_missing_keys_in_lod(gekkos)
utm.print_lod( "****************** RUNNING GEKKOS *******************", gekkos)
if save:
print('GEKKOS SAVED TO: ', srv.save_gekkos(save))
else:
print('****************** NO GEKKOS RUNNING *******************')
if __name__ == '__main__':
init_Gekko()