File size: 3,175 Bytes
d8f6b30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import requests
import json
from tabulate import tabulate

def get_tradingview_data(timeframe):
    # Define the TradingView Scanner URL
    base_url = "https://scanner.tradingview.com/symbol"
    symbol = "OANDA:XAUUSD"
    
    # Fields to request (same as in your URL)
    fields = (
        "Recommend.Other|{tf},Recommend.All|{tf},Recommend.MA|{tf},RSI|{tf},RSI[1]|{tf},"
        "Stoch.K|{tf},Stoch.D|{tf},Stoch.K[1]|{tf},Stoch.D[1]|{tf},CCI20|{tf},CCI20[1]|{tf},"
        "ADX|{tf},ADX+DI|{tf},ADX-DI|{tf},ADX+DI[1]|{tf},ADX-DI[1]|{tf},AO|{tf},AO[1]|{tf},"
        "AO[2]|{tf},Mom|{tf},Mom[1]|{tf},MACD.macd|{tf},MACD.signal|{tf},Rec.Stoch.RSI|{tf},"
        "Stoch.RSI.K|{tf},Rec.WR|{tf},W.R|{tf},Rec.BBPower|{tf},BBPower|{tf},Rec.UO|{tf},"
        "UO|{tf},EMA10|{tf},close|{tf},SMA10|{tf},EMA20|{tf},SMA20|{tf},EMA30|{tf},SMA30|{tf},"
        "EMA50|{tf},SMA50|{tf},EMA100|{tf},SMA100|{tf},EMA200|{tf},SMA200|{tf},Rec.Ichimoku|{tf},"
        "Ichimoku.BLine|{tf},Rec.VWMA|{tf},VWMA|{tf},Rec.HullMA9|{tf},HullMA9|{tf},"
        "Pivot.M.Classic.R3|{tf},Pivot.M.Classic.R2|{tf},Pivot.M.Classic.R1|{tf},"
        "Pivot.M.Classic.Middle|{tf},Pivot.M.Classic.S1|{tf},Pivot.M.Classic.S2|{tf},"
        "Pivot.M.Classic.S3|{tf},Pivot.M.Fibonacci.R3|{tf},Pivot.M.Fibonacci.R2|{tf},"
        "Pivot.M.Fibonacci.R1|{tf},Pivot.M.Fibonacci.Middle|{tf},Pivot.M.Fibonacci.S1|{tf},"
        "Pivot.M.Fibonacci.S2|{tf},Pivot.M.Fibonacci.S3|{tf},Pivot.M.Camarilla.R3|{tf},"
        "Pivot.M.Camarilla.R2|{tf},Pivot.M.Camarilla.R1|{tf},Pivot.M.Camarilla.Middle|{tf},"
        "Pivot.M.Camarilla.S1|{tf},Pivot.M.Camarilla.S2|{tf},Pivot.M.Camarilla.S3|{tf},"
        "Pivot.M.Woodie.R3|{tf},Pivot.M.Woodie.R2|{tf},Pivot.M.Woodie.R1|{tf},"
        "Pivot.M.Woodie.Middle|{tf},Pivot.M.Woodie.S1|{tf},Pivot.M.Woodie.S2|{tf},"
        "Pivot.M.Woodie.S3|{tf},Pivot.M.Demark.R1|{tf},Pivot.M.Demark.Middle|{tf},"
        "Pivot.M.Demark.S1|{tf}"
    ).format(tf=timeframe)

    # Encode fields for URL
    fields_param = fields.replace("|", "%7C").replace(",", "%2C")

    # Construct final URL
    url = f"{base_url}?symbol={symbol}&fields={fields_param}&no_404=true&label-product=popup-technicals"

    # Send GET request
    try:
        response = requests.get(url)
        response.raise_for_status()
        data = response.json()

        print("\n✅ Data fetched successfully!\n")

        # Convert to table and print
        if isinstance(data, dict):
            table = [[k, v] for k, v in sorted(data.items())]
        elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict):
            table = [[k, v] for k, v in sorted(data[0].items())]
        else:
            table = [["Raw", str(data)]]

        print(tabulate(table, headers=["Indicator", "Value"], tablefmt="fancy_grid"))

    except requests.exceptions.HTTPError as e:
        print(f"❌ HTTP error: {e}")
    except requests.exceptions.RequestException as e:
        print(f"❌ Request failed: {e}")
    except Exception as e:
        print(f"❌ Unexpected error: {e}")

if __name__ == "__main__":
    get_tradingview_data()