Linear Regression Channel System
Author: kyokushin
Creation Date: 12/2/2013 5:24 PM
profile picture

kyokushin

#1
Hi

Is it possible to draw a linear regression channel with the drag and drop functionality in WL?

I read through this: http://www2.wealth-lab.com/WL5WIKI/DrawLinRegChannel.ashx, but I'm not familiar with coding.

Thanks for your answer.

Matt
profile picture

Cone

#2
There's LinReg manual drawing tool in the Drawing Toolbar, which will be on the right side of the app (if selected in the View menu).
profile picture

kyokushin

#3
Thanks Cone

Is it possible to make rules with linear regression channels too?
profile picture

Eugene

#4
It is possible, like with any indicator, to make rules with these indicators:

LinearReg
LinearRegSlope
profile picture

kyokushin

#5
But is it possible to make rules with LinReg Channels, not only with the LinReg Line?
profile picture

Eugene

#6
"DrawLinRegChannel" is an eye candy method for plotting channels on your chart. So probably no, I don't think there exists any indicator to simulate linear regression channel rules.
profile picture

Cone

#7
If you run the example from the Wiki link in your first post, you'll see that for a fixed period the channel changes slope with each successive bar, following the LinReg line (which isn't plotted in the example). To use a channel in the way you're probably thinking, you have to identify a period of interest and then extend the channel (until it's not useful anymore). This is not unlike creating a trading system with trendlines - it's beyond the scope of the Strategy Builder because it's a fairly complex coding effort that requires a lot of housekeeping.
profile picture

kyokushin

#8
I was thinking about calculating each day all possible regression lines between n=30 and n=350 days and R² for each regression line too. I would then choose the regression line with the highest R² and a positive slope as entering rule.

To exit I would look for the regression line with the highest R² and a negative slope. Or when the stock leaves the index.

How would I have to code this?
profile picture

Cone

#9
The logic is to identify the LR lines is pretty straightforward, but...

1. After you find the reg line with the highest R², what triggers the entry? R² aside, price is going to be either above or below the linear reg indicator, which is the last point in the associated LR line. Would you buy if the price is higher or lower than the LR indicator?

2. The thread's topic is about LR Channels. Maybe your trade trigger is when price is within some distance of the upper or lower channel?

3. Exit trigger, same question?
profile picture

kyokushin

#10
Hi Cone,

Lets say entry trigger shall be when that highest R² is above 90%. Regardless of the price.

Exit trigger shall be if the slope of the LR line with the highest R² turns negative.

Maybe the topic is misleading by now. I thought about something else in the beginning but it wont work as I figured out.

profile picture

Cone

#11
Note that with those rules you're likely to quickly find another period with an R² over 90% shortly after exiting.

Also, this code ..
1. has a handy method to draw an ad-hoc LR line, and,
2. performs the R² search for periods in steps of 10, which you can easily change.

CODE:
Please log in to see this code.
profile picture

kyokushin

#12
I want to add a filter to improve the performance.

If less than half of the maximum possible positions are actually open I want to close all positions. Entry signals shall just be penciled in until at least half of the maximum possible positions will be open. If that point is reached all entry signals shall be implemented at once. If in the meantime an exit signal was triggered the corresponding entry signal will not be valid anymore.

How would I have to code this?
profile picture

Cone

#13
1. Remove the mutual exclusion between the exit and entry logic. (comment out the "else")
2. Create and manage a List<int> that holds the period, n, for each signal.
3. When there's signal, add it to the list.
4. On each bar, loop through the signal list checking if the slope has fallen below zero. If it has, remove it from the list.
5. On each bar, check if the List.Count is greater than half some maxSignals constant. If it is, buy the positions.
6. Thereafter as more signals occur up to the max, you buy the positions immediately.
7. When the List.Count falls below maxPositions / 2, exit Positions.AllPositions

You'll probably need to add a little hysteresis so that once you put on half your positions, they're not all exited on the next bar because only one of the signals is no longer valid.

Just reviewing the code that I gave you, it probably should check that the LinearRegSlope is greater than zero as condition to enter the trade. If it is not, the trade will be exited immediately even though RSquared > 90%.
profile picture

kyokushin

#14
Thanks, Cone, for your introduction. I'm afraid this is way beyond my few so far gathered capabilities of coding. How would you have done this?
profile picture

Cone

#15
When I get a chance I can knock this out for you, but first you have to be more precise about how signals would be queued in the list.

Currently, there may be many RegLines with R² > 90%, but only the highest R² is bought. Now, with your new idea, do all of the RegLines with R² > 90% (and LinRegSlope > 0) qualify as a signal?

Or, is the highest R² a "signal" on any given bar and another signal is not added until a different line becomes the highest R²?

In either case, the signal would remain in the list until the slope turned negative.
profile picture

kyokushin

#16
It should be like this: The highest R² is a "signal" on any given bar and another signal is not added until a different line becomes the highest R²
profile picture

Cone

#17
It turns out this Strategy has an interesting dynamic. Uncomment the PrintDebug statements to see what's happening. Lines are drawn as follows:

Blue: entering Position(s)
Gray: slope of a signal line falls below zero, but a sufficient number of "other" signals keep the current Positions alive
Red: Exit all Positions for insufficient signals.

CODE:
Please log in to see this code.