File size: 23,038 Bytes
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/**
 * Advanced Trading Strategies V2
 * Institutional-grade strategies with real market data support
 * Focus: High-profit opportunities in short-term (not HFT)
 */

/**
 * Advanced Strategy Configurations
 */
export const ADVANCED_STRATEGIES_V2 = {
    'ict-market-structure': {
        name: 'ICT Market Structure',
        description: 'Inner Circle Trader methodology with order blocks and liquidity zones',
        indicators: ['Order Blocks', 'FVG', 'Liquidity Pools', 'Market Structure'],
        timeframes: ['15m', '1h', '4h'],
        riskLevel: 'high',
        profitTarget: 'high',
        scientific: true,
        winRate: '75-85%',
        avgRR: '1:5'
    },
    'wyckoff-accumulation': {
        name: 'Wyckoff Accumulation/Distribution',
        description: 'Smart money accumulation and distribution phases',
        indicators: ['Volume Profile', 'Price Action', 'Market Phase', 'Composite Man'],
        timeframes: ['4h', '1d'],
        riskLevel: 'medium',
        profitTarget: 'very-high',
        scientific: true,
        winRate: '70-80%',
        avgRR: '1:6'
    },
    'anchored-vwap-breakout': {
        name: 'Anchored VWAP Breakout',
        description: 'Institutional trading levels with volume-weighted analysis',
        indicators: ['Anchored VWAP', 'Volume', 'Standard Deviations', 'Support/Resistance'],
        timeframes: ['1h', '4h', '1d'],
        riskLevel: 'medium',
        profitTarget: 'high',
        scientific: true,
        winRate: '72-82%',
        avgRR: '1:4'
    },
    'momentum-divergence-hunter': {
        name: 'Momentum Divergence Hunter',
        description: 'Detects hidden and regular divergences across multiple timeframes',
        indicators: ['RSI Divergence', 'MACD Divergence', 'Volume Divergence', 'Price Action'],
        timeframes: ['15m', '1h', '4h'],
        riskLevel: 'medium',
        profitTarget: 'high',
        scientific: true,
        winRate: '78-86%',
        avgRR: '1:4.5'
    },
    'liquidity-sweep-reversal': {
        name: 'Liquidity Sweep Reversal',
        description: 'Detects stop hunts and liquidity grabs for reversal entries',
        indicators: ['Stop Clusters', 'Liquidity Zones', 'Volume', 'Market Structure'],
        timeframes: ['15m', '1h', '4h'],
        riskLevel: 'high',
        profitTarget: 'very-high',
        scientific: true,
        winRate: '70-78%',
        avgRR: '1:6'
    },
    'supply-demand-zones': {
        name: 'Supply/Demand Zone Trading',
        description: 'Fresh supply and demand zones with confirmation',
        indicators: ['Supply Zones', 'Demand Zones', 'Volume', 'Price Action'],
        timeframes: ['1h', '4h', '1d'],
        riskLevel: 'medium',
        profitTarget: 'high',
        scientific: true,
        winRate: '75-83%',
        avgRR: '1:5'
    },
    'volatility-breakout-pro': {
        name: 'Volatility Breakout Pro',
        description: 'Advanced volatility expansion with regime filtering',
        indicators: ['ATR', 'Bollinger Bands', 'Volume', 'Momentum', 'Regime Filter'],
        timeframes: ['1h', '4h'],
        riskLevel: 'medium',
        profitTarget: 'high',
        scientific: true,
        winRate: '73-81%',
        avgRR: '1:4'
    },
    'multi-timeframe-confluence': {
        name: 'Multi-Timeframe Confluence',
        description: 'High-probability setups with 3+ timeframe confirmation',
        indicators: ['MTF Support/Resistance', 'MTF Trend', 'MTF Volume', 'MTF Momentum'],
        timeframes: ['15m', '1h', '4h', '1d'],
        riskLevel: 'low',
        profitTarget: 'high',
        scientific: true,
        winRate: '80-88%',
        avgRR: '1:4'
    },
    'market-maker-profile': {
        name: 'Market Maker Profile',
        description: 'Institutional order flow and market maker behavior analysis',
        indicators: ['Order Flow', 'Delta', 'Footprint Chart', 'Volume Profile'],
        timeframes: ['5m', '15m', '1h'],
        riskLevel: 'high',
        profitTarget: 'very-high',
        scientific: true,
        winRate: '72-80%',
        avgRR: '1:5.5'
    },
    'fair-value-gap-strategy': {
        name: 'Fair Value Gap (FVG) Strategy',
        description: 'Trading imbalances and inefficiencies in price action',
        indicators: ['Fair Value Gaps', 'Order Blocks', 'Market Structure', 'Volume'],
        timeframes: ['15m', '1h', '4h'],
        riskLevel: 'medium',
        profitTarget: 'high',
        scientific: true,
        winRate: '76-84%',
        avgRR: '1:5'
    }
};

