!siLagrange

Download script
Author: Copyright � 2007, ������� �������
!siLagrange

This script is a sophisticated technical analysis tool designed to identify and visualize market trends by applying advanced mathematical modeling—specifically Lagrange Polynomial Interpolation—to historical price data. Unlike standard moving averages that simply smooth out price noise, this tool attempts to map the underlying "skeleton" of price movements using high-level geometry.

Core Logic Overview

The script operates through a three-stage process: identifying significant structural points, calculating a mathematical curve, and projecting potential future movements.

1. Identification of Structural Extremes (Pivot Points)

Instead of looking at every single price candle, the script scans the historical data to find "significant" peaks or valleys. It looks for points that are higher (or lower) than all surrounding candles within a specific range (defined by the user as Depth).

By isolating these major turning points, the script filters out "market noise"—the minor fluctuations that often distract traders—and focuses only on the significant structural changes in price.

2. Mathematical Curve Fitting (Lagrange Interpolation)

Once these key structural points are identified, the script applies the Lagrange Polynomial method. In simple terms, this is a mathematical formula used to find a smooth curve that passes exactly through a specific set of coordinates.

  • The Goal: To create a continuous line that connects all the major highs and lows identified in the first step.
  • The Benefit: This creates a "best-fit" trajectory of the market's path, showing the primary trend more clearly than a standard indicator would.

3. Visual Output and Projection

The tool provides three distinct visual elements on your chart:

  • Historical Path: A line representing the calculated mathematical curve over past data.
  • Projected Path: A "future" projection that extends the mathematical curve forward based on the current trajectory. This allows users to visualize where the trend might naturally head if the current momentum continues.
  • Structural Markers: Visual dots placed at the exact peaks and valleys used to build the calculation, helping you verify which price points the script is prioritizing.

Key User Parameters

To customize how the logic behaves, the script offers several adjustable settings:

  • Polynomial Power: Determines the complexity of the curve (higher numbers create more complex curves; lower numbers keep it smoother).
  • Depth: Controls how "significant" a peak must be to be noticed by the script.
  • Mode: Allows you to choose whether the script should track Highs, Lows, or Closing prices.
  • Shift: Determines how far into the "future" the projected curve is displayed.
Miscellaneous
Implements a curve of type %1
17 Views
0 Downloads
0 Favorites
!siLagrange
//+------------------------------------------------------------------+
//|                                         		      siLagrange.mq4 |
//|                                 Copyright © 2007 Ñåðãååâ Àëåêñåé |
//|                                                los@we.kherson.ua |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Ñåðãååâ Àëåêñåé "
#property link      "mailto: los@we.kherson.ua"
//----
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 SkyBlue
#property indicator_color2 Crimson
#property indicator_color3 Crimson
//----
extern int Pow=3;//Ñòåïåíü ïîëèíîìà Ëàãðàíæà. Æåëàòåëüíî 3..8
extern int Depth=12;//ìèíèìàëüíîå ðàññòîÿíèå ìåæäó îïîðíûìè òî÷êàìè
extern int Mode=MODE_HIGH;//MODE_HIGH, MODE_LOW, MODE_CLOSE
extern int TimeFrame=0;
extern int Shift=10;//Íà ñêîëüêî áàðîâ ïðîäëåâàåì â áóäóùåå
//----
double Lx[];//Ïîëèíîì Ëàãðàíæà
double Lx0[];//Ïîëèíîì Ëàãðàíæà â áóäóùåå íà shift áàðîâ
double Pnt[];//Èíäèêàòîð îïîðíûõ òî÷åê ïîñòðîåíèÿ

int X[];//Íîìåðà áàðîâ ïî êîòîðûì ñòðîèòñÿ ïîëèíîì
double Y[];//Çíà÷åíèÿ öåíû â òî÷êàõ (O, H, L èëè C)
double C[];//Êîýôôèöèåíòû ïîëèíîìà

