File size: 10,317 Bytes
07cf142
 
 
 
 
 
467c4ca
 
 
07cf142
 
 
 
467c4ca
07cf142
 
 
 
467c4ca
 
 
07cf142
467c4ca
 
 
 
 
 
 
 
 
 
 
 
07cf142
 
 
a4e1a97
07cf142
26a0a10
467c4ca
 
07cf142
467c4ca
07cf142
 
 
 
 
467c4ca
 
 
 
07cf142
 
467c4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cf142
 
467c4ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cf142
467c4ca
 
 
 
 
 
 
07cf142
 
 
a4e1a97
07cf142
467c4ca
07cf142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a0a10
467c4ca
 
 
26a0a10
467c4ca
 
 
 
 
 
 
 
 
 
 
26a0a10
467c4ca
 
 
07cf142
467c4ca
 
07cf142
 
467c4ca
 
 
26a0a10
467c4ca
 
26a0a10
07cf142
 
 
467c4ca
 
 
26a0a10
07cf142
467c4ca
 
07cf142
467c4ca
 
07cf142
 
 
467c4ca
 
 
26a0a10
467c4ca
 
 
 
 
 
07cf142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26a0a10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
07cf142
26a0a10
 
 
 
 
 
 
467c4ca
26a0a10
 
 
 
 
 
 
 
 
 
 
07cf142
 
 
 
 
 
467c4ca
07cf142
 
467c4ca
 
 
 
07cf142
a4e1a97
 
 
 
 
26a0a10
a4e1a97
 
 
 
 
 
07cf142
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
import { Component, OnInit, inject } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MatCardModule } from '@angular/material/card';
import { Location } from '@angular/common';
import { NgApexchartsModule } from 'ng-apexcharts';
import { ChartService } from './chart.service';
import { MatPaginatorModule } from '@angular/material/paginator';
import { PageEvent } from '@angular/material/paginator';
import { MatSlideToggleModule } from '@angular/material/slide-toggle';

@Component({
  selector: 'app-analysispage',
  standalone: true,
  imports: [CommonModule, MatSlideToggleModule, MatCardModule, MatPaginatorModule, NgApexchartsModule],
  templateUrl: './analysispage.html',
  styleUrls: ['./analysispage.scss']
})
export class Analysispage implements OnInit {

  originalOrder = (): number => 0;
  private location = inject(Location);
  result: any;
  pageIndex = 0;
  pageSize = 5;
  sort: {
    key: 'combined_overall_score' | 'overall_ta_score' | 'overall_fa_score' | 'news_overall_score',
    direction: 'asc' | 'desc'
  } = {
      key: 'combined_overall_score',
      direction: 'desc'
    };


  closeAreaOptions: any;
  candlestickChartOptions: any;
  overallChart: any;
  strategyChart: any;
  strategyChartIndex: number = 0;
  predictedChart: any;
  highLowChartOptions: any;
  selectedIndicator: any = 'RSI';
  selectedStrategy: any = 'RSI 14';
  activeCompany: number = 0;
  isCandlestick = true;
  constructor(private chartService: ChartService) { }

  ngOnInit(): void {
    const s = this.location.getState() as { result?: unknown } | null;
    this.result = s?.result ?? null;
    const validData = this.result.filter((item: any) => !item.error);
    const invalidData = this.result.filter((item: any) => item.error);
    validData.sort((a: any, b: any) => b.combined_overall_score - a.combined_overall_score);
    this.result = [...validData, ...invalidData];
    console.log('Analysis result:', this.result);
    this.loadCharts();
    this.loadPredictedCharts();
    this.loadStrategiesChart('RSI 14');
  }

  // Map a row to the numeric value for a given sort key
  private sortValue(row: any, key: typeof this.sort.key): number {
    switch (key) {
      case 'combined_overall_score': return Number(row?.combined_overall_score ?? -Infinity);
      case 'overall_ta_score': return Number(row?.overall_ta_score ?? -Infinity);
      case 'overall_fa_score': return Number(row?.fundamental_analysis?.overall_fa_score ?? -Infinity);
      case 'news_overall_score': return Number(row?.news_overall_score ?? -Infinity);
    }
  }

