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()