diff --git a/回测数据/haha b/回测数据/haha index 56a5f7f..7688748 100644 --- a/回测数据/haha +++ b/回测数据/haha @@ -110,6 +110,11 @@ if __name__ == '__main__': 这样获取的, -这样的数据,我需要解析开盘价格和结束价格,open就是开盘价格,close就是结束价格,然后id是时间戳,我需要对比,# 第一种情况:当前一笔是涨的,这里又有两种情况。第一种情况:前一笔是涨的,然后当前一笔的价格,开盘的价格小于前一笔,结盘的价格大于前一笔;第二种情况:前一笔是跌的,当前一笔的开盘价格低于前一笔的结盘价格,当前一笔的结盘价格,大于前一笔的开盘价格 , +这样的数据,我需要解析开盘价格和结束价格,open就是开盘价格,close就是结束价格,然后id是时间戳,我需要对比, +# 第一种情况:当前一笔是涨的,这里又有两种情况。第一种情况:前一笔是涨的,当前一笔的价格,开盘的价格小于前一笔,结盘的价格大于前一笔;第二种情况:前一笔是跌的,当前一笔的开盘价格低于前一笔的结盘价格,当前一笔的结盘价格大于前一笔的开盘价格, +# 第二种情况:当前一笔是跌的,这里又有两种情况。第一种情况:前一笔是跌的,当前一笔的价格,开盘的价格大于前一笔,结盘的价格小于前一笔;第二种情况,前一笔是涨的,当前一笔的开盘价格大于前一笔的结盘价格,当前价格的结盘价格小于前一笔的开盘价格, -这样就是当前价格从k线图中看,就是前一笔的开盘价和结盘价处于当前一笔的中间包裹住的情况,然后需要查询出来 \ No newline at end of file +然后我需要预测出胜率,当前一笔是涨的,两笔后的结算价格大于当前一笔的结算价格,就是赚, +当前一笔是跌的,两笔后的结算价格小于当前一笔的结算价格,就是赚, + +这样就是当前价格从k线图中看,就是前一笔的开盘价和结盘价处于当前一笔的开盘价和结盘价中间包裹住的情况,然后需要查询出来 \ No newline at end of file diff --git a/回测数据/接口分析.py b/回测数据/接口分析.py new file mode 100644 index 0000000..8f6180d --- /dev/null +++ b/回测数据/接口分析.py @@ -0,0 +1,58 @@ +import datetime +import requests + +headers = { + 'accept': 'application/json, text/plain, */*', + '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 = int(start_of_day.timestamp()) + end_timestamp = int(end_of_day.timestamp()) + + params = { + 'symbol': 'ETH-USDT', + 'period': '1min', + 'start': start_timestamp, + 'end': 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']) + + for idx in range(1, len(sorted_data)): + prev = sorted_data[idx - 1] # 前一笔 + curr = sorted_data[idx] # 当前一笔 + + po, pc = float(prev['open']), float(prev['close']) + co, cc = float(curr['open']), float(curr['close']) + + prev_up = pc > po # 前一笔涨 + curr_up = cc > co # 当前一笔涨 + + # -------- 第一种情况:当前是涨 -------- + if curr_up: + # ① 前一笔是涨的 + if prev_up and (co < po and cc > pc): + print(f"涨-包裹(前涨,当前涨) id={curr['id']} 前={po,pc} 当前={co,cc}") + # ② 前一笔是跌的 + if not prev_up and (co < pc and cc > po): + print(f"涨-包裹(前跌,当前涨) id={curr['id']} 前={po,pc} 当前={co,cc}") + + # -------- 第二种情况:当前是跌 -------- + else: + # ① 前一笔是跌的 + if not prev_up and (co > po and cc < pc): + print(f"跌-包裹(前跌,当前跌) id={curr['id']} 前={po,pc} 当前={co,cc}") + # ② 前一笔是涨的 + if prev_up and (co > pc and cc < po): + print(f"跌-包裹(前涨,当前跌) id={curr['id']} 前={po,pc} 当前={co,cc}") diff --git a/回测数据/接口分析,回测胜率.py b/回测数据/接口分析,回测胜率.py new file mode 100644 index 0000000..f91a8b7 --- /dev/null +++ b/回测数据/接口分析,回测胜率.py @@ -0,0 +1,80 @@ +import datetime +import requests +import pandas as pd + +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, 24) + 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) + params = { + 'symbol': 'ETH-USDT', + 'period': '1min', + 'start': int(start_of_day.timestamp()), + 'end': int(end_of_day.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']) + + signals = 0 + wins = 0 + + for idx in range(1, len(sorted_data) - 2): # 需要至少留两根K线做验证 + prev = sorted_data[idx - 1] # 前一笔 + curr = sorted_data[idx] # 当前这笔 + future = sorted_data[idx + 2] # 两根后的K线 + + prev_open, prev_close = float(prev['open']), float(prev['close']) + curr_open, curr_close = float(curr['open']), float(curr['close']) + future_close = float(future['close']) + + # 当前为涨 + if curr_close > curr_open: + # 前一笔涨 + 包裹 + if prev_close > prev_open and curr_open < prev_open and curr_close > prev_close: + signals += 1 + if future_close > curr_close: + wins += 1 + # 前一笔跌 + 反包 + elif prev_close < prev_open and curr_open < prev_close and curr_close > prev_open: + signals += 1 + if future_close > curr_close: + wins += 1 + + # 当前为跌 + elif curr_close < curr_open: + # 前一笔跌 + 包裹 + if prev_close < prev_open and curr_open > prev_open and curr_close < prev_close: + signals += 1 + if future_close < curr_close: + wins += 1 + # 前一笔涨 + 反包 + elif prev_close > prev_open and curr_open > prev_close and curr_close < prev_open: + signals += 1 + if future_close < curr_close: + wins += 1 + + if signals > 0: + win_rate = wins / signals * 100 + print(f"信号数={signals}, 胜率={win_rate:.2f}%") + else: + print("没有找到符合条件的形态") diff --git a/回测数据/接口分析,绘制k线.py b/回测数据/接口分析,绘制k线.py new file mode 100644 index 0000000..8e08a7a --- /dev/null +++ b/回测数据/接口分析,绘制k线.py @@ -0,0 +1,120 @@ +import datetime +import requests +import pandas as pd +import plotly.graph_objects as go + +headers = { + 'accept': 'application/json, text/plain, */*', + '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', +} + +def fetch_kline(): + time_ser = datetime.datetime(2025, 9, 26) + 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 = int(start_of_day.timestamp()) + end_timestamp = int(end_of_day.timestamp()) + + params = { + 'symbol': 'ETH-USDT', + 'period': '1min', + 'start': start_timestamp, + 'end': 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']) + + df = pd.DataFrame(sorted_data) + df['Date'] = pd.to_datetime(df['id'], unit='s') + df.set_index('Date', inplace=True) + df.rename(columns={ + 'open': 'Open', + 'close': 'Close', + 'high': 'High', + 'low': 'Low', + 'amount': 'Volume' + }, inplace=True) + df[['Open', 'Close', 'High', 'Low', 'Volume']] = df[['Open', 'Close', 'High', 'Low', 'Volume']].astype(float) + + return df + +def detect_signals(df): + signals = [] + for idx in range(1, len(df)): + prev = df.iloc[idx - 1] + curr = df.iloc[idx] + + po, pc = prev['Open'], prev['Close'] + co, cc = curr['Open'], curr['Close'] + + prev_up = pc > po + curr_up = cc > co + + if curr_up: + if prev_up and (co < po and cc > pc): + signals.append((curr.name, 'bull')) + if not prev_up and (co < pc and cc > po): + signals.append((curr.name, 'bull')) + else: + if not prev_up and (co > po and cc < pc): + signals.append((curr.name, 'bear')) + if prev_up and (co > pc and cc < po): + signals.append((curr.name, 'bear')) + return signals + +def plot_signals(df, signals): + fig = go.Figure() + + # 蜡烛图 + fig.add_trace(go.Candlestick( + x=df.index, + open=df['Open'], + high=df['High'], + low=df['Low'], + close=df['Close'], + name='K线', + increasing_line_color='green', + decreasing_line_color='red' + )) + + # 标记信号 + bull_x = [dt for dt, sig in signals if sig == 'bull'] + bull_y = [df.loc[dt, 'Close'] for dt, sig in signals if sig == 'bull'] + + bear_x = [dt for dt, sig in signals if sig == 'bear'] + bear_y = [df.loc[dt, 'Close'] for dt, sig in signals if sig == 'bear'] + + fig.add_trace(go.Scatter( + x=bull_x, y=bull_y, + mode="markers", + marker=dict(symbol="triangle-up", size=12, color="lime"), + name="看涨信号" + )) + + fig.add_trace(go.Scatter( + x=bear_x, y=bear_y, + mode="markers", + marker=dict(symbol="triangle-down", size=12, color="orange"), + name="看跌信号" + )) + + # 美化布局 + fig.update_layout( + title="ETH-USDT 包裹信号", + xaxis_title="时间", + yaxis_title="价格 (USDT)", + template="plotly_dark", # 黑色主题,比较炫酷 + xaxis_rangeslider_visible=False, # 隐藏默认范围滑块 + hovermode="x unified" # 鼠标悬浮时统一显示数据 + ) + + fig.show() + +if __name__ == "__main__": + df = fetch_kline() + signals = detect_signals(df) + plot_signals(df, signals)