Page 1 of 1

help code ELD -->MQL4

Posted: Sun Dec 30, 2012 10:02 am
by drudominique
Hello sir.
sorry for my english I'm French.

I try desperately to translate the code "ELD" to "MQL4" I tried several forums (forexfactory, tsd, mql4forum etc ...) but nobody could you translate so I turn to you because you are the a rare tradeurs a program in "ELD" and "MQL4" so maybe that you can bring me.
I try it myself but I debuted in programming and I do not know MQL4 or ELD so it is very hard for me but when I even try but the results are completely false.

Here is the code "ELD" and my translation "MQL4."
if you have the time could you then tell me how to translate this into "MQL4"

Array_SetMaxIndex(Polynomial, DataSize*(MaxDegree+1)+1) ;---> MQ4 ?
Array_SetMaxIndex(SmoothedTimeSeries, DataSize);---> MQ4 ?
Array_SetMaxIndex(Coefficient, MaxDegree+1) ;---> MQ4 ?

LastBarOnChart=TRUE ---> MQ4 ?

if barnumber = 1 ---> MQ4 ?

Value1 ---> MQ4 ?
Value2 ---> MQ4 ?



so I can try to translate it myself properly because I would not annoy you because I know you are very busy.

thank you for your help.
friendly
cordially

CODE ELD

Code: Select all

Inputs:   
   TimeSeries((h+l)/2),          //   The original data to fit, put in whatever you want to look at of whatever time series
                           //
   Width(126),                  //    Length = 2*Width+1, the default gives a fit to one year (252) of daily bars
                           //
   MaxDegree(6),               //    The degree of the polynomial fit.  0 <= MaxDegree <= 2*Width+1
                           //
   ForwardExtension(10),         //   The number of bars the fit is to be extended forward in time,
                           //   **you must have this many spaces in: "Format Window/Space to the Right" **
                           //
   ShowLeadingEdge(FALSE),         //   The leading edge is the rightmost point of every fit in the timeseries
                           //
   ShowPredictedEdge(FALSE),      // The predicted edge is the extended from ForwardExtension bars ago
                           //
                           //
   ShowSmoothedTimeSeries(TRUE),   //    This shows the entire fit over the interval 2*Width+1 for the last bar
                           //   plus the ForwardExtension predictions         
                           //
   LeadingEdgeColor(Blue),
   PredictedEdgeColor(Magenta),
   SmoothedTimeSeriesColor(Red),
   ExtendedTimeSeriesColor(Yellow);

 Variables:
    LeadingEdge(0),
   PredictedEdge(0),
      p(0),j(0),
     DataSize((2*Width+1) + ForwardExtension);

Arrays:
   Polynomial[](0),
   Coefficient[](0),
   SmoothedTimeSeries[](0);

if barnumber = 1 then begin

   //Allocate memory for the arrays.  Polynomial is a 2D array
   Array_SetMaxIndex(Polynomial, DataSize*(MaxDegree+1)+1) ;
   Array_SetMaxIndex(SmoothedTimeSeries, DataSize);
   Array_SetMaxIndex(Coefficient, MaxDegree+1) ;

end;

//Now decompose data into the polynomial set and determine the overlap coefficients:
if ShowLeadingEdge=TRUE or ShowPredictedEdge=TRUE or (LastBarOnChart=TRUE and ShowSmoothedTimeSeries=TRUE ) then begin
   
   //Decompose data into the polynomial set and determine the overlap coefficients:
   for p = 0 to MaxDegree begin
      
      Coefficient[p] = 0;

       for j = -Width to +Width begin
            Coefficient[p]=   Coefficient[p] + Polynomial[p*DataSize+Width+j]*TimeSeries[Width-j];
      end;   
   end;

   //Recompose the original price as the truncated polynomial version - create the smoothed time series, including the
   //forward extension
   for j  = -Width to Width + ForwardExtension begin         
      SmoothedTimeSeries[Width+j] = 0;

      for p = 0 to MaxDegree begin
         SmoothedTimeSeries[Width+j] = SmoothedTimeSeries[Width+j] + Coefficient[p]*Polynomial[p*DataSize+Width+j];
      end;   
   end;

   //The leading edge is at j = Width
   LeadingEdge = SmoothedTimeSeries[Width+Width];

   //The predicted edge is at j = Width+ForwardExtension
   PredictedEdge = SmoothedTimeSeries[Width+Width+ForwardExtension];
    Print(" PredictedEdge  =" ,PredictedEdge :1:6 );