  // Sorted list (used by the paginator slice)
  get sortedResults(): any[] {
    if (!Array.isArray(this.result)) return [];
    const arr = [...this.result];
    arr.sort((a, b) => {
      const av = this.sortValue(a, this.sort.key);
      const bv = this.sortValue(b, this.sort.key);
      return this.sort.direction === 'asc' ? av - bv : bv - av;
    });
    return arr;
  }

  // Page slice over the sorted list
  get pagedResults(): any[] {
    const start = this.pageIndex * this.pageSize;
    return this.sortedResults.slice(start, start + this.pageSize);
  }

  // Toggle sort on header click
  toggleSort(key: typeof this.sort.key) {
    if (this.sort.key === key) {
      this.sort.direction = this.sort.direction === 'asc' ? 'desc' : 'asc';
    } else {
      this.sort.key = key;
      this.sort.direction = 'desc'; // default direction on new column
    }
    this.pageIndex = 0; // reset to first page on sort change
  }

  // paginator change
  onPage(e: PageEvent) {
    this.pageIndex = e.pageIndex;
    this.pageSize = e.pageSize;
  }




  showStrategies(selectedIndicator: any) {
    this.selectedIndicator = selectedIndicator;
    const strategies = this.result[this.activeCompany][selectedIndicator];
    const firstStrategyKey = Object.keys(strategies)[0];

    if (firstStrategyKey) {
      this.selectedStrategy = firstStrategyKey;
      this.loadStrategiesChart(firstStrategyKey);
    }
  }

  loadStrategiesChart(strategyName: any) {
    this.strategyChartIndex = 0;
    this.selectedStrategy = strategyName;

    this.strategyChart = this.chartService.getChartOptions(strategyName, this.result[this.activeCompany]);
    console.log(this.strategyChart);
  }

  getClass(signal: any) {
    if (typeof signal === 'string') {
      switch (signal.toLowerCase()) {
        case 'buy':
        case 'good':
        case 'positive':
        case 'bullish':
          return 'green';
        case 'dbuy':
        case 'bad':
        case 'negative':
        case 'bearish':
          return 'red';
        case 'neutral':
        case 'none':
          return 'yellow';
        default:
          return '';
      }
    } else {
      return 'strategyvalue'
    }
  }

  loadCharts() {


    // close price chart

    const data = this.result[this.activeCompany]['ohlc_data'] as Array<{ x: string; y: [number, number, number, number] }>;

    // Optional: keep sorted and remove duplicate dates
    const closeSeries = [...data]
      .sort((a, b) => a.x.localeCompare(b.x))
      .filter((d, i, arr) => i === 0 || d.x !== arr[i - 1].x)
      .map(d => ({ x: d.x, y: d.y[3] })); // close = index 3

    this.closeAreaOptions = {
      chart: { type: 'area', height: 400, width: 1200 },
      series: [
        {
          name: 'Close',
          data: closeSeries  // [{x: '2025-08-01', y: 2350.9}, ...]
        }
      ],
      xaxis: {
        type: 'category',
        labels: { style: { colors: '#ffffff' } }
      },
      yaxis: {
        opposite: true,
        labels: { style: { colors: '#ffffff' } }
      },

      tooltip: {
        theme: 'dark'

      }
    };


    //candlestick chart


    this.candlestickChartOptions = {
      chart: { type: 'candlestick', height: 400, width: 1200 },
      series: [{ name: this.activeCompany, data }], // x can be a plain string when type='category'
      xaxis: {
        type: 'category',
        labels: { style: { colors: '#ffffff' } }
      },
      yaxis: {
        opposite: true,
        labels: { style: { colors: '#ffffff' } }
      },
      tooltip: { theme: 'dark' }
    };

  }

