gfrdegdergr

This commit is contained in:
27942
2025-09-25 18:32:16 +08:00
parent 0cdb915939
commit 38c5714591

95
回测数据/test1.py Normal file
View File

@@ -0,0 +1,95 @@
import datetime
import requests
import plotly.graph_objects as go
headers = {
'accept': 'application/json, text/plain, */*',
'accept-language': 'zh,zh-CN;q=0.9,zh-HK;q=0.8,en;q=0.7',
'cache-control': 'no-cache',
'origin': 'https://www.websea.com',
'pragma': 'no-cache',
'priority': 'u=1, i',
'referer': 'https://www.websea.com/',
'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"',
'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/140.0.0.0 Safari/537.36',
}
if __name__ == '__main__':
time_ser = datetime.datetime(2025, 9, 25)
start_of_day = time_ser.replace(hour=0, minute=0, second=0, microsecond=0)
end_of_day = time_ser.replace(hour=23, minute=59, second=59, microsecond=0)
start_timestamp = start_of_day.timestamp()
end_timestamp = end_of_day.timestamp()
params = {
'symbol': 'ETH-USDT',
'period': '1min',
'start': int(start_timestamp),
'end': int(end_timestamp),
}
response = requests.get('https://capi.websea.com/webApi/market/getKline', params=params, headers=headers)
data = response.json()['result']['data']
sorted_data = sorted(data, key=lambda x: x['id'])
# 处理成列表
times = [datetime.datetime.fromtimestamp(d['id']) for d in sorted_data]
opens = [float(d['open']) for d in sorted_data]
highs = [float(d['high']) for d in sorted_data]
lows = [float(d['low']) for d in sorted_data]
closes = [float(d['close']) for d in sorted_data]
# 找到策略点位
signal_times = []
signal_prices = []
for i in range(1, len(sorted_data)):
current_data = sorted_data[i]
prev_data = sorted_data[i - 1]
if float(current_data['open']) < float(current_data['close']): # 当前上涨
if float(prev_data['open']) < float(prev_data['close']): # 前一笔上涨
if float(current_data['open']) < float(prev_data['open']) and float(current_data['close']) > float(prev_data['close']):
signal_times.append(datetime.datetime.fromtimestamp(current_data['id']))
signal_prices.append(float(current_data['close']))
else: # 前一笔下跌
if float(current_data['open']) < float(prev_data['close']) and float(current_data['close']) > float(prev_data['open']):
signal_times.append(datetime.datetime.fromtimestamp(current_data['id']))
signal_prices.append(float(current_data['close']))
# 绘制K线图
fig = go.Figure(data=[
go.Candlestick(
x=times,
open=opens,
high=highs,
low=lows,
close=closes,
name="K线"
)
])
# 添加信号点
fig.add_trace(go.Scatter(
x=signal_times,
y=signal_prices,
mode="markers",
marker=dict(color="red", size=10, symbol="triangle-up"),
name="做多信号"
))
fig.update_layout(
title="ETH-USDT 1分钟K线图 (含做多信号)",
xaxis_title="时间",
yaxis_title="价格",
xaxis_rangeslider_visible=False,
template="plotly_dark"
)
fig.show()