Spaces:
Running
Running
Upload get_indicators.py
Browse files- get_indicators.py +65 -0
get_indicators.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import json
|
| 3 |
+
from tabulate import tabulate
|
| 4 |
+
|
| 5 |
+
def get_tradingview_data(timeframe):
|
| 6 |
+
# Define the TradingView Scanner URL
|
| 7 |
+
base_url = "https://scanner.tradingview.com/symbol"
|
| 8 |
+
symbol = "OANDA:XAUUSD"
|
| 9 |
+
|
| 10 |
+
# Fields to request (same as in your URL)
|
| 11 |
+
fields = (
|
| 12 |
+
"Recommend.Other|{tf},Recommend.All|{tf},Recommend.MA|{tf},RSI|{tf},RSI[1]|{tf},"
|
| 13 |
+
"Stoch.K|{tf},Stoch.D|{tf},Stoch.K[1]|{tf},Stoch.D[1]|{tf},CCI20|{tf},CCI20[1]|{tf},"
|
| 14 |
+
"ADX|{tf},ADX+DI|{tf},ADX-DI|{tf},ADX+DI[1]|{tf},ADX-DI[1]|{tf},AO|{tf},AO[1]|{tf},"
|
| 15 |
+
"AO[2]|{tf},Mom|{tf},Mom[1]|{tf},MACD.macd|{tf},MACD.signal|{tf},Rec.Stoch.RSI|{tf},"
|
| 16 |
+
"Stoch.RSI.K|{tf},Rec.WR|{tf},W.R|{tf},Rec.BBPower|{tf},BBPower|{tf},Rec.UO|{tf},"
|
| 17 |
+
"UO|{tf},EMA10|{tf},close|{tf},SMA10|{tf},EMA20|{tf},SMA20|{tf},EMA30|{tf},SMA30|{tf},"
|
| 18 |
+
"EMA50|{tf},SMA50|{tf},EMA100|{tf},SMA100|{tf},EMA200|{tf},SMA200|{tf},Rec.Ichimoku|{tf},"
|
| 19 |
+
"Ichimoku.BLine|{tf},Rec.VWMA|{tf},VWMA|{tf},Rec.HullMA9|{tf},HullMA9|{tf},"
|
| 20 |
+
"Pivot.M.Classic.R3|{tf},Pivot.M.Classic.R2|{tf},Pivot.M.Classic.R1|{tf},"
|
| 21 |
+
"Pivot.M.Classic.Middle|{tf},Pivot.M.Classic.S1|{tf},Pivot.M.Classic.S2|{tf},"
|
| 22 |
+
"Pivot.M.Classic.S3|{tf},Pivot.M.Fibonacci.R3|{tf},Pivot.M.Fibonacci.R2|{tf},"
|
| 23 |
+
"Pivot.M.Fibonacci.R1|{tf},Pivot.M.Fibonacci.Middle|{tf},Pivot.M.Fibonacci.S1|{tf},"
|
| 24 |
+
"Pivot.M.Fibonacci.S2|{tf},Pivot.M.Fibonacci.S3|{tf},Pivot.M.Camarilla.R3|{tf},"
|
| 25 |
+
"Pivot.M.Camarilla.R2|{tf},Pivot.M.Camarilla.R1|{tf},Pivot.M.Camarilla.Middle|{tf},"
|
| 26 |
+
"Pivot.M.Camarilla.S1|{tf},Pivot.M.Camarilla.S2|{tf},Pivot.M.Camarilla.S3|{tf},"
|
| 27 |
+
"Pivot.M.Woodie.R3|{tf},Pivot.M.Woodie.R2|{tf},Pivot.M.Woodie.R1|{tf},"
|
| 28 |
+
"Pivot.M.Woodie.Middle|{tf},Pivot.M.Woodie.S1|{tf},Pivot.M.Woodie.S2|{tf},"
|
| 29 |
+
"Pivot.M.Woodie.S3|{tf},Pivot.M.Demark.R1|{tf},Pivot.M.Demark.Middle|{tf},"
|
| 30 |
+
"Pivot.M.Demark.S1|{tf}"
|
| 31 |
+
).format(tf=timeframe)
|
| 32 |
+
|
| 33 |
+
# Encode fields for URL
|
| 34 |
+
fields_param = fields.replace("|", "%7C").replace(",", "%2C")
|
| 35 |
+
|
| 36 |
+
# Construct final URL
|
| 37 |
+
url = f"{base_url}?symbol={symbol}&fields={fields_param}&no_404=true&label-product=popup-technicals"
|
| 38 |
+
|
| 39 |
+
# Send GET request
|
| 40 |
+
try:
|
| 41 |
+
response = requests.get(url)
|
| 42 |
+
response.raise_for_status()
|
| 43 |
+
data = response.json()
|
| 44 |
+
|
| 45 |
+
print("\n✅ Data fetched successfully!\n")
|
| 46 |
+
|
| 47 |
+
# Convert to table and print
|
| 48 |
+
if isinstance(data, dict):
|
| 49 |
+
table = [[k, v] for k, v in sorted(data.items())]
|
| 50 |
+
elif isinstance(data, list) and len(data) > 0 and isinstance(data[0], dict):
|
| 51 |
+
table = [[k, v] for k, v in sorted(data[0].items())]
|
| 52 |
+
else:
|
| 53 |
+
table = [["Raw", str(data)]]
|
| 54 |
+
|
| 55 |
+
print(tabulate(table, headers=["Indicator", "Value"], tablefmt="fancy_grid"))
|
| 56 |
+
|
| 57 |
+
except requests.exceptions.HTTPError as e:
|
| 58 |
+
print(f"❌ HTTP error: {e}")
|
| 59 |
+
except requests.exceptions.RequestException as e:
|
| 60 |
+
print(f"❌ Request failed: {e}")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"❌ Unexpected error: {e}")
|
| 63 |
+
|
| 64 |
+
if __name__ == "__main__":
|
| 65 |
+
get_tradingview_data()
|