From c5fdbaed09480e84edb2fed957dc6eefa04a6930 Mon Sep 17 00:00:00 2001 From: "Bobby Abellana (aider)" Date: Thu, 6 Feb 2025 23:33:18 -0800 Subject: [PATCH] refactor: Update target calculation to be more realistic based on stock price and volatility --- src/indicators/sunny_bands.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/indicators/sunny_bands.py b/src/indicators/sunny_bands.py index d3b601e..4548442 100644 --- a/src/indicators/sunny_bands.py +++ b/src/indicators/sunny_bands.py @@ -23,23 +23,31 @@ class SunnyBands: tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1) return tr.rolling(window=self.length).mean() - def calculate_target(self, dma: float, upper_band: float) -> float: + def calculate_target(self, dma: float, upper_band: float, current_price: float) -> float: """ - Calculate a more aggressive price target + Calculate a realistic price target based on current price and volatility Args: dma (float): Current DMA value upper_band (float): Current upper band value - - Returns: - float: Calculated target price + current_price (float): Current stock price """ - # Calculate band width + # Calculate band width as measure of volatility band_width = upper_band - dma - # Target is 2-3x the band width above the upper band - target_multiple = 2.5 # Can be adjusted between 2-3x for different aggressiveness - target = upper_band + (band_width * target_multiple) + # Calculate percentage moves based on price levels + if current_price <= 10: + target_percent = 0.03 # 3% move for low-priced stocks + elif current_price <= 20: + target_percent = 0.025 # 2.5% move for mid-priced stocks + else: + target_percent = 0.02 # 2% move for higher-priced stocks + + # Calculate target based on current price plus percentage move + base_target = current_price * (1 + target_percent) + + # Adjust target if it's below the upper band + target = max(base_target, upper_band + (band_width * 0.5)) return target @@ -65,7 +73,7 @@ class SunnyBands: lower_band = dma - (atr * self.atr_multiplier) # Calculate targets for each row - targets = [self.calculate_target(d, u) for d, u in zip(dma, upper_band)] + targets = [self.calculate_target(d, u, c) for d, u, c in zip(dma, upper_band, df['close'])] # Generate signals bullish = (df['close'] > lower_band) & (df['close'].shift(1) <= lower_band)