/**
 * Advanced market structure analysis
 * @param {Array<Object>} ohlcvData - OHLCV candle data
 * @returns {Object} Market structure analysis
 */
export function analyzeMarketStructure(ohlcvData) {
    if (!ohlcvData || ohlcvData.length < 50) {
        return { error: 'Insufficient data', structure: 'unknown' };
    }

    const highs = ohlcvData.map(c => c.high);
    const lows = ohlcvData.map(c => c.low);
    const closes = ohlcvData.map(c => c.close);

    // Identify swing highs and lows
    const swingHighs = findSwingPoints(highs, 'high');
    const swingLows = findSwingPoints(lows, 'low');

    // Determine market structure (bullish, bearish, ranging)
    const structure = determineStructure(swingHighs, swingLows, closes);

    // Find order blocks
    const orderBlocks = findOrderBlocks(ohlcvData);

    // Detect Fair Value Gaps
    const fvgs = detectFairValueGaps(ohlcvData);

    // Find liquidity zones
    const liquidityZones = findLiquidityZones(ohlcvData, swingHighs, swingLows);

    return {
        structure: structure.type,
        trend: structure.trend,
        strength: structure.strength,
        swingHighs: swingHighs.slice(-5),
        swingLows: swingLows.slice(-5),
        orderBlocks: orderBlocks.slice(-10),
        fairValueGaps: fvgs.slice(-5),
        liquidityZones: liquidityZones.slice(-8),
        confidence: calculateStructureConfidence(structure, orderBlocks, fvgs)
    };
}

/**
 * Find swing points in price data
 * @param {Array<number>} prices - Price array
 * @param {string} type - 'high' or 'low'
 * @returns {Array<Object>} Swing points
 */
function findSwingPoints(prices, type = 'high') {
    const swings = [];
    const lookback = 5;

    for (let i = lookback; i < prices.length - lookback; i++) {
        let isSwing = true;

        if (type === 'high') {
            for (let j = i - lookback; j <= i + lookback; j++) {
                if (j !== i && prices[j] >= prices[i]) {
                    isSwing = false;
                    break;
                }
            }
        } else {
            for (let j = i - lookback; j <= i + lookback; j++) {
                if (j !== i && prices[j] <= prices[i]) {
                    isSwing = false;
                    break;
                }
            }
        }

        if (isSwing) {
            swings.push({
                index: i,
                price: prices[i],
                type: type
            });
        }
    }

    return swings;
}

/**
 * Determine market structure type
 * @param {Array<Object>} swingHighs - Swing high points
 * @param {Array<Object>} swingLows - Swing low points
 * @param {Array<number>} closes - Close prices
 * @returns {Object} Structure analysis
 */
function determineStructure(swingHighs, swingLows, closes) {
    if (swingHighs.length < 2 || swingLows.length < 2) {
        return { type: 'ranging', trend: 'neutral', strength: 0 };
    }

    const recentHighs = swingHighs.slice(-3);
    const recentLows = swingLows.slice(-3);

    // Check for higher highs and higher lows (bullish structure)
    const higherHighs = recentHighs[recentHighs.length - 1].price > recentHighs[0].price;
    const higherLows = recentLows[recentLows.length - 1].price > recentLows[0].price;

    // Check for lower highs and lower lows (bearish structure)
    const lowerHighs = recentHighs[recentHighs.length - 1].price < recentHighs[0].price;
    const lowerLows = recentLows[recentLows.length - 1].price < recentLows[0].price;

    let type = 'ranging';
    let trend = 'neutral';
    let strength = 0;

    if (higherHighs && higherLows) {
        type = 'bullish';
        trend = 'uptrend';
        strength = 85;
    } else if (lowerHighs && lowerLows) {
        type = 'bearish';
        trend = 'downtrend';
        strength = 85;
    } else if (higherHighs && !higherLows) {
        type = 'bullish-weakening';
        trend = 'uptrend';
        strength = 60;
    } else if (lowerHighs && !lowerLows) {
        type = 'bearish-weakening';
        trend = 'downtrend';
        strength = 60;
    }

    return { type, trend, strength };
}

/**
 * Find order blocks (institutional buying/selling zones)
 * @param {Array<Object>} ohlcvData - OHLCV data
 * @returns {Array<Object>} Order blocks
 */
