##FFTSpectr

Download script
Author: Copyright � 2009, sHell
##FFTSpectr

This script is designed to perform a mathematical analysis of data trends by identifying underlying frequencies within a set of numbers. Instead of looking at price movements as simple linear movements (up or down), it treats the data like an audio signal or a wave, decomposing it into its fundamental components.

How the Logic Works

1. Data Collection and Windowing The script works by looking at a specific "window" of historical data. The size of this window is determined by a user-defined setting; it calculates how many previous data points to analyze to create a representative sample of the current market activity.

2. Signal Processing (The Fourier Transform) The core of this script is the use of a "Fast Fourier Transform" (FFT). In non-technical terms, this is a mathematical process that takes a complex, messy signal (like a fluctuating price chart) and breaks it down into individual simple waves. Imagine taking a chord played on a piano and using a tool to identify every individual note that makes up that sound; the FFT does the same for financial data, identifying the different "rhythms" or cycles present in the movement.

3. Wave Composition Currently, the script is configured to simulate these waves using three distinct periodic functions (sine waves). Each wave has its own:

  • Amplitude: How tall or strong the wave is.
  • Frequency: How often the wave repeats over a period of time.
  • Phase: The starting point of the wave.

While the script currently uses these artificial waves for testing purposes, the logic is structured so that it can be directed to analyze actual price data (like Closing Prices) or other technical indicators.

4. Visualization (The Spectrogram) After the math is performed, the script calculates the "magnitude" of these frequencies. It takes the complex results and simplifies them into a visible format called a spectrogram. This is displayed on your chart as a histogram, allowing you to see which frequencies are dominant in the data.

Summary for the User Essentially, this tool is a frequency analyzer. Rather than telling you "the price is going up," it seeks to show you the "heartbeat" of the market by highlighting the recurring cycles and patterns hidden within the noise of price fluctuations.

Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
##FFTSpectr
//+------------------------------------------------------------------+
//|                                            ##FFTSpectr.mq4      |
//|                                          Copyright © 2009, sHell |
//|                                                  diakin@narod.ru |
//+------------------------------------------------------------------+

//Based on
//+------------------------------------------------------------------+
//|                                           i_i_SpecktrAnalis_1.mq4|
//|                                          Copyright © 2006, klot. |
//|                                                     klot@mail.ru |
//+------------------------------------------------------------------+

// Ïðèìåð èñïîëüçîâàíèÿ áûñòðîãî ïðåîáðàçîâàíèÿ ôóðüå (ÁÏÔ)\ Fast Fourier transform (FFT)
//  êà÷åñòâå òåñòîâîé çàäàåòñÿ ôóíêöèÿ èç òðåõ ñèíóñîèä ñ ðàçíûìè ÷àñòîòàìè è àìïëèòóäàìè.
// Äëÿ ïåðåõîäà ê öåíàì ðàçêîììåíòàðüòå  sig=Close[i];



#property copyright "Copyright © 2009, sHell"
#property link      "diakin@narod.ru"
//---
#include <stdlib.mqh>
#define pi 3.14159265358979323846
//---
#import "#_lib_FFT.ex4"
void realfastfouriertransform(double& a[], int tnn, bool inversefft);
#import
//---
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Red

//---- buffers
double SpectrBuffer[];
//---
extern double n=8;// Çàäàåò ðàçìåð ìàññèâà - êîëè÷åñòâî ñâå÷åé íà êîòîðûõ ïðîèçâîäÿòñÿ âû÷èñëåíèÿ. Ïðè n=8 ÷èñëî ñâå÷åé = 2^8=256 ñâå÷åé


// ïàðàìåòðû òåñòîâîé ôóíêöèè
extern double a1=1.0;// Àìïëèòóäà ïåðèîäè÷åñêîé ôóíêöèè
extern double f1=1.0;// ×àñòîòà ïåðèîäè÷åñêîé ôóíêöèè
extern double ff1=0;// Ôàçà ïåðèîäè÷åñêîé ôóíêöèè

extern double a2=4.0;// Àìïëèòóäà ïåðèîäè÷åñêîé ôóíêöèè
extern double f2=4.0;// ×àñòîòà ïåðèîäè÷åñêîé ôóíêöèè
extern double ff2=0;// Ôàçà ïåðèîäè÷åñêîé ôóíêöèè

extern double a3=8.0;// Àìïëèòóäà ïåðèîäè÷åñêîé ôóíêöèè
extern double f3=8.0;// ×àñòîòà ïåðèîäè÷åñêîé ôóíêöèè
extern double ff3=0;// Ôàçà ïåðèîäè÷åñêîé ôóíêöèè



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexBuffer(0,SpectrBuffer);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int tnn1=MathPow(2,n);//  ðàçìåð ìàññèâà äîëæåí áûòü ñòåïåíüþ äâîéêè
   double aa[];
   int N=ArrayResize(aa,tnn1);
   SetIndexDrawBegin(0,Bars-N);

   // Ïîñòðîåíèå ñïåêòðà 
   double sig;
   for(int i=0; i<=N-1; i++)
   {

//
   sig=a1*MathCos(f1*i/N*(2.0*pi)+ff1*pi)+a2*MathCos(f2*i/N*(2.0*pi)+ff2*pi)+a3*MathCos(f3*i/N*(2.0*pi)+ff3*pi); // Îáûêíîâåííàÿ ïåðèîäè÷åñêàÿ ôóíêöèÿ - ê ðûíêó íå èìåååò îòíîøåíèÿ
 //sig=Close[i]; // ðûíî÷íûå öåíû
 //sig=iRSI(NULL,0,14,PRICE_CLOSE,i+1); // ????????
   aa[i]=sig;
      
   }
   
   // Ïðÿìîå ïðåîáðàçîâàíèå Ôóðüå - ïîñëå âûïîíåíèÿ ôóíêöèè â ìàññèâå aa[] - ñïåêòðîãðàììà
   realfastfouriertransform(aa, tnn1, false); 
   
   //--- Âûâîä ñïåêòðîãðàììû íà ýêðàí
   for( i=0; i<=(N-1)/2; i++)
   {
   // Ìîäóëü êîìïëåêñíîãî ÷èñëà
      SpectrBuffer[i]=(MathSqrt(aa[i*2]*aa[i*2]+aa[i*2+1]*aa[i*2+1]))/(N/2); 
   }


   //--- îáðàòíîå ïðåîáðàçîâàíèå ôóðüå
   /*
   //realfastfouriertransform(aa, tnn1, true);
   for( i=0; i<=N; i++)
   {
      SpectrBuffer[i]=aa[i];
   }*/

   //----
   return(0);
  }
  
//+------------------------------------------------------------------+


//--------------------------------------------------------------------+

// íå çíàþ çà÷åì ýòà ôóíêöèÿ ;)
void InSigNormalize(double& aa[])
{
   double sum_sqrt;
   int element_count=ArraySize(aa);
   sum_sqrt=0;

   for( int i=0; i<=element_count-1; i++)
   {
      sum_sqrt+=MathPow(aa[i],2);
   }
   sum_sqrt=MathSqrt(sum_sqrt);
   
   if (sum_sqrt!=0)
   {
      for( i=0; i<=element_count-1; i++)
      {
         aa[i]=aa[i]/sum_sqrt;
      }
   }
   return;
}
//---------------------------------------------------------------------+

Comments