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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
"""
Technical Analysis Workflow using LangGraph.

This workflow orchestrates the technical analysis team:
1. Fetch market data
2. Indicator Agent -> calculate technical indicators
3. Pattern Agent -> identify patterns (parallel with Trend Agent)
4. Trend Agent -> analyze trends (parallel with Pattern Agent)
5. Decision Agent -> make trading decision
6. Generate charts with annotations
"""

import json
import logging
import time
import traceback
from datetime import datetime, timedelta
from typing import Any, Dict, Literal, Optional

import pandas as pd
from langchain_core.runnables import RunnableConfig
from langgraph.graph import END, StateGraph

# Configure logger
logger = logging.getLogger(__name__)

from agents.technical import DecisionAgent, IndicatorAgent, PatternAgent, TrendAgent
from config.default_config import DEFAULT_CONFIG
from data.providers.provider_factory import ProviderFactory
from data.schemas.market_data import check_minimum_data, validate_ohlc
from graph.state.agent_state import add_agent_message, set_workflow_status
from graph.state.trading_state import (
    TechnicalWorkflowState,
    create_initial_technical_state,
)
from utils.charts.annotations import ChartAnnotations
from utils.charts.chart_generator import ChartGenerator


class TechnicalWorkflow:
    """
    Technical analysis workflow orchestrator.

    This workflow implements User Story 1 (P1 MVP):
    - Fetch OHLC data from data providers
    - Run technical analysis through 4-agent pipeline
    - Generate annotated candlestick charts
    - Produce trading recommendation
    """

    def __init__(self, config: Optional[Dict[str, Any]] = None):
        """
        Initialize technical workflow.

        Args:
            config: Optional configuration override
        """
        self.config = config or DEFAULT_CONFIG

        # Initialize agents
        self.indicator_agent = IndicatorAgent(config=self.config)
        self.pattern_agent = PatternAgent(config=self.config)
        self.trend_agent = TrendAgent(config=self.config)
        self.decision_agent = DecisionAgent(config=self.config)

        # Initialize providers and chart generator
        self.provider_factory = ProviderFactory(config=self.config)
        self.chart_generator = ChartGenerator()

        # Build workflow graph
        self.graph = self._build_graph()

    def _build_graph(self) -> StateGraph:
        """
        Build LangGraph StateGraph for technical workflow.

        Returns:
            Compiled StateGraph
        """
        # Create graph with TechnicalWorkflowState
        workflow = StateGraph(TechnicalWorkflowState)

        # Add nodes
        workflow.add_node("fetch_market_data", self._fetch_market_data_node)
        workflow.add_node("indicator_agent", self._indicator_agent_node)
        workflow.add_node("pattern_agent", self._pattern_agent_node)
        workflow.add_node("trend_agent", self._trend_agent_node)
        workflow.add_node("decision_agent", self._decision_agent_node)
        workflow.add_node("generate_charts", self._generate_charts_node)

        # Define edges (workflow flow)
        workflow.set_entry_point("fetch_market_data")

        # Sequential workflow to avoid concurrent state updates
        workflow.add_edge("fetch_market_data", "indicator_agent")
        workflow.add_edge("indicator_agent", "pattern_agent")
        workflow.add_edge("pattern_agent", "trend_agent")
        workflow.add_edge("trend_agent", "decision_agent")
        workflow.add_edge("decision_agent", "generate_charts")
        workflow.add_edge("generate_charts", END)

        return workflow.compile()

    def run(
        self,
        ticker: str,
        timeframe: str = "1d",
        start_date: Optional[str] = None,
        end_date: Optional[str] = None,
        user_query: Optional[str] = None,
    ) -> TechnicalWorkflowState:
        """
        Run technical analysis workflow for a single timeframe.

        Args:
            ticker: Stock ticker symbol
            timeframe: Analysis timeframe (1m, 5m, 1h, 1d, etc.)
            start_date: Start date (YYYY-MM-DD), default: 30 days ago
            end_date: End date (YYYY-MM-DD), default: today
            user_query: Optional user question/query

        Returns:
            Final workflow state with analysis results
        """
        # Set default dates
        if end_date is None:
            end_date = datetime.now().strftime("%Y-%m-%d")

        if start_date is None:
            # Default to 30 days of history (more for daily, less for intraday)
            days_back = 90 if timeframe in ["1d", "1w"] else 30
            start_date = (datetime.now() - timedelta(days=days_back)).strftime(
                "%Y-%m-%d"
            )

        # Create initial state
        initial_state = create_initial_technical_state(ticker, timeframe, user_query)
        initial_state["market_data"]["start_date"] = start_date
        initial_state["market_data"]["end_date"] = end_date
        initial_state = set_workflow_status(initial_state, "in_progress")

        # Run workflow
        try:
            final_state = self.graph.invoke(initial_state)

            # Mark as completed
            final_state = set_workflow_status(final_state, "completed")

            return final_state

        except Exception as e:
            # Log error with full traceback
            logger.error(
                json.dumps(
                    {
                        "workflow": "technical_workflow",
                        "action": "error",
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            # Mark as failed
            error_state = set_workflow_status(
                initial_state,
                "failed",
                error=str(e),
            )
            return error_state

    def _fetch_market_data_node(
        self, state: TechnicalWorkflowState
    ) -> TechnicalWorkflowState:
        """
        Fetch market data from providers.

        Args:
            state: Current workflow state

        Returns:
            Updated state with market data
        """
        try:
            ticker = state["ticker"]
            timeframe = state["timeframe"]
            start_date = state["market_data"]["start_date"]
            end_date = state["market_data"]["end_date"]

            # Get provider for OHLC data
            provider = self.provider_factory.get_provider("ohlc")

            # Fetch data
            df = provider.fetch_ohlc(
                ticker=ticker,
                timeframe=timeframe,
                start_date=start_date,
                end_date=end_date,
            )

            # Validate data
            df = validate_ohlc(df)
            check_minimum_data(df, min_bars=30, timeframe=timeframe)

            # Calculate data quality score
            quality_score = self._calculate_data_quality(df)

            # Serialize DataFrame for state storage
            serialized_df = self._serialize_dataframe(df)

            # Update state
            new_state = state.copy()
            new_state["market_data"]["ohlc_data"] = serialized_df
            new_state["market_data"]["data_quality_score"] = quality_score

            new_state = add_agent_message(
                new_state,
                "data_fetcher",
                f"Successfully fetched {len(df)} bars of {timeframe} data for {ticker} (quality: {quality_score:.2f})",
                metadata={"bars": len(df), "quality": quality_score},
            )

            return new_state

        except Exception as e:
            # Log error with full traceback
            logger.error(
                json.dumps(
                    {
                        "node": "fetch_market_data",
                        "action": "error",
                        "ticker": state.get("ticker"),
                        "timeframe": state.get("timeframe"),
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            error_state = set_workflow_status(
                state,
                "failed",
                error=f"Failed to fetch market data: {str(e)}",
            )
            return error_state

    def _indicator_agent_node(
        self, state: TechnicalWorkflowState
    ) -> TechnicalWorkflowState:
        """
        Run Indicator Agent.

        Args:
            state: Current workflow state

        Returns:
            Updated state with indicator analysis
        """
        try:
            new_state = set_workflow_status(
                state, "in_progress", current_agent="indicator_agent"
            )
            result = self.indicator_agent.run(new_state)
            return result
        except Exception as e:
            # Log error with full traceback
            logger.error(
                json.dumps(
                    {
                        "node": "indicator_agent",
                        "action": "error",
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            return set_workflow_status(
                state,
                "failed",
                error=f"Indicator agent failed: {str(e)}",
            )

    def _pattern_agent_node(
        self, state: TechnicalWorkflowState
    ) -> TechnicalWorkflowState:
        """
        Run Pattern Agent.

        Args:
            state: Current workflow state

        Returns:
            Updated state with pattern analysis
        """
        try:
            new_state = set_workflow_status(
                state, "in_progress", current_agent="pattern_agent"
            )
            result = self.pattern_agent.run(new_state)
            return result
        except Exception as e:
            # Log error with full traceback
            logger.error(
                json.dumps(
                    {
                        "node": "pattern_agent",
                        "action": "error",
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            return set_workflow_status(
                state,
                "failed",
                error=f"Pattern agent failed: {str(e)}",
            )

    def _trend_agent_node(
        self, state: TechnicalWorkflowState
    ) -> TechnicalWorkflowState:
        """
        Run Trend Agent.

        Args:
            state: Current workflow state

        Returns:
            Updated state with trend analysis
        """
        try:
            new_state = set_workflow_status(
                state, "in_progress", current_agent="trend_agent"
            )
            result = self.trend_agent.run(new_state)
            return result
        except Exception as e:
            # Log error with full traceback
            logger.error(
                json.dumps(
                    {
                        "node": "trend_agent",
                        "action": "error",
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            return set_workflow_status(
                state,
                "failed",
                error=f"Trend agent failed: {str(e)}",
            )

    def _decision_agent_node(
        self, state: TechnicalWorkflowState
    ) -> TechnicalWorkflowState:
        """
        Run Decision Agent.

        Args:
            state: Current workflow state

        Returns:
            Updated state with trading decision
        """
        try:
            new_state = set_workflow_status(
                state, "in_progress", current_agent="decision_agent"
            )
            result = self.decision_agent.run(new_state)
            return result
        except Exception as e:
            # Log error with full traceback
            logger.error(
                json.dumps(
                    {
                        "node": "decision_agent",
                        "action": "error",
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            return set_workflow_status(
                state,
                "failed",
                error=f"Decision agent failed: {str(e)}",
            )

    def _generate_charts_node(
        self, state: TechnicalWorkflowState
    ) -> TechnicalWorkflowState:
        """
        Generate annotated candlestick charts.

        Args:
            state: Current workflow state

        Returns:
            Updated state with chart path
        """
        try:
            # Deserialize DataFrame
            df = self._deserialize_dataframe(state["market_data"]["ohlc_data"])

            # Skip chart generation if market data is empty
            if df.empty:
                logger.warning(
                    json.dumps(
                        {
                            "node": "generate_charts",
                            "action": "skipped",
                            "ticker": state["ticker"],
                            "reason": "market_data is empty",
                            "timestamp": time.time(),
                        }
                    )
                )
                error_state = add_agent_message(
                    state,
                    "chart_generator",
                    "Skipped chart generation: no market data available",
                    metadata={"skipped": True},
                )
                return error_state

            # Prepare indicator overlays
            indicators_to_plot = []

            if state.get("indicators", {}).get("rsi", {}).get("series"):
                rsi_series = pd.Series(state["indicators"]["rsi"]["series"])
                indicators_to_plot.append(
                    {
                        "data": rsi_series,
                        "panel": 1,  # Separate panel
                        "ylabel": "RSI",
                        "color": "purple",
                    }
                )

            # Generate chart
            fig, chart_path = self.chart_generator.generate_candlestick_chart(
                df=df,
                ticker=state["ticker"],
                timeframe=state["timeframe"],
                volume=True,
                save=True,
                indicators=indicators_to_plot if indicators_to_plot else None,
            )

            # Close figure to free memory
            self.chart_generator.close_figure(fig)

            # Update state
            new_state = state.copy()
            new_state["chart_path"] = chart_path

            new_state = add_agent_message(
                new_state,
                "chart_generator",
                f"Generated chart: {chart_path}",
                metadata={"chart_path": chart_path},
            )

            return new_state

        except Exception as e:
            # Log warning with full traceback (non-fatal error)
            logger.warning(
                json.dumps(
                    {
                        "node": "generate_charts",
                        "action": "error_non_fatal",
                        "error": str(e),
                        "traceback": traceback.format_exc(),
                        "timestamp": time.time(),
                    }
                )
            )

            # Chart generation failure shouldn't fail the whole workflow
            error_state = add_agent_message(
                state,
                "chart_generator",
                f"Failed to generate chart: {str(e)}",
                metadata={"error": True},
            )
            return error_state

    def _calculate_data_quality(self, df: pd.DataFrame) -> float:
        """
        Calculate data quality score (0-1).

        Args:
            df: OHLC DataFrame

        Returns:
            Quality score
        """
        score = 1.0

        # Penalize for missing data
        missing_pct = df.isnull().sum().sum() / (len(df) * len(df.columns))
        score -= missing_pct * 0.5

        # Penalize for insufficient data
        if len(df) < 50:
            score -= 0.2

        # Penalize for data inconsistencies (OHLC violations)
        violations = 0
        for i in range(len(df)):
            if (
                df["low"].iloc[i] > df["open"].iloc[i]
                or df["low"].iloc[i] > df["close"].iloc[i]
            ):
                violations += 1
            if (
                df["high"].iloc[i] < df["open"].iloc[i]
                or df["high"].iloc[i] < df["close"].iloc[i]
            ):
                violations += 1

        if violations > 0:
            score -= (violations / len(df)) * 0.3

        return max(0.0, min(1.0, score))

    def _serialize_dataframe(self, df: pd.DataFrame) -> Dict[str, Any]:
        """
        Serialize DataFrame for state storage.

        Args:
            df: pandas DataFrame

        Returns:
            Serialized dict
        """
        # Reset index to include date as column
        df_reset = df.reset_index()

        # Convert to dict with orient='list' for efficient storage
        return df_reset.to_dict(orient="list")

    def _deserialize_dataframe(self, data: Dict[str, Any]) -> pd.DataFrame:
        """
        Deserialize DataFrame from state.

        Args:
            data: Serialized data

        Returns:
            pandas DataFrame
        """
        df = pd.DataFrame(data)

        # Convert timestamp column to datetime if present
        if "timestamp" in df.columns:
            df["timestamp"] = pd.to_datetime(df["timestamp"])
        elif "date" in df.columns:
            df["date"] = pd.to_datetime(df["date"])
            df = df.rename(columns={"date": "timestamp"})
        elif "datetime" in df.columns:
            df["datetime"] = pd.to_datetime(df["datetime"])
            df = df.rename(columns={"datetime": "timestamp"})

        return df