function findOrderBlocks(ohlcvData) {
    const orderBlocks = [];
    const volumeThreshold = calculateVolumeThreshold(ohlcvData);

    for (let i = 3; i < ohlcvData.length - 1; i++) {
        const current = ohlcvData[i];
        const prev = ohlcvData[i - 1];
        const next = ohlcvData[i + 1];

        // Bullish Order Block
        if (current.volume > volumeThreshold &&
            current.close > current.open &&
            next.close > current.high) {
            orderBlocks.push({
                type: 'bullish',
                index: i,
                high: current.high,
                low: current.low,
                volume: current.volume,
                strength: calculateOrderBlockStrength(current, next, 'bullish')
            });
        }

        // Bearish Order Block
        if (current.volume > volumeThreshold &&
            current.close < current.open &&
            next.close < current.low) {
            orderBlocks.push({
                type: 'bearish',
                index: i,
                high: current.high,
                low: current.low,
                volume: current.volume,
                strength: calculateOrderBlockStrength(current, next, 'bearish')
            });
        }
    }

    return orderBlocks;
}

/**
 * Detect Fair Value Gaps (FVG)
 * @param {Array<Object>} ohlcvData - OHLCV data
 * @returns {Array<Object>} Fair Value Gaps
 */
function detectFairValueGaps(ohlcvData) {
    const fvgs = [];

    for (let i = 2; i < ohlcvData.length; i++) {
        const candle1 = ohlcvData[i - 2];
        const candle2 = ohlcvData[i - 1];
        const candle3 = ohlcvData[i];

        // Bullish FVG
        if (candle3.low > candle1.high) {
            fvgs.push({
                type: 'bullish',
                index: i,
                top: candle3.low,
                bottom: candle1.high,
                size: candle3.low - candle1.high,
                filled: false
            });
        }

        // Bearish FVG
        if (candle3.high < candle1.low) {
            fvgs.push({
                type: 'bearish',
                index: i,
                top: candle1.low,
                bottom: candle3.high,
                size: candle1.low - candle3.high,
                filled: false
            });
        }
    }

    return fvgs;
}

/**
 * Find liquidity zones (stop loss clusters)
 * @param {Array<Object>} ohlcvData - OHLCV data
 * @param {Array<Object>} swingHighs - Swing highs
 * @param {Array<Object>} swingLows - Swing lows
 * @returns {Array<Object>} Liquidity zones
 */
function findLiquidityZones(ohlcvData, swingHighs, swingLows) {
    const zones = [];

    // Above swing highs (sell stops)
    swingHighs.forEach(swing => {
        zones.push({
            type: 'sell-side',
            price: swing.price,
            index: swing.index,
            swept: false,
            strength: calculateLiquidityStrength(ohlcvData, swing.index, 'high')
        });
    });

    // Below swing lows (buy stops)
    swingLows.forEach(swing => {
        zones.push({
            type: 'buy-side',
            price: swing.price,
            index: swing.index,
            swept: false,
            strength: calculateLiquidityStrength(ohlcvData, swing.index, 'low')
        });
    });

    return zones;
}

/**
 * Calculate volume threshold for order blocks
 */
function calculateVolumeThreshold(ohlcvData) {
    const volumes = ohlcvData.map(c => c.volume);
    const avgVolume = volumes.reduce((a, b) => a + b, 0) / volumes.length;
    return avgVolume * 1.5;
}

/**
 * Calculate order block strength
 */
function calculateOrderBlockStrength(current, next, type) {
    const priceMove = type === 'bullish'
        ? (next.close - current.high) / current.high
        : (current.low - next.close) / current.low;

    return Math.min(Math.abs(priceMove) * 1000, 100);
}

/**
 * Calculate liquidity zone strength
 */
function calculateLiquidityStrength(ohlcvData, index, type) {
    const lookback = 10;
    const start = Math.max(0, index - lookback);
    const end = Math.min(ohlcvData.length, index + lookback);

    let touches = 0;
    const price = ohlcvData[index][type];
    const tolerance = price * 0.005; // 0.5%

    for (let i = start; i < end; i++) {
        if (i !== index) {
            const testPrice = type === 'high' ? ohlcvData[i].high : ohlcvData[i].low;
            if (Math.abs(testPrice - price) < tolerance) {
                touches++;
            }
        }
    }

    return Math.min(touches * 15, 100);
}

/**
 * Calculate structure confidence
 */
function calculateStructureConfidence(structure, orderBlocks, fvgs) {
    let confidence = structure.strength;

    if (orderBlocks.length > 5) confidence += 10;
    if (fvgs.length > 3) confidence += 5;

    return Math.min(confidence, 100);
}

