Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add blockchain exchange as new option #64

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions arbitrage/observers/specializedtraderbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from arbitrage.observers.observer import Observer
from arbitrage.private_markets import bitstampusd
from arbitrage.private_markets import paymium
from arbitrage.private_markets import bcexusd
from arbitrage.observers.emailer import send_email
from arbitrage import config

Expand All @@ -11,10 +12,12 @@ class SpecializedTraderBot(Observer):
def __init__(self):
self.bitstamp = bitstampusd.PrivateBitstampUSD()
self.btcentral = paymium.PrivatePaymium()
self.clients = {"BitStampUSD": self.bitstamp, "PaymiumEUR": self.btcentral}
self.bcex = bcexusd.PrivateBcexUSD()
self.clients = {"BitStampUSD": self.bitstamp, "PaymiumEUR": self.btcentral, "BcexUSD": self.bcex}
self.profit_percentage_thresholds = { # Graph
"BitStampUSD": {"PaymiumEUR": 3.5},
"PaymiumEUR": {"BitStampUSD": 1},
"BitStampUSD": {"PaymiumEUR": 3.5, "BcexUSD": 3},
"PaymiumEUR": {"BitStampUSD": 1, "BcexUSD": 3},
"BcexUSD": {"BitStampUSD": 3, "PaymiumEUR": 3},
}
self.trade_wait = 60 * 5 # in seconds
self.last_trade = 0
Expand Down
32 changes: 32 additions & 0 deletions arbitrage/private_markets/bcexusd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (C) 2013, Maxime Biais <[email protected]>

from arbitrage import config
from arbitrage.private_markets.market import Market
from bcex.core.bcex_interface import BcexInterface
from bcex.core.websocket_client import Environment
from bcex.core.orders import OrderSide


class PrivateBcexUSD(Market):

def __init__(self):
super().__init__()
self.api_secret = config.bcex_api_secret
self.get_info()
self._client = BcexInterface(symbols=["BTC-USD"],env=Environment.PROD)
self._client.connect()


def buy(self, amount, price):
"""Create a buy limit order"""
self._client.place_order("BTC-USD", OrderSide.BUY, price=price, quantity=amount)

def _sell(self, amount, price):
"""Create a sell limit order"""
self._client.place_order("BTC-USD", OrderSide.SELL, price=price, quantity=amount)

def get_info(self):
"""Get balance"""
balances = self._client.get_balances()
self.btc_balance = float(balances["BTC"]["available"])
self.usd_balance = float(balances["USD"]["available"])
35 changes: 35 additions & 0 deletions arbitrage/public_markets/_bcex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from bcex.core.bcex_interface import BcexInterface
from bcex.core.websocket_client import Environment, Channel
from arbitrage.public_markets.market import Market


class Bcex(Market):
ORDER_BOOK_DEPTH = 10
def __init__(self, currency, code):
super().__init__(currency)
self.code = code
self.update_rate = 30
self._client = BcexInterface(symbols=[code],env=Environment.PROD, channels=[Channel.L2])
self._client.connect()

def update_depth(self):
ob = self._client.get_order_book(self.code)
self.depth = self.format_depth(ob) #KEY IS PRICE VALUE IS AMOUNT SORTED LOW TO HIGH

def sort_and_format(self, l, reverse=False):
r = []
for i in range(self.ORDER_BOOK_DEPTH):
if not reverse:
if len(l) >= i:
item = l.peekitem(i)
r.append({"price": float(item[0]), "amount": float(item[1])})
else:
if len(l) >= i:
item = l.peekitem(-(i+1))
r.append({"price": float(item[0]), "amount": float(item[1])})
return r

def format_depth(self, depth):
bids = self.sort_and_format(depth["bids"], True)
asks = self.sort_and_format(depth["asks"], False)
return {"asks": asks, "bids": bids}
14 changes: 14 additions & 0 deletions arbitrage/public_markets/bcexusd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from arbitrage.public_markets._bcex import Bcex
from time import sleep

class BcexUSD(Bcex):
def __init__(self):
super().__init__("USD", "BTC-USD")


if __name__ == "__main__":
market = BcexUSD()
while True:
sleep(2)
market.update_depth()
print(market.get_ticker())
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bcex==1.0.1