Skip to content

Commit

Permalink
cleanup formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoscon committed Jan 10, 2019
1 parent ae41a56 commit 106eb10
Show file tree
Hide file tree
Showing 28 changed files with 80 additions and 72 deletions.
1 change: 0 additions & 1 deletion cryptofeed/backends/aggregate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def __init__(self, *args, timer=60, **kwargs):
self.timer = timer
self.last_update = 0


async def __call__(self, **kwargs):
now = time.time()
if now - self.last_update > self.timer:
Expand Down
3 changes: 2 additions & 1 deletion cryptofeed/backends/arctic.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ async def __call__(self, *, feed: str, pair: str, side: str, amount: Decimal, pr
else:
ts = timestamp_normalize(feed, timestamp)

df = pd.DataFrame({'feed': [feed], 'pair': [pair], 'id': [order_id], 'date': [dt.utcfromtimestamp(ts)], 'side': [side], 'amount': [float(amount)], 'price': [float(price)]})
df = pd.DataFrame({'feed': [feed], 'pair': [pair], 'id': [order_id], 'date': [dt.utcfromtimestamp(ts)],
'side': [side], 'amount': [float(amount)], 'price': [float(price)]})
df['date'] = pd.to_datetime(df.date)
df.set_index(['date'], inplace=True)
self.lib.append(self.key, df, upsert=True)
Expand Down
3 changes: 1 addition & 2 deletions cryptofeed/backends/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
LOG = logging.getLogger('feedhandler')



class InfluxCallback:
def __init__(self, addr: str, db: str, **kwargs):
"""
Expand Down Expand Up @@ -69,7 +68,7 @@ async def __call__(self, *, feed: str, pair: str, side: str, amount: Decimal, pr
price = float(price)
trade = f'{feed},pair={pair},feed=trades,side={side}'
if order_id:
trade+= f',id={order_id}'
trade += f',id={order_id}'
trade += f' amount={amount},price={price}'

if timestamp:
Expand Down
4 changes: 2 additions & 2 deletions cryptofeed/backends/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ async def __call__(self, *, feed: str, pair: str, side: str, amount: Decimal, pr
else:
ts = timestamp_normalize(feed, timestamp)

data = json.dumps({'feed': feed, 'pair': pair, 'id': order_id, 'timestamp': timestamp, 'side': side, 'amount': float(amount), 'price': float(price)})
data = json.dumps({'feed': feed, 'pair': pair, 'id': order_id, 'timestamp': timestamp,
'side': side, 'amount': float(amount), 'price': float(price)})

await self.redis.zadd("{}-{}-{}".format(self.key, feed, pair), ts, data, exist=self.redis.ZSET_IF_NOT_EXIST)

Expand Down Expand Up @@ -85,7 +86,6 @@ def __init__(self, *args, **kwargs):
self.depth = kwargs.get('depth', None)
self.previous = {BID: {}, ASK: {}}


async def __call__(self, *, feed, pair, book, timestamp):
ts = time.time()

Expand Down
2 changes: 1 addition & 1 deletion cryptofeed/backends/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def connection_made(self, transport):
self.transport = transport

def datagram_received(self, data, addr):
pass
pass

def error_received(self, exc):
LOG.error('UDP backend received exception: %s', exc)
Expand Down
6 changes: 3 additions & 3 deletions cryptofeed/binance/binance.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ async def _book(self, msg, pair):
}
"""
self.l2_book = {
BID: sd({Decimal(bid[0]): Decimal(bid[1]) for bid in msg['bids']}),
ASK: sd({Decimal(ask[0]): Decimal(ask[1]) for ask in msg['asks']})
}
BID: sd({Decimal(bid[0]): Decimal(bid[1]) for bid in msg['bids']}),
ASK: sd({Decimal(ask[0]): Decimal(ask[1]) for ask in msg['asks']})
}

await self.callbacks[L2_BOOK](feed=self.id, pair=pair, book=self.l2_book, timestamp=time.time() * 1000)

