예제 #1
0
def test_binance_address_generation():
    symbols = Binance.info()['symbols']
    channels = Binance.info()['channels']
    for length in (10, 20, 30, 40, 50, 100, 200, 500, len(symbols)):
        syms = []
        chans = []

        sub = random.sample(symbols, length)
        addr = Binance(symbols=sub, channels=channels)._address()
        if length * len(channels) < 200:
            assert isinstance(addr, str)
            value = addr.split("=", 1)[1]
            value = value.split("/")
            for entry in value:
                sym, chan = entry.split("@", 1)
                syms.append(sym)
                chans.append(chan)
        else:
            assert isinstance(addr, dict)

            for _, value in addr.items():
                value = value.split("=", 1)[1]
                value = value.split("/")
                for entry in value:
                    sym, chan = entry.split("@", 1)
                    syms.append(sym)
                    chans.append(chan)
        assert len(chans) == len(channels) * length == len(syms)
        assert len(set(chans)) == len(channels)
        assert (len(set(syms))) == length
예제 #2
0
def test_binance_address_generation():
    symbols = Binance.symbols()
    channels = [
        channel for channel in Binance.info()['channels']['websocket']
        if not Binance.is_authenticated_channel(channel)
    ]
    for length in (10, 20, 30, 40, 50, 100, 200, 500, len(symbols)):
        syms = []
        chans = []

        sub = random.sample(symbols, length)
        addr = Binance(symbols=sub, channels=channels)._address()

        if length * len(channels) < 200:
            assert isinstance(addr, str)
            value = addr.split("=", 1)[1]
            value = value.split("/")
            for entry in value:
                sym, chan = entry.split("@", 1)
                syms.append(sym)
                chans.append(chan)
        else:
            assert isinstance(addr, list)

            for value in addr:
                value = value.split("=", 1)[1]
                value = value.split("/")
                for entry in value:
                    sym, chan = entry.split("@", 1)
                    syms.append(sym)
                    chans.append(chan)
        assert len(chans) == len(channels) * length == len(syms)
        assert len(set(chans)) == len(channels)
        assert (len(set(syms))) == length
예제 #3
0
def main():
    try:
        # p = Process(target=receiver, args=(5678,))
        # p.start()

        path_to_config = os.path.join(Path.home(), 'config.yaml')

        f = FeedHandler()
        binance_futures_symbols = defaultdict(list)
        for instrument in BinanceFutures.get_instrument_objects():
            binance_futures_symbols[instrument.instrument_type].append(instrument.instrument_name)
        print(binance_futures_symbols)

        binance_delivery_symbols = defaultdict(list)
        for instrument in BinanceDelivery.get_instrument_objects():
            binance_delivery_symbols[instrument.instrument_type].append(instrument.instrument_name)
        print(binance_delivery_symbols)

        # binance_symbols = set()
        # for instrument in BinanceDelivery.get_instrument_objects():
        #     binance_symbols.add(instrument.base + '-USDT')
        # print(binance_symbols)
        binance_symbols = []
        for instrument in Binance.info()['symbols']:
            if instrument.endswith('-USDT'):
                binance_symbols.append(instrument)
        print(binance_symbols)

        feeds = []
        feeds.append(BinanceDelivery(candle_interval='1d', symbols=binance_delivery_symbols[PERPETUAL], channels=[FUTURES_INDEX, FUNDING, TICKER, TRADES, VOLUME], callbacks={
            FUNDING: FundingZMQ(port=5678), 
            TICKER: TickerZMQ(port=5679), 
            TRADES: TradeZMQ(port=5682), 
            FUTURES_INDEX: FuturesIndexZMQ(port=5684),
            VOLUME: VolumeZMQ(port=5685)}))
        feeds.append(BinanceDelivery(candle_interval='1d', symbols=binance_delivery_symbols[FUTURE], channels=[FUTURES_INDEX, TICKER, TRADES, VOLUME], callbacks={
            TICKER: TickerZMQ(port=5687), 
            TRADES: TradeZMQ(port=5688), 
            FUTURES_INDEX: FuturesIndexZMQ(port=5689),
            VOLUME: VolumeZMQ(port=5690)}))
        feeds.append(BinanceDelivery(config=path_to_config, channels=[USER_BALANCE, USER_POSITION], symbols=[], callbacks={
            USER_BALANCE: UserBalanceZMQ(port=5691),
            USER_POSITION: UserPositionZMQ(port=5692)}))

        feeds.append(BinanceFutures(symbols=binance_futures_symbols[PERPETUAL], channels=[FUNDING], callbacks={FUNDING: FundingZMQ(port=5680)}))
        
        feeds.append(Binance(symbols=list(binance_symbols), channels=[TICKER, TRADES, VOLUME], callbacks={
            TICKER: TickerZMQ(port=5681), 
            TRADES: TradeZMQ(port=5683),
            VOLUME: VolumeZMQ(port=5686)}))
        feeds.append(Binance(config=path_to_config, channels=[USER_BALANCE], symbols=[], callbacks={
            USER_BALANCE: UserBalanceZMQ(port=5693)}))
        
        tasks = []
        for feed in feeds:
            if feed.requires_authentication:
                tasks.append(do_periodically_every(0, 30, 0, feed.auth.refresh_token))
            f.add_feed(feed)
        f.run(tasks=tasks)
    
    finally:
        p.terminate()
