6th Degree Poly Help! - page 2

 
gooly:

Hi dennisj2,

are you sure you really need your formula?

May be you try first to search for other filters like Kalman, Ehlers, Gauss, Jurik (JMA), DEMA. Most of them derived from low-pass (physical) frequency filters and most of them have found their way into mt4-code.

May be a JMA (there are many variations) with a longer period-set does what you are looking for?



Do you have any links I can research?

 
dennisj2:



SDC - you are now at the same level that I am - the linear regression formula I found works - and there are two distinctly different formulas I found that produce identical results. These formulas are great if linear regression (a straight trend line) is what we were after.

First, some reference material:

  Microsoft:      http://office.microsoft.com/en-us/excel-help/linest-HP005209155.aspx

  IntegralCalc:  https://www.youtube.com/watch?v=1pawL_5QYxE&noredirect=1


Given the linear regression equation y = mx + b:

Where y = price, (e.g., Close[x])

and     x = index (e.g., Bar[x])

and    m = Slope (the coefficient applied to each (x,y) pair)

and    b = Y-intercept (the base value of Y-intercept applied to each (x,y) pair)


Method A: From IntegralCalc                                                                                      Method B: From Microsoft (where x(overbar) and y(overbar) are means)

        


Even the polynomial regression trend line formula to the nth degree is relatively easy to apply:

Given the formula: y = m1*x1 + m2*x2 + m3*x3 + ... + b

Where the variables x, y, m, and b carry the same definition described in the linear equation.

Seems I have everything, so what's missing?

What is missing is the calculation for the m (Slope) and the b (Y-intercept) for a polynomial regression; the linear equation for computing these values does not apply to a polynomial regression. From what I've learned, the polynomial regression requires a formula that calculates least squares based on a system of polynomial equations using a matrix. Look at the example graphs above. In the first graph, the poly(6) lines are absolutely immaculate - tops and bottoms are clearly called out. With this data, I will be able to hold trades longer than I do today ultimately doubling, potentially tripling my average pips per trade.

In the second graph, I show the results of the poly(6) vs. the i-regr method that uses a gauss matrix to solve for the slope coefficients - and, needless to say, it's fairly useless due to the substantial latency of the method. On the other hand, a simple moving average is even worse - the SMA is far too sensitive during intermediate market corrections that could cause an overreaction of my EA.

I am willing to pay someone to develop a true polynomial regression indicator - but, the results absolutely must mirror the results that Excel produces. The LINEST() function is a black box where much voodoo is used to calculate the coefficients. Quite simply, I need to understand this voodoo.

Update: I attached a file showing the method implemented in Excel.


I coded a linear regression a while back here is the link to my code: Linear regression I don't guarantee it is 100% correct though, I think I identified an issue with using the zero x index in the calculations which which I fixed some time later.

I coded a moving version of that too, the moving version creates a parabola similar to a smoothed moving average because at each bar it only draws the last co-ordinate of the linear regression line.

I have been trying to learn how to use a 2nd degree poly nominal for your line. I figured if we can do it with a quadratic we can expand that do it with a 6th degree.

I will read your links and see if I can learn something,

 
SDC:

I coded a linear regression a while back here is the link to my code: Linear regression I don't guarantee it is 100% correct though, I think I identified an issue with using the zero x index in the calculations which which I fixed some time later.

I coded a moving version of that too, the moving version creates a parabola similar to a smoothed moving average because at each bar it only draws the last co-ordinate of the linear regression.

I have been trying to learn how to use a 2nd degree poly nominal for your line. I figured if we can do it with a quadratic we can expand that do it with a 6th degree.

I will read your links and see if I can learn something,


SDC: I agree - a quadratic might not be enough, but I am tending to believe that a cubic could be enough to extend to higher degrees.
 
dennisj2:


Do you have any links I can research?

Just google: mt4   indicator

and then one of the names..

May be you add (lowpass) filter.

Some are in the mt4 code base - some only not and we should not post external links..

 
SDC: I have another link - could be exactly what we need
 