Expand Down
1 change: 1 addition & 0 deletions cryptofeed/binance/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ def gen_pairs():
ret[normalized] = symbol['symbol']
return ret


binance_pair_mapping = gen_pairs()
18 changes: 8 additions & 10 deletions cryptofeed/bitfinex/bitfinex.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def __reset(self):
self.order_map = defaultdict(dict)
self.seq_no = 0


async def _ticker(self, msg):
chan_id = msg[0]
if msg[1] == 'hb':
Expand Down Expand Up @@ -102,12 +101,12 @@ async def _trade_update(trade):
period=period)
else:
await self.callbacks[TRADES](feed=self.id,
pair=pair,
side=side,
amount=amount,
price=price,
order_id=order_id,
timestamp=timestamp)
pair=pair,
side=side,
amount=amount,
price=price,
order_id=order_id,
timestamp=timestamp)

if isinstance(msg[1], list):
# snapshot
Expand Down Expand Up @@ -180,12 +179,12 @@ async def _book(self, msg):

await self.book_callback(pair, L2_BOOK, forced, delta, timestamp)


async def _raw_book(self, msg):
"""
For L3 book updates
"""
timestamp = time.time() * 1000

def add_to_book(pair, side, price, order_id, amount):
if price in self.l3_book[pair][side]:
self.l3_book[pair][side][price][order_id] = amount
Expand Down Expand Up @@ -253,7 +252,6 @@ def remove_from_book(pair, side, order_id):
add_to_book(pair, side, price, order_id, amount)
self.order_map[pair][side][order_id] = {'price': price, 'amount': amount}


elif msg[1] == 'hb':
return
else:
Expand Down Expand Up @@ -310,7 +308,7 @@ async def subscribe(self, websocket):
message = {'event': 'subscribe',
'channel': channel,
'symbol': pair
}
}
if 'book' in channel:
parts = channel.split('-')
if len(parts) != 1:
Expand Down
2 changes: 1 addition & 1 deletion cryptofeed/bitmex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
Please see the LICENSE file for the terms and conditions
associated with this software.
'''
from .bitmex import Bitmex
from .bitmex import Bitmex
3 changes: 1 addition & 2 deletions cryptofeed/bitmex/bitmex.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ async def _funding(self, msg):
interval=data['fundingInterval'],
rate=data['fundingRate'],
rate_daily=data['fundingRateDaily']
)

)

async def message_handler(self, msg):
msg = json.loads(msg, parse_float=Decimal)
Expand Down
20 changes: 9 additions & 11 deletions cryptofeed/coinbase/coinbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,14 @@ async def _book_update(self, msg):
await self.book_callback(pair, L3_BOOK, False, delta, timestamp)

await self.callbacks[TRADES](
feed=self.id,
pair=msg['product_id'],
order_id=msg['trade_id'],
side=BID if msg['side'] == 'buy' else ASK,
amount=Decimal(msg['size']),
price=Decimal(msg['price']),
timestamp=msg['time']
)
feed=self.id,
pair=msg['product_id'],
order_id=msg['trade_id'],
side=BID if msg['side'] == 'buy' else ASK,
amount=Decimal(msg['size']),
price=Decimal(msg['price']),
timestamp=msg['time']
)

async def _pair_level2_snapshot(self, msg):
timestamp = dt.utcnow()
Expand Down Expand Up @@ -194,7 +194,6 @@ async def _open(self, msg):
order_id = msg['order_id']
timestamp = msg['time']


if price in self.l3_book[pair][side]:
self.l3_book[pair][side][price][order_id] = size
else:
Expand Down Expand Up @@ -268,7 +267,6 @@ async def message_handler(self, msg):

self.seq_no[pair] = msg['sequence']


if 'type' in msg:
if msg['type'] == 'ticker':
await self._ticker(msg)
Expand Down Expand Up @@ -298,6 +296,6 @@ async def subscribe(self, websocket):
await websocket.send(json.dumps({"type": "subscribe",
"product_ids": self.pairs,
"channels": self.channels
}))
}))
if 'full' in self.channels:
await self._book_snapshot()
2 changes: 1 addition & 1 deletion cryptofeed/coinbase/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
def gen_pairs():
r = requests.get('https://api.pro.coinbase.com/products').json()

return {data['id'] : data['id'] for data in r}
return {data['id']: data['id'] for data in r}


coinbase_pair_mapping = gen_pairs()
2 changes: 2 additions & 0 deletions cryptofeed/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
associated with this software.
'''


