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>
45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from app.analysis.bar_space import index_at, price_in_bar_space
|
|
from app.analysis.levels import Level, LevelKind, Side
|
|
from app.bars.models import Timeframe
|
|
|
|
MINUTE = 60
|
|
|
|
|
|
def sloped(anchor_t: int, anchor_p: float, last_t: int, last_p: float) -> Level:
|
|
slope = (last_p - anchor_p) / (last_t - anchor_t)
|
|
return Level(
|
|
"ml", LevelKind.MANUAL, Timeframe.M1, Side.SUPPORT, 1, 1, "line",
|
|
anchor_t, anchor_p, slope, None, 0, anchor_t, last_t, False, False,
|
|
)
|
|
|
|
|
|
def test_index_is_linear_when_bars_are_evenly_spaced():
|
|
times = [i * MINUTE for i in range(10)]
|
|
assert index_at(times, 0) == 0
|
|
assert index_at(times, 5 * MINUTE) == 5
|
|
assert index_at(times, int(2.5 * MINUTE)) == 2.5
|
|
|
|
|
|
def test_a_gap_costs_one_index_however_long_it_is():
|
|
# Two bars either side of a weekend are adjacent on screen.
|
|
times = [0, MINUTE, MINUTE + 49 * 3600, MINUTE + 49 * 3600 + MINUTE]
|
|
assert index_at(times, MINUTE) == 1
|
|
assert index_at(times, MINUTE + 49 * 3600) == 2
|
|
|
|
|
|
def test_line_stays_straight_in_bar_space_across_a_gap():
|
|
# Bars: ten minutes, a 49-hour weekend, then ten more.
|
|
weekend = 49 * 3600
|
|
times = [i * MINUTE for i in range(10)] + [10 * MINUTE + weekend + i * MINUTE for i in range(10)]
|
|
level = sloped(times[0], 100.0, times[9], 109.0) # 1 point per bar
|
|
|
|
# Extending nine further bars must add nine more points, gap notwithstanding.
|
|
assert price_in_bar_space(level, times, times[18]) == 118.0
|
|
# Clock-based pricing would have run away during the halt.
|
|
assert level.price_at(times[18]) > 3000
|
|
|
|
|
|
def test_flat_anchors_return_the_anchor_price():
|
|
times = [i * MINUTE for i in range(5)]
|
|
level = sloped(0, 100.0, MINUTE, 100.0)
|
|
assert price_in_bar_space(level, times, times[4]) == 100.0
|