int init()
{	
//---- indicator line
	SetIndexBuffer(0,Lx); SetIndexStyle(0,DRAW_LINE);
	SetIndexBuffer(1,Lx0); SetIndexStyle(1,DRAW_LINE); SetIndexShift(1, Shift); 
	SetIndexBuffer(2,Pnt); SetIndexStyle(2,DRAW_ARROW); SetIndexArrow(2, 159);

	ArrayResize(X, Pow); ArrayResize(Y, Pow); ArrayResize(C, Pow);

	return(0);
}

void FindPoint()
{
	int pos, pos2, pos_prev = -1;
	if (Depth<=0) Depth=2;
	int i=Depth;
	int n=0;//ñ÷åò÷èê òî÷åê (äî Pow)
	while((i<Bars-Depth) && (n<Pow))
	{
		if (Mode==MODE_HIGH)
		{
			pos = iHighest(NULL,0,MODE_HIGH,2*Depth+1,i-Depth);	pos2 = iHighest(NULL,0,MODE_HIGH,2*Depth+1,pos-Depth); 
			if ((pos==pos2)&&(pos!=pos_prev))
			{
				X[n] = pos; Y[n]=High[pos]; n++; 
				pos_prev = pos; 
			}
		}
		if (Mode==MODE_LOW)
		{
			pos = iLowest(NULL,0,MODE_LOW,2*Depth+1,i-Depth);	pos2 = iLowest(NULL,0,MODE_LOW,2*Depth+1,pos-Depth); 
			if ((pos==pos2)&&(pos!=pos_prev))
			{
				X[n] = pos; Y[n]=Low[pos]; n++; 
				pos_prev = pos; 
			}
		}
		i++;
	}
}

void Lagrange()
{
	int i, j, b;
	//1. Íàéäåì êîýôôèöèåíòû ïîëèíîìà
	for (i = 0; i<Pow; i++)
	{
		C[i] = 1.0;
		for (j = 0; j<Pow; j++)
			 if (j != i)  C[i] = C[i]*(X[i]-X[j]);
		C[i] = Y[i]/C[i];
	}
	// 2. Òåïåðü ïðîõîäÿ ïî âñåì áàðàì äî ìàñèìàëüíîãî áàðà, óêàçàííîãî â ðàñ÷åòå ïîëèíîìà 
	//ðàñ÷èòûâàåì ñàì ïîëèíîì (èíäèêàòîð Lx)
	int bars = X[ArrayMaximum(X)];
	double sx;
	for (b = 0; b<=bars; b++)
	{
		Lx[b] = 0.0; Pnt[b] = 0;
		for (i = 0; i<Pow; i++)
		{
			sx = 1;
			for (j = 0; j<Pow; j++)
				if (j != i)	sx = sx*(b-X[j]);
			Lx[b] = Lx[b]+C[i]*sx;
		}
	}
	//3. Çàïîëíèëè ìàññèâ îïîðíûõ òî÷åê
	for (i = 0; i<Pow; i++) Pnt[X[i]] = Y[i];

	//4. È ñòðîåì èíäèêàòîð Lx0 â áóäóùåå íà Shift
	for (b = -Shift; b<=0; b++)
	{
		Lx0[b+Shift] = 0.0;
		for (i = 0; i<Pow; i++)
		{
			sx = 1;
			for (j = 0; j<Pow; j++)
				if (j != i)	sx = sx*(b-X[j]);
			Lx0[b+Shift] = Lx0[b+Shift]+C[i]*sx;
		}
	}
	
}

int start()
{
	if (Pow<=1) Pow = 2;//ïðîâåðêà êîððåêòíîñòè
	if (Pow>=21) Pow = 20;//ïðîâåðêà êîððåêòíîñòè

	//0. Îïðåäåëÿåì òàáëèöó çíà÷åíèé X è Y äëÿ ïîñòðîåíèÿ ïîëèíîìà 
	FindPoint();
	//
	Lagrange();
	//----
	return(0);
}
//+------------------------------------------------------------------+

Comments