class MissingSequenceNumber(Exception):
pass


class MissingMessage(Exception):
pass
1 change: 0 additions & 1 deletion cryptofeed/gemini/gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ async def _update(self, msg):
if forced:
await self.book_callback(self.pair, L2_BOOK, True, None, timestamp)


async def message_handler(self, msg):
msg = json.loads(msg, parse_float=Decimal)
seq_no = msg['socket_sequence']
Expand Down
3 changes: 2 additions & 1 deletion cryptofeed/gemini/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

def gen_pairs():
ret = {}
defaults = ['btcusd', 'ethusd', 'ethbtc','zecusd','zecbtc','zeceth','zecbch','zecltc','bchusd','bchbtc','bcheth','ltcusd','ltcbtc','ltceth','ltcbch']
defaults = ['btcusd', 'ethusd', 'ethbtc', 'zecusd', 'zecbtc', 'zeceth', 'zecbch',
'zecltc', 'bchusd', 'bchbtc', 'bcheth', 'ltcusd', 'ltcbtc', 'ltceth', 'ltcbch']

try:
r = requests.get('https://api.gemini.com/v1/symbols').json()
Expand Down
3 changes: 2 additions & 1 deletion cryptofeed/hitbtc/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ def gen_pairs():
ret[normalized] = symbol['id']
return ret

hitbtc_pair_mapping = gen_pairs()

hitbtc_pair_mapping = gen_pairs()
12 changes: 6 additions & 6 deletions cryptofeed/kraken/kraken.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ async def _trades(self, session, pair):
# <price>, <volume>, <time>, <buy/sell>, <market/limit>, <miscellaneous>
price, amount, timestamp, side, _, _ = trade
await self.callbacks[TRADES](feed=self.id,
pair=pair_exchange_to_std(pair),
side=BID if side == 'b' else ASK,
amount=Decimal(amount),
price=Decimal(price),
order_id=None,
timestamp=timestamp)
pair=pair_exchange_to_std(pair),
side=BID if side == 'b' else ASK,
amount=Decimal(amount),
price=Decimal(price),
order_id=None,
timestamp=timestamp)

async def _ticker(self, session, pair):
async with session.get("{}Ticker?pair={}&count=100".format(self.address, pair)) as response:
Expand Down
1 change: 1 addition & 0 deletions cryptofeed/nbbo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from cryptofeed.callback import Callback
from cryptofeed.defines import BID, ASK


class NBBO(Callback):
def __init__(self, callback, pairs):
self.bids = {pair: {} for pair in pairs}
Expand Down
3 changes: 2 additions & 1 deletion cryptofeed/poloniex/pairs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def gen_pairs():
ret[pairs[pair]['id']] = pair
return ret


poloniex_id_pair_mapping = gen_pairs()
poloniex_pair_id_mapping = {value: key for key, value in poloniex_id_pair_mapping.items()}
poloniex_pair_mapping = {value.split("_")[1] + "-" + value.split("_")[0] : value for _, value in poloniex_id_pair_mapping.items()}
poloniex_pair_mapping = {value.split("_")[1] + "-" + value.split("_")[0]: value for _, value in poloniex_id_pair_mapping.items()}
4 changes: 2 additions & 2 deletions cryptofeed/poloniex/poloniex.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ async def message_handler(self, msg):
# the trading pair being updated
seq_no = msg[1]

