示例#1
0
    def __init__(self, config, debug=True):
        key_config = config['apikey']
        self.debug = debug
        self.exchanges = {
            'zb': Zb(key_config['zb']['key'], key_config['zb']['secret']),
            'okex': Okex(key_config['okex']['key'],
                         key_config['okex']['secret']),
            'weex': Weex(key_config['weex']['key'],
                         key_config['weex']['secret']),
            # 'huobipro': Huobipro(key_config['huobipro']['key'], key_config['huobipro']['secret']),
            'bithumb': ccxt.bithumb(),
            'binance': ccxt.binance(),
            'bitflyer': ccxt.bitflyer(),
            'hitbtc': ccxt.hitbtc(),
            'coincheck': ccxt.coincheck(),
            "quoinex": ccxt.quoinex(),
            "bitfinex": ccxt.bitfinex(),
            "hitbtc": ccxt.hitbtc(),
        }

        self.engine = MysqlEngine(config['db']['url'])
        self.pool = ThreadPoolExecutor(
            3)  # for many urls, this should probably be capped at some value.

        self. async = False
示例#2
0
def job():
    ##symbols = ['BCH/USD', 'ETH/USD', 'LTC/USD', 'DASH/USD']
    symbols = ['BCH/USD', 'LTC/USD', 'ETH/USD']
    pairs = [(r, s) for s in symbols for r in symbols if r is not s]
    gdax = ccxt.gdax({'password': '', 'apiKey': '', 'secret': ''})
    gdax.load_markets()
    bitfinex = ccxt.bitfinex({'apiKey': '', 'secret': ''})
    bitfinex.load_markets()
    print("Connected to exchanges")
    exchanges = [gdax, bitfinex]
    for r, s in pairs:
        print("Fetching values")
        asks, bids = {}, {}
        for x in exchanges:
            # TODO check if exchange supports symbol
            rPrice = x.fetch_ticker(r)
            sPrice = x.fetch_ticker(s)
            print(x.name, rPrice['ask'], rPrice['bid'])
            print(x.name, sPrice['bid'], sPrice['ask'])
            asks[x.name] = rPrice['ask'] / rPrice['bid']
            bids[x.name] = sPrice['bid'] / sPrice['ask']
        lowX = min(asks, key=asks.get)
        highX = max(bids, key=bids.get)
        print(r, s, bids[highX] / asks[lowX])
        if (bids[highX] / asks[lowX]) > 1.005:
            print("CONGRATS you found an opportunity")
            print("Short %s on %s, Long %s on %s" % (s, highX, r, lowX))
        time.sleep(1)
def ccxt_get_bitfinex_k_data(symbol='BTC/USDT',
                             timeframe='1m',
                             start_time_str='2018-12-03 00:00:00',
                             limit=1000,
                             timeout=1000):
    '''
    从一个开始时点获取之后的limit根k线数据
    :param symbol: str, just like 'BTC/USDT'
    :param timeframe: str, 三个取值('1m', '5m', '15m')
    :param start_time_str: str,开始时间点
    :param limit: int
    :param timeout: int,超时毫秒(超时秒数乘以1000)
    :return: pandas.DataFrame
    '''
    bitfinex = ccxt.bitfinex({'timeout': timeout})
    local_timestamp = pd.to_datetime(start_time_str).timestamp()
    utc_timestamp = local_timestamp - 8 * 60 * 60
    data = bitfinex.fetch_ohlcv(symbol=symbol,
                                timeframe=timeframe,
                                limit=limit,
                                since=utc_timestamp * 1000)
    result = pd.DataFrame(data)
    result.columns = ['time', 'open', 'high', 'low', 'close', 'volume']
    result['time'] = pd.to_datetime(result['time'],
                                    unit='ms') + pd.Timedelta(hours=8)
    return result
示例#4
0
def get_or_create_worker(exchange_name):
    worker = EXCHANGES.get(exchange_name)
    if worker is None:
        if exchange_name == 'huobi':
            worker = ccxt.huobipro({
                'proxies':
                PROXY_SETTING if USING_PROXY else None,
                'timeout':
                30000,
            })
        elif exchange_name == 'binance':
            worker = ccxt.binance({
                'proxies': PROXY_SETTING if USING_PROXY else None,
                'apiKey': BINANCE_APIKEY,
                'secret': BINANCE_APISECRET,
                'timeout': 30000,
                'enableRateLimit': True,
            })
        elif exchange_name == 'bitfinex':
            worker = ccxt.bitfinex({
                'proxies':
                PROXY_SETTING if USING_PROXY else None,
                'timeout':
                30000,
            })
        else:
            print(f"{exchange_name} not supported")
            raise Exception(f"{exchange_name} worker unsupported")

        worker.load_markets()

        EXCHANGES[exchange_name] = worker

    return worker
示例#5
0
 def __init__(self):
     #try except
     #Initiate exchange data with objects for each exchange
     binance = ccxt.binance()
     bittrex = ccxt.bittrex()
     bitfinex = ccxt.bitfinex()
     kraken = ccxt.kraken()
     kucoin = ccxt.kucoin()
     #Dictionary with the ccxt objects
     self.exchanges = {
         'binance': binance,
         'bittrex': bittrex,
         'bitfinex': bitfinex,
         'kraken': kraken,
         'kucoin': kucoin
     }
     #Get a list of tickers from Binance exchange to use throughout
     self.ticker_list = []
     self.get_tickers_list()
     #CREATE A DATAFRAME FOR THE PRICE AND VOLUME DATA
     self.volume = pd.DataFrame(index=self.ticker_list,
                                columns=list(self.exchanges.keys()))
     self.pricedf = pd.DataFrame(index=self.ticker_list,
                                 columns=list(self.exchanges.keys()))
     self.spread = pd.DataFrame(index=self.ticker_list,
                                columns=list(self.exchanges.keys()))
