1

I can do the following to retrieve and write my holdings on Bittrex to a file so I can use that in another program:

import ccxt
exchange_id = 'bittrex'
exchange_class = getattr(ccxt, exchange_id)
exchange = exchange_class({
    'apiKey': config.BITTREX_API_KEY,
    'secret': config.BITTREX_API_SECRET,
    'timeout': 30000,
    'enableRateLimit': True,
})

account = exchange.fetch_balance()

fieldnames = ['currencySymbol', 'total', 'available', 'updatedAt']
balances = account['info']

fname = 'bittrex_holdings.txt'

# open the file in the write mode
with open(os.path.join(dirname,subfolder,fname), 'w', encoding='UTF8', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writerows(balances)

This gives a file that looks like this:

AKRO,0.00000000,0.00000000,2021-02-09T14:59:54.81Z
ALGO,1000.00000000,1000.00000000,2021-01-03T10:14:22.29Z

Now I would like to do the same with fetch_tickers but I only get a file with one dump. What I did so far:

fname = 'bittrex_prices.txt'

prices = exchange.fetch_tickers()
with open(os.path.join(dirname,subfolder,fname), 'w', encoding='UTF8', newline='') as f:
    print(prices, file=f)

This give me a file that looks like this:

{'4ART/BTC': {'symbol': '4ART/BTC', 'timestamp': None, 'datetime': None, 'high': None, 'low': None, 'bid': 1.98e-06, 'bidVolume': None, 'ask': 2.01e-06, 'askVolume': None, 'vwap': None, 'open': None, 'close': 2e-06, 'last': 2e-06, 'previousClose': None, 'change': None, 'percentage': None, 'average': None, 'baseVolume': None, 'quoteVolume': None, 'info': {'symbol': '4ART-BTC', 'lastTradeRate': '0.00000200', 'bidRate': '0.00000198', 'askRate': '0.00000201'}}, '4ART/USDT': {'symbol': '4ART/USDT', 'timestamp': None, 'datetime': None, 'high': None, 'low': None, 'bid': 0.119, 'bidVolume': None, 'ask': 0.12059, 'askVolume': None, 'vwap': None, 'open': None, 'close': 0.1206, 'last': 0.1206, 'previousClose': None, 'change': None, 'percentage': None, 'average': None, 'baseVolume': None, 'quoteVolume': None, 'info': {'symbol': '4ART-USDT', 'lastTradeRate': '0.12060000', 'bidRate': '0.11900000', 'askRate': '0.12059000'}},

Does anyone know how I can get all tickers on separate lines? Shouldn't be that difficult but I'm not that familiar with python.

  • edit *

I tried this but that gave an error:

account = exchange.fetch_tickers()

fieldnames = ['symbol', 'lastTradeRate', 'bidRate', 'askRate']
prices = account['info']

fname = 'bittrex_prices.txt'

# open the file in the write mode
with open(os.path.join(dirname,subfolder,fname), 'w', encoding='UTF8', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writerows(prices)

    prices = account['info']
KeyError: 'info'
Miguel Terlaak
  • 175
  • 1
  • 13

0 Answers0