if chan_id not in self.seq_no :
if chan_id not in self.seq_no:
self.seq_no[chan_id] = seq_no
elif self.seq_no[chan_id] + 1 != seq_no:
LOG.warning("%s: missing sequence number. Received %d, expected %d", self.id, seq_no, self.seq_no+1)
Expand All @@ -158,4 +158,4 @@ async def subscribe(self, websocket):
for channel in self.channels:
await websocket.send(json.dumps({"command": "subscribe",
"channel": channel
}))
}))
2 changes: 1 addition & 1 deletion cryptofeed/rest/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from cryptofeed.rest.rest import Rest
from cryptofeed.rest.rest import Rest
2 changes: 1 addition & 1 deletion cryptofeed/rest/bitfinex.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Bitfinex(API):
def _nonce(self):
return str(int(round(time.time() * 1000)))

def _generate_signature(self, url: str, body = json.dumps({})):
def _generate_signature(self, url: str, body=json.dumps({})):
nonce = self._nonce()
signature = "/api/" + url + nonce + body
h = hmac.new(self.key_secret.encode('utf8'), signature.encode('utf8'), hashlib.sha384)
Expand Down
3 changes: 2 additions & 1 deletion cryptofeed/rest/bitmex.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def _get(self, ep, symbol, start_date, end_date, retry, retry_wait, freq='6H'):

@request_retry(self.ID, retry, retry_wait)
def helper(start, start_date, end_date):
endpoint = '/api/v1/{}?symbol={}&count={}&reverse=false&start={}&startTime={}&endTime={}'.format(ep, symbol, API_MAX, start, start_date, end_date)
endpoint = '/api/v1/{}?symbol={}&count={}&reverse=false&start={}&startTime={}&endTime={}'.format(
ep, symbol, API_MAX, start, start_date, end_date)
header = {}
if self.key_id and self.key_secret:
header = self._generate_signature("GET", endpoint)
Expand Down
18 changes: 11 additions & 7 deletions cryptofeed/rest/coinbase.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import time, json, hashlib, hmac, requests, base64
import time
import json
import hashlib
import hmac
import requests
import base64
from time import sleep
from datetime import datetime as dt
import logging
Expand All @@ -21,18 +26,18 @@ class Coinbase(API):
api = "https://api.pro.coinbase.com"
sandbox_api = "https://api-public.sandbox.pro.coinbase.com"

def _generate_signature(self, endpoint: str, method: str, body = ''):
def _generate_signature(self, endpoint: str, method: str, body=''):
timestamp = str(time.time())
message = ''.join([timestamp, method, endpoint, body])
hmac_key = base64.b64decode(self.key_secret)
signature = hmac.new(hmac_key, message.encode('ascii'), hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest()).decode('utf-8')

return {
'CB-ACCESS-KEY': self.key_id, # The api key as a string.
'CB-ACCESS-SIGN': signature_b64, # The base64-encoded signature (see Signing a Message).
'CB-ACCESS-TIMESTAMP': timestamp, # A timestamp for your request.
'CB-ACCESS-PASSPHRASE': self.key_passphrase, # The passphrase you specified when creating the API key
'CB-ACCESS-KEY': self.key_id, # The api key as a string.
'CB-ACCESS-SIGN': signature_b64, # The base64-encoded signature (see Signing a Message).
'CB-ACCESS-TIMESTAMP': timestamp, # A timestamp for your request.
'CB-ACCESS-PASSPHRASE': self.key_passphrase, # The passphrase you specified when creating the API key
'Content-Type': 'Application/JSON',
}

Expand Down Expand Up @@ -203,7 +208,6 @@ def fills(self, symbol=None, start=None, end=None, retry=None, retry_wait=10):
"""
return self._get_fills(symbol=symbol, retry=retry, retry_wait=retry_wait, start_date=start, end_date=end)


def execute_trades(self, trades_to_make: list):
"""
https://docs.gdax.com/?python#place-a-new-order
Expand Down
Loading

0 comments on commit 106eb10

Please sign in to comment.