Devendra21 commited on
Commit
c29a7b0
·
verified ·
1 Parent(s): 71ba210

Update utils/forex_signals.py

Browse files
Files changed (1) hide show
  1. utils/forex_signals.py +65 -39
utils/forex_signals.py CHANGED
@@ -1,45 +1,71 @@
1
- import random
2
- from datetime import datetime
 
 
3
 
4
- # Function to generate Forex signals
5
  def generate_forex_signals(trading_capital, market_risk, user_timezone, additional_pairs=None):
6
- """
7
- Generates trading signals for the forex market.
8
- :param trading_capital: The amount of capital the user has for trading.
9
- :param market_risk: The risk level selected by the user (Low, Medium, High).
10
- :param user_timezone: The user's timezone.
11
- :param additional_pairs: List of additional currency pairs to consider (optional).
12
- :return: Dictionary of forex signals with relevant details.
13
- """
14
-
15
- # Default list of currency pairs
16
- default_pairs = [
17
- "EUR/USD", "GBP/USD", "USD/JPY", "AUD/USD", "USD/CHF",
18
- "USD/CAD", "NZD/USD", "EUR/GBP", "EUR/JPY", "GBP/JPY"
19
- ]
20
-
21
- # Add additional pairs if provided
22
- if additional_pairs:
23
- all_pairs = default_pairs + additional_pairs
24
- else:
25
- all_pairs = default_pairs
26
-
27
- # Example of generating signals (simplified logic for demo purposes)
28
  signals = []
29
- for pair in all_pairs:
30
- signal = {
31
- "currency_pair": pair,
32
- "entry_time": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
33
- "exit_time": (datetime.now().strftime("%Y-%m-%d %H:%M:%S")), # Simplified; should be dynamic
34
- "roi": random.uniform(5, 15), # Random ROI for demonstration
35
- "signal_strength": random.uniform(50, 100) # Random signal strength for demo
36
- }
37
- signals.append(signal)
38
-
39
- # Determine the best signal based on ROI (just a simplified example)
40
- best_signal = max(signals, key=lambda x: x["roi"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
  return {
43
- "best_signal": best_signal,
44
- "all_signals": signals
45
  }
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import numpy as np
3
+ import talib
4
+ from datetime import datetime, timedelta
5
 
 
6
  def generate_forex_signals(trading_capital, market_risk, user_timezone, additional_pairs=None):
7
+ # Placeholder: Retrieve historical data for each currency pair (e.g., from an API like Yahoo Finance or a local dataset)
8
+ # In practice, you'll fetch this data dynamically or from a database.
9
+ currency_pairs = additional_pairs if additional_pairs else ["EUR/USD", "GBP/USD", "AUD/USD"] # Example pairs
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  signals = []
11
+
12
+ for pair in currency_pairs:
13
+ # Fetch historical data for the currency pair
14
+ # Placeholder: Load data from a file, API, or a database.
15
+ data = fetch_historical_data(pair) # Replace with real function to fetch data
16
+
17
+ # Calculate technical indicators for entry/exit points
18
+ data['SMA_50'] = talib.SMA(data['close'], timeperiod=50) # Simple Moving Average
19
+ data['SMA_200'] = talib.SMA(data['close'], timeperiod=200)
20
+ data['RSI'] = talib.RSI(data['close'], timeperiod=14) # Relative Strength Index
21
+ data['BB_upper'], data['BB_middle'], data['BB_lower'] = talib.BBANDS(data['close'], timeperiod=20)
22
+
23
+ # Define strategy for entry and exit (example strategy)
24
+ entry_signal = None
25
+ exit_signal = None
26
+ entry_time = None
27
+ exit_time = None
28
+ max_roi = -float('inf')
29
+ signal_strength = 0
30
+
31
+ for i in range(200, len(data)): # Skip first few rows due to moving average window
32
+ # Check if we should enter the market based on SMA crossover
33
+ if data['SMA_50'][i] > data['SMA_200'][i] and data['RSI'][i] < 30: # Buy signal (bullish crossover)
34
+ entry_signal = "Buy"
35
+ entry_time = data.index[i]
36
+ entry_price = data['close'][i]
37
+
38
+ # Look ahead for the best exit signal within the next 2 hours (adjustable window)
39
+ for j in range(i+1, min(i+12, len(data))): # Look 2 hours ahead (12 data points for 15-min intervals)
40
+ if data['close'][j] > entry_price: # Check if price has gone up
41
+ roi = (data['close'][j] - entry_price) / entry_price * 100
42
+ if roi > max_roi:
43
+ max_roi = roi
44
+ exit_signal = "Sell"
45
+ exit_time = data.index[j]
46
+ signal_strength = 100 # Simplified for now (could be refined further)
47
+
48
+ # Append to the list of signals
49
+ if entry_signal and exit_signal:
50
+ signals.append({
51
+ 'currency_pair': pair,
52
+ 'entry_time': entry_time,
53
+ 'exit_time': exit_time,
54
+ 'roi': max_roi,
55
+ 'signal_strength': signal_strength
56
+ })
57
 
58
  return {
59
+ 'best_signal': max(signals, key=lambda x: x['roi'], default={}),
60
+ 'all_signals': signals
61
  }
62
+
63
+ def fetch_historical_data(currency_pair):
64
+ # Placeholder function to fetch historical price data for a given currency pair
65
+ # Ideally, replace this with actual API calls to get real-time data
66
+ data = pd.DataFrame({
67
+ 'date': pd.date_range(start="2025-01-01", periods=100, freq='15T'),
68
+ 'close': np.random.rand(100) * 1.5 + 1.1 # Random price data (replace with actual data)
69
+ })
70
+ data.set_index('date', inplace=True)
71
+ return data