簡體   English   中英

獲取幣安現貨市場當前交易對的當前價格

[英]Retrieve current price for currentr trading pairs in the spot market of binance

我正在嘗試獲取所有當前在 binance 中交易的 usdt 對的當前價格,我使用這段代碼,但我得到了所有價格,即使是已下架的貨幣對,我不知道這怎么可能,以及如何限制當前交易對:

def get_price(PAIR_WITH):
    '''Return the current price for all coins on binance'''

    initial_price = {}
    prices = client.get_all_tickers()

    for coin in prices:

        # only Return USDT pairs and exlcude margin symbols like BTCDOWNUSDT
        if PAIR_WITH in coin['symbol'] and all(item not in coin['symbol'] for item in FIATS):
            initial_price[coin['symbol']] = { 'price': coin['price'], 'time': datetime.now()}

    return initial_price

usdtprices = get_price(PAIR_WITH = 'USDT')

這是使用 Binance API 獲得它的方法

import requests
import json
import re
from pprint import pprint

payload={}
headers = {
  'Content-Type': 'application/json'
}

url = "https://api.binance.com/api/v3/exchangeInfo"

response = requests.request("GET", url, headers=headers, data=payload)
markets = json.loads(response.text)
active = {}
for market in markets['symbols']:
    symbol = market['symbol']
    status = market['status']
    active[symbol] = status == 'TRADING'

url = "https://api.binance.com/api/v3/ticker/24hr"

response = requests.request("GET", url, headers=headers, data=payload)
tickers = json.loads(response.text)

prices = {}
for ticker in tickers:
    symbol = ticker['symbol']
    isUSDT = re.search("USDT$", symbol)
    if (isUSDT and active[symbol]):
        prices[symbol] = ticker['lastPrice']

pprint(prices)

您可以使用他們的postman 示例獲取所有 binance 請求的示例並導出代碼


你也可以使用CCXT ,它是一個類似於 SDK 的庫,用於大約 120 次交換,然后你可以對任何這些交換使用相同的代碼,它還處理簽名和精度限制等類似的東西,它們有一堆ccxt/examples

import ccxt
from pprint import pprint

exchange = ccxt.binance({})

tickers = exchange.fetch_tickers()

prices = {}
for symbol, ticker in tickers.items():
    market = exchange.market(symbol)
    if market['quote'] == 'USDT' and market['active'] == True:
        prices[symbol] = ticker['close']

pprint(prices)

這兩個示例都返回資產交易的最新價格,但您可以將close價替換為bidask您是否想要人們要求的當前限價,或者將lastPrice替換為bidPrice或詢問askPrice


您可能只使用re過濾BEARDOWNBULL ,但不確定UP因為您可能會排除一些正常市場

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM