Spaces:
Sleeping
Sleeping
Update utils/forex_signals.py
Browse files- utils/forex_signals.py +68 -50
utils/forex_signals.py
CHANGED
|
@@ -1,63 +1,81 @@
|
|
| 1 |
import requests
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
|
| 5 |
API_KEY = "89SEdLScHxHk6j8J9OoH4sLFS3Mri4oW"
|
| 6 |
-
BASE_URL = "https://financialmodelingprep.com/api/v3/forex"
|
| 7 |
|
| 8 |
-
|
| 9 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
response = requests.get(url)
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
if
|
| 15 |
-
|
| 16 |
-
if not data or "historical" not in data:
|
| 17 |
-
return None # No data available for this pair
|
| 18 |
-
return data["historical"]
|
| 19 |
else:
|
| 20 |
-
print(f"
|
| 21 |
return None
|
| 22 |
except Exception as e:
|
| 23 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
signals = []
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
continue
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
"signal_strength": signal_strength,
|
| 49 |
-
})
|
| 50 |
-
|
| 51 |
-
# Sort signals by ROI (descending) to recommend the best signal
|
| 52 |
-
signals.sort(key=lambda x: x["roi"], reverse=True)
|
| 53 |
-
|
| 54 |
-
if signals:
|
| 55 |
-
return {
|
| 56 |
-
"best_signal": signals[0],
|
| 57 |
-
"all_signals": signals,
|
| 58 |
-
}
|
| 59 |
-
else:
|
| 60 |
-
return {
|
| 61 |
-
"best_signal": None,
|
| 62 |
-
"all_signals": [],
|
| 63 |
-
}
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
|
| 5 |
API_KEY = "89SEdLScHxHk6j8J9OoH4sLFS3Mri4oW"
|
|
|
|
| 6 |
|
| 7 |
+
CURRENCY_PAIRS = [
|
| 8 |
+
"EUR/USD", "GBP/USD", "USD/JPY", "AUD/USD",
|
| 9 |
+
"CAD/JPY", "NZD/USD", "CHF/JPY", "AUD/JPY",
|
| 10 |
+
"GBP/CHF", "EUR/GBP"
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def fetch_forex_data(currency_pair):
|
| 15 |
try:
|
| 16 |
+
# Convert the pair into the format required by the API (EURUSD, GBPUSD, etc.)
|
| 17 |
+
formatted_pair = currency_pair.replace("/", "")
|
| 18 |
+
url = f"https://financialmodelingprep.com/api/v3/forex/{formatted_pair}?apikey={API_KEY}"
|
| 19 |
response = requests.get(url)
|
| 20 |
+
response.raise_for_status()
|
| 21 |
+
|
| 22 |
+
data = response.json()
|
| 23 |
|
| 24 |
+
if "historical" in data and len(data["historical"]) > 0:
|
| 25 |
+
return pd.DataFrame(data["historical"])
|
|
|
|
|
|
|
|
|
|
| 26 |
else:
|
| 27 |
+
print(f"No data available for {currency_pair}.")
|
| 28 |
return None
|
| 29 |
except Exception as e:
|
| 30 |
+
print(f"Error fetching data for {currency_pair}: {e}")
|
| 31 |
+
return None
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def calculate_indicators(df):
|
| 35 |
+
if df is None or df.empty:
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
try:
|
| 39 |
+
df["SMA_50"] = df["close"].rolling(window=50).mean()
|
| 40 |
+
df["SMA_200"] = df["close"].rolling(window=200).mean()
|
| 41 |
+
df["RSI"] = compute_rsi(df["close"])
|
| 42 |
+
return df
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"Error calculating indicators: {e}")
|
| 45 |
return None
|
| 46 |
|
| 47 |
+
|
| 48 |
+
def compute_rsi(series, period=14):
|
| 49 |
+
delta = series.diff(1)
|
| 50 |
+
gain = delta.where(delta > 0, 0)
|
| 51 |
+
loss = -delta.where(delta < 0, 0)
|
| 52 |
+
|
| 53 |
+
avg_gain = gain.rolling(window=period).mean()
|
| 54 |
+
avg_loss = loss.rolling(window=period).mean()
|
| 55 |
+
|
| 56 |
+
rs = avg_gain / avg_loss
|
| 57 |
+
rsi = 100 - (100 / (1 + rs))
|
| 58 |
+
return rsi
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def generate_forex_signals():
|
| 62 |
signals = []
|
| 63 |
+
|
| 64 |
+
for pair in CURRENCY_PAIRS:
|
| 65 |
+
print(f"Processing currency pair: {pair}")
|
| 66 |
+
|
| 67 |
+
df = fetch_forex_data(pair)
|
| 68 |
+
if df is None:
|
| 69 |
+
continue
|
| 70 |
+
|
| 71 |
+
df = calculate_indicators(df)
|
| 72 |
+
if df is None or df.empty:
|
| 73 |
+
continue
|
| 74 |
+
|
| 75 |
+
# Check for crossover signals
|
| 76 |
+
if df["SMA_50"].iloc[-1] > df["SMA_200"].iloc[-1] and df["RSI"].iloc[-1] < 70:
|
| 77 |
+
signals.append({"pair": pair, "signal": "Buy"})
|
| 78 |
+
elif df["SMA_50"].iloc[-1] < df["SMA_200"].iloc[-1] and df["RSI"].iloc[-1] > 30:
|
| 79 |
+
signals.append({"pair": pair, "signal": "Sell"})
|
| 80 |
+
|
| 81 |
+
return signals
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|