OneSideGaussianLibrary

Download script
Author:
OneSideGaussianLibrary

This technical library is designed to provide a sophisticated method for smoothing financial data, such as price movements or other market indicators. Unlike standard moving averages that give equal weight to all recent data points, this script utilizes a mathematical concept known as the Gaussian Distribution to prioritize information more intelligently.

Core Logic Overview

The primary goal of this script is to "smooth out" the noise in market charts while maintaining a clear relationship with current price action. It achieves this through three main processes:

1. The Gaussian Weighting System Instead of treating every past price point as equally important, the library calculates a specific weight for each point based on a Gaussian curve (similar to the "bell curve" used in statistics). This ensures that prices closer to the current moment have a more significant influence on the resulting average, while older data points gradually lose influence.

2. Multi-Level Smoothing Options The script provides eight distinct "levels" of intensity for this smoothing effect. These levels are based on a specific mathematical sequence (Fibonacci-style numbers). By choosing different levels, a user can decide how much "smoothing" they want to apply:

  • Lower levels provide a very tight, responsive average that reacts quickly to price changes.
  • Higher levels provide a much smoother, more stabilized line that filters out minor fluctuations and focuses on long-term trends.

3. Dynamic Integration The script is built to be versatile, allowing it to work in two different ways depending on the user's needs:

  • Indicator Smoothing: It can take standard Moving Averages (SMA) as an input and refine them using the Gaussian weightings.
  • Raw Data Smoothing: It can also take any custom array of numbers (such as raw price data or other calculated values) and apply the same Gaussian smoothing logic to that data.

Key Behavioral Features

  • Boundary Protection: The script includes a safety check to ensure it doesn't attempt to calculate data for points that don't exist (e.g., trying to look back further than the chart's history allows). In these cases, it gracefully defaults to a standard calculation to maintain stability.
  • Pre-Calculated Efficiency: To ensure the trading platform remains fast and responsive, the script pre-calculates its weight buffers during initialization. This means the complex math is done once at the start, allowing for high-speed calculations in real-time.

In summary, this library offers a professional tool for traders who want a more nuanced, mathematically "elegant" way to view trends, moving beyond simple averages to a weighted system that emphasizes current market relevance.

Indicators Used
Moving average indicator
11 Views
1 Downloads
0 Favorites
OneSideGaussianLibrary
//+------------------------------------------------------------------+
//|                                       OneSideGaussianLibrary.mq4 |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""
#property library

#define MaxLength 34

//
//
//
//
//

int    levels[] = {1,2,3,5,8,13,21,34};
double GaussianBuffer[][8];

//+------------------------------------------------------------------+
//| Gaussian function                                                |
//+------------------------------------------------------------------+
// Counts function Exp((x - x0)^2/s^2)
// x0 - higher point of function
// x  - point function is counted at
// s  - width of function // don't forget about 3-sigma rule

double Gaussian(int Size, int X)
{
   return (MathExp(-X*X*9/((Size + 1)*(Size + 1))));
}

//
//
//
//
//

void BuffersInit()
{
   int i;
   
   ArrayResize(GaussianBuffer,MaxLength);
   for (int k = 0; k < 8; k++)
   {
      double sum = 0.0;
         for (i = 0; i < MaxLength; i++) { if (i>=levels[k]) break; GaussianBuffer[i][k]  = Gaussian(levels[k],i); sum += GaussianBuffer[i][k];}
         for (i = 0; i < MaxLength; i++) { if (i>=levels[k]) break; GaussianBuffer[i][k] /= sum; }
   }                              
}

//
//
//
//
//

double Smooth(int level,int appliedPrice, int shift)
{
   double sum   = 0;
   int    limit = levels[level];
   
   if (shift >= (Bars - limit)) return(iMA(NULL,0,1,0,MODE_SMA,appliedPrice,shift));
   
   //
   //
   //
   //
   //
   
   for (int i = 0; i < limit; i++)
           sum += GaussianBuffer[i][level]*iMA(NULL,0,1,0,MODE_SMA,appliedPrice,shift+i);
   return (sum);
}

double SmoothArray(int level, double& array[], int shift)
{
   double sum   = 0;
   int    limit = levels[level];
   
   if (shift >= (Bars - limit)) return(array[shift]);
   
   for (int i = 0; i < limit; i++)
           sum += GaussianBuffer[i][level]*array[shift + i];
   return (sum);
}

Comments