import requests
import datetime

cookies = {
    'bitget_lang': 'zh-CN',
    '_dx_kvani5r': '24e0dceb12f85d9a1279183ca29c09f772f8c61124e4eca228d8dd909c9b24b0019c2b9a',
    'OptanonAlertBoxClosed': 'Wed%20Sep%2024%202025%2017:12:32%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)',
    'OptanonConsent': 'isMarketing=1&isStatistic=1',
    '_cfuvid': 'IHXjOVVjx5iewfwL1nrgeHsZ6whu9s4iYYYvNvJvOQM-1758867462928-0.0.1.1-604800000',
    'dy_token': '68d630093VQFDNL1nUkv46lfDdhjHGYpoz9dddX1',
    'g_state': '{"i_l":0}',
    'theme': 'white',
    'BITGET_LOCAL_COOKIE': '{%22bitget_lang%22:%22zh-CN%22%2C%22bitget_unit%22:%22CNY%22%2C%22bitget_showasset%22:true%2C%22bitget_theme%22:%22white%22%2C%22bitget_layout%22:%22right%22%2C%22bitget_valuationunit%22:0%2C%22bitget_valuationunit_new%22:1%2C%22bitget_valuationunitandfiat%22:1%2C%22bitgt_login%22:false%2C%22theme%22:%22black%22%2C%22brand_simulation_trade%22:false%2C%22bitget_currency%22:%22%22}',
    'bt_rtoken': 'upex:session:id:1d00ce6487e37f4d7230a5aafe3ffc82e799156a86e9778bacb2310c7cf39b98',
    'bt_uid': '619BB9EC4D4A3F9FC1AC092D38818A00',
    'bt_sessonid': '0bf4ef42-798c-4f8d-8fe5-c07364d3b6b5',
    'bt_newsessionid': 'eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ZjJmNDBjYS0zNTkyLTRiNmItOTdiMy01OWQ5MTU5Y2E2YmQ0NzQ5ODIwMTAiLCJ1aWQiOiIxV00rVjRqcTJYSVRQemdJVWhUMnNRPT0iLCJzdWIiOiJsenoqKipvbSIsImlwIjoiM3dNK05yTkxjQXVQTG95dWZkWkI1QT09IiwiZGlkIjoiSjBZZjQ1NkV5RUxaSGhaa2tWYnNQZTVlSFVYYUNKZS9CM21wTHJ5SHJWMTEyUklzWlFLTTVIQmtTNHlUUmhuVyIsInN0cyI6MCwiaWF0IjoxNzU4ODY3NTgxLCJleHAiOjE3NTkyOTk1ODEsInB1c2hpZCI6ImRka1NMR1VDak9Sd1pFdU1rMFlaMWc9PSIsImlzcyI6InVwZXgifQ.Buwhs7UF7dOnOAso8l-RIyc_A47UdWSeVMiwIonOdiY',
    'USD': 'CNY',
    '__cf_bm': '3AMy5P3a.qp5uEqe9d_DonCDcL621CKULNKaUpfn2rE-1758868368-1.0.1.1-JzjFQidqwDxVV3gJH8ls0NeCGe.nsK.SFYP0hPXUcupuLTBpscmBYQRMfAjeOlnaZRBsQROFL2fETfXatDwQwDHU7aHOT4IMwECKB3sTIN4',
}

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh,zh-CN;q=0.9,zh-HK;q=0.8,en;q=0.7',
    'baggage': 'sentry-environment=online,sentry-release=905e3a416e2b0ad3c98a82ab9b94dd87f1a5c1fd,sentry-public_key=8aeef93d782e4ef5ae75634bfee9b590,sentry-trace_id=e7afec8af26a4480893c8b58257af49b',
    'cache-control': 'no-cache',
    'custom-token': '765eb29bf89843258756f8851ed5a48f',
    'deviceid': '7c88d47fdc39e5c422eff197b10d3485',
    'dy-token': '68d630093VQFDNL1nUkv46lfDdhjHGYpoz9dddX1',
    'language': 'zh_CN',
    'locale': 'zh_CN',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.bitget.com/zh-CN/futures/usdt/ETHUSDT',
    '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-origin',
    'sentry-trace': 'e7afec8af26a4480893c8b58257af49b-8aacec1747b38bd7-0',
    'terminalcode': 'cc98a9dd01af7e2d50d9b9858723b25e',
    'terminaltype': '1',
    'tm': '1758868503025',
    'uhti': 'w17588685039139806ab12716',
    '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',
    'website': 'mix',
    # 'cookie': 'bitget_lang=zh-CN; _dx_kvani5r=24e0dceb12f85d9a1279183ca29c09f772f8c61124e4eca228d8dd909c9b24b0019c2b9a; OptanonAlertBoxClosed=Wed%20Sep%2024%202025%2017:12:32%20GMT+0800%20(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4); OptanonConsent=isMarketing=1&isStatistic=1; _cfuvid=IHXjOVVjx5iewfwL1nrgeHsZ6whu9s4iYYYvNvJvOQM-1758867462928-0.0.1.1-604800000; dy_token=68d630093VQFDNL1nUkv46lfDdhjHGYpoz9dddX1; g_state={"i_l":0}; theme=white; BITGET_LOCAL_COOKIE={%22bitget_lang%22:%22zh-CN%22%2C%22bitget_unit%22:%22CNY%22%2C%22bitget_showasset%22:true%2C%22bitget_theme%22:%22white%22%2C%22bitget_layout%22:%22right%22%2C%22bitget_valuationunit%22:0%2C%22bitget_valuationunit_new%22:1%2C%22bitget_valuationunitandfiat%22:1%2C%22bitgt_login%22:false%2C%22theme%22:%22black%22%2C%22brand_simulation_trade%22:false%2C%22bitget_currency%22:%22%22}; bt_rtoken=upex:session:id:1d00ce6487e37f4d7230a5aafe3ffc82e799156a86e9778bacb2310c7cf39b98; bt_uid=619BB9EC4D4A3F9FC1AC092D38818A00; bt_sessonid=0bf4ef42-798c-4f8d-8fe5-c07364d3b6b5; bt_newsessionid=eyJhbGciOiJIUzI1NiJ9.eyJqdGkiOiI4ZjJmNDBjYS0zNTkyLTRiNmItOTdiMy01OWQ5MTU5Y2E2YmQ0NzQ5ODIwMTAiLCJ1aWQiOiIxV00rVjRqcTJYSVRQemdJVWhUMnNRPT0iLCJzdWIiOiJsenoqKipvbSIsImlwIjoiM3dNK05yTkxjQXVQTG95dWZkWkI1QT09IiwiZGlkIjoiSjBZZjQ1NkV5RUxaSGhaa2tWYnNQZTVlSFVYYUNKZS9CM21wTHJ5SHJWMTEyUklzWlFLTTVIQmtTNHlUUmhuVyIsInN0cyI6MCwiaWF0IjoxNzU4ODY3NTgxLCJleHAiOjE3NTkyOTk1ODEsInB1c2hpZCI6ImRka1NMR1VDak9Sd1pFdU1rMFlaMWc9PSIsImlzcyI6InVwZXgifQ.Buwhs7UF7dOnOAso8l-RIyc_A47UdWSeVMiwIonOdiY; USD=CNY; __cf_bm=3AMy5P3a.qp5uEqe9d_DonCDcL621CKULNKaUpfn2rE-1758868368-1.0.1.1-JzjFQidqwDxVV3gJH8ls0NeCGe.nsK.SFYP0hPXUcupuLTBpscmBYQRMfAjeOlnaZRBsQROFL2fETfXatDwQwDHU7aHOT4IMwECKB3sTIN4',
}
if __name__ == '__main__':

    params = {
        'symbolId': 'ETHUSDT_UMCBL',
        'kLineStep': '1m',
        'kLineType': '1',
        'endTime': '1758864600000',
    }

    response = requests.get(
        'https://www.bitget.com/v1/kline/getMoreKlineDataV2',
        params=params, cookies=cookies, headers=headers
    )

    raw_data = response.json()["data"]

    print(raw_data)

    # 转换成字典格式，方便后续处理
    data = []
    for i in raw_data:
        data.append({
            'id': int(i[0]) // 1000,  # 秒级时间戳
            'open': float(i[1]),
            'high': float(i[2]),
            'low': float(i[3]),
            'close': float(i[4]),
        })

        print(i)

    print(data)

    # 按时间排序
    sorted_data = sorted(data, key=lambda x: x['id'])

    print(sorted_data)

    signals = 0
    wins = 0
    lig_price = 0
    low_price = 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]

        prev_open, prev_close = prev['open'], prev['close']
        curr_open, curr_close = curr['open'], curr['close']
        future_close = future['close']

        # 当前为涨
        if curr_close > curr_open:
            # 前一笔涨 + 包裹
            if prev_close > prev_open and curr_open < prev_open and curr_close > prev_close:
                signals += 1
                lig_price += (future_close - curr_close)
                if future_close > curr_close:
                    wins += 1
            # 前一笔跌 + 反包
            elif prev_close < prev_open and curr_open < prev_close and curr_close > prev_open:
                signals += 1
                lig_price += (future_close - curr_close)
                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
                low_price += (curr_close - future_close)
                if future_close < curr_close:
                    wins += 1
            # 前一笔涨 + 反包
            elif prev_close > prev_open and curr_open > prev_close and curr_close < prev_open:
                signals += 1
                low_price += (curr_close - future_close)
                if future_close < curr_close:
                    wins += 1

    if signals > 0:
        win_rate = wins / signals * 100
        print(f"信号数={signals}, 胜率={win_rate:.2f}%")
    else:
        print("没有找到符合条件的形态")

    print("上涨盈亏合计:", lig_price)
    print("下跌盈亏合计:", low_price)