示例#6
0
    def transforming_user_dic(self, dic, inter, update, context):
        user_dic = copy.deepcopy(dict(dic))

        for exch in inter:
            try:
                exchange_bin = ccxt.binance()
                exchange_exm = ccxt.exmo()
                exchange_bifin = ccxt.bitfinex()
                exchanges = {'Exmo': exchange_exm.fetchTickers(), 'Binance': exchange_bin.fetchTickers(),
                             'Bitfinex': exchange_bifin.fetchTickers()}
                api_pairs = exchanges[exch]
                user_pairs = user_dic[exch]
            except:
                print('проблемы с вызовом api')
                self.checking_and_activate(user_dic, update, context)

            intersection = list(set(api_pairs.keys()) & set(user_pairs.keys()))
            for key in intersection:
                t = list(user_pairs[key])
                if len(t) < 3:
                    t.append(api_pairs[key]['bid'])
                    t.append(api_pairs[key]['ask'])
                else:
                    t[2] == api_pairs[key]['bid']
                    t[3] == api_pairs[key]['ask']
                user_pairs[key] = t
            user_dic[exch] = user_pairs
        try:
            self.compare_bid_ask(user_dic, update, context)
        except:
            print('something wrong with compare bid_ask function')
示例#7
0
def update_monero_payments():
    accounts = Account.objects.all()
    payment_accounts = dict()
    payment_ids = set()
    for account in accounts:
        payment_accounts[account.payment_id] = account
        payment_ids.add(account.payment_id)
    # Get payments for all payment IDs..
    if payment_ids:
        try:
            monero_data = ccxt.bitfinex().fetch_ticker('XMR/USD')
            monero_usd_rate = monero_data['last']
        except Exception as e:
            raise MoneroException(
                'Could not get Monero data from bitfinex: {}'.format(e))

        last_block_height = MoneroLog.objects.max('block_height')
        mw = apps.get_app_config('account').mw
        payments = mw.get_bulk_payments(payment_ids, last_block_height)
        new_payment_ids = ()

        for payment in payments:
            dollar_amount = payment['amount'] * monero_usd_rate
            minutes = dollar_amount * settings.DOLLARS_PER_MINUTE
            monero_payment = MoneroPayment(
                tx_hash=payment['tx_hash'],
                amount=payment['amount'],
                block_height=payment['block_height'],
                payment_id=payment['payment_id'],
                unlock_time=payment['unlock_time'],
                account_minutes=minutes,
                usd_rate=monero_usd_rate,
                dollar_value=dollar_amount)

            # Update block height
            if payment['block_height'] > last_block_height:
                last_block_height = payment['block_height']

            new_payment_ids.add(payment['payment_id'])
            monero_payment.save()

            # Update the account
            account = payment_accounts[payment['payment_id']]
            minutes = minutes_paid_for(payment['amount'])

            if account.expires < timezone.now():
                account.expires = timezone.now() + datetime.timedelta(
                    minutes=minutes)
            else:
                account.expires = account.expires + datetime.timedelta(
                    minutes=minutes)

            account.save()

            # Log the update
            log = MoneroLog(new_payments=len(payments),
                            payment_ids=len(new_payment_ids),
                            block_height=last_block_height)

            log.save()
示例#8
0
    def _load_bitfinex_ticker_data(symbol):
        bitfinex = ccxt.bitfinex({'verbose': True})
        markets = bitfinex.load_markets()
        ticker = bitfinex.fetch_ticker(symbol)

        ticker.pop('info')
        ticker2 = {}
        ticker2['symbol'] = ticker.pop('symbol')
        ticker2['priceChange'] = ticker.pop('change')
        ticker2['priceChangePercent'] = ticker.pop('percentage')
        ticker2['weightedAvgPrice'] = ticker.pop('vwap')
        ticker2['prevClosePrice'] = ticker.pop('previousClose')
        ticker2['lastPrice'] = ticker.pop('last')
        ticker2['lastQty'] = 'none'
        ticker2['bidPrice'] = ticker.pop('bid')
        ticker2['bidQty'] = ticker.pop('bidVolume')
        ticker2['askPrice'] = ticker.pop('ask')
        ticker2['askQty'] = ticker.pop('askVolume')
        ticker2['openPrice'] = ticker.pop('open')
        ticker2['highPrice'] = ticker.pop('high')
        ticker2['lowPrice'] = ticker.pop('low')
        ticker2['volume'] = ticker.pop('baseVolume')
        ticker2['quoteVolume'] = ticker.pop('quoteVolume')
        ticker2['openTime'] = ticker.pop('datetime')
        ticker2['closeTime'] = ticker.pop('timestamp')
        ticker2['firstId'] = 'none'
        ticker2['lastId'] = 'none'
        ticker2['count'] = 'none'
        ticker = ticker2
        return ticker
示例#9
0
def bitfinexData():
    betime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
    try:
        bitfinex = ccxt.bitfinex()
        coindata = list(bitfinex.fetch_tickers().values())
        columns = ['symbol', 'ask', 'bid', 'close', 'last', 'high', 'low', 'info', 'datetime']
        df = pd.DataFrame(coindata)
        df = df[columns]
        df['exchange'] = 'bitfinex'
        df['vol'] = [i['volume'] for i in df['info']]
        df['datetime'] = [time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(i))) for i in [x['timestamp'] for x in df['info']]]
        df = df.drop(['info'], axis=1)
        df['basecurrency'] = list(i.split('/')[0] for i in df['symbol'])
        df['quotcurrency'] = list(i.split('/')[1] for i in df['symbol'])
        df['createtime'] = starttime
        df['codeid'] = 2
        try:
            engine = create_engine("mysql+pymysql://coin:[email protected]:3306/coindata?charset=utf8")
            df.to_sql('coindata_tickers', con=engine, if_exists='append', index=False)
        except:
            with open('coindataerr.log','a') as f:
                f.write('%s:bitfinex数据入库失败!\n' % betime)
                pass
    except:
        with open('coindataerr.log','a') as f:
            f.write('%s:bitfinex数据获取失败!\n' % betime)
        pass
示例#10
0
def btcPrice(interval="1h"):
    client = ccxt.bitfinex()
    #print(client.fetch_markets())
    data = client.fetch_ohlcv("BTC/USD", interval)
    datum = []
    for i in range(len(data)):
        datum.append(float(data[i][-2]))
    return list(reversed(datum))
