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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
"""
Matplotlib annotation helpers for technical analysis charts.

This module provides utilities to draw trend lines, support/resistance levels,
and other technical analysis annotations on matplotlib/mplfinance charts.
"""

from datetime import datetime
from typing import Any, Dict, List, Optional, Tuple

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.axes import Axes
from matplotlib.lines import Line2D
from matplotlib.patches import Rectangle


class ChartAnnotations:
    """Helper class for adding technical analysis annotations to charts."""

    DEFAULT_TRENDLINE_COLOR = "blue"
    DEFAULT_TRENDLINE_STYLE = "--"
    DEFAULT_TRENDLINE_WIDTH = 1.5

    DEFAULT_SUPPORT_COLOR = "green"
    DEFAULT_RESISTANCE_COLOR = "red"
    DEFAULT_LEVEL_STYLE = "-"
    DEFAULT_LEVEL_WIDTH = 2.0
    DEFAULT_LEVEL_ALPHA = 0.7

    DEFAULT_ZONE_ALPHA = 0.2

    @classmethod
    def draw_trend_line(
        cls,
        ax: Axes,
        start_date: datetime,
        start_price: float,
        end_date: datetime,
        end_price: float,
        color: str = DEFAULT_TRENDLINE_COLOR,
        linestyle: str = DEFAULT_TRENDLINE_STYLE,
        linewidth: float = DEFAULT_TRENDLINE_WIDTH,
        label: Optional[str] = None,
        extend: bool = False,
    ) -> Line2D:
        """
        Draw a trend line on the chart.

        Args:
            ax: Matplotlib axes to draw on
            start_date: Start datetime
            start_price: Start price
            end_date: End datetime
            end_price: End price
            color: Line color
            linestyle: Line style (-, --, :, -.)
            linewidth: Line width
            label: Optional label for legend
            extend: Whether to extend line to edge of chart

        Returns:
            Line2D object
        """
        dates = [mdates.date2num(start_date), mdates.date2num(end_date)]
        prices = [start_price, end_price]

        if extend:
            xlim = ax.get_xlim()
            slope = (end_price - start_price) / (dates[1] - dates[0])

            # Extend to left edge
            extended_start_price = start_price + slope * (xlim[0] - dates[0])
            dates.insert(0, xlim[0])
            prices.insert(0, extended_start_price)

            # Extend to right edge
            extended_end_price = end_price + slope * (xlim[1] - dates[1])
            dates.append(xlim[1])
            prices.append(extended_end_price)

        line = ax.plot(
            dates,
            prices,
            color=color,
            linestyle=linestyle,
            linewidth=linewidth,
            label=label,
        )[0]

        return line

    @classmethod
    def draw_support_level(
        cls,
        ax: Axes,
        price: float,
        color: str = DEFAULT_SUPPORT_COLOR,
        linestyle: str = DEFAULT_LEVEL_STYLE,
        linewidth: float = DEFAULT_LEVEL_WIDTH,
        alpha: float = DEFAULT_LEVEL_ALPHA,
        label: Optional[str] = None,
    ) -> Line2D:
        """
        Draw a horizontal support level across the chart.

        Args:
            ax: Matplotlib axes
            price: Support price level
            color: Line color
            linestyle: Line style
            linewidth: Line width
            alpha: Transparency (0-1)
            label: Optional label

        Returns:
            Line2D object
        """
        label = label or f"Support: ${price:.2f}"

        line = ax.axhline(
            y=price,
            color=color,
            linestyle=linestyle,
            linewidth=linewidth,
            alpha=alpha,
            label=label,
        )

        return line

    @classmethod
    def draw_resistance_level(
        cls,
        ax: Axes,
        price: float,
        color: str = DEFAULT_RESISTANCE_COLOR,
        linestyle: str = DEFAULT_LEVEL_STYLE,
        linewidth: float = DEFAULT_LEVEL_WIDTH,
        alpha: float = DEFAULT_LEVEL_ALPHA,
        label: Optional[str] = None,
    ) -> Line2D:
        """
        Draw a horizontal resistance level across the chart.

        Args:
            ax: Matplotlib axes
            price: Resistance price level
            color: Line color
            linestyle: Line style
            linewidth: Line width
            alpha: Transparency
            label: Optional label

        Returns:
            Line2D object
        """
        label = label or f"Resistance: ${price:.2f}"

        line = ax.axhline(
            y=price,
            color=color,
            linestyle=linestyle,
            linewidth=linewidth,
            alpha=alpha,
            label=label,
        )

        return line

    @classmethod
    def draw_support_resistance_zone(
        cls,
        ax: Axes,
        lower_price: float,
        upper_price: float,
        zone_type: str = "support",
        alpha: float = DEFAULT_ZONE_ALPHA,
    ) -> Rectangle:
        """
        Draw a shaded zone for support or resistance.

        Args:
            ax: Matplotlib axes
            lower_price: Lower bound of zone
            upper_price: Upper bound of zone
            zone_type: "support" or "resistance"
            alpha: Transparency

        Returns:
            Rectangle patch
        """
        xlim = ax.get_xlim()
        color = (
            cls.DEFAULT_SUPPORT_COLOR
            if zone_type == "support"
            else cls.DEFAULT_RESISTANCE_COLOR
        )

        rect = Rectangle(
            (xlim[0], lower_price),
            xlim[1] - xlim[0],
            upper_price - lower_price,
            facecolor=color,
            alpha=alpha,
            edgecolor=None,
            label=f"{zone_type.capitalize()} Zone: ${lower_price:.2f}-${upper_price:.2f}",
        )

        ax.add_patch(rect)
        return rect

    @classmethod
    def draw_price_channel(
        cls,
        ax: Axes,
        upper_line: Tuple[datetime, float, datetime, float],
        lower_line: Tuple[datetime, float, datetime, float],
        color: str = "purple",
        linestyle: str = DEFAULT_TRENDLINE_STYLE,
        linewidth: float = DEFAULT_TRENDLINE_WIDTH,
        fill: bool = True,
        fill_alpha: float = 0.1,
    ) -> Tuple[Line2D, Line2D]:
        """
        Draw a price channel with upper and lower bounds.

        Args:
            ax: Matplotlib axes
            upper_line: (start_date, start_price, end_date, end_price) for upper bound
            lower_line: (start_date, start_price, end_date, end_price) for lower bound
            color: Line color
            linestyle: Line style
            linewidth: Line width
            fill: Whether to fill area between lines
            fill_alpha: Fill transparency

        Returns:
            Tuple of (upper_line, lower_line) Line2D objects
        """
        upper = cls.draw_trend_line(
            ax,
            upper_line[0],
            upper_line[1],
            upper_line[2],
            upper_line[3],
            color=color,
            linestyle=linestyle,
            linewidth=linewidth,
            label="Upper Channel",
        )

        lower = cls.draw_trend_line(
            ax,
            lower_line[0],
            lower_line[1],
            lower_line[2],
            lower_line[3],
            color=color,
            linestyle=linestyle,
            linewidth=linewidth,
            label="Lower Channel",
        )

        if fill:
            upper_dates = [
                mdates.date2num(upper_line[0]),
                mdates.date2num(upper_line[2]),
            ]
            upper_prices = [upper_line[1], upper_line[3]]
            lower_dates = [
                mdates.date2num(lower_line[0]),
                mdates.date2num(lower_line[2]),
            ]
            lower_prices = [lower_line[1], lower_line[3]]

            ax.fill_between(
                upper_dates,
                upper_prices,
                lower_prices,
                color=color,
                alpha=fill_alpha,
            )

        return upper, lower

    @classmethod
    def annotate_signal(
        cls,
        ax: Axes,
        date: datetime,
        price: float,
        signal_type: str,
        text: Optional[str] = None,
        arrow_color: Optional[str] = None,
    ) -> None:
        """
        Annotate a buy/sell signal on the chart.

        Args:
            ax: Matplotlib axes
            date: Signal datetime
            price: Price at signal
            signal_type: "buy" or "sell"
            text: Custom annotation text
            arrow_color: Custom arrow color
        """
        is_buy = signal_type.lower() == "buy"

        if text is None:
            text = "BUY" if is_buy else "SELL"

        if arrow_color is None:
            arrow_color = "green" if is_buy else "red"

        # Position text above/below based on signal type
        xytext_offset = (0, 20) if is_buy else (0, -20)
        va = "bottom" if is_buy else "top"

        ax.annotate(
            text,
            xy=(mdates.date2num(date), price),
            xytext=xytext_offset,
            textcoords="offset points",
            ha="center",
            va=va,
            fontsize=10,
            fontweight="bold",
            color=arrow_color,
            bbox=dict(
                boxstyle="round,pad=0.3",
                facecolor="white",
                edgecolor=arrow_color,
                alpha=0.8,
            ),
            arrowprops=dict(
                arrowstyle="->",
                color=arrow_color,
                lw=2,
            ),
        )

    @classmethod
    def add_legend(
        cls,
        ax: Axes,
        loc: str = "best",
        fontsize: int = 10,
    ) -> None:
        """
        Add legend to chart with custom styling.

        Args:
            ax: Matplotlib axes
            loc: Legend location
            fontsize: Font size
        """
        ax.legend(
            loc=loc,
            fontsize=fontsize,
            framealpha=0.9,
            shadow=True,
        )

    @classmethod
    def find_support_resistance_levels(
        cls,
        df: pd.DataFrame,
        window: int = 20,
        num_levels: int = 3,
    ) -> Dict[str, List[float]]:
        """
        Automatically identify support and resistance levels from OHLC data.

        Uses a simple algorithm based on local minima (support) and maxima (resistance).

        Args:
            df: OHLC DataFrame
            window: Rolling window for local extrema detection
            num_levels: Number of top levels to return for each type

        Returns:
            Dict with "support" and "resistance" lists of price levels
        """
        supports = []
        resistances = []

        # Find local minima (support)
        for i in range(window, len(df) - window):
            if df["low"].iloc[i] == df["low"].iloc[i - window : i + window].min():
                supports.append(df["low"].iloc[i])

        # Find local maxima (resistance)
        for i in range(window, len(df) - window):
            if df["high"].iloc[i] == df["high"].iloc[i - window : i + window].max():
                resistances.append(df["high"].iloc[i])

        # Cluster similar levels (within 1% of each other)
        supports = cls._cluster_levels(supports, tolerance=0.01)
        resistances = cls._cluster_levels(resistances, tolerance=0.01)

        # Return top N most significant levels
        supports = sorted(supports, reverse=True)[:num_levels]
        resistances = sorted(resistances)[:num_levels]

        return {
            "support": supports,
            "resistance": resistances,
        }

    @staticmethod
    def _cluster_levels(levels: List[float], tolerance: float = 0.01) -> List[float]:
        """
        Cluster price levels that are within tolerance % of each other.

        Args:
            levels: List of price levels
            tolerance: Percentage tolerance (0.01 = 1%)

        Returns:
            List of clustered levels (averages)
        """
        if not levels:
            return []

        levels = sorted(levels)
        clustered = []
        current_cluster = [levels[0]]

        for level in levels[1:]:
            if abs(level - current_cluster[-1]) / current_cluster[-1] <= tolerance:
                current_cluster.append(level)
            else:
                clustered.append(sum(current_cluster) / len(current_cluster))
                current_cluster = [level]

        # Add last cluster
        clustered.append(sum(current_cluster) / len(current_cluster))

        return clustered