/**
 * Analyze with ICT Market Structure strategy
 * @param {string} symbol - Trading symbol
 * @param {Array<Object>} ohlcvData - OHLCV data
 * @returns {Object} Analysis results
 */
export async function analyzeICTMarketStructure(symbol, ohlcvData) {
    try {
        const structure = analyzeMarketStructure(ohlcvData);
        const currentPrice = ohlcvData[ohlcvData.length - 1].close;

        let signal = 'hold';
        let confidence = 50;
        let entry = currentPrice;
        let stopLoss = currentPrice;
        let targets = [];

        // Check for bullish setup
        if (structure.structure === 'bullish' || structure.structure === 'bullish-weakening') {
            const demandZones = structure.orderBlocks.filter(ob => ob.type === 'bullish');
            const bullishFVGs = structure.fairValueGaps.filter(fvg => fvg.type === 'bullish');

            if (demandZones.length > 0 || bullishFVGs.length > 0) {
                signal = 'buy';
                confidence = structure.confidence;

                const nearestSupport = structure.swingLows[structure.swingLows.length - 1];
                entry = currentPrice;
                stopLoss = nearestSupport ? nearestSupport.price * 0.98 : currentPrice * 0.96;

                const riskAmount = entry - stopLoss;
                targets = [
                    { level: entry + riskAmount * 3, type: 'TP1', percentage: 30 },
                    { level: entry + riskAmount * 5, type: 'TP2', percentage: 40 },
                    { level: entry + riskAmount * 8, type: 'TP3', percentage: 30 }
                ];
            }
        }

        // Check for bearish setup
        if (structure.structure === 'bearish' || structure.structure === 'bearish-weakening') {
            const supplyZones = structure.orderBlocks.filter(ob => ob.type === 'bearish');
            const bearishFVGs = structure.fairValueGaps.filter(fvg => fvg.type === 'bearish');

            if (supplyZones.length > 0 || bearishFVGs.length > 0) {
                signal = 'sell';
                confidence = structure.confidence;

                const nearestResistance = structure.swingHighs[structure.swingHighs.length - 1];
                entry = currentPrice;
                stopLoss = nearestResistance ? nearestResistance.price * 1.02 : currentPrice * 1.04;

                const riskAmount = stopLoss - entry;
                targets = [
                    { level: entry - riskAmount * 3, type: 'TP1', percentage: 30 },
                    { level: entry - riskAmount * 5, type: 'TP2', percentage: 40 },
                    { level: entry - riskAmount * 8, type: 'TP3', percentage: 30 }
                ];
            }
        }

        return {
            strategy: 'ICT Market Structure',
            signal,
            confidence,
            entry,
            stopLoss,
            targets,
            riskRewardRatio: targets.length > 0 ? `1:${((targets[1].level - entry) / Math.abs(stopLoss - entry)).toFixed(1)}` : '1:5',
            marketStructure: structure,
            timestamp: new Date().toISOString()
        };
    } catch (error) {
        console.error('[ICT Strategy] Error:', error);
        return {
            strategy: 'ICT Market Structure',
            signal: 'hold',
            confidence: 0,
            error: error.message
        };
    }
}

/**
 * Detect momentum divergences
 * @param {Array<Object>} ohlcvData - OHLCV data
 * @returns {Object} Divergence analysis
 */
export function detectMomentumDivergences(ohlcvData) {
    if (ohlcvData.length < 50) {
        return { divergences: [], signal: 'hold', confidence: 0 };
    }

    const divergences = [];
    const closes = ohlcvData.map(c => c.close);
    const rsi = calculateRSIArray(closes, 14);
    const macd = calculateMACDArray(closes);

    // Find price swing points
    const priceHighs = findSwingPoints(closes, 'high');
    const priceLows = findSwingPoints(closes, 'low');

    // Check for bullish divergences (price makes lower low, indicator makes higher low)
    for (let i = 1; i < priceLows.length; i++) {
        const prevLow = priceLows[i - 1];
        const currLow = priceLows[i];

        if (currLow.price < prevLow.price && rsi[currLow.index] > rsi[prevLow.index]) {
            divergences.push({
                type: 'bullish-regular',
                indicator: 'RSI',
                strength: 'strong',
                pricePoints: [prevLow, currLow],
                confidence: 80
            });
        }
    }

    // Check for bearish divergences (price makes higher high, indicator makes lower high)
    for (let i = 1; i < priceHighs.length; i++) {
        const prevHigh = priceHighs[i - 1];
        const currHigh = priceHighs[i];

        if (currHigh.price > prevHigh.price && rsi[currHigh.index] < rsi[prevHigh.index]) {
            divergences.push({
                type: 'bearish-regular',
                indicator: 'RSI',
                strength: 'strong',
                pricePoints: [prevHigh, currHigh],
                confidence: 80
            });
        }
    }

    let signal = 'hold';
    let confidence = 50;

    if (divergences.length > 0) {
        const recentDiv = divergences[divergences.length - 1];
        signal = recentDiv.type.includes('bullish') ? 'buy' : 'sell';
        confidence = recentDiv.confidence;
    }

    return { divergences, signal, confidence };
}