示例#11
0
 def __init__(self):
     self.bitfinex = ccxt.bitfinex({'nonce': ccxt.bitfinex.milliseconds})
     if (settings['bitfinex_exporter'].get('api_key')
             and (settings['bitfinex_exporter'].get('api_secret'))):
         self.bitfinex.apiKey = settings['bitfinex_exporter'].get('api_key')
         self.bitfinex.secret = settings['bitfinex_exporter'].get(
             'api_secret')
         self.hasApiCredentials = True
示例#12
0
def create_exchanges():
    """
        instantiate the markets
        to include more exchanges use this function.
        new exchanges need to be hand-coded here
    """
    coinbasepro = ccxt.coinbasepro({
        'apiKey': api_keys.coinbasepro['apiKey'],
        'secret': api_keys.coinbasepro['secret'],
    })

    poloniex = ccxt.poloniex({
        'apiKey': api_keys.poloniex['apiKey'],
        'secret': api_keys.poloniex['secret'],
    })

    bittrex = ccxt.bittrex({
        'apiKey': api_keys.bittrex['apiKey'],
        'secret': api_keys.bittrex['secret'],
    })

    binance = ccxt.binance({
        'apiKey': api_keys.binance['apiKey'],
        'secret': api_keys.binance['secret'],
    })

    bitfinex = ccxt.bitfinex({
        'apiKey': api_keys.bitfinex['apiKey'],
        'secret': api_keys.bitfinex['secret'],
    })

    kraken = ccxt.kraken({
        'apiKey': api_keys.kraken['apiKey'],
        'secret': api_keys.kraken['secret'],
    })

    bitmex = ccxt.bitmex({
        'apiKey': api_keys.bitmex['apiKey'],
        'secret': api_keys.bitmex['secret'],
    })

    okex = ccxt.okex({
        'apiKey': api_keys.okex['apiKey'],
        'secret': api_keys.okex['secret'],
    })

    exchanges = [
        coinbasepro, poloniex, bittrex, binance, bitfinex, kraken, bitmex, okex
    ]
    timing_limits = [.35, .35, 1, .35, 2, 1, 1,
                     .35]  # requesting period limit per exchange

    for exchange, timing in zip(exchanges, timing_limits):
        g_storage.timer[exchange.name] = [0, timing]

    # exchanges.pop(exchanges.index(kraken))

    return exchanges
示例#13
0
def main():
    goBackDays = 30
    finex = ccxt.bitfinex()
    print("Trying to get %d days worth of data from %s" %
          (goBackDays, finex.name))
    data = fetchCandleData(finex, _getOneOClockStamp(goBackDays))
    maList, emaList = findTradingSignals(data)
    for ma in emaList:
        print(ma)
示例#14
0
文件: bitfinex.py 项目: jm16/backtest
 def __init__(self, symbol, timescale):
     self.exchange = ccxt.bitfinex({
         'rateLimit': 10000,
         'enableRateLimit': True,
         # 'verbose': True,
     })
     self.symbol = symbol
     self.timescale = timescale
     self.timescale_msec = self._timescale_to_msec()
     self.data = []
示例#15
0
def Collect_Gain_Report():
    print("started billing")
    from account.models import Trading_Platform, MyUser
    import ccxt  # noqa: E402
    for user in MyUser.objects.all():
        context = {}
        for exchange in ['Quadrigacx', 'Quoine', 'Kraken', 'Bitfinex']:
            try:
                api_credentials = Trading_Platform.objects.get(
                    user=user, trading_platform=exchange)
            except:
                api_credentials = 404

            if exchange == "Quadrigacx" and api_credentials != 404:
                context['Quadrigacx_data'] = ccxt.quadrigacx({
                    "uid":
                    str(api_credentials.client_id),
                    "apiKey":
                    api_credentials.api_key,
                    "secret":
                    api_credentials.secret
                })
                context['Quadrigacx_transactions'], context[
                    'Quadrigacx_data'] = context[
                        'Quadrigacx_data'].privatePostUserTransactions(), dir(
                            context['Quadrigacx_data'])
            elif exchange == "Quoine" and api_credentials != 404:
                context['Quoinex_data'] = ccxt.quoinex({
                    "apiKey":
                    api_credentials.api_key,
                    "secret":
                    api_credentials.secret
                })
                context['Quoinex_transactions'], context[
                    'Quoinex_data'] = context['Quoinex_data'].privateGetTrades(
                    ), dir(context['Quoinex_data'])
            elif exchange == "Kraken" and api_credentials != 404:
                context['Kraken_data'] = ccxt.kraken({
                    "apiKey":
                    api_credentials.api_key,
                    "secret":
                    api_credentials.secret
                })
                context['Kraken_transactions'] = context[
                    'Kraken_data'].privatePostTradesHistory()
            elif exchange == "Bitfinex" and api_credentials != 404:
                context['Bitfinex_data'] = ccxt.bitfinex({
                    "apiKey":
                    api_credentials.api_key,
                    "secret":
                    api_credentials.secret
                })
                context['Bitfinex_transactions'] = context[
                    'Bitfinex_data'].privatePostMytrades()
        print(context)
