支撑与阻力位

2024-09-05  本文已影响0人  Leoguo小哥

import numpy as np
import pandas as pd
import requests


def fetch_klines(symbol, interval, limit=500):
    url = "https://fapi.binance.com/fapi/v1/klines"
    params = {
        'symbol': symbol,
        'interval': interval,
        'limit': limit
    }
    response = requests.get(url, params=params)
    data = response.json()
    return pd.DataFrame(data, columns=['Open Time', 'Open', 'High', 'Low', 'Close', 'Volume',
                                       'Close Time', 'Quote Asset Volume', 'Number of Trades',
                                       'Taker Buy Base Asset Volume', 'Taker Buy Quote Asset Volume', 'Ignore'])


def calculate_gann_levels(df, price_level=3):
    close_prices = df['Close'].astype(float)

    # Calculate the Gann Numbers
    max_val = 20
    gann_numbers = []

    for min in range(max_val + 1):
        for i in range(4):
            gNum = 0  # Initialize gNum to zero
            if min == 0 and i == 0:
                gNum = min + (min + 2)
            elif min > 0 and i == 0:
                gNum = round(gann_numbers[-1]) + (min + 1) + min
            else:
                gNum = round(gann_numbers[-1]) + (min + 2) + min
            gann_numbers.append(gNum)

    # Calculate price and support/resistance levels
    last_close = close_prices.iloc[-1]

    # Set denominator based on last close price
    if last_close >= 10000:
        denominator = 0.01
    elif last_close >= 1000:
        denominator = 0.1
    elif last_close >= 100:
        denominator = 1
    elif last_close >= 10:
        denominator = 10
    elif last_close >= 0.05:
        denominator = 100
    else:
        denominator = 1000

    price = last_close * denominator
    resistance = 0.0
    support = 0.0

    # Find support and resistance levels
    for i in range(len(gann_numbers) - 1):
        if gann_numbers[i] <= price < gann_numbers[i + 1]:
            resistance = gann_numbers[i + 1] / denominator
            support = gann_numbers[i] / denominator
            break

    blue_gann_price = (support + resistance) / 2

    return support, resistance, blue_gann_price


def main():
    symbol = "BTCUSDT"
    interval = "1h"
    price_level = 3  # Change as needed (3 or 5)

    # Fetch Kline data
    df = fetch_klines(symbol, interval)

    # Calculate Gann levels
    support, resistance, blue_gann_price = calculate_gann_levels(df, price_level)

    # Print the results
    print(f"最低支撑价格: {support:.2f}")
    print(f"阻力位: {resistance:.2f}")
    print(f"当价格在  {blue_gann_price:.2f} 之上时,可能表示市场处于上涨趋势,而当价格在其下方时,可能表示下跌趋势: {blue_gann_price:.2f}")


if __name__ == "__main__":
    main()

上一篇 下一篇

猜你喜欢

热点阅读