end;

//Plotting routines
if ShowLeadingEdge=TRUE and LeadingEdge<>0  then Plot1(LeadingEdge,"LdngEdge",LeadingEdgeColor);

if ShowPredictedEdge=TRUE and PredictedEdge<>0 then Plot2[-ForwardExtension](PredictedEdge,"FwrdEdge",PredictedEdgeColor);

if LastBarOnChart=TRUE and ShowSmoothedTimeSeries=TRUE then begin      

   for j  = -Width to Width begin
      plot3[Width-j](SmoothedTimeSeries[Width+j],"Fit",SmoothedTimeSeriesColor);
   end;
   
   if ForwardExtension > 0 then begin
      for j  = 1 to ForwardExtension begin
         plot4[-j](SmoothedTimeSeries[Width+Width+j],"Prediction",ExtendedTimeSeriesColor);
      end;
   end;

end;

   
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//   Create the Orthoginal Legendre Polynomials during the first bar, including ForwardExtension data
//
//////////////////////////////////////////////////////////////////////////////////////////////////   
if barnumber = 1 then begin

//The first polynomial is a constant, easy to create
for j = - Width to + Width + ForwardExtension begin
   Polynomial[0*DataSize + Width+j] = 1;
end;

//The second polynomial is a line
if MaxDegree>=1 then begin
   for j = -Width to +Width + ForwardExtension begin
      Polynomial[1*DataSize + Width+j] = j;
   end;
end;

//We use the discrete Legendre polynomial recurrence relations to create the rest
if MaxDegree > 1 then begin      
   for p = 1 to MaxDegree - 1 begin                        // create the polynomial for degree p+1      
      for j = -Width to Width + ForwardExtension begin                        // sum over the interval
         
         Value1 =    j*(2*p+1)/(p+1);                     //discrete Legendre polynomial solution
         Value2 =     - (p/(p+1))*(2*Width+1+p)*(2*Width+1-p)/4;    //discrete Legendre polynomial solution

         Polynomial[(p+1)*DataSize+Width+j]                   //The recurrence relation
         = Value1 * Polynomial[p*DataSize+Width+j] + Value2 * Polynomial[(p-1)*DataSize+Width+j];
      end;
   end;
end;

//Now we have to normalize each polynomial, so that the sum of the square of the polynomial
//over the interval is 1. Instead of doing the calculation however, we apply the pre-determined answer[4]:
for p = 0 to MaxDegree begin      
   Value1 = Power(2,-2*p)/(2*p+1);
   for j = -p to p begin Value1 = Value1 * (2*Width+1+j); end;
   if Value1 > 0 then Value1 = 1/squareroot(Value1);  //this is the normalization coefficient

    for j = -Width to +Width + ForwardExtension begin
      Polynomial[p*DataSize+Width+j] = Value1 * Polynomial[p*DataSize+Width+j];
   end;
end;

//Done!  We now have a orthogonal normalized set of polynomials
      
end;


CODE MQL4 FALSE

Code: Select all

//+------------------------------------------------------------------+
//|                                             XCAP_POLIPREDICT.mq4 |
//|                        Copyright 2012, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2012, MetaQuotes Software Corp."
#property link      "http: //www.metaquotes.net"

#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
#import "kernel32.dll"
   void OutputDebugStringA(string msg);
#import
int    Width                   = 126;
double TimeSeries = 0.00;

