dededdew
This commit is contained in:
81
test1.py
81
test1.py
@@ -1,27 +1,68 @@
|
||||
import time
|
||||
import datetime
|
||||
import requests
|
||||
|
||||
# 获取当前时间戳
|
||||
current_timestamp = time.time()
|
||||
# 将当前时间戳转换为 datetime 对象
|
||||
current_datetime = datetime.datetime.fromtimestamp(current_timestamp)
|
||||
headers = {
|
||||
'accept': 'application/json, text/plain, */*',
|
||||
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
|
||||
'cache-control': 'no-cache',
|
||||
'dnt': '1',
|
||||
'origin': 'https://www.websea.com',
|
||||
'pragma': 'no-cache',
|
||||
'priority': 'u=1, i',
|
||||
'referer': 'https://www.websea.com/',
|
||||
'sec-ch-ua': '"Chromium";v="142", "Microsoft Edge";v="142", "Not_A Brand";v="99"',
|
||||
'sec-ch-ua-mobile': '?0',
|
||||
'sec-ch-ua-platform': '"Windows"',
|
||||
'sec-fetch-dest': 'empty',
|
||||
'sec-fetch-mode': 'cors',
|
||||
'sec-fetch-site': 'same-site',
|
||||
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0',
|
||||
}
|
||||
|
||||
# 计算距离当前时间最近的整点或 30 分时刻
|
||||
if current_datetime.minute < 30:
|
||||
target_datetime = current_datetime.replace(minute=0, second=0, microsecond=0)
|
||||
# # 获取当前日期(UTC)
|
||||
# today = datetime.datetime.now(datetime.timezone.utc).date()
|
||||
#
|
||||
# # 当天开始和结束时间
|
||||
# start_of_day = datetime.datetime.combine(today, datetime.time.min, tzinfo=datetime.timezone.utc)
|
||||
# end_of_day = datetime.datetime.combine(today, datetime.time.max, tzinfo=datetime.timezone.utc)
|
||||
|
||||
# 获取今天日期(本地时间,中国时区 UTC+8)
|
||||
tz = datetime.timezone(datetime.timedelta(hours=8))
|
||||
today = datetime.datetime.now(tz).date()
|
||||
|
||||
# 当天开始时间(北京时间 00:00)
|
||||
start_of_day = datetime.datetime.combine(today, datetime.time.min, tzinfo=tz)
|
||||
# 当天结束时间(北京时间 23:59:59.999999)
|
||||
end_of_day = datetime.datetime.combine(today, datetime.time.max, tzinfo=tz)
|
||||
|
||||
params = {
|
||||
'symbol': 'BTC-USDT',
|
||||
'period': '30min',
|
||||
'start': int(start_of_day.timestamp()),
|
||||
'end': int(end_of_day.timestamp()),
|
||||
}
|
||||
|
||||
response = requests.get('https://eapi.websea.com/webApi/market/getKline', params=params, headers=headers)
|
||||
data = response.json()["result"]["data"]
|
||||
|
||||
if not data:
|
||||
print("没有获取到数据")
|
||||
else:
|
||||
target_datetime = current_datetime.replace(minute=30, second=0, microsecond=0)
|
||||
# 当前价格 = 最新一条 K 线的 close
|
||||
current_price = float(data[0]['close'])
|
||||
|
||||
# 将目标 datetime 对象转换为时间戳
|
||||
target_timestamp = target_datetime.timestamp()
|
||||
# 当天最高价 = 当天所有 K 线的 high
|
||||
today_high = max(float(item['high']) for item in data)
|
||||
# 当天最低价 = 当天所有 K 线的 low
|
||||
today_low = min(float(item['low']) for item in data)
|
||||
|
||||
print(f"当前时间戳: {current_timestamp}")
|
||||
print(f"目标时间戳: {target_timestamp}")
|
||||
# 24 小时涨幅计算(用最新一条 close 与 24 小时前的价格对比)
|
||||
# 如果接口只能获取当天数据,可能不足 24 小时,可根据需求扩展
|
||||
# 假设获取过去 24 小时数据,使用第一条 K 线的 open 作为 24 小时前价格
|
||||
price_24h_ago = float(data[-1]['open'])
|
||||
change_24h = (current_price - price_24h_ago) / price_24h_ago * 100
|
||||
|
||||
# 进行时间戳比对
|
||||
if current_timestamp == target_timestamp:
|
||||
print("当前时间就是目标时间。")
|
||||
elif current_timestamp < target_timestamp:
|
||||
print(f"当前时间早于目标时间,还需等待 {target_timestamp - current_timestamp} 秒。")
|
||||
else:
|
||||
print(f"当前时间晚于目标时间,已经过去了 {current_timestamp - target_timestamp} 秒。")
|
||||
print(f"当前价格: {current_price}")
|
||||
print(f"24小时涨幅: {change_24h:.2f}%")
|
||||
print(f"当天最高价: {today_high}")
|
||||
print(f"当天最低价: {today_low}")
|
||||
|
||||
Reference in New Issue
Block a user