示例#16
0
 def __init__(self):
     self.exchanges = {
         'binanceus': ccxt.binanceus(),
         'bittrex': ccxt.bittrex(),
         # 'coinbase': ccxt.coinbase(),   # coinbase has most currency pairs, by like 3 times the next highest, consider removing. Also coinbase limits API to 3-6 calls/sec
         'gemini': ccxt.gemini(),
         # 'kraken': ccxt.kraken(),       # updating their API
         'livecoin': ccxt.livecoin(),
         'theocean': ccxt.theocean(),
         # 'okex': ccxt.okex(),            #Canadian, does not allow us
         'bitmart': ccxt.bitmart(),
         # 'cex': ccxt.cex(),  # EU
         # 'bitbay': ccxt.bitbay(),  # EU, Updating API
         # 'bcex': ccxt.bcex(),            #candian exch, their API is updating
         # 'bitbay': ccxt.bitbay(),
         'paymium': ccxt.paymium(),
         'binance': ccxt.binance(),
         'okcoin': ccxt.okcoin(),
         'bitfinex': ccxt.bitfinex()  # non-US
     }
     # creates a markets variable in each exchange instance.  ex. exchages[0].markets will return markets
     self.loadMarkets()
     # these are tickers available on exchnage, but not US customers, or don't allow deposits/withdrawals
     self.unavailableTickers = {
         'binanceus': [],
         'bittrex': [
             'LUNA/BTC', 'ABBC/BTC', 'Capricoin/BTC', 'DRGN/BTC', 'CVT/BTC',
             'NXT/BTC'
         ],
         # 'coinbase': [],
         'gemini': [],
         # 'kraken': [],               # Updating their API
         'livecoin': [
             'BTM/BTC', 'BTM/ETH', 'NANO/BTC', 'NANO/ETH', 'XTZ/BTC',
             'XTZ/ETH', 'THETA/BTC', 'THETA/ETH', 'ABBC/BTC', 'ABBC/ETH',
             'AE/BTC', 'AE/ETH', 'IOST/BTC', 'IOST/ETH'
         ],
         'theocean': [],
         # 'okex': ['AET/ETH','AET/BTC'],             # does not allow US, but allows canadian
         'bitmart': [],
         # 'cex': [],
         # 'bitbay': [],
         # 'bcex': [],             #candian exch, their API is updating
         'bitbay': [],
         'paymium': [],
         'binance': [],
         'okcoin': [],
         'bitfinex': []
     }
     self.commonTickers = self.getCommonTickers()
     # then only call fetch_tickers for common_tickers between exchanges
     self.minProfit = 1  # percent profit
     # in USD NOTE: still need to incorporate this. I think coinmarketcap API has a quick conversion call
     self.minVolume = 200
     self.txfrCosts = []
def init_exchange(exchange):
    params = {
        'apiKey': api_key,
        'secret': api_secret
    }
    if exchange == 'bitmex':
        return ccxt.bitmex(params)
    elif exchange == 'bitfinex':
        return ccxt.bitfinex(params)
    else:
        return
示例#18
0
def init_supported_exchanges():
    objects = {
        "binance": ccxt.binance(),
        "bitfinex": ccxt.bitfinex(),
        "bitflyer": ccxt.bitflyer(),
        "bitstamp": ccxt.bitstamp(),
        "bittrex": ccxt.bittrex(),
        "coinbase": ccxt.coinbase(),
        "kraken": ccxt.kraken(),
        "poloniex": ccxt.poloniex()
    }
    return objects
示例#19
0
def get_ohlc_data(local_data=True):
    if local_data:
        local_data_name = 'btc_usd_1h.csv'
        local_data_file = os.path.join(basedir, local_data_name)
        # dataframe = pd.read_csv(local_data_file, index_col='dt', parse_dates=True)
        dataframe = pd.read_csv(local_data_file,
                                skiprows=0,
                                header=0,
                                parse_dates=True,
                                index_col=0)
        dataframe['openinterest'] = 0
        # print(dataframe.head(10))
        # 会包括9.25,只是因为9.25的0点是24日的24点
        return bt.feeds.PandasData(dataname=dataframe,
                                   fromdate=datetime.datetime(2017, 10, 1),
                                   todate=datetime.datetime(2017, 11, 24))
    else:
        exchange = 'bitfinex'
        limit = 300
        freq = '1h'
        freq_mapper = {'1h': 60 * 60 * 1000}
        proxy_setting = {
            'http': 'socks5://127.0.0.1:1080',
            'https': 'socks5h://127.0.0.1:1080'
        }
        worker = ccxt.bitfinex({
            'proxies': proxy_setting,
            'timeout': 30000,
        })
        worker.load_markets()
        symbol = 'BTC/USD'
        symbol_real = worker.find_symbol(symbol)
        # print(symbol_real)
        end_time = worker.milliseconds()
        since_time = end_time - limit * freq_mapper[freq]
        data = worker.fetch_ohlcv(symbol=symbol_real,
                                  timeframe=freq,
                                  limit=limit,
                                  since=since_time)

        df = pd.DataFrame(
            data, columns=['date', 'open', 'high', 'low', 'close', 'volume'])
        df['date'] = pd.to_datetime(df['date'],
                                    unit='ms')  # CCXT返回的是ts,需要转换成毫秒
        df['openinterest'] = 0
        df.set_index('date', inplace=True)
        df.dropna(how='all')  # 数据整形
        fromdate = datetime.datetime.fromtimestamp(since_time / 1000)
        todate = datetime.datetime.fromtimestamp(end_time / 1000)
        return bt.feeds.PandasData(dataname=df,
                                   fromdate=fromdate,
                                   todate=todate)
示例#20
0
 def __init__(self, time_interval='15m', setProxy=True):
     print('__init__')
     self.setProxy = setProxy
     self.URL = 'https://api.bitfinex.com/v1'
     self.bitfinex2 = ccxt.bitfinex2()  # 创建交易所,此处为bitfinex v2 协议
     self.bitfinex1 = ccxt.bitfinex()  # 创建交易所,此处为bitfinex v1 协议
     self.time_interval = time_interval
     self.proxies = {
             "http": "socks5h://127.0.0.1:1080",
             "https": "socks5h://127.0.0.1:1080"
         }
     if self.setProxy:
         self.bitfinex2.proxies = self.proxies
         self.bitfinex1.proxies = self.proxies
示例#21
0
def bitfinexData():
    bitfinex = ccxt.bitfinex()
    coindata = list(bitfinex.fetch_tickers().values())

    columns = ['ask', 'bid', 'close', 'high', 'low', 'last', 'symbol', 'average', 'baseVolume', 'timestamp']
    df = pd.DataFrame(coindata)
    df = df[columns]
    df['exchange'] = 'bitfinex'
    df['createtime'] = starttime
    df['baseCurrency'] = list(i.split('/')[0] for i in df['symbol'])
    df['quotCurrency'] = list(i.split('/')[1] for i in df['symbol'])

    engine = create_engine("mysql+pymysql://quziyou:0739#[email protected]:5389/coinData?charset=utf8")
    df.to_sql('coindata_tickers', con=engine, if_exists='append', index=False)
