!siLagrangePnt

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

This script functions as a sophisticated mathematical forecasting tool for financial charts. Rather than relying on standard moving averages or basic trend lines, it utilizes a method called Lagrange Interpolation to connect specific data points and project potential future price movements.

Core Logic Overview

The primary objective of this tool is to take specific "anchor points" identified by the user on a chart and generate a smooth mathematical curve that passes through them. This curve serves two purposes: it validates the historical path of the price and provides a visual estimation of where the price might head next.

How the Process Works

1. Data Collection (Point Identification) The script scans your trading chart for specific markers (arrows) that have been tagged with a unique identifier. Instead of looking at every single price tick, it focuses only on these significant coordinates. It captures two main pieces of information for each marker:

  • The Time: Where the point sits horizontally on the timeline.
  • The Price: The exact value of the point on the vertical axis.

2. Mathematical Organization Once the points are identified, the script organizes them chronologically. It ensures that the data is sorted correctly so that the mathematical model can "understand" the progression of time from left to right. If there are not enough points provided to meet the required complexity (the "Power" setting), the system alerts the user.

3. Curve Construction (Interpolation) Using the Lagrange method, the script calculates a complex polynomial equation. Think of this as finding the perfect "thread" that weaves through all your selected markers perfectly.

  • Historical Tracking: It draws a line (the Lx line) across your existing chart data to show how the curve fits the historical price action.
  • Future Projection: This is the most unique feature of the script. Based on the trajectory established by your markers, it extends that same mathematical curve into the "empty space" ahead of the current price (the Lx0 line).

Key Features for the User

  • Customizable Complexity: You can adjust the "Power" setting to determine how complex the curve should be. A higher power allows the curve to bend more to accommodate more points, while a lower power keeps the projection smoother and less sensitive to minor fluctuations.
  • Visual Projection: By looking at the projected line extending into the future (the Shift), you can visually estimate where the current trend is heading based on the landmarks you have identified as significant.
  • Object-Based Inputs: The script is entirely reactive to your manual input. It only builds its logic based on the specific objects you place on the chart, giving you total control over which price "peaks" or "valleys" the model should respect.
Miscellaneous
Implements a curve of type %1
17 Views
0 Downloads
0 Favorites
!siLagrangePnt
//+------------------------------------------------------------------+
//|                                         		      siLagrange.mq4 |
//|                                 Copyright © 2007 Ñåðãååâ Àëåêñåé |
//|                                                los@we.kherson.ua |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, Ñåðãååâ Àëåêñåé "
#property link      "mailto: los@we.kherson.ua"

#include <WinUser32.mqh>
//----
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 SkyBlue
#property indicator_color2 Crimson
//----
extern int Pow=3;//Ñòåïåíü ïîëèíîìà Ëàãðàíæà. Æåëàòåëüíî 3..8
extern string ID="Lx";//Èäåíòèôèêàòîð îáúåêòà äëÿ íàõîæäåíèÿ îïîðíûõ òî÷åê
extern int Shift=10;//Íà ñêîëüêî áàðîâ ïðîäëåâàåì â áóäóùåå
//----
double Lx[];//Ïîëèíîì Ëàãðàíæà
double Lx0[];//Ïîëèíîì Ëàãðàíæà â áóäóùåå íà shift áàðîâ

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

int init()
{	
//---- indicator line
	SetIndexBuffer(0,Lx); SetIndexStyle(0,DRAW_LINE);
	SetIndexBuffer(1,Lx0); SetIndexStyle(1,DRAW_LINE); SetIndexShift(1, Shift); 
	return(0);
}

void GetPoint()
{
	string name;
	int i, j=0, n = ObjectsTotal();
	ArrayResize(X, n); ArrayResize(Y, n); ArrayResize(C, n);

	for (i = 0; i<=n; i++)
	{
		name = ObjectName(i);
		if ((ObjectType(name)==OBJ_ARROW)&&(StringFind(name, ID)>=0))
		{
			X[j] = iBarShift(NULL, 0, ObjectGet(name, OBJPROP_TIME1));
			Y[j] = ObjectGet(name, OBJPROP_PRICE1);
			j++;
		}
	}
	if (j<Pow) Print("Íóæíî åùå "+(Pow-j-1)+" îïîðíûõ òî÷åê äëÿ "+ID+" ïîëèíîìà.");
	//Ñîðòèðîâêà ìàññèâîâ (ìåòîäîì "ïóçûðüêà")
	double y;	bool b=true;
	while (b)
	{
		i = 0; b = false;
		while (i<j-1)
		{
			if (X[i]>X[i+1])
			{
				n = X[i]; X[i] = X[i+1]; X[i+1] = n;
				y = Y[i]; Y[i] = Y[i+1]; Y[i+1] = y;
				b = true;
			}
			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[Pow-1];
	double sx;
	for (b = 0; b<=bars; b++)
	{
		Lx[b] = 0.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. È ñòðîåì èíäèêàòîð 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()
{
	//1. Îïðåäåëÿåì òàáëèöó çíà÷åíèé X è Y äëÿ ïîñòðîåíèÿ ïîëèíîìà 
	GetPoint();
	//2.Ñòðîèì ïîëèíîì
	Lagrange();
	//----
	return(0);
}
//+------------------------------------------------------------------+

Comments