Just remember that regression is curve fitting and as somebody already pointed out regressions re-paint. I am not sure what predictive value pure curve fitting has. From my own experiments their predictive value was not very good.
 
rocketman99:
Just remember that regression is curve fitting and as somebody already pointed out regressions re-paint. I am not sure what predictive value pure curve fitting has. From my own experiments their predictive value was not very good.



Rocketman,

I agree with you in part, in that the regression models tend to repaint historical trends and are not very reliable. This is clearly the case of the i-regr indicator - I've been watching this indicator perform live the past few days and I can honestly say that this indicator offers no value for three reasons: 1) it's hypersensitive to short term corrections, and 2) its severe latency (with the e-regr EA) by the time the indicator paints the lower bound entry point for long positions, or the upper bound for short positions, the market has moved well beyond the entry points and, in most cases, is contrarian to market trend, and 3) from observation alone, when the market headed higher, the indicator was pointing down, and vice-versa so its use as a directional trend indicator is 50% reliable at best.

Last week the USDJPY was hover around 102.35-102.50 area, i-regr showed a market trend to the downside (it had curved down and was moving lower), e-regr issued a sell, then the indicator just 5 periods after shifted to long and repainted as if it had always been long as the USDJPY advanced to 104.20)

In support of the point that re-paint type indicators offer no value and are not reliable - given what I've seen worked with in the MQL4 libs available, I assure you I understand the frustration expressed by those that have worked with these indicators - given what we have, nothing works. That said, I still have yet to see a reliable polynomial regression indicator that calculates to the 6th degree. I will also assure that once we do have a reliable polynomial regression, that the results will be spectacular.

 
dennisj2:



Rocketman,

I agree with you in part, in that the regression models tend to repaint historical trends and are not very reliable. This is clearly the case of the i-regr indicator - I've been watching this indicator perform live the past few days and I can honestly say that this indicator offers no value for three reasons: 1) it's hypersensitive to short term corrections, and 2) its severe latency (with the e-regr EA) by the time the indicator paints the lower bound entry point for long positions, or the upper bound for short positions, the market has moved well beyond the entry points and, in most cases, is contrarian to market trend, and 3) from observation alone, when the market headed higher, the indicator was pointing down, and vice-versa so its use as a directional trend indicator is 50% reliable at best.

Last week the USDJPY was hover around 102.35-102.50 area, i-regr showed a market trend to the downside (it had curved down and was moving lower), e-regr issued a sell, then the indicator just 5 periods after shifted to long and repainted as if it had always been long as the USDJPY advanced to 104.20)

In support of the point that re-paint type indicators offer no value and are not reliable - given what I've seen worked with in the MQL4 libs available, I assure you I understand the frustration expressed by those that have worked with these indicators - given what we have, nothing works. That said, I still have yet to see a reliable polynomial regression indicator that calculates to the 6th degree. I will also assure that once we do have a reliable polynomial regression, that the results will be spectacular.




I am not a mathematician, but my experience with any form of regression was not very good. As you note there are many ways to do regressions which may ultimately result in a better statistical curve fit. But the question remains on it's predictive power.

If you really want to go hardcore with this stuff then you need to investigate R and try some of the advanced regressions available (google for ARIMA, ARCH/GARCH etc): http://talksonmarkets.files.wordpress.com/2012/09/time-series-analysis-with-arima-e28093-arch013.pdf

I spent weeks on this stuff and also investigated co-integration and my success was near zero.

I don't think MQL4 coding is up to the task for this advanced statistical analysis.

 

well, hmmm hmmm..... 

 

I don't think MQL4 coding is up to the task for this advanced statistical analysis.

anything can be done in MQL, this is not the limiting factor. It looks like porting one of these sources would be sufficient:
http://rosettacode.org/wiki/Polynomial_regression

Solving a P6 (polynom of 6th degree) regression is solving a system of 6 equations with 6 variables on a discrete numerical function, which perhaps is not simple, but isn't impossible to do. 

