File size: 7,042 Bytes
3452823 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | """
Borsa Uygulaması için Analiz Yardımcı Modülü
Bu modül, risk seviyesi ve öneri hesaplamaları için parametrik fonksiyonlar içerir.
"""
import pandas as pd
import numpy as np
from config import RISK_THRESHOLDS, RECOMMENDATION_THRESHOLDS
def calculate_risk_level(volatility, thresholds=None):
"""
Volatilite değerine göre risk seviyesini hesaplar.
Args:
volatility (float): Volatilite değeri (%)
thresholds (dict, optional): Risk eşik değerleri. Varsayılan: RISK_THRESHOLDS
Returns:
tuple: (risk_level, risk_color) risk seviyesi ve renk kodu
"""
if thresholds is None:
thresholds = RISK_THRESHOLDS
if volatility is None:
return "BELİRSİZ", "gray"
if volatility <= thresholds["low"]:
return "DÜŞÜK", "green"
elif volatility <= thresholds["medium"]:
return "ORTA", "orange"
else:
return "YÜKSEK", "red"
def calculate_recommendation(total_signal, indicator_count, thresholds=None):
"""
Sinyal skorlarına göre yatırım tavsiyesi hesaplar.
Args:
total_signal (float): Toplam sinyal skoru
indicator_count (int): Toplam gösterge sayısı
thresholds (dict, optional): Öneri eşik değerleri. Varsayılan: RECOMMENDATION_THRESHOLDS
Returns:
tuple: (recommendation_text, recommendation_color) tavsiye metni ve renk kodu
"""
if thresholds is None:
thresholds = RECOMMENDATION_THRESHOLDS
ratio = total_signal / indicator_count if indicator_count > 0 else 0
if ratio > thresholds["strong_buy"]:
return "GÜÇLÜ AL", "darkgreen"
elif ratio > 0:
return "AL", "green"
elif ratio < thresholds["strong_sell"]:
return "GÜÇLÜ SAT", "darkred"
elif ratio < 0:
return "SAT", "red"
else:
return "NÖTR", "gray"
def determine_trend(df, sma_columns):
"""
Fiyat hareketleri ve hareketli ortalamalardan trend yönünü belirler.
Args:
df (DataFrame): Veri seti (Close fiyatları ve hareketli ortalamalar içermeli)
sma_columns (list): Hareketli ortalama sütun adları (örn. ['SMA20', 'SMA50', 'SMA200'])
Returns:
dict: Trend bilgileri
"""
trend_info = {}
close_price = df['Close'].iloc[-1]
# Kısa-orta-uzun vadeli trend belirlemeleri
trend_info['short_term'] = "Yükseliş" if close_price > df[sma_columns[0]].iloc[-1] else "Düşüş"
trend_info['medium_term'] = "Yükseliş" if close_price > df[sma_columns[1]].iloc[-1] else "Düşüş"
trend_info['long_term'] = "Yükseliş" if close_price > df[sma_columns[2]].iloc[-1] else "Düşüş"
# Trendin gücü
trend_info['short_term_strength'] = abs((close_price / df[sma_columns[0]].iloc[-1] - 1) * 100)
trend_info['medium_term_strength'] = abs((close_price / df[sma_columns[1]].iloc[-1] - 1) * 100)
trend_info['long_term_strength'] = abs((close_price / df[sma_columns[2]].iloc[-1] - 1) * 100)
# Genel trend yönü
if trend_info['short_term'] == "Yükseliş" and trend_info['medium_term'] == "Yükseliş":
trend_info['direction'] = "yükseliş eğiliminde"
elif trend_info['short_term'] == "Düşüş" and trend_info['medium_term'] == "Düşüş":
trend_info['direction'] = "düşüş eğiliminde"
elif trend_info['short_term'] == "Yükseliş" and trend_info['medium_term'] == "Düşüş":
trend_info['direction'] = "kısa vadede yükseliş gösterse de genel düşüş eğiliminde"
elif trend_info['short_term'] == "Düşüş" and trend_info['medium_term'] == "Yükseliş":
trend_info['direction'] = "kısa vadede düşüş gösterse de genel yükseliş eğiliminde"
else:
trend_info['direction'] = "yatay seyretmekte"
# Genel trend gücü ve rengi
if trend_info['short_term'] == trend_info['medium_term'] == trend_info['long_term'] == "Yükseliş":
trend_info['overall'] = "GÜÇLÜ YÜKSELİŞ TRENDİ"
trend_info['color'] = "darkgreen"
elif trend_info['short_term'] == trend_info['medium_term'] == trend_info['long_term'] == "Düşüş":
trend_info['overall'] = "GÜÇLÜ DÜŞÜŞ TRENDİ"
trend_info['color'] = "darkred"
elif trend_info['long_term'] == "Yükseliş" and (trend_info['short_term'] == "Yükseliş" or trend_info['medium_term'] == "Yükseliş"):
trend_info['overall'] = "YÜKSELİŞ TRENDİ"
trend_info['color'] = "green"
elif trend_info['long_term'] == "Düşüş" and (trend_info['short_term'] == "Düşüş" or trend_info['medium_term'] == "Düşüş"):
trend_info['overall'] = "DÜŞÜŞ TRENDİ"
trend_info['color'] = "red"
elif trend_info['short_term'] != trend_info['medium_term'] or trend_info['medium_term'] != trend_info['long_term']:
trend_info['overall'] = "TREND BELİRSİZ"
trend_info['color'] = "gray"
else:
trend_info['overall'] = "YATAY TREND"
trend_info['color'] = "blue"
return trend_info
def generate_analysis_summary(stock_symbol, trend_info, risk_level, recommendation, price_changes, market_info="", news_info=""):
"""
Analiz sonuçlarına göre özet metin oluşturur.
Args:
stock_symbol (str): Hisse senedi sembolü
trend_info (dict): Trend bilgileri
risk_level (str): Risk seviyesi
recommendation (str): Tavsiye metni
price_changes (dict): Fiyat değişim bilgileri
market_info (str, optional): Piyasa bilgisi metni
news_info (str, optional): Haber bilgisi metni
Returns:
str: Analiz özet metni
"""
risk_desc = "düşük riskli" if risk_level == "DÜŞÜK" else ("orta riskli" if risk_level == "ORTA" else "yüksek riskli")
# Öneriye göre aksiyon belirleme
if "AL" in recommendation:
if "GÜÇLÜ" in recommendation:
action = "alım için uygun görünüyor"
else:
action = "dikkatli bir şekilde alım için değerlendirilebilir"
elif "SAT" in recommendation:
if "GÜÇLÜ" in recommendation:
action = "satış için uygun görünüyor"
else:
action = "satış düşünülebilir"
else:
action = "bekleme pozisyonunda kalınması uygun olabilir"
# Değerlendirmeyi birleştir
last_week_trend = f"Son bir haftada %{price_changes.get('1w', 0):.2f} " if price_changes.get('1w') is not None else ""
summary = f"{stock_symbol} hissesi şu anda {trend_info['direction']} ve {risk_desc} bir yatırım olarak değerlendirilmektedir. " + \
f"{last_week_trend}değişim göstermiş olup, teknik analiz sonuçlarına göre {action}. " + \
f"{market_info} {news_info} " + \
f"Yatırım kararınızda bu analiz sonuçlarının yanı sıra şirketin temel verilerini ve piyasa koşullarını da dikkate almanız önerilir."
return summary |