The trendline bug: a line continued past its second anchor at a different slope. Two conventions were fighting, and both were wrong. Lightweight Charts spaces bars evenly however much time separates them — a weekend is forty-nine hours and one bar wide. The renderer extended the line by interpolating between bar indices, which looked straight but disagreed with the server, since price_at() advances per second. Measured on real bars that reached 147 points: the chart drew a level the alerts did not believe in. Making the renderer match price_at() fixed the disagreement and made the visible kick worse, because now the line really did climb an hour's worth of slope across a one-bar maintenance break. Neither convention is what a person means by drawing a line. A trendline advances per bar, so both sides now evaluate in bar space: a new bar_space module the runtime uses to position sloped levels, mirrored by indexAt() in the chart. The line is straight on screen and the alert fires where it is drawn. Also, from testing against the live chart: - A plain click with the trendline tool armed did nothing and left the tool armed, so the next click began a new line — which is how the slope change was first noticed. Click-click and press-drag-release are both supported now, with the rubber band following the cursor between clicks. - Hand-placed levels are armed, fire once, then disarm themselves, and can be re-armed from the sidebar. Verified end to end: created armed, tripped within thirty seconds, disarmed, re-armed. - Layers is collapsible. - The 1h moving averages are gone; only the daily set remains. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
77 lines
2.2 KiB
Python
77 lines
2.2 KiB
Python
from dataclasses import asdict, dataclass
|
|
from enum import Enum
|
|
from typing import Any
|
|
|
|
from app.bars.models import Timeframe
|
|
|
|
|
|
class LevelKind(str, Enum):
|
|
MANUAL = "manual"
|
|
MA = "ma"
|
|
VWAP = "vwap"
|
|
TRENDLINE = "trendline"
|
|
HORIZONTAL = "horizontal"
|
|
|
|
|
|
class Side(str, Enum):
|
|
SUPPORT = "support"
|
|
RESISTANCE = "resistance"
|
|
|
|
|
|
@dataclass(slots=True)
|
|
class Level:
|
|
id: str
|
|
kind: LevelKind
|
|
tf: Timeframe
|
|
side: Side
|
|
weight: float
|
|
score: float
|
|
label: str
|
|
anchor_t: int
|
|
anchor_p: float
|
|
slope: float
|
|
points: list[tuple[int, float]] | None
|
|
touches: int
|
|
first_t: int
|
|
last_t: int
|
|
provisional: bool
|
|
hidden: bool
|
|
period: int | None = None
|
|
color: str | None = None
|
|
line_width: int | None = None
|
|
number: int | None = None
|
|
cutoff_t: int | None = None
|
|
# Hand-placed levels fire once and disarm themselves; everything derived
|
|
# (averages, prior-day, VWAP) is permanently armed.
|
|
armed: bool = True
|
|
# Sloped lines are evaluated across bars, not seconds (see bar_space). The
|
|
# runtime fills this in where the bar series is available; price_at() is the
|
|
# fallback for levels that are already flat or have no series to measure.
|
|
current_p: float | None = None
|
|
|
|
def price_at(self, t: int) -> float:
|
|
return self.anchor_p + self.slope * (t - self.anchor_t)
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
value = asdict(self)
|
|
value["kind"] = self.kind.value
|
|
value["tf"] = self.tf.value
|
|
value["side"] = self.side.value
|
|
return value
|
|
|
|
def summary(self) -> dict[str, Any]:
|
|
"""Compact form for embedding inside a cluster.
|
|
|
|
Clusters go out on every closed 1m bar, and a moving average carries its
|
|
whole point history — hundreds of entries reaching back years. Embedding
|
|
the full level duplicated the entire levels payload once a minute. The
|
|
client already holds the full levels and joins on id.
|
|
"""
|
|
return {
|
|
"id": self.id,
|
|
"kind": self.kind.value,
|
|
"tf": self.tf.value,
|
|
"side": self.side.value,
|
|
"weight": self.weight,
|
|
"label": self.label,
|
|
}
|