示例#22
0
 def __init__(self, exchange):
     # super(Bitfinex2, self).__init__()
     self.exchange = exchange
     self.exchange_v1 = ccxt.bitfinex()
     self.exchange_v1.apiKey = self.exchange.apiKey
     self.exchange_v1.secret = self.exchange.secret
     self.exchange_v1.proxies = self.exchange.proxies
     self.exchange_v1.userAgent = self.exchange.userAgent
     self.exchange_v1.enableRateLimit = self.exchange.enableRateLimit
     self.bfx2api = Bitfinex2API()
     self.bfx2api.api_key = self.exchange.apiKey
     self.bfx2api.api_secret = self.exchange.secret
     self.bfx2api.user_agent = self.exchange.userAgent
     self.bfx2api.proxies = self.exchange.proxies
示例#23
0
def exchangeObject(exchange_in):
    exchanges = [ccxt.acx(),ccxt.bitbay(),ccxt.bitfinex(),ccxt.bitflyer(),ccxt.bithumb(),ccxt.bitlish(),ccxt.bitmarket(),ccxt.bitmex(),ccxt.bitso(),
                         ccxt.bitstamp(),ccxt.bitstamp1(),ccxt.bittrex(),ccxt.bl3p(),ccxt.bleutrade(),ccxt.btcbox(),ccxt.btcchina(),ccxt.btcexchange(),ccxt.btcmarkets(),ccxt.btctradeua(),ccxt.btcturk(),
                         ccxt.btcx(),ccxt.bxinth(),ccxt.ccex(),ccxt.cex(),ccxt.chbtc(),ccxt.chilebit(),ccxt.coincheck(),ccxt.coinfloor(),ccxt.coingi(),ccxt.coinmarketcap(),ccxt.coinmate(),
                         ccxt.coinsecure(),ccxt.coinspot(),ccxt.cryptopia(),ccxt.dsx(),ccxt.exmo(),ccxt.flowbtc(),ccxt.foxbit(),ccxt.fybse(),ccxt.fybsg(),ccxt.gatecoin(),ccxt.gateio(),ccxt.gdax(),
                         ccxt.gemini(),ccxt.getbtc(),ccxt.hitbtc(),ccxt.huobi(),ccxt.huobicny(),ccxt.independentreserve(),ccxt.itbit(),ccxt.jubi(),ccxt.kraken(),ccxt.kucoin(),
                         ccxt.kuna(),ccxt.lakebtc(),ccxt.liqui(),ccxt.livecoin(),ccxt.luno(),ccxt.mercado(),ccxt.mixcoins(),ccxt.nova(),ccxt.okcoincny(),ccxt.okcoinusd(),ccxt.okex(),ccxt.paymium(),
                         ccxt.poloniex(),ccxt.qryptos(),ccxt.quadrigacx(),ccxt.southxchange(),ccxt.surbitcoin(),ccxt.therock(),ccxt.tidex(),ccxt.urdubit(),ccxt.vaultoro(),ccxt.vbtc(),
                         ccxt.virwox(),ccxt.wex(),ccxt.xbtce(),ccxt.yobit(),ccxt.yunbi(),ccxt.zaif(),ccxt.zb()]

    for count, exchange in enumerate([str(x) for x in exchanges]):
            if exchange_in.lower() in exchange:
                return exchanges[count]
                break
示例#24
0
    def __init__(self):
        with open('bitfinex_exchange/bitfinex_secrets.json') as f:
            secrets = json.load(f)

        self.bfx = ccxt.bitfinex({
            'apiKey': secrets['apiKey'],
            'secret': secrets['secret'],
            'verbose':
            True,  # switch it to False if you don't want the HTTP log
        })

        self._get_wallets()

        self._update_book_orders()

        super(BitfinexExchange, self).__init__()
示例#25
0
def get_exchange_symbols():

    assets = Assets()
    slugs = get_coincap_slugs()
    exchanges = ['binance', 'bitfinex', 'bittrex', 'poloniex', 'kraken']

    coins = {}

    # loop through exchanges, grab symbols
    for exchange_str in exchanges:
        if exchange_str == 'binance':
            exchange = ccxt.binance()
        elif exchange_str == 'bitfinex':
            exchange = ccxt.bitfinex()
        elif exchange_str == 'bittrex':
            exchange = ccxt.bittrex()
        elif exchange_str == 'poloniex':
            exchange = ccxt.poloniex()
        elif exchange_str == 'kraken':
            exchange = ccxt.kraken()
        else:
            exchange = None

        if exchange:
            exchange.load_markets()
            for sym in exchange.symbols:
                if '/BTC' in sym:
                    sym = sym.replace('/BTC', '')
                    if sym not in coins:
                        coins[sym] = {'exchanges': [exchange_str]}
                    else:
                        coins[sym]['exchanges'].append(exchange_str)

    # handle felix and coincap assets
    for coin in coins:
        if coin in assets:
            coins[coin]['exchanges'].append('felix')
        if coin in slugs:
            coins[coin]['id'] = slugs[coin]['id']
            coins[coin]['name'] = slugs[coin]['name']
        else:
            coins[coin]['id'] = None
            coins[coin]['name'] = None

    sorted_coins = collections.OrderedDict(sorted(coins.items()))
    return sorted_coins
示例#26
0
 def initialize(self):
     for exchange_name in self.exchanges.keys():
         if exchange_name == EXCHANGE_BITFINEX:
             self.exchanges[exchange_name]["client"] = ccxt.bitfinex2({
                 'apiKey':
                 self.exchanges[EXCHANGE_BITFINEX]["api_key"],
                 'secret':
                 self.exchanges[EXCHANGE_BITFINEX]["api_secret"],
                 'enableRateLimit':
                 True
             })
             self.exchanges[exchange_name]["client_"] = ccxt.bitfinex({
                 'apiKey':
                 self.exchanges[EXCHANGE_BITFINEX]["api_key"],
                 'secret':
                 self.exchanges[EXCHANGE_BITFINEX]["api_secret"],
                 'enableRateLimit':
                 True
             })
         elif exchange_name == EXCHANGE_BINANCE:
             self.exchanges[exchange_name]["client"] = ccxt.binance({
                 'apiKey':
                 self.exchanges[EXCHANGE_BINANCE]["api_key"],
                 'secret':
                 self.exchanges[EXCHANGE_BINANCE]["api_secret"],
                 'enableRateLimit':
                 True,
                 'options': {
                     'adjustForTimeDifference': True
                 }
             })
         elif exchange_name == EXCHANGE_KRAKEN:
             self.exchanges[exchange_name]["client"] = ccxt.kraken({
                 'apiKey':
                 self.exchanges[EXCHANGE_KRAKEN]["api_key"],
                 'secret':
                 self.exchanges[EXCHANGE_KRAKEN]["api_secret"],
                 'enableRateLimit':
                 True
             })
         elif exchange_name == EXCHANGE_OANDA:
             self.exchanges[exchange_name]["client"] = oanda.oanda(self.exchanges[EXCHANGE_OANDA]["account_id"], \
                                                                   self.exchanges[EXCHANGE_OANDA]["account_token"])
         else:
             print("Please configure a valid Exchange.")
             sys.exit(1)
示例#27
0
 def setup_clients(self):
     self.exchanges['Bittrex'] = ccxt.bittrex({
         'apiKey':
         self.meta['Bittrex']['public'],
         'secret':
         self.meta['Bittrex']['secret'],
     })
     self.exchanges['Poloniex'] = ccxt.poloniex({
         'apiKey':
         self.meta['Poloniex']['public'],
         'secret':
         self.meta['Poloniex']['secret'],
     })
     self.exchanges['YoBit'] = ccxt.yobit({
         'apiKey':
         self.meta['YoBit']['public'],
         'secret':
         self.meta['YoBit']['secret'],
     })
     self.exchanges['HitBTC'] = ccxt.hitbtc2({
         'apiKey':
         self.meta['HitBTC']['public'],
         'secret':
         self.meta['HitBTC']['secret'],
     })
     self.exchanges['Tidex'] = ccxt.tidex({
         'apiKey':
         self.meta['Tidex']['public'],
         'secret':
         self.meta['Tidex']['secret'],
     })
     self.exchanges['Binance'] = ccxt.binance({
         'options': {
             'adjustForTimeDifference': True
         },
         'apiKey':
         self.meta['Binance']['public'],
         'secret':
         self.meta['Binance']['secret'],
     })
     self.exchanges['Bitfinex'] = ccxt.bitfinex({
         'apiKey':
         self.meta['Bitfinex']['public'],
         'secret':
         self.meta['Bitfinex']['secret'],
     })
示例#28
0
 def __init__(self):
     self.k = ct.kraken()
     self.y = ct.yobit()
     self.p = ct.poloniex()
     self.coinmarket = ct.coinmarketcap()
     self.liqui = ct.bitfinex()
     print(ct.exchanges)
     print(self.k.hasFetchOHLCV, self.k.rateLimit)
     print(self.y.hasFetchOHLCV, self.y.rateLimit)
     print(self.p.hasFetchOHLCV, self.p.rateLimit)
     # print(self.coinmarket.hasFetchOHLCV, self.coinmarket.rateLimit)
     keys_conf = conf.TradeKeys()
     #print(keys_conf.keys_info)
     self.k.apiKey = keys_conf.keys_info['kraken']['public']
     self.k.secret = keys_conf.keys_info['kraken']['secret']
     #self.k.load_markets()
     print(self.k.symbols)
示例#29
0
def bitfinex():
    bfx = ccxt.bitfinex()
    params = {'limit_asks':3000, 'limit_bids':3000}
    data = bfx.fetch_order_book('BTC/USD', params=params)
    asks = data['asks']
    bids = data['bids']

    price = (asks[0][0]+bids[0][0])/2
    bids_info = get_ratio_sum(bids, 'bids')
    asks_info = get_ratio_sum(asks, 'asks')

    res = {}
    res.update(bids_info)
    res.update(asks_info)
    res['price'] = price
    res['exchange'] = 'bitfinex'

    return res
示例#30
0
    def test_ccxt(self):
        #print(ccxt.markets)  # print a list of all available market classes
        hitbtc = ccxt.hitbtc({'verbose': True})
        bitmex = ccxt.bitmex()
        huobi = ccxt.huobi()
        bitfinex = ccxt.bitfinex({
            'apiKey': 'YOUR-API-KEY',
            'secret': 'YOUR-API-KEY',
        })
        okcoinusd = ccxt.okcoinusd({
            'apiKey': 'YOUR-API-KEY',
            'secret': 'YOUR-API-KEY',
        })

        trade_amount = 0.1
        market = 'ETH_USD'
        ticker = okcoinusd.fetch_ticker(market)
        print(ticker)
        print('debug %s' % okcoinusd.create_limit_buy_order(
            market, trade_amount, ticker['ask'] + 1))
示例#31
0
    def __init__(self, config, debug=True):
        key_config = config['apikey']
        self.debug = debug
        self.exchanges = {
            'zb': Zb(key_config['zb']['key'], key_config['zb']['secret']),
            'okex': Okex(key_config['okex']['key'], key_config['okex']['secret']),
            'weex': Weex(key_config['weex']['key'], key_config['weex']['secret']),
            # 'huobipro': Huobipro(key_config['huobipro']['key'], key_config['huobipro']['secret']),
            'bithumb': ccxt.bithumb(),
            'binance': ccxt.binance(),
            'bitflyer': ccxt.bitflyer(),
            'hitbtc': ccxt.hitbtc(),
            'coincheck': ccxt.coincheck(),
            "quoinex": ccxt.quoinex(),
            "bitfinex": ccxt.bitfinex(),
            "hitbtc": ccxt.hitbtc(),
        }

        self.engine = MysqlEngine(config['db']['url'])
        self.pool = ThreadPoolExecutor(3)  # for many urls, this should probably be capped at some value.

        self.async = False
def get_bitfinex_data(symbol, timeframe, length, include_current_candle = True, file_format = ['txt', 'csv'], api_cooldown_seconds = 0):

    """
    Download Crypto OHLCV Data from Bitfinex.

    The function fetches OHLCV data via the specified paramteres from the public Bitfinex API,
    merges it into a Pandas Data Frame and optionally saves the Data Frame as .txt and/or .csv file.
    Through concatenation more data than specified by the maximal request length of the exchange
    can be fetched.
    Additional timeframes are available (merged from smaller timeframes).

    Parameters
    ----------
    symbol : str
        Symbol of the underlying data, e.g. 'BTC/USD'

    timeframe : str
        Timeframe of the underlying data, e.g. '1h'

    length : int
        Number of Timeframes (Data length), e.g. 500

    include_current_candle : bool
        Include the current, not finished, candle, e.g. True

    file_format : str, list
        Which file format should be used to save the data, e.g. 'csv' or ['txt', 'csv']

    api_cooldown_seconds : int, float
        When fetching a lot of data this allows each request to the API to be made after
        the amount in seconds has passed.
        This prevents possible API errors due to too many requests within a short time.

    Returns
    -------
    Pandas Data Frame and optionally .txt and/or .csv file.
        A table with headers 'Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume' containing the requested data.

    Dependencies
    ------------
    import ccxt
    import time
    import csv
    import pandas as pd

    """

    data_limit = 1000 # int(ccxt.bitfinex({}).describe()['ratelimit']) returns 1500

    bitfinex_symbols = [market['symbol'] for market in ccxt.bitfinex({}).fetch_markets()]
    bitfinex_timeframes = {'1m': 60*1000, '5m': 5*60*1000, '15m': 15*60*1000, '30m': 30*60*1000, '1h': 60*60*1000, '3h': 3*60*60*1000, '6h': 6*60*60*1000, '12h': 12*60*60*1000, '1d': 24*60*60*1000, '1w': 7*24*60*60*1000, '2w': 14*24*60*60*1000}
    missing_timeframes = {'3m': ['1m', 3, 60*1000], '2h': ['1h', 2, 60*60*1000], '4h': ['1h', 4, 60*60*1000], '3d': ['1d', 3, 24*60*60*1000]}

    timestamp = []
    openx = []
    high = []
    low = []
    close = []
    volume = []

    timestamp_temp = []
    openx_temp = []
    high_temp = []
    low_temp = []
    close_temp = []
    volume_temp = []
    
    proceed = True

    if symbol not in bitfinex_symbols:
        print("ERROR:\tPlease use one of the following Symbols:\n\t" + "\n\t".join(sorted(bitfinex_symbols)))
        proceed = False

    if not(timeframe in bitfinex_timeframes.keys() or timeframe in missing_timeframes.keys()):
        print("ERROR:\tPlease use one of the following Timeframes:\n\tMinute:\t'1m', '3m', '5m', '15m', '30m',\n\tHour:\t'1h', '2h', '3h', '4h', '6h', '12h',\n\tDay:\t'1d', '3d',\n\tWeek:\t'1w', '2w'")
        proceed = False
    
    if not isinstance(length, int) or length < 1:
        print("ERROR:\tPlease use a reasonable number for the argument 'length'.")
        proceed = False

    if type(include_current_candle) != bool:
        print("ERROR:\tPlease use boolean values for argument 'include_current_candle' only.")
        proceed = False

    if not(file_format == 'txt' or file_format == 'csv' or file_format == ['txt'] or file_format == ['csv'] or file_format == ['csv', 'txt'] or file_format == ['txt', 'csv'] or
            file_format == '' or file_format == [] or file_format == [''] or file_format == None):
        print("ERROR:\tAllowed values for argument 'file_format' are 'csv', 'txt', ['csv', 'txt'].\n\tIf you do not wish to save the data please use either '', [''], [], None.")
        proceed = False
    
    if file_format == '' or file_format == [] or file_format == [''] or file_format == None:
        file_format = []

    if file_format == 'csv':
        file_format = ['csv']
    if file_format == 'txt':
        file_format = ['txt']

    if not isinstance(api_cooldown_seconds, (int, float)) or api_cooldown_seconds < 0 or api_cooldown_seconds > 60:
        print("ERROR:\tPlease use a reasonable 'api_cooldown_seconds' number of seconds (between 0 and 60).")
        proceed = False

    if proceed == True:
        if timeframe in missing_timeframes.keys():
            if include_current_candle == True:
                n_bulk = (length * missing_timeframes[timeframe][1]) // data_limit
                remainder = (length * missing_timeframes[timeframe][1]) % data_limit
            if include_current_candle == False:
                n_bulk = ((length + 1) * missing_timeframes[timeframe][1]) // data_limit
                remainder = ((length + 1) * missing_timeframes[timeframe][1]) % data_limit

            while n_bulk > 0:
                since = round(ccxt.bitfinex({}).milliseconds() - (ccxt.bitfinex({}).milliseconds() % (missing_timeframes[timeframe][1] * missing_timeframes[timeframe][2])) - (n_bulk * data_limit * missing_timeframes[timeframe][2]) - (remainder * missing_timeframes[timeframe][2]))
                for block in ccxt.bitfinex({}).fetch_ohlcv(symbol = symbol, timeframe = missing_timeframes[timeframe][0], since = (since + (missing_timeframes[timeframe][1] * missing_timeframes[timeframe][2])), limit = data_limit):
                    timestamp_temp.append(block[0])
                    openx_temp.append(block[1])
                    high_temp.append(block[2])
                    low_temp.append(block[3])
                    close_temp.append(block[4])
                    volume_temp.append(block[5])
                n_bulk -= 1
                if n_bulk > 0 or remainder > 0:
                    time.sleep(api_cooldown_seconds)

            if remainder > 0:
                since = round(ccxt.bitfinex({}).milliseconds() - (ccxt.bitfinex({}).milliseconds() % (missing_timeframes[timeframe][1] * missing_timeframes[timeframe][2])) - (remainder * missing_timeframes[timeframe][2]))
                for block in ccxt.bitfinex({}).fetch_ohlcv(symbol = symbol, timeframe = missing_timeframes[timeframe][0], since = (since + (missing_timeframes[timeframe][1] * missing_timeframes[timeframe][2])), limit = remainder + 1):
                    timestamp_temp.append(block[0])
                    openx_temp.append(block[1])
                    high_temp.append(block[2])
                    low_temp.append(block[3])
                    close_temp.append(block[4])
                    volume_temp.append(block[5])

            if length > 1:
                for i in [num for num in range(0, len(timestamp_temp), missing_timeframes[timeframe][1])][:-1]:
                    timestamp.append(timestamp_temp[i])
                    openx.append(openx_temp[i])
                    high.append(max(high_temp[i:i + missing_timeframes[timeframe][1]]))
                    low.append(min(low_temp[i:i + missing_timeframes[timeframe][1]]))
                    close.append(close_temp[i + (missing_timeframes[timeframe][1] - 1)])
                    volume.append(sum(volume_temp[i:i + missing_timeframes[timeframe][1]]))

                # append all available remaining smaller timeframes to the lists
                timestamp.append(timestamp_temp[i + missing_timeframes[timeframe][1]])
                openx.append(openx_temp[i + missing_timeframes[timeframe][1]])
                high.append(max(high_temp[i + missing_timeframes[timeframe][1]:]))
                low.append(min(low_temp[i + missing_timeframes[timeframe][1]:]))
                close.append(close_temp[-1])
                volume.append(sum(volume_temp[i + missing_timeframes[timeframe][1]:]))

            if length == 1:
                timestamp.append(timestamp_temp[0])
                openx.append(openx_temp[0])
                high.append(max(high_temp[0:]))
                low.append(min(low_temp[0:]))
                close.append(close_temp[-1])
                volume.append(sum(volume_temp[0:]))

        if timeframe not in missing_timeframes.keys():
            if include_current_candle == True:
                n_bulk = length // data_limit
                remainder = length % data_limit
            if include_current_candle == False:
                n_bulk = (length + 1) // data_limit
                remainder = (length + 1) % data_limit

            while n_bulk > 0:
                since = ccxt.bitfinex({}).milliseconds() - (n_bulk * data_limit * bitfinex_timeframes[timeframe]) - (remainder * bitfinex_timeframes[timeframe])
                for block in ccxt.bitfinex({}).fetch_ohlcv(symbol = symbol, timeframe = timeframe, since = since, limit = data_limit):
                    timestamp.append(block[0])
                    openx.append(block[1])
                    high.append(block[2])
                    low.append(block[3])
                    close.append(block[4])
                    volume.append(block[5])
                n_bulk -= 1
                if n_bulk > 0 or remainder > 0:
                    time.sleep(api_cooldown_seconds)

            if remainder > 0:
                since = ccxt.bitfinex({}).milliseconds() - (remainder * bitfinex_timeframes[timeframe])
                for block in ccxt.bitfinex({}).fetch_ohlcv(symbol = symbol, timeframe = timeframe, since = since, limit = remainder):
                    timestamp.append(block[0])
                    openx.append(block[1])
                    high.append(block[2])
                    low.append(block[3])
                    close.append(block[4])
                    volume.append(block[5])

        data_identifier = 'bitfinex_' + ''.join(symbol.split('/')) + '_' + str(timeframe) + '_' + str(length) + ('_including_current_candle' if include_current_candle == True else '_NOT_including_current_candle')

        if file_format != []:
            for ending in file_format:
                # specify where to save the files e.g.
                #with open('/Users/username/Desktop/' + data_identifier + '.' + str(ending), 'w') as csvfile:
                with open(data_identifier + '.' + str(ending), 'w') as csvfile:
                    writer = csv.writer(csvfile, delimiter = ',', quotechar = '|', quoting = csv.QUOTE_MINIMAL)
                    writer.writerow([head for head in ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume']])
                    if include_current_candle == True:
                        write = zip(timestamp, openx, high, low, close, volume)
                    if include_current_candle == False:
                        write = zip(timestamp[:-1], openx[:-1], high[:-1], low[:-1], close[:-1], volume[:-1])
                    for entry in write:
                        writer.writerow(entry)

        if include_current_candle == True:
            df = pd.DataFrame(list(zip(timestamp, openx, high, low, close, volume)), columns = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume'])
            df.name = data_identifier
            return df
        if include_current_candle == False:
            df = pd.DataFrame(list(zip(timestamp[:-1], openx[:-1], high[:-1], low[:-1], close[:-1], volume[:-1])), columns = ['Timestamp', 'Open', 'High', 'Low', 'Close', 'Volume'])
            df.name = data_identifier
            return df
示例#33
0
import ccxt

bitfinex = ccxt.bitfinex()
bitfinex.verbose = True
ohlcvs = bitfinex.fetch_ohlcv('ETH/BTC', '30m', 1504224000000)

dt = bitfinex.iso8601(ohlcvs[0][0])
print(dt)  # should print '2017-09-01T00:00:00.000Z'
示例#34
0
import ccxt
print(ccxt.bitfinex().fetch_ticker('BTC/USD'))
# -----------------------------------------------------------------------------

import ccxt  # noqa: E402

# -----------------------------------------------------------------------------
# common constants

msec = 1000
minute = 60 * msec
hold = 30

# -----------------------------------------------------------------------------

exchange = ccxt.bitfinex({
    'rateLimit': 10000,
    'enableRateLimit': True,
    # 'verbose': True,
})

# -----------------------------------------------------------------------------

from_datetime = '2017-01-01 00:00:00'
from_timestamp = exchange.parse8601(from_datetime)

# -----------------------------------------------------------------------------

now = exchange.milliseconds()

# -----------------------------------------------------------------------------

data = []