예제 #4
0
from datetime import datetime
from collections import defaultdict
from multiprocessing import Process
import os
from pathlib import Path
import sys
from yapic import json

from cryptofeed import FeedHandler
from cryptofeed.backends.zmq import CandlesZMQ, FundingZMQ, FuturesIndexZMQ, TickerZMQ, TradeZMQ, VolumeZMQ, UserBalanceZMQ, UserPositionZMQ
from cryptofeed.defines import CANDLES, FUTURES_INDEX, L2_BOOK, FUNDING, TICKER, PERPETUAL, FUTURE, TRADES, VOLUME, USER_BALANCE, USER_POSITION
from cryptofeed.exchanges import BinanceFutures, BinanceDelivery, Binance

binance_delivery_data_info = BinanceDelivery.info()
# binance_futures_data_info = BinanceFutures.info()
binance_data_info = Binance.info()
print(binance_data_info)

import uvloop
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())

def receiver(port):
    async def listen():
        while True:
            data = await s.recv_string()
            print(data)
            key, msg = data.split(" ", 1)
            print(key)
            # print(json.loads(msg))
            msg = json.loads(msg)
            res[msg['feed']][msg['symbol']] = msg['rate']
예제 #5
0
def main():
    config = {'log': {'filename': 'demo.log', 'level': 'INFO'}}
    # the config will be automatically passed into any exchanges set up by string. Instantiated exchange objects would need to pass the config in manually.
    f = FeedHandler(config=config)
    # Note: EXX is extremely unreliable - sometimes a connection can take many many retries
    # from cryptofeed.exchanges import EXX
    # f.add_feed(EXX(symbols=['BTC-USDT'], channels=[L2_BOOK, TRADES], callbacks={L2_BOOK: BookCallback(book), TRADES: TradeCallback(trade)}))
    f.add_feed(
        KuCoin(symbols=['BTC-USDT', 'ETH-USDT'],
               channels=[L2_BOOK, CANDLES, TICKER, TRADES],
               callbacks={
                   L2_BOOK: book,
                   CANDLES: candle_callback,
                   TICKER: ticker,
                   TRADES: trade
               }))
    f.add_feed(
        Gateio(symbols=['BTC-USDT', 'ETH-USDT'],
               channels=[CANDLES, TICKER, TRADES, L2_BOOK],
               callbacks={
                   CANDLES: candle_callback,
                   L2_BOOK: book,
                   TRADES: trade,
                   TICKER: ticker
               }))
    pairs = Binance.info()['symbols']
    f.add_feed(
        Binance(symbols=pairs,
                channels=[TRADES],
                callbacks={TRADES: TradeCallback(trade)}))
    f.add_feed(COINBASE,
               symbols=['BTC-USD'],
               channels=[TICKER],
               callbacks={TICKER: TickerCallback(ticker)})
    f.add_feed(
        Coinbase(symbols=['BTC-USD'],
                 channels=[TRADES],
                 callbacks={TRADES: TradeCallback(trade)}))
    f.add_feed(
        Coinbase(subscription={
            L2_BOOK: ['BTC-USD', 'ETH-USD'],
            TRADES: ['ETH-USD']
        },
                 callbacks={
                     TRADES: TradeCallback(trade),
                     L2_BOOK: BookCallback(book)
                 }))
    f.add_feed(
        Coinbase(subscription={L3_BOOK: ['LTC-USD']},
                 callbacks={L3_BOOK: BookCallback(book)}))
    f.add_feed(
        Bitfinex(symbols=['BTC-USDT'],
                 channels=[L2_BOOK],
                 callbacks={L2_BOOK: BookCallback(book)}))
    f.add_feed(
        Bitfinex(symbols=['BTC'],
                 channels=[FUNDING],
                 callbacks={FUNDING: FundingCallback(funding)}))
    f.add_feed(
        Poloniex(symbols=['BTC-ETH'],
                 channels=[TICKER, TRADES, VOLUME],
                 callbacks={
                     VOLUME: volume,
                     TICKER: TickerCallback(ticker),
                     TRADES: TradeCallback(trade)
                 }))
    f.add_feed(
        Poloniex(subscription={
            TRADES: ['BTC-DOGE'],
            L2_BOOK: ['BTC-LTC']
        },
                 callbacks={
                     TRADES: TradeCallback(trade),
                     TICKER: TickerCallback(ticker),
                     L2_BOOK: BookCallback(book)
                 }))
    f.add_feed(GEMINI,
               subscription={
                   L2_BOOK: ['BTC-USD', 'ETH-USD'],
                   TRADES: ['ETH-USD', 'BTC-USD']
               },
               callbacks={
                   TRADES: TradeCallback(trade),
                   L2_BOOK: BookCallback(book)
               })
    f.add_feed(
        HitBTC(channels=[TRADES],
               symbols=['BTC-USD'],
               callbacks={TRADES: TradeCallback(trade)}))
    f.add_feed(
        HitBTC(channels=[L2_BOOK],
               symbols=['BTC-USD'],
               callbacks={L2_BOOK: BookCallback(book)}))
    f.add_feed(
        Bitstamp(channels=[L2_BOOK, TRADES],
                 symbols=['BTC-USD'],
                 callbacks={
                     L2_BOOK: BookCallback(book),
                     TRADES: TradeCallback(trade)
                 }))
    bitmex_symbols = Bitmex.info()['symbols']
    f.add_feed(
        Bitmex(channels=[OPEN_INTEREST],
               symbols=['BTC-USD'],
               callbacks={OPEN_INTEREST: oi}))
    f.add_feed(
        Bitmex(channels=[TRADES],
               symbols=bitmex_symbols,
               callbacks={TRADES: TradeCallback(trade)}))
    f.add_feed(
        Bitmex(symbols=['BTC-USD'],
               channels=[FUNDING, TRADES],
               callbacks={
                   FUNDING: FundingCallback(funding),
                   TRADES: TradeCallback(trade)
               }))
    f.add_feed(
        Bitmex(symbols=['BTC-USD'],
               channels=[L2_BOOK],
               callbacks={L2_BOOK: BookCallback(book)}))
    f.add_feed(
        Kraken(checksum_validation=True,
               subscription={
                   L2_BOOK: ['BTC-USD'],
                   TRADES: ['BTC-USD'],
                   TICKER: ['ETH-USD']
               },
               callbacks={
                   L2_BOOK: book,
                   TRADES: TradeCallback(trade),
                   TICKER: TickerCallback(ticker)
               }))
    sub = {TRADES: ['BTC-USDT', 'ETH-USDT'], L2_BOOK: ['BTC-USDT']}
    f.add_feed(
        Huobi(subscription=sub,
              callbacks={
                  TRADES: TradeCallback(trade),
                  L2_BOOK: BookCallback(book)
              }))
    f.add_feed(
        Huobi(symbols=['BTC-USDT'],
              channels=[CANDLES],
              callbacks={CANDLES: candle_callback}))
    sub = {L2_BOOK: ['BTC_CQ', 'BTC_NQ']}
    f.add_feed(
        HuobiDM(subscription=sub,
                callbacks={
                    TRADES: TradeCallback(trade),
                    L2_BOOK: BookCallback(book)
                }))
    pairs = ['BTC-USD', 'ETH-USD', 'EOS-USD', 'BCH-USD', 'BSV-USD', 'LTC-USD']
    f.add_feed(
        HuobiSwap(symbols=pairs,
                  channels=[TRADES, L2_BOOK, FUNDING],
                  callbacks={
                      FUNDING: funding,
                      TRADES: TradeCallback(trade),
                      L2_BOOK: BookCallback(book)
                  }))
    f.add_feed(
        OKCoin(symbols=['BTC-USD'],
               channels=[L2_BOOK],
               callbacks={L2_BOOK: BookCallback(book)}))
    f.add_feed(
        OKEx(symbols=['BTC-USDT'],
             channels=[TRADES],
             callbacks={TRADES: TradeCallback(trade)}))
    f.add_feed(
        Bittrex(subscription={
            TRADES: ['BTC-USD'],
            TICKER: ['ETH-USD'],
            L2_BOOK: ['BTC-USDT']
        },
                callbacks={
                    L2_BOOK: BookCallback(book),
                    TICKER: TickerCallback(ticker),
                    TRADES: TradeCallback(trade)
                }))
    f.add_feed(
        FTX(symbols=[
            'ADA-PERP', 'ALGO-PERP', 'ALT-PERP', 'ATOM-PERP', 'BCH-PERP',
            'BNB-PERP', 'BSV-PERP', 'BTC-PERP', 'BTMX-PERP', 'DOGE-PERP',
            'DRGN-PERP', 'EOS-PERP', 'ETC-PERP'
        ],
            channels=[TICKER],
            callbacks={
                TICKER: ticker,
                TRADES: TradeCallback(trade)
            }))
    f.add_feed(
        Bybit(symbols=['BTC-USDT', 'BTC-USD'],
              channels=[FUTURES_INDEX],
              callbacks={
                  OPEN_INTEREST: OpenInterestCallback(oi),
                  FUTURES_INDEX: FuturesIndexCallback(futures_index)
              }))
    f.add_feed(
        Bybit(symbols=['BTC-USDT', 'BTC-USD'],
              channels=[L2_BOOK, TRADES],
              callbacks={
                  TRADES: trade,
                  L2_BOOK: book
              }))
    f.add_feed(BLOCKCHAIN,
               symbols=['BTC-USD', 'ETH-USD'],
               channels=[L2_BOOK, TRADES],
               callbacks={
                   L2_BOOK: BookCallback(book),
                   TRADES: trade
               })
    f.add_feed(
        Bitmax(symbols=['XRP-USDT', 'BTC-USDT'],
               channels=[L2_BOOK],
               callbacks={
                   TRADES: trade,
                   L2_BOOK: book
               }))
    f.add_feed(
        Bitflyer(symbols=['BTC-JPY'],
                 channels=[L2_BOOK, TRADES, TICKER],
                 callbacks={
                     L2_BOOK: book,
                     BOOK_DELTA: delta,
                     TICKER: ticker,
                     TRADES: trade
                 }))
    f.add_feed(
        BinanceFutures(symbols=['BTC-USDT'],
                       channels=[TICKER],
                       callbacks={TICKER: ticker}))
    f.add_feed(
        BinanceFutures(subscription={
            TRADES: ['BTC-USDT'],
            CANDLES: ['BTC-USDT', 'BTC-USDT-PINDEX']
        },
                       callbacks={
                           CANDLES: candle_callback,
                           TRADES: trade
                       }))

    f.run()