Implemented parallel async API calls and implemented support for bittrex and poloniex. Added missing files to commit
This commit is contained in:
26
bot/core.py
26
bot/core.py
@@ -1,13 +1,15 @@
|
||||
#!/usr/bin/python
|
||||
from . import helpers
|
||||
from . import market_data_crawler
|
||||
|
||||
from tornado import httpserver
|
||||
from tornado import gen
|
||||
from tornado.ioloop import IOLoop
|
||||
import tornado.web
|
||||
import json
|
||||
import sys
|
||||
|
||||
|
||||
class MainHandler(tornado.web.RequestHandler):
|
||||
@gen.coroutine
|
||||
def post(self):
|
||||
print("POST received from IP {0}".format(self.request.remote_ip))
|
||||
|
||||
@@ -21,10 +23,19 @@ class MainHandler(tornado.web.RequestHandler):
|
||||
|
||||
if "command" in request:
|
||||
print("Command received: {0}".format(request["command"]))
|
||||
response["msg"] = "All good - command received"
|
||||
|
||||
if request["command"] == "init_market_data":
|
||||
yield self.update_market_data(request, 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 delete(self):
|
||||
print("Stopping server...")
|
||||
|
||||
@@ -42,9 +53,9 @@ class Application(tornado.web.Application):
|
||||
tornado.web.Application.__init__(self, handlers)
|
||||
|
||||
|
||||
def main():
|
||||
def main(port):
|
||||
app = Application()
|
||||
app.listen(666)
|
||||
app.listen(port)
|
||||
IOLoop.instance().start()
|
||||
|
||||
|
||||
@@ -53,5 +64,6 @@ def exists():
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
print("Starting arbitrage bot...")
|
||||
main()
|
||||
port = int(sys.argv[1])
|
||||
print("Starting arbitrage bot on port {0}...".format(port))
|
||||
main(port)
|
||||
@@ -1,37 +1,74 @@
|
||||
import tornado.escape, tornado.httpclient
|
||||
#!/usr/bin/python
|
||||
|
||||
import tornado.escape, tornado.httpclient
|
||||
import tornado.httpclient
|
||||
from tornado import gen
|
||||
import time
|
||||
from collections import defaultdict
|
||||
|
||||
|
||||
@gen.coroutine
|
||||
def update_market_data_for_basecoin(basecoin):
|
||||
global market_data
|
||||
market_data = {}
|
||||
market_data = defaultdict(list)
|
||||
market_requests = []
|
||||
|
||||
"""Bittrex"""
|
||||
@gen.coroutine
|
||||
def call_market_data(url, response_handler):
|
||||
http_client = tornado.httpclient.AsyncHTTPClient()
|
||||
response = yield http_client.fetch(url)
|
||||
return response_handler(response)
|
||||
|
||||
"""
|
||||
Bittrex
|
||||
"""
|
||||
def handle_response_bittrex(response):
|
||||
if response.error:
|
||||
print("Error: %s" % response.error)
|
||||
return False
|
||||
|
||||
else:
|
||||
print("Response received from Bittrex - handling now!")
|
||||
response_data = tornado.escape.json_decode(response.body)
|
||||
|
||||
for market in response_data["result"]:
|
||||
base, target = market["MarketName"].split("-")
|
||||
if base == basecoin:
|
||||
market_data.update({
|
||||
target: {
|
||||
"Bittrex": market["Last"]
|
||||
}
|
||||
})
|
||||
market_data[target].append({"Bittrex": market["Last"]})
|
||||
|
||||
return True
|
||||
|
||||
market_requests.append(
|
||||
call_market_data(
|
||||
"https://bittrex.com/api/v1.1/public/getmarketsummaries",
|
||||
handle_response_bittrex))
|
||||
|
||||
"""
|
||||
Poloniex
|
||||
"""
|
||||
def handle_response_poloniex(response):
|
||||
if response.error:
|
||||
print("Error: %s" % response.error)
|
||||
return False
|
||||
|
||||
else:
|
||||
print("Response received from Poloniex - handling now!")
|
||||
response_data = tornado.escape.json_decode(response.body)
|
||||
|
||||
for market in response_data:
|
||||
base, target = market.split("_")
|
||||
if base == basecoin:
|
||||
market_data[target].append({"Poloniex": response_data[market]["last"]})
|
||||
|
||||
|
||||
bittrex = {
|
||||
"url": "https://bittrex.com/api/v1.1/public/getmarketsummaries",
|
||||
"response_handler": handle_response_bittrex
|
||||
}
|
||||
return True
|
||||
|
||||
market_requests.append(bittrex)
|
||||
|
||||
http_client = tornado.httpclient.AsyncHTTPClient()
|
||||
for request in market_requests:
|
||||
print("Doing request to {0}".format(request["url"]))
|
||||
http_client.fetch(request["url"], request["response_handler"])
|
||||
market_requests.append(
|
||||
call_market_data(
|
||||
"https://poloniex.com/public?command=returnTicker",
|
||||
handle_response_poloniex))
|
||||
|
||||
print("--- Retrieve market data now ---")
|
||||
start_time = time.time()
|
||||
response_dict = yield market_requests
|
||||
print("--- Marked data updated in {0} seconds. Responses: {1} ---".format(time.time() - start_time, response_dict))
|
||||
|
||||
Reference in New Issue
Block a user