refactor: Update target calculation to be more realistic based on stock price and volatility

This commit is contained in:
Bobby Abellana (aider) 2025-02-06 23:33:18 -08:00
parent 1daf868a4d
commit c5fdbaed09
No known key found for this signature in database
GPG Key ID: 647714CC45F3647B

View File

@ -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)