0

下面来自 ccxt 的 python 示例将获取历史报价。

https://github.com/ccxt/ccxt/blob/master/examples/py/binance-fetch-ohlcv-to-csv.py

该代码未显示如何设置时区。如何将获取的报价时间戳设置为亚洲香港/新加坡时区?

我正在使用 python 3.9

4

1 回答 1

1

我认为您可以将带有时区的日期时间字符串传递给您的示例2017-08-17T00:00:00+08:00中的scrape_candles_to_csv函数([亚洲香港/新加坡有 UTC+8 时区)

方法exchange.parse8601正确处理带有时区的字符串

>>> exchange.parse8601('2017-08-17T00:00:00+08:00')
1502899200000
>>> exchange.parse8601('2017-08-17T00:00:00Z')
1502928000000

所以我跑了

scrape_candles_to_csv('binance.csv', 'binance', 3, 'BTC/USDT', '1h', '2021-12-17T00:00:00+08:00', 100)

并得到

100 candles in total from 2021-12-16T16:00:00.000Z to 2021-12-20T19:00:00.000Z
200 candles in total from 2021-12-16T16:00:00.000Z to 2021-12-24T23:00:00.000Z
260 candles in total from 2021-12-16T16:00:00.000Z to 2021-12-27T11:00:00.000Z
260 candles in total from 2021-12-16T16:00:00.000Z to 2021-12-27T11:00:00.000Z
Saved 260 candles from 2021-12-16T16:00:00.000Z to 2021-12-27T11:00:00.000Z to binance.csv

binance.csv

1639670400000,48578.61,48717.43,48302.49,48506.58,1778.62433
1639674000000,48506.57,48520.73,48060.0,48084.14,1593.51908
1639677600000,48084.15,48313.29,47862.6,47983.5,1434.94112
1639681200000,47983.49,48129.67,47666.0,47866.01,1763.75104
1639684800000,47866.01,48387.54,47832.34,47929.53,1510.25811
1639688400000,47929.53,48167.75,47900.0,48089.61,766.62189
1639692000000,48089.61,48167.84,47511.0,47821.27,1428.35052
1639695600000,47821.27,47821.27,47532.86,47632.38,1046.44879
...

第一个时间戳1639670400000等于2017-08-17T00:00:00+08:00

于 2021-12-27T11:49:30.057 回答