Skip to content

refactor: extract magic numbers in compute_signal_score and classify_trend into named constants #51

@Codex-Crusader

Description

@Codex-Crusader

Summary

PEP 8 and clean-code principles discourage magic numbers (unexplained numeric literals) in function bodies. pulseengine/core/signals.py and pulseengine/core/price.py contain several such constants that are not defined in config.py.

Affected locations

pulseengine/core/signals.pycompute_signal_score

raw["trend"] = {"uptrend": 2.0, "downtrend": -2.0, "sideways": 0.0}.get(trend, 0.0)
raw["momentum"] = round(max(-2.0, min(2.0, roc / 5.0)), 2)  # 5.0 is undocumented
sentiment_raw = max(-2.0, min(2.0, avg_sent * 4.0))  # 4.0 is undocumented
raw["trend_strength"] = round(max(-1.0, min(1.0, ts / 3.0)), 2)  # 3.0 is undocumented

pulseengine/core/price.pyclassify_trend

if ma7 > ma30 * 1.01:   # 1% band threshold
    return "uptrend"
if ma7 < ma30 * 0.99:   # same
    return "downtrend"

Proposed fix

Add named constants to pulseengine/core/config.py:

# Signal scoring normalisation denominators
SIGNAL_TREND_MAX = 2.0
SIGNAL_MOMENTUM_NORMALISER = 5.0
SIGNAL_SENTIMENT_SCALE = 4.0
SIGNAL_TREND_STRENGTH_NORMALISER = 3.0

# Trend classification band (1 % MA divergence triggers uptrend/downtrend)
TREND_BAND_PCT = 0.01

Then reference these constants in the relevant functions instead of the bare literals.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions