Is LinearRegLine (from WL4) the same as LinearReg
Author: zanek
Creation Date: 7/21/2009 7:02 PM
profile picture

zanek

#1
I'm trying to generate Linear Regression Channels, and the code I'm using as a starting point is from WL4 ( http://wl4.wealth-lab.com/cgi-bin/WealthLab.DLL/editsystem?id=29484 )

Anyone know if the LinearRegLine (from WL4) is the same as LinearReg (WL5) ?

If there is any WL5 code for creating Linear Regression Channels already out there, please let me know. Thanks

profile picture

Cone

#2
LinearRegLine will make it in 5.5, but to draw a channel directly in code, it's convenient to use the DrawLinRegChannel function already provided in Community.Components.
profile picture

zanek

#3
Hmm, I integrated the DrawLinRegChannel into my code, but its the 'fancy' Linear regression channels with blurred fan lines. I really need to be able to generate just the upper and lower Linear Regression Channels, so I can programmatically access the upper/lower channel values at any time.

I just need the Linear Regression Channel to look like this:


Do you know if the LinearRegLine (from WL4) is the same as LinearReg (WL5) ?

I'm really surprised WealthLab doesnt have this already. This is standard in all the other software I use (eSignal, Tradestation, etc)
profile picture

Eugene

#4
The lines are fancy, but the code is open source (hint).

See below for an example of coding LinearRegLine with LineExtendX:

SuperBands, with Linear Regression Analysis
profile picture

Cone

#5
Suggestion: try to understand the examples. Look through the code to see why they do what they do.

The "fans" are a result of re-drawing a 45-bar channel 100 times for each the last 100 bars in the chart. It demonstrates that a regression channel is most often not constant, and in fact is constantly changing with the data. If you want to trade reg channels, you need to decide when to stop drawing it, and then trade it only from that bar forward - you can't look back.

Long story short, remove the loop in the example to draw the channel once -
CODE:
Please log in to see this code.
profile picture

zanek

#6
Cone, Eugene,

Thanks very much for your help ! I certainly was trying my best to understand what was going on in the code (and the math behind generating the Linear Regression Channels). I certainly dont want to burden you all with helping drawing the linear regression channels.

I was able to draw the linear regression channels.

Just to give you all and anyone else trying to use LRC's what I did, here is the code

CODE:
Please log in to see this code.


I'm still trying to figure out how to extend the channels throughout the entire chart so the channels look like channels in this image: http://www.metaquotes.ru/images/techanalysis/linestudies/linear_regression_channel/linear_regression_channel.gif
profile picture

zanek

#7
I just realized (correct me if I'm wrong), but to draw a continuous Linear Regression Channel(LRC) like the one in the image above, I would have to draw and erase the LRC on every bar (since its being recalculated on every bar). In the case of backtesting, I guess I could just draw the LRC on the last bar (from the start to finish).
profile picture

zanek

#8
Ok, I figured out how to draw the Linear Regression Channels for any time period. Thanks Cone and Eugene.

The very last thing I'm stuck on is, how to draw it so its one continuous line throughout a given time period (as seen here: http://www.metaquotes.ru/images/techanalysis/linestudies/linear_regression_channel/linear_regression_channel.gif ). Currently, if I use 65 periods on one bar, then recalculate the LRC the next bar, the channels arent linked together as one continuous line. I never thought I'd have to create a basic tech analysis indicator myself (argh)

Edit: Currently I get the effect seen in the 'fancy' Linear Regression Channel code, but just one LRC per bar.

Will let you all know if I figure it out. If anyone knows how to do this, please let me know. Thanks

profile picture

Cone

#9
QUOTE:
I would have to draw and erase the LRC on every bar
You would never do this. Drawing is only for an analyst's visual benefit. There's no reason to draw if you're not going to look at it, so there's no need to erase. Only draw the lines you want to see (conditionally).

QUOTE:
Currently, if I use 65 periods on one bar, then recalculate the LRC the next bar, the channels arent linked together as one continuous line.
Of course not. As I said, the LRC changes for every new bar... sometimes a lot, others not by much. Run the "fan" example again.

QUOTE:
If anyone knows how to do this
I already explained it, but I'm not going to code it for you - it's a complex solution to do this for a dynamic backtest (and needs detailed requirements). Look, the image that you keep pointing is probably drawn by hand. You can do that in Wealth-Lab too - grab the LRC drawing tool. You can also draw it in code, but you need to know 2 points on each line and then extend it - see the LineExtendY function.

(Note: Back to the draw/erase thing - LineExtendY solves for a Y value for some X (bar). It doesn't actually draw the line. That's done by the DrawLine method. Consequently, your code can recognize where lines are without you actually drawing them on the chart.)
profile picture

zanek

#10
Cone,

Thanks for your help. Again, I am not asking you to code it for me. As I said before, Linear Regression Channels are computed automatically in almost every other software package I've used (for example, eSignal computes it in realtime as one continuous line, without the user having to draw anything on the screen).

The problem I'm having is that I cant get the linear channels to be anywhere close to channels produced from other software packages (eg: eSignal). This makes me believe that the math or code to generate them are off, so I'm not able to use them yet in my code.
profile picture

Cone

#11
QUOTE:
eSignal computes it in realtime as one continuous line
If is a continuous line, it's not a LR Channel, instead it's the LinearReg indicator.

Here's a quick exercise for you to get familiar with LR channels and the LR indicator, what they mean, and when:

1. Drag and drop and LinearReg indicator from the Indicators pane. Make the period, say 50 (bars).
2. Click the button in the Toolbar for "Restore Bar Spacing".
3. Now, click the LR channel in the manual drawing tools and drag it over any 50 bars (same as the indicator). Use the Bar number in the chart status bar to help you identify the 50-bar range.

If you've done everything right, you'll notice that the center of the LR channel ends precisely on the LR indicator, and you can repeat step 3 for any 50 bars in the chart. In short, the LR indicator is the end point of the linear regression line for the same period. Almost no one seems to know that.

-----
Edit:
By the way, where do you select the eSignal feature that you're talking about? I can see only the "Regression Trend Channels", which is precisely the same as Wealth-Lab's Regression Channels in the manual drawing tools.
profile picture

zanek

#12
Hi Cone,

In eSignal, to see the Linear Regression Channels, create an Advanced Chart, and then right click on the chart, select "Basic Studies" and then click "Linear Regression". If you right click again after that and click "Edit Studies" you can modify the channel deviations and etc.

profile picture

Cone

#13
Thanks for pointing that out. Studies like that are easy to create in Wealth-Lab, although I like the idea of adding a StdDev option to our manual LR drawing tool.
profile picture

zanek

#14
Hmm, I've been finding it to pretty tough. Its the last thing holding me up from trading currently, since I depend on LRC's in my strategy. I didnt think it would be this tough to just get the current upper/lower Linear Regression Channel values with WL Pro (I get the values currently, but they are way off from the LRC values in eSignal :-(.
profile picture

Cone

#15
That's because the manual drawing tool doesn't use an arbitrary std dev for the channels. Instead, it uses the greatest deviation w.r.t. closing price.

Though I don't have eSignals requirements, it looks like all you have to do is find the max std deviation over the period of your channel and then add and subtract it to your Linear Reg line. This will be slightly easier in 5.5 when LinearRegLine is added back to WealthScript, but you should be able to do it pretty easily with the Community component.


Edit: My mistake. Community.Components doesn't have the LinearRegLine function, it draws the channels directly. When I get a chance, I'll add a new Draw method (or overload) to draw the LR channels based on n Std Deviations.
profile picture

Cone

#16
Here's something to chew on until a new overload methods can be added to Community.Components.DrawLinRegChannel.
CODE:
Please log in to see this code.
profile picture

Eugene

#17
Thank you Robert, your code has been added.
profile picture

zanek

#18
Thanks Cone !
profile picture

grahamgk

#19
Cone,

If you are only looking for a direct replacement of the LinearRegLine function from WL4 to use in WL5, can you do the following?

For WL4, you have:
LinearRegLine(series, start, end, predict)
start = first bar in series
end = last bar in series
predict = the bar at which we want to calculate the value

Would the equivalent for WL5 be?
LinearRegSlope.Value(end, series, end-start)*(predict-end) + LinearReg.Value(end, series, end-start)
profile picture

Eugene

#20
btw LinearRegLine is part of Wealth-Lab 5.5 - to be released shortly.
CODE:
Please log in to see this code.