Spaces:
Running
Running
Update utils/model_inference.py
Browse files- utils/model_inference.py +29 -45
utils/model_inference.py
CHANGED
@@ -1,60 +1,44 @@
|
|
1 |
import numpy as np
|
2 |
-
import
|
3 |
-
from datetime import datetime
|
4 |
import pytz
|
5 |
-
import requests
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
# Function to get user timezone based on their IP address
|
11 |
-
def get_user_timezone():
|
12 |
-
try:
|
13 |
-
# Get the public IP address of the user
|
14 |
-
response = requests.get(f'https://ipinfo.io?token={ACCESS_TOKEN}')
|
15 |
-
data = response.json()
|
16 |
-
|
17 |
-
# Extract the timezone information from the response
|
18 |
-
user_timezone = data['timezone']
|
19 |
-
return user_timezone
|
20 |
-
except Exception as e:
|
21 |
-
print(f"Error fetching timezone: {e}")
|
22 |
-
return 'UTC' # Fallback to UTC if there's an error
|
23 |
-
|
24 |
-
# Function to generate forex signals
|
25 |
-
def generate_forex_signals(trading_capital, market_risk):
|
26 |
-
# Get the user's timezone based on their IP address
|
27 |
-
user_timezone = get_user_timezone()
|
28 |
-
|
29 |
# Ensure the user timezone is valid
|
30 |
try:
|
31 |
user_tz = pytz.timezone(user_timezone)
|
32 |
except pytz.UnknownTimeZoneError:
|
33 |
raise ValueError("Invalid timezone entered. Please check the format.")
|
34 |
|
35 |
-
#
|
36 |
risk_level = {'Low': 0.01, 'Medium': 0.03, 'High': 0.05}
|
37 |
-
|
38 |
if market_risk not in risk_level:
|
39 |
-
raise ValueError("Invalid risk level. Choose from Low, Medium, High.")
|
40 |
-
|
41 |
risk_percentage = risk_level[market_risk]
|
42 |
-
|
43 |
-
# Dummy signal generation (Replace with your model inference logic)
|
44 |
-
currency_pair = "EUR/USD"
|
45 |
-
|
46 |
-
# Get current time in the user's timezone and format it
|
47 |
-
entry_time = datetime.now(user_tz).strftime("%Y-%m-%d %I:%M:%S %p")
|
48 |
-
exit_time = (datetime.now(user_tz) + pd.Timedelta(hours=2)).strftime("%Y-%m-%d %I:%M:%S %p")
|
49 |
-
|
50 |
-
roi = np.random.uniform(5, 15) # Random ROI between 5% and 15%
|
51 |
-
signal_strength = np.random.uniform(0.7, 1.0) # Random strength between 0.7 and 1.0
|
52 |
|
53 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
return {
|
55 |
-
"
|
56 |
-
"
|
57 |
-
"exit_time": exit_time,
|
58 |
-
"roi": roi,
|
59 |
-
"signal_strength": signal_strength
|
60 |
}
|
|
|
1 |
import numpy as np
|
2 |
+
from datetime import datetime, timedelta
|
|
|
3 |
import pytz
|
|
|
4 |
|
5 |
+
# Function to generate signals for multiple currency pairs
|
6 |
+
def generate_forex_signals(trading_capital, market_risk, user_timezone):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
# Ensure the user timezone is valid
|
8 |
try:
|
9 |
user_tz = pytz.timezone(user_timezone)
|
10 |
except pytz.UnknownTimeZoneError:
|
11 |
raise ValueError("Invalid timezone entered. Please check the format.")
|
12 |
|
13 |
+
# Define market risk levels and their corresponding risk percentages
|
14 |
risk_level = {'Low': 0.01, 'Medium': 0.03, 'High': 0.05}
|
|
|
15 |
if market_risk not in risk_level:
|
16 |
+
raise ValueError("Invalid risk level. Choose from Low, Medium, or High.")
|
|
|
17 |
risk_percentage = risk_level[market_risk]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
# Currency pairs to evaluate
|
20 |
+
currency_pairs = ["EUR/USD", "GBP/USD", "USD/JPY"]
|
21 |
+
|
22 |
+
# Generate dummy signals for each currency pair (replace this with your model's predictions)
|
23 |
+
signals = []
|
24 |
+
for pair in currency_pairs:
|
25 |
+
entry_time = datetime.now(user_tz).strftime("%Y-%m-%d %I:%M:%S %p")
|
26 |
+
exit_time = (datetime.now(user_tz) + timedelta(hours=2)).strftime("%Y-%m-%d %I:%M:%S %p")
|
27 |
+
roi = np.random.uniform(5, 20) # Random ROI between 5% and 20%
|
28 |
+
signal_strength = np.random.uniform(0.7, 1.0) # Random signal strength
|
29 |
+
signals.append({
|
30 |
+
"currency_pair": pair,
|
31 |
+
"entry_time": entry_time,
|
32 |
+
"exit_time": exit_time,
|
33 |
+
"roi": roi,
|
34 |
+
"signal_strength": signal_strength
|
35 |
+
})
|
36 |
+
|
37 |
+
# Find the signal with the highest ROI
|
38 |
+
best_signal = max(signals, key=lambda x: x["roi"])
|
39 |
+
|
40 |
+
# Return the best signal and all signals
|
41 |
return {
|
42 |
+
"best_signal": best_signal,
|
43 |
+
"all_signals": signals
|
|
|
|
|
|
|
44 |
}
|