You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
1.0 KiB
35 lines
1.0 KiB
import os
|
|
os.environ["http_proxy"] = "http://127.0.0.1:7890"
|
|
os.environ["https_proxy"] = "http://127.0.0.1:7890"
|
|
|
|
# pip install yfinance
|
|
import yfinance as yf
|
|
from datetime import datetime
|
|
|
|
def crypto_last(ticker: str) -> float:
|
|
"""
|
|
返回某个币对 USD 的最新收盘价(1m 线最近一次)
|
|
ticker 示例: "BTC-USD", "ETH-USD", "SOL-USD"
|
|
"""
|
|
data = yf.download(
|
|
tickers=ticker,
|
|
period="1d",
|
|
interval="1m",
|
|
auto_adjust=True,
|
|
progress=False,
|
|
threads=False
|
|
)
|
|
if data.empty:
|
|
raise RuntimeError("yfinance 返回为空,可能 ticker 写错或网络问题")
|
|
# 关键修正:把 Series 转成标量
|
|
last_close = data["Close"].iloc[-1].item()
|
|
return last_close
|
|
|
|
if __name__ == "__main__":
|
|
pairs = ["BTC-USD", "ETH-USD", "SOL-USD", "SUI-USD"]
|
|
for p in pairs:
|
|
try:
|
|
price = crypto_last(p)
|
|
except Exception as e:
|
|
print(e)
|
|
print(f"{datetime.now():%Y-%m-%d %H:%M:%S} {p}: {price}") |