Fourier transform in BTCUSDT 1d trading. params: ‘sl_method’: ‘percent_0.08’ ‘ts_method’: ‘percent_0.08’ ‘tp_method’: ‘percent_0.23’

def fourier_analysis(prices, lookback):
    """
    Performs FFT analysis on a given price series, reconstructs a signal
    using dominant frequencies, and returns the current signal value and its trend.
    """
    # Ensure we have enough data for the lookback period
    if len(prices) < lookback:
        return 0, 0 # Return zeros if not enough data

    # 1. Detrend the data: Remove the linear trend to make the data more stationary for FFT
    x = np.arange(len(prices)) # Create an array for the x-axis (time index)
    coeffs = np.polyfit(x, prices, 1) # Fit a 1st degree polynomial (linear trend)
    trend_line = np.polyval(coeffs, x) # Calculate the trend line values
    detrended_prices = prices - trend_line # Subtract the trend to get detrended data

    # 2. Apply Fast Fourier Transform (FFT)
    fft_values = np.fft.fft(detrended_prices) # Compute FFT of the detrended data
    # frequencies = np.fft.fftfreq(len(detrended_prices)) # Not directly used for reconstruction here, but useful for analysis

    # 3. Identify and select dominant frequencies
    # Calculate the magnitude (amplitude) of each frequency component
    magnitude = np.abs(fft_values)
    # Sort indices by magnitude and select the top 'num_components' (excluding DC component, which is at index 0)
    # We take the top 'num_components' from both positive and negative frequencies, effectively
    # selecting 'num_components' dominant cycles.
    # Ensure we don't pick the 0th (DC) component for cycles if it's not truly dominant/relevant.
    # For simplicity, we sort and pick the largest `num_components` directly.

    # Exclude the DC component (index 0) if it's not a desired "cycle"
    # Create a temporary array, set magnitude at index 0 to -infinity to ignore it
    temp_magnitude = magnitude.copy()
    num_components = 3
    if num_components > 0: # Only if we actually need components
        temp_magnitude[0] = -np.inf # Ignore DC component for cycle detection

    dominant_indices = np.argsort(temp_magnitude)[-num_components:]

    # 4. Reconstruct signal using only the dominant frequencies
    reconstructed_fft_spectrum = np.zeros_like(fft_values, dtype=complex) # Initialize with zeros
    # Copy only the FFT values corresponding to dominant frequencies
    reconstructed_fft_spectrum[dominant_indices] = fft_values[dominant_indices]

    # Apply inverse FFT to convert back to time domain
    reconstructed_signal = np.real(np.fft.ifft(reconstructed_fft_spectrum))

    # 5. Extract current signal value and its directional trend
    current_signal = reconstructed_signal[-1] # The last value of the reconstructed signal
    # The trend of the reconstructed signal (current value minus previous value)
    signal_trend = reconstructed_signal[-1] - reconstructed_signal[-2] if len(reconstructed_signal) > 1 else 0

    return current_signal, signal_trend

lookback = window
sma = df['close'].rolling(lookback, min_periods=lookback).mean().to_list()
signal, signal_trend = fourier_analysis(prices=df['close'].values, lookback=lookback)
if not hasattr(self, 'fft_signal') or self.fft_signal is None:
    self.fft_signal = 0.0

if not hasattr(self, 'fft_trend') or self.fft_trend is None:
    self.fft_trend = 0.0

prev_signal = self.fft_signal
self.fft_signal = signal
self.fft_trend = signal_trend


condition_long = signal_trend > 0 and prev_signal < 0 and close > sma[-1]
condition_short = signal_trend < 0 and prev_signal > 0 and close < sma[-1]
==========BTCUSDT_1d==========
策略名称: 		stg_name:fourier_trans_55,window_size:55, atr:55, is_use_percent_of_equity:False,
is_sl:True, is_tp:False, sl_method: percent_0.08, tp_method: percent_0.23
运行天数: 		640.00天
年化收益率: 		109.29%, LINEAR: 85.43%
夏普比率: 		2.037
运行时间区间: 		2023-09-04 08:00:00 ~ 2025-06-05 08:00:00
总交易次数: 		21
---|最大连续盈利次数: 	2
---|最大连续亏损次数: 	7
---|盈利次数: 		6
---|亏损次数: 		15
---|胜率: 		28.57%
---|最大回撤: 		-36.37%