  loadPredictedCharts() {
    //donut chart


    this.overallChart = {
      series: [
        this.result[this.activeCompany].overall_ta_score,
        this.result[this.activeCompany].overall_fa_score,
        this.result[this.activeCompany].news_overall_score
      ],  // Three values for the donut chart
      chart: {
        type: 'donut',
        width: 500
      },
      labels: ['TA', 'FA', 'News'],  // Set the labels to show on the chart
      plotOptions: {
        pie: {
          donut: {
            size: '60%',  // Control the thickness of the donut
            labels: {
              show: true,
              name: {
                show: false
              },
              value: {
                show: true,  // Show values inside the donut chart
                fontSize: '16px', // Font size for values
                color: 'white'   // White color for the values
              },
              total: {
                show: true,
                label: 'Total', // Label at the center
                formatter: (w: any) => {
                  // Display the total value (sum of all slices)
                  return w.globals.seriesTotals.reduce((a: any, b: any) => a + b, 0) + '%';
                }
              }
            }
          }
        }
      },
      fill: {
        type: 'solid',
        colors: ['#ff5733', '#33ff57', '#ffcc00'] // Colors for TA, FA, and News
      },
      tooltip: {
        enabled: true,  // Enable tooltips for the chart
        fillSeriesColor: false,
        theme: 'dark',
        style: {
          fontSize: '14px',
          color: 'white'  // White text color for tooltips
        }
      },
      legend: {
        show: true,
        position: 'bottom',  // Position the legend at the bottom
        labels: {
          useSeriesColors: true,  // Use the series colors for legend
          colors: ['white']  // White text color for legend
        }
      }
    };





    // Predicted High/Low 15-day chart (single chart with two series)
    const highsRaw = this.result[this.activeCompany].ai_predicted_daily_high_15 ?? [];
    const lowsRaw = this.result[this.activeCompany].ai_predicted_daily_low_15 ?? [];
    const dates15 = this.result[this.activeCompany].ai_predicted_dates_15 ?? [];

    const highs = highsRaw.map((v: any) => Number(Number(v).toFixed(2)));
    const lows = lowsRaw.map((v: any) => Number(Number(v).toFixed(2)));

    if (highs.length && lows.length && dates15.length && highs.length === lows.length && highs.length === dates15.length) {
      this.highLowChartOptions = {
        chart: { type: 'line', height: 400, width: 1500, toolbar: { show: false } },
        series: [
          { name: 'Predicted High', data: highs, color: '#22C55E' }, // green
          { name: 'Predicted Low', data: lows, color: '#EF4444' }    // red
        ],
        stroke: { width: 2, curve: 'smooth' },
        markers: { size: 3 },
        xaxis: { categories: dates15, type: 'category', tickPlacement: 'on', labels: { rotate: -45, rotateAlways: true, hideOverlappingLabels: false, trim: false, minHeight: 70, style: { colors: '#ffffff', fontSize: '12px' } } },
        yaxis: {
          opposite: true,
          labels: { style: { colors: '#ffffff' } }
        },
        tooltip: {
          shared: true,
          intersect: false,
          theme: 'dark',
          y: {
            formatter: (val: number) => (val != null ? val.toFixed(2) : '')
          }
        },
        legend: {
          position: 'top',
          labels: {
            colors: '#ffffff',
            useSeriesColors: false
          }
        }
      };
    } else {
      this.highLowChartOptions = null;
    }
  }


  selectCompany(index: number) {
    this.activeCompany = index;
    this.loadCharts();
    this.loadPredictedCharts();
  }

  onModeChange(checked: boolean) {
    this.isCandlestick = checked;
    this.loadCharts();
  }

  previousChart(): void {
    if (this.strategyChartIndex > 0) {
      this.strategyChartIndex--;
    }
  }

  nextChart(): void {
    if (this.strategyChartIndex < this.strategyChart.length - 1) {
      this.strategyChartIndex++;
    }
  }

}