File size: 2,794 Bytes
7d603aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5f1a4ce
 
 
7d603aa
5f1a4ce
7d603aa
 
 
 
5f1a4ce
 
 
 
 
 
 
 
 
 
 
7d603aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Reliability Score Calculator (Pseudo-code)
Pillars:
- 40% Fundamental Health (Piotroski F-Score + Altman Z-Score)
- 30% AI News Sentiment (30-day NLP)
- 30% Technical Volatility (Beta + Std Dev penalty)
"""

def compute_fundamentals_score(financials):
    """
    Simulates calculating Piotroski F-score and Altman Z-score.
    Maps results to a base 100-point scale.
    """
    print(f"[*] Calculating fundamental solvency for {financials.get('ticker')}...")
    f_score = 7  # Out of 9
    z_score = 3.2 # Safe zone > 2.99
    
    # Base mapping: F-Score gives up to 60 pts, Z score up to 40 pts
    fundamental_strength = (f_score / 9) * 60 + min(40, (z_score / 3.0) * 40)
    return fundamental_strength


def compute_sentiment_score(nlp_results):
    """
    Analyzes historical sentiment data. Penalizes highly erratic behavior.
    """
    bullish_pct = nlp_results.count('bullish') / len(nlp_results)
    bearish_pct = nlp_results.count('bearish') / len(nlp_results)
    
    # Scale from 0 to 100. We want a strong bullish ratio.
    net_sentiment = bullish_pct - bearish_pct
    base = 50 + (net_sentiment * 50) 
    return max(0, min(100, base))


def compute_volatility_score(beta, std_dev):
    """
    Scores the stock's stability relative to the index. 
    Beta ~ 1.0 is stable. 
    High beta / high std dev is penalized for 'long term reliability'.
    """
    # Ideal beta for stable reliability is ~0.8 to 1.1
    beta_penalty = abs(1.0 - beta) * 50
    score = 100 - beta_penalty
    
    if std_dev > 0.05:  # e.g., >5% daily volatility
        score -= 20
        
    return max(0, min(100, score))


def calculate_long_term_reliability(ticker: str, fcf: float, sentiment_score: float, beta: float, std_dev: float):
    # Base normalization
    financials = {"ticker": ticker, "fcf": fcf}
    
    # Pillar Integration weights
    W_FUNDAMENTAL = 0.40
    W_SENTIMENT   = 0.30
    W_VOLATILITY  = 0.30
    
    # Pseudo-fundamental compute (simulating Piostroski)
    f_score = 7 if fcf > 0 else 4
    z_score = 3.2 if fcf > 100_000_000 else 2.5
    fundamental_strength = (f_score / 9) * 60 + min(40, (z_score / 3.0) * 40)
    score_fund = fundamental_strength
    
    # Sentiment calculation mapping
    score_sent = max(0, min(100, 50 + (sentiment_score * 50)))
    
    # Volatility scoring
    score_vol = compute_volatility_score(beta, std_dev)
    
    final_score = (
        (score_fund * W_FUNDAMENTAL) +
        (score_sent * W_SENTIMENT) +
        (score_vol * W_VOLATILITY)
    )
    
    return {
        "ticker": ticker,
        "score_out_of_100": int(final_score),
        "pillars": {
            "fundamentals": round(score_fund, 2),
            "sentiment": round(score_sent, 2),
            "volatility": round(score_vol, 2)
        }
    }