I can look later what method exactly is used in i_regr.mq4.

 

my experience with any form of regression was not very good

I have to agree, this was the point of my first comment. Because ... what are we doing? We are fitting with part of random curve (the actual PA (Price Action)) to P6, in this case. And then we expect the next point of PA to act as it is a continuation of this curve?

OK, it seems that Dennis is using this curve as small latency transformation function of PA, and this has some sense, however how good can this be? This must be done on higher TF because of noise filtering, and i don't think it will be possible to avoid the losses when PA gets into ranging which is the problem with such type of trading strategy.


this indicator offers no value for three reasons: 1) it's hypersensitive to short term corrections, and 2) its severe latency (with the e-regr EA) by the time the indicator paints the lower bound entry point for long positions, or the upper bound for short positions, the market has moved well beyond the entry points and, in most cases, is contrarian to market trend, and 3) from observation alone, when the market headed higher, the indicator was pointing down, and vice-versa so its use as a directional trend indicator is 50% reliable at best

Well, the i-regr doesn't work so bad. To see it in action, just use visual mode backtest of e-regr.mq4 and drop the indicator on the test window.
I guess it uses some kind of fast regression or interpolation, but its result are not so bad. It follows the PA and adapts to the curve.

Your points:
1) all indicators are. this is usually solved with adding another type of indicator as filter, or higher TF.
2) well, with better curve fitting latency will improve. but primary problem is not the latency but the e-regr entry criterias. counter trend triggering on the edges of constant size channel? without adequate recovery for trend change? with no true money management? ridiculous.
my primary idea would be entry/exit on change of regression curve direction, but i am still not sure how a complete repaint indicator would be good for this.

Anyway, i had an impression that you are using intersection of two regression curves with different coefficient of determination for entry/exit

 
T

Yes as Graziani said we can code anything in mql4, it all comes down to how well we understand how to implement this math.

As for repainting, that will not be an issue, the only bar we care about is the zero bar, the current price trading bar. The way I would code this (assuming we can figure out the math) is the equation will be performed on the prices from the last n bars, the resulting value for the zero bar will be drawn, the line on all previous bars will be left how it was from the previous calculation. For one, there is no need to refit the entire line to any chart history when we can only trade on the zero bar and secondly it would show a real history of how the line was applied to each bar when it was the live price bar.

From the link Dennis Posted Polynominal Regression I found this to be very interesting from the point of an adaptive indicator.

"In polynomial regression, different powers of the X variable (X, X2, X3…) are added to an equation to see whether they increase the r2 significantly. First a linear regression is done, fitting an equation of the form Y=a+bX to the data. Then an equation of the form Y=a+b1X+b2X2, which produces a parabola, is fit to the data. The r2 will always increase when you add a higher-order term, but the question is whether the increase in r2 is significantly greater than expected due to chance. Next, an equation of the form Y=a+b1X+b2X2+b3X3, which produces an S-shaped line, is fit and the increase in r2 is tested. This can continue until adding another term does not increase r2 significantly"

This could be all we need to know to code it, notice how the author writes y=a+b1X+b2X2 instead of y=ax2+bx+c  ? That was what was throwing me off, I thought c had to be entirely different coefficient. It appears from this article, the coefficients c,d,e,f,g in y=ax6+bx5+cx4+dx3+ex2+fx+g  are directly related to b which we already know from linear regression, is slope.

Having said that I am finding it difficult to believe it is really that simple ? Why would they write a,b,c,d,e,f,g, coefficients if it really can be written as y=a+b1X+b2X2+b3X3+b4X4+b5X5+b6X? Also as Dennis said, other articles seem to suggest slope intercept as in a linear regression does not apply to poly regression. Sometimes I wonder if they don't just make this stuff up as they go along.

Aside from that, the article appears to imply r2 can be used to measure how well the line fits the data curve while we are testing increases in the polyline equation by degree,

I am thinking r2 might have something to do with variance ? Can anyone say what r2 is for sure ?

Reason: