1

我可以执行以下操作来检索我在 Bittrex 上的持有并将其写入文件,以便我可以在另一个程序中使用它:

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)

这给出了一个如下所示的文件:

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

现在我想对 fetch_tickers 做同样的事情,但我只得到一个带有一个转储的文件。到目前为止我做了什么:

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)

这给了我一个看起来像这样的文件:

{'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'}},

有谁知道我如何将所有代码放在不同的行上?应该不难,但我对python不太熟悉。

  • 编辑 *

我试过这个,但给出了一个错误:

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'
4

0 回答 0