如何在Python中创建1分钟的蜡烛来交易对?

发布于 2025-01-21 10:46:27 字数 339 浏览 6 评论 0原文

我试图弄清楚如何使用Python中的Binance软件包创建一个1分钟的烛台,用于交易对BTC-USD。

到目前为止,我可以弄清楚这一点:

from binance.client import Client

candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

for candlestick in candles:
    print(candlestick)

但是我不太确定这是否正确。

I'm trying to figure out how to create a 1 minute candlestick for trading pair BTC-USD using the binance package in Python.

So far I can get this down:

from binance.client import Client

candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

for candlestick in candles:
    print(candlestick)

But I'm not quite sure if this is correct.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

大姐,你呐 2025-01-28 10:46:27

假设您已经使用client(capital-case)或至少是异步等效asyncclient来创建client,那么以下内容将打印出最多的打印最近的1分钟烛台。例如,

from binance import Client

client = Client(api_key = 'Enter Your Key', api_secret = 'Enter Your Secret Key')
candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

print(candles[-1])
print(f"\nLength of candles: {len(candles)}")

默认

[1650011040000,       # timestamp (opening time)
 '40203.99000000',    # opening price
 '40237.03000000',    # high price
 '40198.01000000',    # low price 
 '40198.01000000',    # closing price
 '20.07769000',       # volume
 1650011099999,       # timestamp (closing time)
 '807538.88381350',   # quote asset volume
 488,                 # number of trades
 '6.25446000',        # taker buy base asset volume
 '251560.61470140',   # taker buy quote asset volume
 '0']                 # ignore

Length of candles: 500

情况下,蜡烛将是btcusdt交易对的最后500 1分钟蜡烛(每个蜡烛本身)的列表,您可以使用get_klines方法指定最多1000个最后1分钟蜡烛。最新的蜡烛是在列表的末尾蜡烛,最古老的是一开始。

因此,您的代码(假设您已经使用了clientryncclient create client)应该打印所有最后1分钟蜡烛的500个最古老到最近的。

可以找到与蜡烛相对应的列表中的每个元素的详细信息在这里。特别是,在上面的输出(显示了最新的1分钟烛台)中,第一个要素是时间戳,第二个要素是开业价格,第三个高价,低价的第四个,第五个,第五次。 ,其他要素不是交易蜡烛的严格组成部分,而是有用的信息(例如量)。

顺便说一句,如果您不想仅限于最新的500 1分钟蜡烛,则可以使用get_historical_klines方法,该方法允许您指定开始日期/时间和结束日期/时间(仍然限制为每次通话1000蜡烛)。

Assuming that you have used Client (capital-case) or at least the asynchronous equivalent AsyncClient to create client, then the following will print the most recent 1-minute candlestick. For example,

from binance import Client

client = Client(api_key = 'Enter Your Key', api_secret = 'Enter Your Secret Key')
candles = client.get_klines(symbol='BTCUSDT', interval=Client.KLINE_INTERVAL_1MINUTE)

print(candles[-1])
print(f"\nLength of candles: {len(candles)}")

Output

[1650011040000,       # timestamp (opening time)
 '40203.99000000',    # opening price
 '40237.03000000',    # high price
 '40198.01000000',    # low price 
 '40198.01000000',    # closing price
 '20.07769000',       # volume
 1650011099999,       # timestamp (closing time)
 '807538.88381350',   # quote asset volume
 488,                 # number of trades
 '6.25446000',        # taker buy base asset volume
 '251560.61470140',   # taker buy quote asset volume
 '0']                 # ignore

Length of candles: 500

By default, candles will be a list of the last 500 1-minute candles (each candle itself represented by a list) for the BTCUSDT trading pair and you can specify a maximum of 1000 last 1-minute candles using the get_klines method. The most recent candles are at the end of the list candles and the oldest are at the beginning.

So your code (assuming you have used Client or AsyncClient to create client) should be printing all 500 of the last 1-minute candles starting from the oldest to the most recent.

The details of each element in a list corresponding to a candle can be found here. In particular, in the output above (showing the most recent 1-minute candlestick), the first element is the timestamp, the second element is the opening price, the third the high price, the fourth the low price, the fifth the closing price, and the other elements are not strictly components of a trading candle but are useful information nonetheless (e.g. volume).

By the way, if you do not want to be restricted to only the most recent 500 1-minute candles, you can make use of the get_historical_klines method which allows you to specify a start date/time and end date/time (still limited to 1000 candles per call).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文