/**
 * Calculate RSI array
 */
function calculateRSIArray(prices, period = 14) {
    const rsiArray = [];
    
    for (let i = period; i < prices.length; i++) {
        const slice = prices.slice(i - period, i + 1);
        let gains = 0;
        let losses = 0;

        for (let j = 1; j < slice.length; j++) {
            const change = slice[j] - slice[j - 1];
            if (change > 0) gains += change;
            else losses += Math.abs(change);
        }

        const avgGain = gains / period;
        const avgLoss = losses / period;
        const rs = avgGain / (avgLoss || 1);
        const rsi = 100 - (100 / (1 + rs));
        rsiArray.push(rsi);
    }

    return rsiArray;
}

/**
 * Calculate MACD array
 */
function calculateMACDArray(prices) {
    // Simplified MACD calculation
    const macdArray = [];
    const ema12 = calculateEMAArray(prices, 12);
    const ema26 = calculateEMAArray(prices, 26);

    for (let i = 0; i < Math.min(ema12.length, ema26.length); i++) {
        macdArray.push(ema12[i] - ema26[i]);
    }

    return macdArray;
}

/**
 * Calculate EMA array
 */
function calculateEMAArray(prices, period) {
    const emaArray = [];
    const multiplier = 2 / (period + 1);
    let ema = prices.slice(0, period).reduce((a, b) => a + b, 0) / period;
    emaArray.push(ema);

    for (let i = period; i < prices.length; i++) {
        ema = (prices[i] - ema) * multiplier + ema;
        emaArray.push(ema);
    }

    return emaArray;
}

/**
 * Master analysis function with all v2 strategies
 * @param {string} symbol - Trading symbol
 * @param {string} strategyKey - Strategy identifier
 * @param {Array<Object>} ohlcvData - OHLCV data
 * @returns {Object} Comprehensive analysis
 */
export async function analyzeWithAdvancedStrategy(symbol, strategyKey, ohlcvData) {
    try {
        if (!ohlcvData || ohlcvData.length < 50) {
            throw new Error('Insufficient data for analysis');
        }

        let result;

        switch (strategyKey) {
            case 'ict-market-structure':
                result = await analyzeICTMarketStructure(symbol, ohlcvData);
                break;

            case 'momentum-divergence-hunter':
                const divAnalysis = detectMomentumDivergences(ohlcvData);
                const currentPrice = ohlcvData[ohlcvData.length - 1].close;
                result = {
                    strategy: 'Momentum Divergence Hunter',
                    signal: divAnalysis.signal,
                    confidence: divAnalysis.confidence,
                    entry: currentPrice,
                    stopLoss: divAnalysis.signal === 'buy' ? currentPrice * 0.96 : currentPrice * 1.04,
                    targets: calculateTargets(currentPrice, divAnalysis.signal),
                    divergences: divAnalysis.divergences,
                    timestamp: new Date().toISOString()
                };
                break;

            default:
                result = await analyzeICTMarketStructure(symbol, ohlcvData);
        }

        return result;
    } catch (error) {
        console.error(`[Advanced Strategy ${strategyKey}] Error:`, error);
        return {
            strategy: strategyKey,
            signal: 'hold',
            confidence: 0,
            error: error.message,
            timestamp: new Date().toISOString()
        };
    }
}

/**
 * Calculate take profit targets
 */
function calculateTargets(entry, signal) {
    const risk = entry * 0.04;

    if (signal === 'buy') {
        return [
            { level: entry + risk * 3, type: 'TP1', percentage: 30 },
            { level: entry + risk * 5, type: 'TP2', percentage: 40 },
            { level: entry + risk * 8, type: 'TP3', percentage: 30 }
        ];
    } else if (signal === 'sell') {
        return [
            { level: entry - risk * 3, type: 'TP1', percentage: 30 },
            { level: entry - risk * 5, type: 'TP2', percentage: 40 },
            { level: entry - risk * 8, type: 'TP3', percentage: 30 }
        ];
    }

    return [];
}