-1

I'm trying to fetch the orderbooks of crypto exchanges how support a specific pair (for example ETH/BTC). Because my function needs to run every minute it's very time consuming to check this every time. I'm using ccxt to fetch the orderbooks of the exchanges.

With this lines of code I check every exchange.

import ccxt

binance   = ccxt.binance()
livecoin  = ccxt.livecoin()
kucoin    = ccxt.kucoin()
hitbtc    = ccxt.hitbtc()
kraken    = ccxt.kraken()
crex24    = ccxt.crex24()
okex      = ccxt.okex()

headerList = ["time","type","pair"]

try:
    orderbookBinance = binance.fetch_order_book(self.pair,5)
    headerList.append("binance")
    headerList.append("binanceAmount")
except:
    print("Pair isn't available in binance")

try:
    orderbookLivecoin = livecoin.fetch_order_book(self.pair,5)
    headerList.append("livecoin")
    headerList.append("livecoinAmount")
except:
    print("Pair isn't available in livecoin")

try:
    orderbookKucoin = kucoin.fetch_order_book(self.pair,5)
    headerList.append("kucoin")
    headerList.append("kucoinAmount")
except:
    print("Pair isn't available in kucoin")

try:
    orderbookHitbtc = hitbtc.fetch_order_book(self.pair,5)
    headerList.append("hitbtc")
    headerList.append("hitbtcAmount")
except:
    print("Pair isn't available in hitbtc")

try:
    orderbookKraken = kraken.fetch_order_book(self.pair,5)
    headerList.append("kraken")
    headerList.append("krakenAmount")
except:
    print("Pair isn't available in kraken")

try:
    orderbookCrex24 = crex24.fetch_order_book(self.pair,5)
    headerList.append("crex24")
    headerList.append("crex24Amount")
except:
    print("Pair isn't available in crex24")

try:
    orderbookOkex = okex.fetch_order_book(self.pair,5)
    headerList.append("okex")
    headerList.append("okexAmount")
except:
    print("Pair isn't available in okex")

Now I need to add the first line of all the try-blocks if they can exetude. Is this possible in python?

4

1 回答 1

2

You are taking the wrong approach there.

"Lines of code", just as variable names are supposed to be fixed in the program. Python's super-dynamic nature would even allow one to "add lines of code", and recompile the module in runtime - but that would be complicated, complex, overkill and subject to a lot of error, when all you need is a staright-forward approach

What you need is to keep the objects that refer to your external exchanges in a data-structure, such as plain dictionary. Then you just have to iterate over the dictionary to perform the method calls and other actions needed by each of those - and any part of the program can update the dictionary with a simple, ordinary attribution.

import ccxt


exchanges = {}
for exchange_name in "binance livecoin kucoin hitbtc kraken crex24 okex".split():
    exchanges[exchange_name] = getattr(ccxt, exchange_name)()

...
for exchange_name, exchange in exchange.items():
    try:
        orderbook = exchange.fetch_order_book(self.pair,5)
        headerList.append(exchange_name)
        headerList.append(f"{exchange_name}Amount")
    except:
        print(f"Pair isn't available in {exchange_name}")

If in your actual code for some exchanges you need to run different code, just improve the data structure: instead of associating the exchange name directly with the exchange instance, you can create another, inner dictionary, which will contain, in other key/value pairs not only the ccxt exchange instance, but the names of the methods to be called, the parameters to be passed to each, what to check in the response obtained and so on:

The code in your program is fixed! Parts of it can be run depending on configuration data that is present in your data structure.

于 2019-02-01T15:29:19.063 回答