FT1 / utils /forex_signals.py
Devendra21's picture
Update utils/forex_signals.py
c29a7b0 verified
raw
history blame
3.41 kB
import pandas as pd
import numpy as np
import talib
from datetime import datetime, timedelta
def generate_forex_signals(trading_capital, market_risk, user_timezone, additional_pairs=None):
# Placeholder: Retrieve historical data for each currency pair (e.g., from an API like Yahoo Finance or a local dataset)
# In practice, you'll fetch this data dynamically or from a database.
currency_pairs = additional_pairs if additional_pairs else ["EUR/USD", "GBP/USD", "AUD/USD"] # Example pairs
signals = []
for pair in currency_pairs:
# Fetch historical data for the currency pair
# Placeholder: Load data from a file, API, or a database.
data = fetch_historical_data(pair) # Replace with real function to fetch data
# Calculate technical indicators for entry/exit points
data['SMA_50'] = talib.SMA(data['close'], timeperiod=50) # Simple Moving Average
data['SMA_200'] = talib.SMA(data['close'], timeperiod=200)
data['RSI'] = talib.RSI(data['close'], timeperiod=14) # Relative Strength Index
data['BB_upper'], data['BB_middle'], data['BB_lower'] = talib.BBANDS(data['close'], timeperiod=20)
# Define strategy for entry and exit (example strategy)
entry_signal = None
exit_signal = None
entry_time = None
exit_time = None
max_roi = -float('inf')
signal_strength = 0
for i in range(200, len(data)): # Skip first few rows due to moving average window
# Check if we should enter the market based on SMA crossover
if data['SMA_50'][i] > data['SMA_200'][i] and data['RSI'][i] < 30: # Buy signal (bullish crossover)
entry_signal = "Buy"
entry_time = data.index[i]
entry_price = data['close'][i]
# Look ahead for the best exit signal within the next 2 hours (adjustable window)
for j in range(i+1, min(i+12, len(data))): # Look 2 hours ahead (12 data points for 15-min intervals)
if data['close'][j] > entry_price: # Check if price has gone up
roi = (data['close'][j] - entry_price) / entry_price * 100
if roi > max_roi:
max_roi = roi
exit_signal = "Sell"
exit_time = data.index[j]
signal_strength = 100 # Simplified for now (could be refined further)
# Append to the list of signals
if entry_signal and exit_signal:
signals.append({
'currency_pair': pair,
'entry_time': entry_time,
'exit_time': exit_time,
'roi': max_roi,
'signal_strength': signal_strength
})
return {
'best_signal': max(signals, key=lambda x: x['roi'], default={}),
'all_signals': signals
}
def fetch_historical_data(currency_pair):
# Placeholder function to fetch historical price data for a given currency pair
# Ideally, replace this with actual API calls to get real-time data
data = pd.DataFrame({
'date': pd.date_range(start="2025-01-01", periods=100, freq='15T'),
'close': np.random.rand(100) * 1.5 + 1.1 # Random price data (replace with actual data)
})
data.set_index('date', inplace=True)
return data