Administrator
2025-12-28 e22b96f9f9596052805d60c03ab815a17861e9f0
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
package com.xcong.excoin.modules.okxNewPrice.indicator;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
 
/**
 * MACD Indicator Test Class
 * Used to verify the correctness of MACD calculation logic
 */
public class MACDTest {
 
    public static void main(String[] args) {
        // Create MACD instance
        MACD macd = new MACD();
        
        // Set MACD bars parameters (optional, using default values here)
        macd.setMacdBarsMultiplier(2); // Default multiplier
        macd.setMacdBarsSmoothingPeriod(0); // No smoothing
        
        // Generate test price data (simple upward trend)
        List<BigDecimal> prices = generateTestPrices(30);
        
        System.out.println("=== MACD Indicator Test Start ===");
        System.out.println("Price count: " + prices.size());
        System.out.println("MACD parameters: fast=" + macd.getFastPeriod() + ", slow=" + macd.getSlowPeriod() + ", signal=" + macd.getSignalPeriod());
        System.out.println("MACD bars parameters: multiplier=" + macd.getMacdBarsMultiplier() + ", smoothing=" + macd.getMacdBarsSmoothingPeriod());
        System.out.println();
        
        // Calculate MACD
        macd.calculate(prices);
        
        // Output results
        System.out.println("=== MACD Calculation Results ===");
        System.out.println("DIF: " + macd.getDif());
        System.out.println("DEA: " + macd.getDea());
        System.out.println("MACD Bars: " + macd.getMacdBar());
        System.out.println();
        
        // Trend judgment
        System.out.println("=== Trend Judgment ===");
        if (macd.getDif().compareTo(BigDecimal.ZERO) > 0) {
            System.out.println("DIFF > 0: Bullish trend");
        } else if (macd.getDif().compareTo(BigDecimal.ZERO) < 0) {
            System.out.println("DIFF < 0: Bearish trend");
        } else {
            System.out.println("DIFF = 0: No trend");
        }
        
        // Test smoothing function
        testSmoothingFunction();
        
        System.out.println("=== MACD Indicator Test End ===");
    }
    
    /**
     * Generate test price data
     * @param count Number of data points
     * @return Price list
     */
    private static List<BigDecimal> generateTestPrices(int count) {
        List<BigDecimal> prices = new ArrayList<>();
        // Start from 100, simple upward trend with some random fluctuations
        BigDecimal basePrice = new BigDecimal(100);
        for (int i = 0; i < count; i++) {
            // Add random fluctuation between 0.1 and 0.5
            BigDecimal price = basePrice.add(new BigDecimal(Math.random() * 0.4 + 0.1));
            prices.add(price.setScale(2, BigDecimal.ROUND_HALF_UP));
            basePrice = price;
        }
        return prices;
    }
    
    /**
     * Test MACD bars smoothing function
     */
    private static void testSmoothingFunction() {
        System.out.println("=== MACD Bars Smoothing Function Test ===");
        
        MACD macd = new MACD();
        macd.setMacdBarsMultiplier(2);
        macd.setMacdBarsSmoothingPeriod(3); // 3-day smoothing
        
        // Generate test price data with more fluctuations
        List<BigDecimal> prices = generateVolatileTestPrices(30);
        
        macd.calculate(prices);
        
        System.out.println("Price count: " + prices.size());
        System.out.println("MACD parameters: fast=" + macd.getFastPeriod() + ", slow=" + macd.getSlowPeriod() + ", signal=" + macd.getSignalPeriod());
        System.out.println("MACD bars parameters: multiplier=" + macd.getMacdBarsMultiplier() + ", smoothing=" + macd.getMacdBarsSmoothingPeriod());
        System.out.println();
        
        System.out.println("Smoothed MACD Results:");
        System.out.println("DIF: " + macd.getDif());
        System.out.println("DEA: " + macd.getDea());
        System.out.println("MACD Bars: " + macd.getMacdBar());
        System.out.println();
    }
    
    /**
     * Generate test price data with more fluctuations
     * @param count Number of data points
     * @return Price list
     */
    private static List<BigDecimal> generateVolatileTestPrices(int count) {
        List<BigDecimal> prices = new ArrayList<>();
        // Start from 100 with more random fluctuations
        BigDecimal basePrice = new BigDecimal(100);
        for (int i = 0; i < count; i++) {
            // Add random fluctuation between -1.0 and 1.0
            BigDecimal price = basePrice.add(new BigDecimal(Math.random() * 2 - 1));
            prices.add(price.setScale(2, BigDecimal.ROUND_HALF_UP));
            basePrice = price;
        }
        return prices;
    }
}