盈亏: 		26509.11
---|最大盈利金额: 	15249.45
---|最大亏损金额: 	-607.76
---|盈利金额: 		32133.92
---|亏损金额: 		-5624.81

每笔平均盈利: 		1262.339
---|盈利单平均盈利: 		5355.654
---|亏损单平均亏损: 		-374.987

盈亏比(总金额): 		5.713
盈亏比(单笔平均): 		14.282
+++++BTCUSDT_1d+++++
2023-09-04 08:00:00, Short, 25826.02, -0.24152022843628249
2023-09-19 08:00:00, Short TS, 27210.26, -0.24152022843628249
loss
2023-10-27 08:00:00, Long, 30805.763174858406, 1.113632023341609
2024-01-12 08:00:00, Long TS, 42782.73, 1.113632023341609
win
2024-01-22 08:00:00, Short, 40985.06961448729, -0.22828457931160312
2024-01-26 08:00:00, Short TS, 41823.51, -0.22828457931160312
loss
2024-01-26 08:00:00, Short, 41823.51, -0.14913875592938036
2024-02-08 08:00:00, Short TS, 45288.65, -0.14913875592938036
loss
2024-05-01 08:00:00, Short, 61860.42800371468, -0.15124789266634497
2024-05-03 08:00:00, Short TS, 62882.01, -0.15124789266634497
loss
2024-05-03 08:00:00, Short, 62882.01, -0.09919381155277955
2024-05-15 08:00:00, Short TS, 66206.5, -0.09919381155277955
loss
2024-05-16 08:00:00, Short, 65235.21, -0.09561563839527765
2024-05-20 08:00:00, Short TS, 71446.62, -0.09561563839527765
loss
2024-08-17 08:00:00, Short, 59491.99, -0.1048461523979951
2024-08-23 08:00:00, Short TS, 64037.24, -0.1048461523979951
loss
2024-09-21 08:00:00, Long, 63348.96, 0.09846264642702895
2024-10-01 08:00:00, Long TS, 60805.78, 0.09846264642702895
loss
2024-10-30 08:00:00, Long, 67356.47979168742, 0.509324187978627
2024-12-19 08:00:00, Long TS, 97461.86, 0.509324187978627
win
2024-12-29 08:00:00, Short, 93738.2, -0.06654177539146261
2025-01-06 08:00:00, Short TS, 102235.6, -0.06654177539146261
loss
2025-01-08 08:00:00, Short, 95060.61, -0.06561609745613878
2025-01-14 08:00:00, Short TS, 96560.86, -0.06561609745613878
loss
2025-01-14 08:00:00, Short, 96560.86, -0.06459663107805792
2025-01-17 08:00:00, Short SL, 104285.72880000001, -0.06459663107805792
loss
2025-02-02 08:00:00, Short, 97700.59, -0.06384307658735736
2025-02-03 08:00:00, Short TS, 101328.52, -0.06384307658735736
loss
2025-02-28 08:00:00, Short, 89529.31115227792, -0.2438449664587254
2025-03-01 08:00:00, Short TS, 86064.53, -0.2438449664587254
win
2025-03-01 08:00:00, Short, 86064.53, -0.0724747610891502
2025-03-02 08:00:00, Short TS, 94270.0, -0.0724747610891502
loss
2025-03-03 08:00:00, Short, 91424.91536014121, -0.10233817923860038
2025-03-05 08:00:00, Short TS, 90606.01, -0.10233817923860038
win
2025-03-10 08:00:00, Short, 84775.92674237504, -0.14715277059618856
2025-03-11 08:00:00, Short TS, 82932.99, -0.14715277059618856
win
2025-03-11 08:00:00, Short, 82932.99, -0.07521139958899346
2025-03-19 08:00:00, Short TS, 86845.94, -0.07521139958899346
loss
2025-04-08 08:00:00, Short, 81464.9110902523, -0.2297003519621965
2025-04-09 08:00:00, Short TS, 82615.22, -0.2297003519621965
loss
2025-05-09 08:00:00, Long, 94393.47806885169, 0.3634391387716066
2025-06-05 08:00:00, Long TS, 101508.68, 0.3634391387716066
win