Skip to content

Commit

Permalink
finally a simple arbitrage monitor finished
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiaoguo committed Jan 14, 2018
1 parent dfcb4cc commit 609c696
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 3 additions & 0 deletions bbot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# coding=utf8
from __future__ import print_function

from market_data.market_price_triangle_diff import start_market_subscribe

if __name__ == '__main__':
print('Hello world')
start_market_subscribe("BNB")
19 changes: 13 additions & 6 deletions market_data/market_price_triangle_diff.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
# coding=utf8
import csv
import json
import ssl
import traceback

import websocket

from common import config
from signals.triangle_price_diff import Triangle

t = Triangle('BTC')
t = Triangle()


def on_message(ws, message):
ticker_list = json.loads(message)
t.create_signal(ticker_list)
try:
print('received: ' + message)
ticker_list = json.loads(message)
t.create_signal(ticker_list)
except ValueError as e:
traceback.print_exc()


def on_error(ws, error):
Expand All @@ -28,15 +32,18 @@ def on_open(ws):
print("web socket open")


def start_market_subscribe():
def start_market_subscribe(symbol):
t.prepare(symbol)
websocket.enableTrace(True)
ws = websocket.WebSocketApp(config.bbot_config['wss_endpoint']+'/ws/!ticker@arr',

ws = websocket.WebSocketApp(config.bbot_config['wss_endpoint'] + '/ws/!ticker@arr',
on_message=on_message,
on_error=on_error,
on_close=on_close,
on_open=on_open)
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})



if __name__ == '__main__':
start_market_subscribe()
17 changes: 12 additions & 5 deletions signals/triangle_price_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ def get_trade_pairs():


class Triangle:
def __init__(self, symbol=None):
def __init__(self):
self.trade_pairs = get_trade_pairs()
self.trade_triangles = self.prepare(symbol)
self.price_map = dict()
self.max_diff = [0, None]
self.trade_triangles = list()

def _get_trade_triangle(self, symbol_a, symbol_b, symbol_c):
return [self.trade_pairs[(symbol_a, symbol_b)],
Expand All @@ -46,7 +47,7 @@ def prepare(self, symbol):
t_t.append(self._get_trade_triangle(symbol, a, b))
t_t.append(self._get_trade_triangle(symbol, b, a))
print(t_t)
return t_t
self.trade_triangles = t_t

def update_price_map(self, ticker_price_list):
for ticker_price in ticker_price_list:
Expand All @@ -64,9 +65,15 @@ def calc(self):
price3 = self._get_best_price(triangle[2][0], triangle[2][1])
if price1 and price2 and price3:
results.append(self._calc_diff([price1, price2, price3]))
results.sort(key=lambda x: x[0])
results = filter(lambda r: r[0] > 0, results)
results.sort(key=lambda x: x[0], reverse=True)
if len(results) > 0:
self.max_diff = results[0] if self.max_diff[0] < results[0][0] else self.max_diff
print('------statistics------')
for result in results:
print(result)
print('------max_price------')
print(self.max_diff)

def _calc_diff(self, price_list):
numerator = 1
Expand All @@ -88,4 +95,4 @@ def _get_best_price(self, side, ticker):
else:
price = ticker_price['b']
quantity = ticker_price['B']
return side, ticker, price, quantity
return side, ticker, float(price), float(quantity), ticker_price['E']

0 comments on commit 609c696

Please sign in to comment.