def main(): ftx = Rest(config='config.yaml')['ftx'] print(ftx.ticker('ETH-USD')) print(ftx.orders(symbol='USDT-USD')) f = FeedHandler(config="config.yaml") f.add_feed( FTX(config="config.yaml", symbols=['BTC-USD', 'BCH-USD', 'USDT-USD'], channels=[TRADES, USER_FILLS], callbacks={ TRADES: TradeCallback(trade), USER_FILLS: fill })) f.run()
def test_rest_bitmex(): r = Rest() ret = [] for data in r.bitmex.trades('XBTUSD', start='2019-05-01 00:00:09', end='2019-05-01 00:00:15'): ret.extend(data) assert len(ret) == 2
def main(): subscriptions = read_cfg("conf//subscriptions.yaml") r = Rest() rest_clients = { 'coinbase': r.coinbase, 'binance': r.binance, 'kraken': r.kraken, 'poloniex': r.poloniex } for exch, data in subscriptions.items(): for pair in data['pairs']: print(f'Getting trade data for {exch} {pair}') trades = rest_clients[exch].trades(pair) for trade in trades: for t in trade: hwt = str(datetime.utcnow().isoformat()).replace("T","D").replace("-",".") ts = str(datetime.fromtimestamp(t['timestamp']).isoformat()).replace("T","D").replace("-",".") qStr = f"`trades insert (`timestamp${hwt};`timestamp${ts};" \ f"`{t['feed']};`$\"{t['pair']}\";`{t['side']};`float${t['amount']};" \ f"`float${t['price']};`int${t['id']})" try: q.sendSync(qStr, param=None) except QException as e: print(f"Error executing query {qStr} against server. {e}")
def test_rest_bitmex(): r = Rest() ret = [] end = pd.Timestamp.now() start = end - pd.Timedelta(minutes=2) for data in r.bitmex.trades('XBTUSD', start=start, end=end): ret.extend(data) assert len(ret) > 0
def test_rest_deribit(): expected = {'timestamp': 1550062892.378, 'symbol': 'BTC-PERPETUAL', 'id': 15340745, 'feed': 'DERIBIT', 'side': BUY, 'amount': 700.0, 'price': 3580.25} r = Rest() ret = [] for data in r.deribit.trades('BTC-PERPETUAL', start='2019-02-13 12:59:10', end='2019-02-13 13:01:33'): ret.extend(data) assert ret[0] == expected
def test_rest_ftx(): expected = {'timestamp': 1591992000.0, 'pair': 'BTC-PERP', 'feed': 'FTX', 'rate': -9e-06} r = Rest() ret = [] data = r.ftx.funding('BTC-PERP', start_date='2020-06-10 12:59:10', end_date='2020-06-13 13:01:33') ret.extend(data) try: assert ret[0] == expected except AssertionError as ex: print(f'test_rest_ftx failed: {ex!r}') print('Please check the start_date, because FTX only saves 4 months worth of funding data')
def test_rest_bitfinex(): expected = {'timestamp': 1483228812.0, 'symbol': 'BTC-USD', 'id': 25291508, 'feed': 'BITFINEX', 'side': SELL, 'amount': 1.65, 'price': 966.61} r = Rest() ret = [] for data in r.bitfinex.trades('BTC-USD', start='2017-01-01 00:00:00', end='2017-01-01 0:00:13'): ret.extend(data) assert len(ret) == 1 assert ret[0] == expected
def test_rest_bitmex(): expected = {'timestamp': '2018-09-29T00:00:09.939Z', 'pair': 'XBTUSD', 'id': '58db4844-82b2-40e9-de90-c4d1672af7cc', 'feed': 'BITMEX', 'side': 'Sell', 'amount': 265, 'price': 6620} r = Rest() ret = [] for data in r.bitmex.trades('XBTUSD', start='2018-09-29 00:00:09.939', end='2018-09-29 00:00:10'): ret.extend(data) assert len(ret) == 1 assert ret[0] == expected
def main(): path_to_config = os.path.join(Path.home(), 'config.yaml') ftx = Rest(config=path_to_config, subaccount='Gutenberg')['ftx'] # print(ftx.config) print(ftx.ticker('ETH-USD')) print(ftx.orders(symbol='USDT-USD')) print( ftx.place_order(symbol='USDT-USD', side=BUY, amount=0.01, price=0.9995, post_only=True)) print(ftx.orders(symbol='USDT-USD'))
def test_rest_deribit(): r = Rest() ret = [] for data in r.deribit.trades('BTC-PERPETUAL'): ret.extend(data) assert len(ret) > 1
import pytest from cryptofeed.defines import BID, ASK from cryptofeed.rest import Rest public = Rest().Gemini sandbox = Rest(sandbox=True).Gemini def test_ticker(): ticker = public.ticker('BTC-USD') assert BID in ticker assert ASK in ticker def test_order_book(): current_order_book = public.l2_book('BTC-USD') assert BID in current_order_book assert len(current_order_book[BID]) > 0 def test_trade_history(): trade_history = list(public.trades('BTC-USD')) assert len(trade_history) > 0 @pytest.mark.skipif(sandbox.key_id is None or sandbox.key_secret is None, reason="No api key provided")
import pathlib import pytest from cryptofeed.defines import BID, ASK, LIMIT, BUY, CANCELLED from cryptofeed.rest import Rest CFG = str(pathlib.Path().absolute()) + "/config.yaml" public = Rest().Gemini sandbox = Rest(config=CFG, sandbox=True).Gemini def test_ticker(): ticker = public.ticker('BTC-USD') assert BID in ticker assert ASK in ticker def test_order_book(): current_order_book = public.l2_book('BTC-USD') assert BID in current_order_book assert len(current_order_book[BID]) > 0 def test_trade_history(): trade_history = list(public.trades('BTC-USD'))
import pytest from decimal import Decimal import pandas as pd from cryptofeed.defines import BID, ASK from cryptofeed.rest import Rest public = Rest().Coinbase sandbox = Rest(sandbox=True).Coinbase def test_ticker(): ticker = public.ticker('BTC-USD') assert BID in ticker assert ASK in ticker def test_order_book(): current_order_book = public.l2_book('BTC-USD') assert BID in current_order_book assert len(current_order_book[BID]) > 0 def test_trade_history(): trade_history = list(public.trades('BTC-USD')) assert len(trade_history) > 0 def test_trade_history_specific_time():
def _worker(self, exchange): r = Rest() storage = Storage(self.config) for pair in self.config.backfill[exchange]: try: start = self.config.backfill[exchange][pair].start while True: end = storage.get_start_date(exchange, 'trades', pair) if not all(e == end[0] for e in end): raise InconsistentStorage( "Stored data differs, cannot backfill") end = end[0] if end: break time.sleep(10) end = Timestamp(end, unit='s') end -= Timedelta(microseconds=1) start = Timestamp(start) if end <= Timestamp(start): LOG.info( "Data in storage is earlier than backfill start date for %s - %s", exchange, pair) continue LOG.info("Backfill - Starting for %s - %s for range %s - %s", exchange, pair, start, str(end)) # Backfill from end date to start date, 1 day at a time, in reverse order (from end -> start) while start < end: seg_start = end.replace(hour=0, minute=0, second=0, microsecond=0, nanosecond=0) if start > seg_start: seg_start = start LOG.info("Backfill - Reading %s to %s for %s - %s", seg_start, end, exchange, pair) trades = [] try: for t in r[exchange].trades(pair, str(seg_start), str(end)): trades.extend(t) except Exception: LOG.warning( "Backfill - encountered error backfilling %s - %s, trying again...", exchange, pair, exc_info=True) time.sleep(300) continue if not trades: end = seg_start - Timedelta(nanoseconds=1) continue storage.aggregate(trades) storage.write(exchange, 'trades', pair, end.timestamp()) LOG.info("Backfill - Wrote %s to %s for %s - %s", seg_start, end, exchange, pair) end = seg_start - Timedelta(nanoseconds=1) LOG.info("Backfill for %s - %s completed", exchange, pair) except Exception: LOG.error("Backfill failed for %s - %s", exchange, pair, exc_info=True)
def _worker(self, exchange): r = Rest() storage = Storage(self.config) for pair in self.config.backfill[exchange]: try: start = self.config.backfill[exchange][pair].start while True: end = storage.get_start_date(exchange, 'trades', pair) if all(e for e in end): break time.sleep(10) ends = list( map( lambda x: Timestamp(x, unit='s') - Timedelta( microseconds=1), end)) if any(e <= Timestamp(start) for e in ends): LOG.info( "Data in storage is earlier than backfill start date for %s - %s", exchange, pair) continue LOG.info("Backfill - Starting for %s - %s for range %s - %s", exchange, pair, start, str(max(ends))) # Backfill from end date to start date, 1 day at a time, in reverse order (from end -> start) end = max(ends) start = Timestamp(start) while start < end: seg_start = end.replace(hour=0, minute=0, second=0, microsecond=0, nanosecond=0) if start > seg_start: seg_start = start LOG.info("Backfill - Reading %s to %s for %s - %s", seg_start, end, exchange, pair) trades = [] try: for t in r[exchange].trades(pair, str(seg_start), str(end)): trades.extend(t) except Exception: LOG.warning( "Backfill - encountered error backfilling %s - %s, trying again...", exchange, pair, exc_info=True) time.sleep(300) continue if not trades: end = seg_start - Timedelta(nanoseconds=1) continue for trade in trades: trade['price'] = float(trade['price']) trade['amount'] = float(trade['amount']) def gen_pos(): counter = 0 while True: yield counter % len(ends) counter += 1 pos = gen_pos() ends_float = [x.timestamp() for x in ends] def timestamp_filter(data): boundary = ends_float[next(pos)] return list( filter(lambda x: x['timestamp'] < boundary, copy.copy(data))) storage.aggregate(trades, transform=timestamp_filter) storage.write(exchange, 'trades', pair, end.timestamp()) LOG.info("Backfill - Wrote %s to %s for %s - %s", seg_start, end, exchange, pair) end = seg_start - Timedelta(nanoseconds=1) LOG.info("Backfill for %s - %s completed", exchange, pair) except Exception: LOG.error("Backfill failed for %s - %s", exchange, pair, exc_info=True)
import pytest from cryptofeed.defines import BID, ASK from cryptofeed.rest import Rest poloniex = Rest().Poloniex def test_get_ticker(): ticker = poloniex.ticker('BTC-USDT') assert ticker['bid'] > 0 def test_order_book(): order_book = poloniex.l2_book('BTC-USDT') assert BID in order_book assert ASK in order_book assert len(order_book[BID]) > 0 def test_trade_history(): trade_history = list(next(poloniex.trades('BTC-USDT', start='2020-12-30', end='2020-12-31'))) assert len(trade_history) > 0 assert float(trade_history[0]['amount']) > 0 @pytest.mark.skipif(not poloniex.config.key_id or not poloniex.config.key_secret, reason="No api key provided") def test_balances(): balances = poloniex.balances()
def test_open_orders(): poloniex = Rest().Poloniex open_orders = poloniex.open_orders() assert 'BTC_BCN' in open_orders
import pytest from cryptofeed.rest import Rest kraken = Rest('config.yaml').kraken def test_get_server_time(): server_time = kraken.get_server_time() assert len(server_time['error']) == 0 def test_get_assets(): info = kraken.get_asset_info() assert 'ADA' in info['result'] def test_get_assets_payload(): info = kraken.get_asset_info({"asset": "ada,eos,bch"}) assert 'ADA' in info['result'] def test_tradeable_pairs(): tradeable = kraken.get_tradeable_pairs() assert len(tradeable['result']) > 0 def test_tradeable_pairs_payload(): tradeable = kraken.get_tradeable_pairs({ "info": "leverage", "pair": "adacad" })
import pytest from cryptofeed.defines import BID from cryptofeed.rest import Rest kraken = Rest().kraken def test_get_order_book(): book = kraken.l2_book('BTC-USD') assert len(book[BID]) > 0 def test_get_recent_trades(): trades = list(kraken.trades('BTC-USD')) assert len(trades) > 0 @pytest.mark.skipif(not kraken.config.key_id or not kraken.config.key_secret, reason="No api key provided") def test_get_account_balance(): balance = kraken.get_account_balance() assert balance['error'] == [] @pytest.mark.skipif(not kraken.config.key_id or not kraken.config.key_secret, reason="No api key provided") def test_get_open_orders(): open_orders = kraken.get_open_orders() assert open_orders['error'] == []
import time from cryptofeed.rest import Rest import pytest public = Rest('config.yaml').Gemini sandbox = Rest('config.yaml', sandbox=True).Gemini def test_ticker(): ticker = public.ticker('BTC-USD') assert 'bid' in ticker assert 'ask' in ticker def test_order_book(): current_order_book = public.l2_book('BTC-USD') assert 'bid' in current_order_book assert len(current_order_book['bid']) > 0 def test_trade_history(): trade_history = list(public.trades('BTC-USD')) assert len(trade_history) > 0 @pytest.mark.skipif(sandbox.key_id is None or sandbox.key_secret is None,
def test_open_orders(): poloniex = Rest('config.yaml').Poloniex open_orders = poloniex.open_orders() assert 'BTC_BCN' in open_orders
import pytest from cryptofeed.defines import BID, ASK from cryptofeed.rest import Rest poloniex = Rest('config.yaml').Poloniex def test_get_ticker(): ticker = poloniex.ticker('BTC-USDT') assert ticker['bid'] > 0 def test_order_book(): order_book = poloniex.l2_book('BTC-USDT') assert BID in order_book assert ASK in order_book assert len(order_book[BID]) > 0 def test_trade_history(): trade_history = list( next(poloniex.trades('BTC-USDT', start='2020-01-01', end='2020-01-02'))) assert len(trade_history) > 0 assert float(trade_history[0]['amount']) > 0 @pytest.mark.skipif(poloniex.key_id is None or poloniex.key_secret is None, reason="No api key provided")