Analyzer now finds spread, perc and prints a sorted list; core is now able to start and stop the bot
This commit is contained in:
49
bot/core.py
49
bot/core.py
@@ -1,5 +1,5 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
from . import market_data_crawler, market_data_analyzer
|
from . import market_data_crawler, market_data_analyzer, shared_config
|
||||||
|
|
||||||
from tornado import gen
|
from tornado import gen
|
||||||
from tornado.ioloop import IOLoop
|
from tornado.ioloop import IOLoop
|
||||||
@@ -7,7 +7,6 @@ import tornado.web
|
|||||||
import json
|
import json
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class MainHandler(tornado.web.RequestHandler):
|
class MainHandler(tornado.web.RequestHandler):
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def post(self):
|
def post(self):
|
||||||
@@ -16,7 +15,7 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
response = {'error': False, 'msg': "None"}
|
response = {'error': False, 'msg': "None"}
|
||||||
request = json.loads(self.request.body.decode('utf-8'))
|
request = json.loads(self.request.body.decode('utf-8'))
|
||||||
|
|
||||||
if "token" not in request or request["token"] != "abc":
|
if "token" not in request or request["token"] != "den":
|
||||||
response["msg"] = "Wrong token - no access granted"
|
response["msg"] = "Wrong token - no access granted"
|
||||||
self.write(json.dumps(response))
|
self.write(json.dumps(response))
|
||||||
return
|
return
|
||||||
@@ -24,32 +23,15 @@ class MainHandler(tornado.web.RequestHandler):
|
|||||||
if "command" in request:
|
if "command" in request:
|
||||||
print("Command received: {0}".format(request["command"]))
|
print("Command received: {0}".format(request["command"]))
|
||||||
|
|
||||||
if request["command"] == "init_market_data":
|
if request["command"] == "start_bot":
|
||||||
yield self.update_market_data(request, response)
|
shared_config.run_bot = True
|
||||||
elif request["command"] == "get_market_data":
|
elif request["command"] == "stop_bot":
|
||||||
yield self.get_market_data(request, response)
|
shared_config.run_bot = False
|
||||||
elif request["command"] == "calc_arbitr_opport":
|
else:
|
||||||
yield self.update_market_data(request, response)
|
response["msg"] = "Unknown command"
|
||||||
yield self.get_arbitrage_opportunities(request, response)
|
|
||||||
|
|
||||||
self.write(json.dumps(response))
|
self.write(json.dumps(response))
|
||||||
|
|
||||||
@gen.coroutine
|
|
||||||
def update_market_data(self, request, response):
|
|
||||||
yield market_data_crawler.update_market_data_for_basecoin(request["basecoin"])
|
|
||||||
response["msg"] = "Market Data initialized"
|
|
||||||
response["data"] = market_data_crawler.market_data
|
|
||||||
|
|
||||||
@gen.coroutine
|
|
||||||
def get_market_data(self, request, response):
|
|
||||||
response["msg"] = "Market Data Retrieved"
|
|
||||||
response["data"] = market_data_crawler.market_data
|
|
||||||
|
|
||||||
@gen.coroutine
|
|
||||||
def get_arbitrage_opportunities(self, request, response):
|
|
||||||
response["msg"] = "Arbitrage Oportunities Retrieved"
|
|
||||||
response["data"] = market_data_analyzer.calculate_arbitrage_opportunities(request["basecoin"])
|
|
||||||
|
|
||||||
@gen.coroutine
|
@gen.coroutine
|
||||||
def delete(self):
|
def delete(self):
|
||||||
print("Stopping server...")
|
print("Stopping server...")
|
||||||
@@ -68,17 +50,22 @@ class Application(tornado.web.Application):
|
|||||||
tornado.web.Application.__init__(self, handlers)
|
tornado.web.Application.__init__(self, handlers)
|
||||||
|
|
||||||
|
|
||||||
|
@gen.coroutine
|
||||||
|
def run_bot():
|
||||||
|
while True:
|
||||||
|
yield gen.sleep(30)
|
||||||
|
if shared_config.run_bot:
|
||||||
|
market_data_analyzer.calculate_arbitrage_opportunities(['kraken', 'bitfinex', 'binance', 'hitbtc', 'gdax', 'bittrex', 'poloniex'])
|
||||||
|
|
||||||
|
|
||||||
def main(port):
|
def main(port):
|
||||||
app = Application()
|
app = Application()
|
||||||
app.listen(port)
|
app.listen(port)
|
||||||
|
run_bot()
|
||||||
IOLoop.instance().start()
|
IOLoop.instance().start()
|
||||||
|
|
||||||
|
|
||||||
def exists():
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
port = int(sys.argv[1])
|
port = int(sys.argv[1])
|
||||||
print("Starting arbitrage bot on port {0}...".format(port))
|
print("Starting arbitrage bot on port {0}...".format(port))
|
||||||
main(port)
|
main(port)
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ import sys
|
|||||||
import operator
|
import operator
|
||||||
import pprint
|
import pprint
|
||||||
|
|
||||||
|
|
||||||
def calculate_arbitrage_opportunities(exchanges):
|
def calculate_arbitrage_opportunities(exchanges):
|
||||||
|
start_time = time.time()
|
||||||
market_data = market_data_crawler.update_market_data_for_symbol_and_exchange(exchanges)
|
market_data = market_data_crawler.update_market_data_for_symbol_and_exchange(exchanges)
|
||||||
sorted_market_data = {}
|
sorted_market_data = {}
|
||||||
|
|
||||||
@@ -32,11 +34,6 @@ def calculate_arbitrage_opportunities(exchanges):
|
|||||||
highest_bid = None
|
highest_bid = None
|
||||||
market_opport.update({symbol: {}})
|
market_opport.update({symbol: {}})
|
||||||
for exchange_name, order_book in exchanges.items():
|
for exchange_name, order_book in exchanges.items():
|
||||||
|
|
||||||
"""""""""
|
|
||||||
TODO: This is wrong - you have to calculate the biggest spread!!!
|
|
||||||
"""""""""
|
|
||||||
|
|
||||||
if lowest_ask is None or lowest_ask['value'] < order_book['asks'][0]:
|
if lowest_ask is None or lowest_ask['value'] < order_book['asks'][0]:
|
||||||
lowest_ask = {"exchange_name":exchange_name,
|
lowest_ask = {"exchange_name":exchange_name,
|
||||||
"value":order_book['asks'][0],
|
"value":order_book['asks'][0],
|
||||||
@@ -51,41 +48,23 @@ def calculate_arbitrage_opportunities(exchanges):
|
|||||||
|
|
||||||
market_opport[symbol].update({"highest_bid": highest_bid,
|
market_opport[symbol].update({"highest_bid": highest_bid,
|
||||||
"lowest_ask": lowest_ask,
|
"lowest_ask": lowest_ask,
|
||||||
"spread": spread})
|
"spread": spread,
|
||||||
|
"spread_perc": round((spread / float(highest_bid['value'][0])) * 100, 2),
|
||||||
|
"symbol": symbol})
|
||||||
|
|
||||||
|
if spread > 0:
|
||||||
|
with open("market_opportunity_found.txt", "a") as file:
|
||||||
|
file.write("\n+n--- Arbitrage oppportunity found! ---\n\n")
|
||||||
|
pprint.pprint(market_opport[symbol], stream=file)
|
||||||
|
|
||||||
|
sorted_list = sorted(market_opport.values(), key=operator.itemgetter("spread_perc"), reverse=True)
|
||||||
|
|
||||||
with open("market_analyzation.txt", "w") as file:
|
with open("market_analyzation.txt", "w") as file:
|
||||||
pprint.pprint(market_opport, stream=file)
|
pprint.pprint(sorted_list, stream=file)
|
||||||
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
start_time = time.time()
|
|
||||||
|
|
||||||
for pair in market_data:
|
|
||||||
if len(market_data[pair]) > 1:
|
|
||||||
price_list = {}
|
|
||||||
|
|
||||||
for price in market_data[pair]:
|
|
||||||
key, value = price.popitem()
|
|
||||||
price_list[key] = float(value)
|
|
||||||
|
|
||||||
sorted_x = sorted(price_list.items(), key=operator.itemgetter(1), reverse=True)
|
|
||||||
spread = sorted_x[0][1] - sorted_x[-1][1]
|
|
||||||
spread_perc = round((spread / sorted_x[0][1]) * 100, 2)
|
|
||||||
|
|
||||||
market_opport[pair] = {"highest_market": sorted_x[0][0],
|
|
||||||
"highest_price": sorted_x[0][1],
|
|
||||||
"lowest_market": sorted_x[-1][0],
|
|
||||||
"lowest_price": sorted_x[-1][1],
|
|
||||||
"spread": spread,
|
|
||||||
"spread_perc": spread_perc,
|
|
||||||
"pair": pair}
|
|
||||||
|
|
||||||
market_opport = sorted(market_opport.values(), key=operator.itemgetter("spread_perc"), reverse=True)
|
|
||||||
|
|
||||||
print("--- Arbitrage oportunities calculated in {0:.3f}ms ---".format((time.time() - start_time)*100))
|
print("--- Arbitrage oportunities calculated in {0:.3f}ms ---".format((time.time() - start_time)*100))
|
||||||
|
|
||||||
return market_opport
|
return market_opport
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
calculate_arbitrage_opportunities(sys.argv[1:])
|
calculate_arbitrage_opportunities(sys.argv[1:])
|
||||||
|
|||||||
@@ -40,3 +40,5 @@ proxies = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
basecoin = "ETH"
|
basecoin = "ETH"
|
||||||
|
|
||||||
|
run_bot = False
|
||||||
Reference in New Issue
Block a user