FUNCTION get_mean_reversion_signals(symbol, index, lookback_days, investment_amount):
TRY:
// --- Setup ---
GET the official ticker using get_ticker_symbol().
GET the currency symbol using get_currency_symbol().
CALCULATE total days of data needed (lookback_days + a buffer).
CONVERT total days to a Yahoo Finance range string (e.g., "6mo").
// --- Data Fetching & Prep ---
FETCH historical data using get_yahoo_finance_history().
IF no data is returned, DISPLAY error and RETURN None.
TRIM the data to the exact lookback_days.
// --- Calculations ---
DETERMINE dynamic short and long SMA periods based on lookback_days.
GET current price and historical prices.
CALCULATE Short SMA, Long SMA, and Standard Deviation.
CALCULATE Bollinger Bands (SMA +/- 2 * Standard Deviation).
CALCULATE Z-Score ((Price - Long SMA) / Standard Deviation).
CALCULATE recent volatility.
DETERMINE a dynamic Z-Score threshold.
CALCULATE VWAP (Volume Weighted Average Price).
// --- Signal Generation ---
DETERMINE action ('BUY', 'SELL', 'HOLD') based on the Z-Score vs. threshold.
CALCULATE expected reversion price (the Long SMA).
CALCULATE potential return percentage.
CALCULATE stop-loss and take-profit price levels.
// --- Final Output ---
GATHER all results into a dictionary.
RETURN the result dictionary.
CATCH any exception:
DISPLAY error message.
RETURN None.
END FUNCTION
FUNCTION get_stock_recommendations(search_term, market_index):
IF search_term is empty, RETURN an empty list.
// Attempt 1: Live API search
CALL get_yahoo_finance_suggestions() with the search_term.
// Attempt 2: Fallback to local data
IF the live search returned no suggestions:
DEFINE a local database of common stocks for all markets.
SELECT the stock list for the given market_index.
FILTER the local list for symbols or names containing the search_term.
SORT the filtered list to prioritize better matches.
RETURN the top 5 suggestions found.
END FUNCTION