import pandas as pd from indicators.sma import calculate_21_50_sma from indicators.bollinger_bands import calculate_bollinger_bands def calculate_standard_deviation(data): """ Calculate the standard deviation of the closing prices over a 21-period window. Parameters: - data (pd.DataFrame): The stock data with 'Close' column. Returns: - pd.DataFrame: The stock data with an added 'SD_21' column for the standard deviation. """ data['SD_21'] = data['Close'].rolling(window=21).std() return data def check_buy_signal(data): """ Analyzes stock data to identify buy signals based on enhanced criteria: - On the 1 day time frame, the 21-period SMA is above the 50-period SMA. - The 21-period SMA has been above the 50-period SMA for more than 1 day. - On the 1-hour time frame, the 21-period SMA has just crossed above the 50-period SMA from below. - On the 1-day time frame, the price is either below the 21-period SMA or less than 0.25 SD above the 21-period SMA. Parameters: - data (pd.DataFrame): The stock data with 'Close', 'SMA_21', 'SMA_50', 'SD_21' columns. Returns: - pd.Series: A boolean series indicating buy signals. """ price_position = data['Close'] - data['SMA_21'] within_sd_limit = (price_position > 0) & (price_position <= 0.25 * data['SD_21']) buy_signal = ((data['SMA_21'] > data['SMA_50']) & (data['SMA_21'].shift(1) > data['SMA_50'].shift(1)) & ((data['Close'] < data['SMA_21']) | within_sd_limit)) return buy_signal def check_sell_signal(data): """ Analyzes stock data to identify sell signals based on the criteria: - The price has crossed above the upper band of the 1.7SD Bollinger Band on the 21-period SMA. Parameters: - data (pd.DataFrame): The stock data with 'Close', 'BB_Upper' columns. Returns: - pd.Series: A boolean series indicating sell signals. """ sell_signal = data['Close'] > data['BB_Upper'] return sell_signal def generate_signals(stock_data): """ Main function to generate buy and sell signals for a given stock. Parameters: - stock_data (pd.DataFrame): The stock data. Returns: - pd.DataFrame: The stock data with additional columns 'Buy_Signal' and 'Sell_Signal'. """ # Ensure the necessary SMA, Bollinger Bands, and standard deviation calculations are performed stock_data = calculate_21_50_sma(stock_data) stock_data = calculate_bollinger_bands(stock_data) stock_data = calculate_standard_deviation(stock_data) # Generate buy and sell signals stock_data['Buy_Signal'] = check_buy_signal(stock_data) stock_data['Sell_Signal'] = check_sell_signal(stock_data) return stock_data if __name__ == "__main__": # Example usage dates = pd.date_range(start='2023-01-01', periods=100, freq='D') close_prices = pd.Series((100 + pd.np.random.randn(100).cumsum()), index=dates) sample_data = pd.DataFrame({'Close': close_prices}) # Simulating the adding of SMA and SD columns for the example sample_data = calculate_21_50_sma(sample_data) sample_data = calculate_bollinger_bands(sample_data) sample_data = calculate_standard_deviation(sample_data) signals_data = generate_signals(sample_data) print(signals_data[['Buy_Signal', 'Sell_Signal']].tail())