This Python script performs time series analysis on financial data (specifically EUR/USD closing prices) using polynomial detrending, Fourier transformation, and forecasting techniques. The code includes methods for removing trends, applying spectral analysis, and making future price predictions.
- Data Loading: Reads EUR/USD price data from CSV files
- Detrending: Implements two detrending methods (difference and linear)
- Polynomial Fitting: Fits polynomial curves to price data for trend extraction
- Fourier Analysis: Applies FFT for frequency domain analysis and filtering
- Forecasting: Generates future price predictions using Fourier components
- Visualization: Creates plots to compare original data, trends, and predictions
pandas
numpy
matplotlib
scikit-learn (for LinearRegression - note: currently missing import)pip install pandas numpy matplotlib scikit-learn- Place your EUR/USD price data in a CSV file at the path
'Prices/EURUSD_.csv' - Run the script:
python timeseries_analysis.pyThe script includes a detrend() function with two methods:
# Difference method
detrended = detrend(prices, method='difference')
# Linear regression method
detrended = detrend(prices, method='linear')training: Number of data points used for training (default: 226)foresight: Number of future points to predict (default: 20)deg: Polynomial degree for fitting (default: 10)
-
Data Preparation (
lines 36-55):- Loads and preprocesses EUR/USD price data
- Sets up training and testing datasets
-
Polynomial Fitting (
lines 57-67):- Fits a 10th-degree polynomial to training data
- Calculates percentage error of the fit
-
Residual Analysis (
lines 70-75):- Computes differences between actual prices and polynomial fit
-
Fourier Analysis (
lines 78-112):- Applies FFT to residuals
- Filters frequency components
- Reconstructs signal using inverse FFT
-
Forecasting (
lines 114-138):- Extends Fourier components for future predictions
- Combines polynomial trend and Fourier components
- Calculates prediction weights
-
Visualization (
lines 140-145):- Plots original data, polynomial fit, and predictions
The code currently uses LinearRegression without importing it. Add this import at the top:
from sklearn.linear_model import LinearRegression-
Console Output:
- Percentage error of polynomial fit
- Fourier component values
- Prediction weight
- Combined trend and cyclical components
-
Visual Output:
- Plot 1: Polynomial fit vs. actual closing prices
- Plot 2: Final predictions vs. original data (training and full dataset)
Percentage error: 0.XXX %
The prediction weight is: Y.YYY
The prediction weight represents the average of forecasted Fourier components and can be used as an indicator of future price movements.
- The code uses a fixed training size of 226 points
- Fourier filtering thresholds are hard-coded and may need adjustment
- The polynomial degree (10) might lead to overfitting
- Error handling for the detrending function is basic
- File paths are hard-coded and may need modification
To adapt this script for your needs:
- Modify
df=pd.read_csv('Prices/EURUSD_.csv')to point to your data file - Adjust
trainingandforesightparameters based on your data size - Change polynomial degree (
deg=10) as needed - Modify Fourier filtering thresholds for different frequency cutoffs
- Missing error handling for file I/O
- LinearRegression import is missing
- Hard-coded parameters may not generalize well
- Limited comments in complex sections
- No validation of forecast accuracy
- Add cross-validation for parameter selection
- Implement more robust error handling
- Create configuration file for parameters
- Add performance metrics for predictions
- Implement multiple forecasting methods for comparison
Code for time series analysis using polynomial and Fourier methods
#Fourier analysis for Forex predictions https://medium.com/@mnyandenilunga/fourier-analysis-for-forex-predictions-c2313adc6379