int    MaxDegree               = 6;
int    ForwardExtension        = 10;
bool   ShowLeadingEdge         = false;
bool   ShowPredictedEdge       = FALSE;
bool   ShowSmoothedTimeSeries  = TRUE;
color  LeadingEdgeColor        = Blue;
color  PredictedEdgeColor      = Magenta;
color  SmoothedTimeSeriesColor = Red;
color  ExtendedTimeSeriesColor = Yellow;

double LeadingEdge;
double PredictedEdge;
int    p, j, Value1, Value2;
int    DataSize;

double Polynomial[263];
double Coefficient[7];
double SmoothedTimeSeries[];




int init()
{
//---- indicators
DataSize = ((2 * Width + 1) + ForwardExtension);
 //ArrayResize(Polynomial,DataSize);
 

   IndicatorBuffers(8);
   SetIndexBuffer(0,Polynomial);
   SetIndexBuffer(1,Coefficient);
   SetIndexBuffer(2,SmoothedTimeSeries);//----
   ArrayResize(Polynomial, DataSize*(MaxDegree+1)+1) ;
   ArrayResize(SmoothedTimeSeries, DataSize);
   ArrayResize(Coefficient, MaxDegree+1) ;




    return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
//----

//----
    return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
 
   
   
   
   //***************************************************
   
    if (ShowLeadingEdge == TRUE || ShowPredictedEdge == TRUE || ShowSmoothedTimeSeries==TRUE )
   {
        for (p = 0; p <= MaxDegree; p++)
        {
            Coefficient[p] = 0;
         
            for (j = -Width; j < Width; j++)
           
               
              {
                TimeSeries = (High[Width - j] + Low[Width - j]) / 2;
                Coefficient[p] = Coefficient[p] + Polynomial[p * DataSize + Width + j] * TimeSeries;
                  // Print( " Coefficient[p]   =  " ,DoubleToStr(Coefficient[p],7));
                 // Print(" j =  ",DoubleToStr(j,2) );
                //  Log(j,TimeSeries,Coefficient[p],Polynomial[p * DataSize + Width + j]);
               }
        }
             
             
              //   Print( " TimeSeries   =  " ,TimeSeries);
              //  Print( " Coefficient[p]   =  " ,DoubleToStr(Coefficient[p],7));
              //  Print( " Polynomial[p * DataSize + Width + j]   =  " , DoubleToStr(Polynomial[p * DataSize + Width + j],8));
               
           
       
        for (j = -Width; j < (Width + ForwardExtension); j++)
        {
            SmoothedTimeSeries[Width + j] = 0;

            for (p = 0; p < MaxDegree; p++)
                SmoothedTimeSeries[Width + j] = SmoothedTimeSeries[Width + j] + Coefficient[p] * Polynomial[p * DataSize + Width + j];



            LeadingEdge = SmoothedTimeSeries[Width + Width];

            PredictedEdge = SmoothedTimeSeries[Width + Width + ForwardExtension];
    }   
 }
        Print(" PredictedEdge   =  ", PredictedEdge);
        Print(" LeadingEdge   =  ", LeadingEdge);
        for (j = -Width; j < (Width + ForwardExtension); j++)
        Polynomial[0 * DataSize + Width + j] = 1;


    if (MaxDegree >= 1)
    {
        for (j = -Width; j < (Width + ForwardExtension); j++)
            Polynomial[1 * DataSize + Width + j] = j;
    }

    if (MaxDegree > 1)
    {
        for (p = 1; p == MaxDegree - 1; p++)
            {                                                   // create the polynomial for degree p+1
            for (j = -Width; j < (Width + ForwardExtension); j++)
            {
                Value1 = j * (2 * p + 1) / (p + 1);                                      //discrete Legendre polynomial solution
                Value2 = -(p / (p + 1)) * (2 * Width + 1 + p) * (2 * Width + 1 - p) / 4; //discrete Legendre polynomial solution

                Polynomial[(p + 1) * DataSize + Width + j] = Value1 * Polynomial[p * DataSize + Width + j] + Value2 * Polynomial[(p - 1) * DataSize + Width + j];
            }
        }
    }

    for (p = 0; p < MaxDegree; p++)
    {
        Value1 = MathPow(2, -2 * p) / (2 * p + 1);
        for (j = -p; j < p; j++)
        {
       
            Value1 = Value1 * (2 * Width + 1 + j);
        }
        if (Value1 > 0)
            Value1 = 1 / MathSqrt(Value1);             //this is the normalization coefficient

        for (j = -Width; j < (Width + ForwardExtension); j++)
        {
            Polynomial[p * DataSize + Width + j] = Value1 * Polynomial[p * DataSize + Width + j];
        }
    }
 //} 
    //****************************
//************************************************

if(Bars==1)
{
   
 for (j = -Width; j < (Width + ForwardExtension); j++)
        Polynomial[0 * DataSize + Width + j] = 1;


    if (MaxDegree >= 1)
    {
        for (j = -Width; j < (Width + ForwardExtension); j++)
            Polynomial[1 * DataSize + Width + j] = j;
    }

    if (MaxDegree > 1)
    {
        for (p = 1; p == MaxDegree - 1; p++)
            {                                                   // create the polynomial for degree p+1
            for (j = -Width; j < (Width + ForwardExtension); j++)
            {
                Value1 = j * (2 * p + 1) / (p + 1);                                      //discrete Legendre polynomial solution
                Value2 = -(p / (p + 1)) * (2 * Width + 1 + p) * (2 * Width + 1 - p) / 4; //discrete Legendre polynomial solution

                Polynomial[(p + 1) * DataSize + Width + j] = Value1 * Polynomial[p * DataSize + Width + j] + Value2 * Polynomial[(p - 1) * DataSize + Width + j];
            }
        }
    }
}
    for (p = 0; p < MaxDegree; p++)
    {
        Value1 = MathPow(2, -2 * p) / (2 * p + 1);
        for (j = -p; j < p; j++)
        {
       
            Value1 = Value1 * (2 * Width + 1 + j);
        }
        if (Value1 > 0)
            Value1 = 1 / MathSqrt(Value1);             //this is the normalization coefficient

        for (j = -Width; j < (Width + ForwardExtension); j++)
        {
            Polynomial[p * DataSize + Width + j] = Value1 * Polynomial[p * DataSize + Width + j];
        }
    }


Re: help code ELD -->MQL4

Posted: Sun Dec 30, 2012 7:12 pm
by forexjake80
hi there Dominique,

i think we gonna be able to help if you ask questions like "how do i do this or that" in mq4. Remember that we have no idear about the logik in your program. Thats why you should always comment in your sourcecode a lot.

Posted: Mon Dec 31, 2012 12:19 am
by drudominique
Good afternoon.
thank you for your answer.
I'm trying to convert a code "ELD" in MQL4 and for me it is very difficult because I do not know programming and I do not speak English I use google translate :-)

the original source code here it is the end of my post.
as you can see I tried but the results are completely false that makes several weeks I try but my knowledge is too low so I hope you will come to help me.
eg its instructions "ELD" how to convert "MQL4?"

Array_SetMaxIndex(Polynomial, DataSize*(MaxDegree+1)+1) ;---> MQ4 ?
Array_SetMaxIndex(SmoothedTimeSeries, DataSize);---> MQ4 ?
Array_SetMaxIndex(Coefficient, MaxDegree+1) ;---> MQ4 ?

LastBarOnChart=TRUE ---> MQ4 ?

if barnumber = 1 ---> MQ4 ?

Value1 ---> MQ4 ?
Value2 ---> MQ4 ?


thank you for your help.
source code "ELD"

{
XCAP_iPolyFitPredict:

Uses a fit to polynomials to create a least squares best fit for a given maximum degree. Extends the fit
forward in time for trading purposes. See reference [1] for more information.

Author: Paul A. Griffin
January 4, 2007
Extrema Capital, 2007

This indicator provides as output the least square best fit to a set of polynomials of maximum power (degree) using
discrete Legendre polynomials. This is accomplished by decomposing the time series into discrete Legendre polynomials
over the interval of orthogonality given by 2 * Width + 1 and discarding the polynomial fits degrees above some maximum degree.
The remaining smoothed series is continued forward in time by following the polynomial fit.

Some Comments:

In this indicator, we are displaying the least squares fit to a low degree set of polynomials and extending that fit
into the future. This as a tool to extend a low order trend into the future.

Some Plots:

All Charts: Daily chart with a width of 126 bars (126*2+1 = 253, about 1 trading year)

For @US (30 year US Treasury Bond Futures), and SPY roughly for year 2006

MaxDegrees: 1, 2, 3

ForwardExtension: 10

ShowLeadingEdge = TRUE

As you move forward along the time series, the fit is generated. What is displayed is the leading, rightmost bar, "[0]", for
each fit.

ShowPredictedEdge = TRUE

As you move forward along the time series, the fit is generated, and then a prediction is made.
What is displayed is the predicted, beyond the rightmost bar, "[-ForwardExtension]", for each fit.

ShowLastFit = TRUE

This displays the entire last fit for just the last bar on the chart, so you will only see an output on the last
2*Width + 1 bars of your chart. It will also display the polynomial extension of the fit forward in time to bar
[-ForwardExtension]

References:

[1] Legendre Polynomial Fits https://www.tradestation.com/Discussion ... c_ID=58534

[2] Savitzky Golay https://www.tradestation.com/Discussion ... c_ID=59250

[3] http://en.wikipedia.org/wiki/Legendre_polynomials

[4] Peter Seffen, "On Digital Smoothing Filters: A Brief Review of Closed Form Solutions and Two New Filter Approaches",
Circuits Systems Signal Process, Vol. 5, No 2, 1986


}

Inputs:
TimeSeries((h+l)/2), // The original data to fit, put in whatever you want to look at of whatever time series
//
Width(126), // Length = 2*Width+1, the default gives a fit to one year (252) of daily bars
//
MaxDegree(6), // The degree of the polynomial fit. 0 <= MaxDegree <= 2*Width+1
//
ForwardExtension(10), // The number of bars the fit is to be extended forward in time,
// **you must have this many spaces in: "Format Window/Space to the Right" **
//
ShowLeadingEdge(FALSE), // The leading edge is the rightmost point of every fit in the timeseries
//
ShowPredictedEdge(FALSE), // The predicted edge is the extended from ForwardExtension bars ago
//
//
ShowSmoothedTimeSeries(TRUE), // This shows the entire fit over the interval 2*Width+1 for the last bar
// plus the ForwardExtension predictions
//
LeadingEdgeColor(Blue),
PredictedEdgeColor(Magenta),
SmoothedTimeSeriesColor(Red),
ExtendedTimeSeriesColor(Yellow);

Variables:
LeadingEdge(0),
PredictedEdge(0),
p(0),j(0),
DataSize((2*Width+1) + ForwardExtension);

Arrays:
Polynomial[](0),
Coefficient[](0),
SmoothedTimeSeries[](0);

if barnumber = 1 then begin

//Allocate memory for the arrays. Polynomial is a 2D array
Array_SetMaxIndex(Polynomial, DataSize*(MaxDegree+1)+1) ;
Array_SetMaxIndex(SmoothedTimeSeries, DataSize);
Array_SetMaxIndex(Coefficient, MaxDegree+1) ;

end;

//Now decompose data into the polynomial set and determine the overlap coefficients:
if ShowLeadingEdge=TRUE or ShowPredictedEdge=TRUE or (LastBarOnChart=TRUE and ShowSmoothedTimeSeries=TRUE ) then begin

//Decompose data into the polynomial set and determine the overlap coefficients:
for p = 0 to MaxDegree begin

Coefficient[p] = 0;

for j = -Width to +Width begin
Coefficient[p]= Coefficient[p] + Polynomial[p*DataSize+Width+j]*TimeSeries[Width-j];
end;
end;

//Recompose the original price as the truncated polynomial version - create the smoothed time series, including the
//forward extension
for j = -Width to Width + ForwardExtension begin
SmoothedTimeSeries[Width+j] = 0;

for p = 0 to MaxDegree begin
SmoothedTimeSeries[Width+j] = SmoothedTimeSeries[Width+j] + Coefficient[p]*Polynomial[p*DataSize+Width+j];
end;
end;

//The leading edge is at j = Width
LeadingEdge = SmoothedTimeSeries[Width+Width];

//The predicted edge is at j = Width+ForwardExtension
PredictedEdge = SmoothedTimeSeries[Width+Width+ForwardExtension];
Print(" PredictedEdge =" ,PredictedEdge :1:6 );
end;

//Plotting routines
if ShowLeadingEdge=TRUE and LeadingEdge<>0 then Plot1(LeadingEdge,"LdngEdge",LeadingEdgeColor);

if ShowPredictedEdge=TRUE and PredictedEdge<>0 then Plot2[-ForwardExtension](PredictedEdge,"FwrdEdge",PredictedEdgeColor);

if LastBarOnChart=TRUE and ShowSmoothedTimeSeries=TRUE then begin

for j = -Width to Width begin
plot3[Width-j](SmoothedTimeSeries[Width+j],"Fit",SmoothedTimeSeriesColor);
end;

if ForwardExtension > 0 then begin
for j = 1 to ForwardExtension begin
plot4[-j](SmoothedTimeSeries[Width+Width+j],"Prediction",ExtendedTimeSeriesColor);
end;
end;

end;


//////////////////////////////////////////////////////////////////////////////////////////////////
//
// Create the Orthoginal Legendre Polynomials during the first bar, including ForwardExtension data
//
//////////////////////////////////////////////////////////////////////////////////////////////////
if barnumber = 1 then begin

//The first polynomial is a constant, easy to create
for j = - Width to + Width + ForwardExtension begin
Polynomial[0*DataSize + Width+j] = 1;
end;

//The second polynomial is a line
if MaxDegree>=1 then begin
for j = -Width to +Width + ForwardExtension begin
Polynomial[1*DataSize + Width+j] = j;
end;
end;

//We use the discrete Legendre polynomial recurrence relations to create the rest
if MaxDegree > 1 then begin
for p = 1 to MaxDegree - 1 begin // create the polynomial for degree p+1
for j = -Width to Width + ForwardExtension begin // sum over the interval

Value1 = j*(2*p+1)/(p+1); //discrete Legendre polynomial solution
Value2 = - (p/(p+1))*(2*Width+1+p)*(2*Width+1-p)/4; //discrete Legendre polynomial solution

Polynomial[(p+1)*DataSize+Width+j] //The recurrence relation
= Value1 * Polynomial[p*DataSize+Width+j] + Value2 * Polynomial[(p-1)*DataSize+Width+j];
end;
end;
end;

//Now we have to normalize each polynomial, so that the sum of the square of the polynomial
//over the interval is 1. Instead of doing the calculation however, we apply the pre-determined answer[4]:
for p = 0 to MaxDegree begin
Value1 = Power(2,-2*p)/(2*p+1);
for j = -p to p begin Value1 = Value1 * (2*Width+1+j); end;
if Value1 > 0 then Value1 = 1/squareroot(Value1); //this is the normalization coefficient

for j = -Width to +Width + ForwardExtension begin
Polynomial[p*DataSize+Width+j] = Value1 * Polynomial[p*DataSize+Width+j];
end;
end;

//Done! We now have a orthogonal normalized set of polynomials

end;

Posted: Tue Jan 08, 2013 11:07 pm
by drudominique
Good afternoon.
nobody to help me?
friendly

Posted: Sat Jan 19, 2013 1:29 am
by drudominique
Well I think I'll go somewhere else I would have no answer here.
Russian forums are much more competent and sympathetic.
cordially

Posted: Tue Jan 22, 2013 7:25 am
by drudominique
Good afternoon.
unnecessary to answer my questions the solution was given me a Russian forum and without moral lesson :-)
not to say Russian programmers are by far the best anyway I do not think this forum was the level to answer me except with blabla.
good luck.