Custom screen: linear regression and other indicators
Author: denoving
Creation Date: 8/16/2012 11:47 AM
profile picture

denoving

#1
Eugene: regarding your post on July 27, 2012 at 1:19PM....

Note: rather than wait for your response, I decided to start a new topic, as rereading your
previous post, I see that is what your preference was before.

You posted the following code in response to my request............ (again, thank you for this).....

CODE:
Please log in to see this code.



The problem is that this doesn't seem to behave as advertised.

For example, today it did not find SDT, and PER both of which would
appear to meet the parameters.

Is there some way to find out what WL specifically thinks that
each of the parameters are for each stock: e.g.

1) slope positive or negative?
2) price 2.5% below linreg,
3) price 1.5% below SMA20,
4) price > $4,
5) volume > 30,000?

This information would be useful in debugging the algorythm.

regards,
Dan
profile picture

denoving

#2
I went back and noticed that I made a mistake. The scanner did find PER and SDT,
only as a "short" alert, rather than a "long" alert.

I'm going to reseach this a little more and
try to understand better what's going on here, as I would not have thought that
this would have popped up as a "short" alert, but rather as a long alert.

I'll post again after I've figured out what it is that I'm not understanding.

Thanks again.
Dan
profile picture

Eugene

#3
Hi Dan,

I do not know what "as advertised" exactly is, but by issuing Buy and Short alerts I intended to highlight candidates with different ROC sign (positive/negative change) - thinking it would be convenient:
CODE:
Please log in to see this code.

You can turn the individual conditions on or off:
CODE:
Please log in to see this code.


Anyway, you have a working screen so feel free to adjusting it up to your taste!
profile picture

denoving

#4
Eugene:

Thank you for your quick response.

I see what you are saying about how the long and short alerts are produced.

I was interested in being able to determine if a dividend or distribution was paid on the stock
within the trailing 15 market sessions.

Is there a way to do this?

regards,
Dan

profile picture

Eugene

#5
Dan,

If you ask "is there a way", then you probably haven't read the FAQ ;)

FAQ: Is it possible?

Otherwise, the context of your last question is not clear to me. Depends on what exactly you want to do with this.
profile picture

denoving

#6
Sorry for the ambiguity....

if there was a dividend paid that is greater than 0.9% within the last 5 sessions, and if the stock price is down > 2% from the average stock price for the 10 days preceeding exdiv day then I want to buy the stock [this is a scanner]
profile picture

Eugene

#7
Dan, with the introduction of fundamental items do we have to rename the thread again? :)

Here's the scan:
CODE:
Please log in to see this code.

Requires Fidelity fundamental data to be updated prior to running the screen.

Should you have any general inquiries on how to debug the algorithm, please start right here: FAQ > How do I start with C# ?
profile picture

denoving

#8
referencing back to the code posted above ( on August 16th of 2012 ), I would like to also plot the linear regression line on the chart for the scanner. Could you please help me out with this?

regards,
Dan
profile picture

Cone

#9
Add this routine and just pass the bar number, series, etc.

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

denoving

#10
Thank you Cone.

I put that code into my program, and it compiled except for two parameters:

"close" meaning the closing stock price (series?), and
"solid" meaning the linestyle is a solid line (?)

here is the calling statement:

DrawLinRegLine(bar,close,90,Color.Blue,solid,1);


Apparently I'm not doing what WL6 is anticipating.

Apparently it wants something different from "CLOSE" for the series, and
something different than "solid" for the linestyle.

Also hopefully I've positioned the code in the proper location.

Please advise if you have any suggestions. Thank you.



I've included the scanner C# code, below......

regards,
Dan

- - - - - - - - - - - - - - - - - - - - -- -


Here is the C# code.....

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

Eugene

#11
You're almost there. Just replace your call to DrawLinRegLine with this line:
CODE:
Please log in to see this code.
profile picture

denoving

#12
Thanks Eugene.

I hate the fact that C# distinguishes between uppercase and lower case. Ug. Oh Well.

Yea, it worked good once I capitalized the letters.

I'm getting more confidence with C#.

I'm probably going to have more questions about painting the charts, but I need
to define my requirements better before I can ask my questions.

I notice that WL6 has borrowed heavily from WL4 in the charting area, which should
be a great help in ramping up more quickly.

I'm still having trouble with my understanding of the C# coding basics and structure, which
is impeding my progress with the mechanics of implementing some of the things that I want
to do. It's the structure of interfaces and variables and procedures and functions that
has me slowed down, and the way to structure loops and the basic programming stuff. UG.

In a nutshell it's the whole OOP vs. procedural thing that has me bogged down.

Thanks again for your help.

regards,
Dan
profile picture

denoving

#13
I'm still working on my list.

I don't have it all sorted out yet, but I want to be able to
do the following things if you could help me out:

1) I'd like to draw parallel lines, 5% above and 5% below LINREG for the trailing 90 days.
2) I'd like to paint a number on the chart representing the slope of the linreg in cents per day.
3) I'd like to paint a number on the chart which is the difference between the most recent closing
price and the LINREG value for the most recent day in percent.

regards,
Dan
profile picture

Cone

#14
1)
CODE:
Please log in to see this code.


2) DrawText or AnnotateBar will work just fine.
3) ditto 2)
profile picture

denoving

#15

Cone: thank you for the linreg channel that worked like a champ.

I looked up the drawtext to put the computed number on the chart, and
here's what I got. Unfortch, I got an error message....

"error CS0118: "Wealthlab.wealthscript.close" is a 'property' but is used like a 'method'.

Here is the code that I wrote to get this error message.......

CODE:
Please log in to see this code.


Since I'm just trying to assign a variable to a computed number and I
don't know what a property or a method is, I'm kinda stuck.

Any suggestions?

Sorry to be so lame.

regards,
Dan
profile picture

Eugene

#16
QUOTE:
Close(bar)

This error comes from not studying the manuals:

WealthScript Programming Guide: DataSeries > Accessing a Single Value from a DataSeries > How to: Access a value at a specific bar

QUOTE:
string pctbelow_linreg

Why a "string"? Check out the QuickRef for DrawText and you'll know the proper type for the pctbelow_linreg variable instantly.

QUOTE:
error CS0118

When you notice something like this next time, give Google a go and a MSDN page with error description will be right on top.

profile picture

denoving

#17
thanks for the response Eugene.

I went out to MSDN like you suggested, and got this:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Compiler Error CS0118
Visual Studio 2005 Other Versions Visual Studio .NET 2003
7 out of 76 rated this helpful - Rate this topic
Error Message
'construct1_name' is a 'construct1' but is used like a 'construct2'


The compiler detected a situation in which a construct was used in some erroneous way or a disallowed operation was attempted on a construct. Some common examples include:

An attempt to instantiate a namespace (rather than a class)

An attempt to call a field (rather than a method)

An attempt to use a type as a variable

An attempt to use an extern alias as a type.

To resolve this error, make sure the operation you are performing is valid for the type you are performing the operation on.

Example
The following sample generates CS0118.

Copy
// CS0118.cs
// compile with: /target:library
namespace MyNamespace
{
class MyClass
{
// MyNamespace not a class
MyNamespace ix = new MyNamespace (); // CS0118
}
}

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -


unfortunately that information doesn't do me any good.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

So rather than dwell on the message,

I've been working at it for several hours, and here's what I finally got:

CODE:
Please log in to see this code.



Bottom line is that Where I calculate the initial point of the 90 day linreg, I'm doing something wrong.

If you have any suggestions, they would be appreciated. Thanks.






regards,
Dan
profile picture

Eugene

#18
You're not doing anything wrong, the code runs fine for me.
profile picture

denoving

#19
As always, thanks for your quick response!

On closer examination, you may discover that it doesn't work as
well as you think. Take a test case and verify the numbers on
a stock symbol. I think that you will find that the linreg[bar-90]
datapoint is not correct.

I agree that the code compiles, and I agree that it runs, but it gives the
wrong data for the linreg[bar-90] value.

For example if you take the stock symbol "ACAS", you see that it finds the
LINREG[bar] value correctly at $10.78, however it finds the original point,
that is to say LINREG[bar-90] value to be $9.36, which it is not, and
instead it should be $8.90.

It seems to compute the slope fine except for the initial value (linreg[bar-90])
does not seem to be correct.

Also, the degree of crazyness of the results seem to depend on whether the
slope is positive or negative and steep vs. flat.

Any suggestions on how to fix that? What am I doing wrong?

Your help is appreciated.

regards,
Dan

profile picture

Eugene

#20
Dan, I did not imply that it was producing correct or incorrect results. I don't know what you think your algorithm is doing wrong as I did not analyze it. The code compiles, runs fine, and that's great in my book.

As to the initial value of the 90-period LinearReg indicator at [bar-90], it is correct at $9.36, and I see no reason why it should be $8.90 as you said. Why? Drop the indicator on a chart of ACAS and notice its value on 4/20/2012: $9.36.
profile picture

denoving

#21
Sorry, but I'm looking at my chart, and that's not at all what it says.

Is there an email address where I can email you a jpeg of my screen capture?

My chart says $8.90, not $9.36.

You're using the 90 bar LinReg line, correct? If I use the 252 bar LinReg line,
then I get $9.15, but still not $9.36.

Here, let me post the whole script, and you can see what I mean....



CODE:
Please log in to see this code.

regards,
Dan
profile picture

Eugene

#22
QUOTE:
You're using the 90 bar LinReg line, correct?

Yes. $8.90 is incorrect assumption. I see what you're seeing. You are probably wondering why the DrawLinRegChannel output does not match the start of the blue LinearReg indicator line. Is this the source of confusion?
profile picture

denoving

#23
right exactly.

That's exactly what is the source of confusion.

Yes, thank you.

So you're saying that the right number is not what
the blue line reflects??

So if $9.36 is the correct value, how come the blue line
does not start there?

I don't understand.

regards,
Dan

profile picture

denoving

#24
When I just "eyeball" the chart, the blue line looks correct for the
LinReg line drawn, which is what was making me question the $9.36 value.

$8.90 "looks" to be correct for the start of the blue line, and hence
seems like it should be the correct value.

regards,
Dan
profile picture

denoving

#25
It really does look to me like there might be something wrong with LinReg.series, but as I said above, that's only based on a visual observation, rather than a rigorous mathematical analysis and review.
profile picture

Eugene

#26
I'm not familiar with this stuff to judge what's wrong with your script, sorry.
profile picture

Cone

#27
Just about everyone who starts looking at LinearReg lines for the first time falls into the same trap. LinearRegSlope is the instantaneous slope of the LR line at a given bar. Note that the slope changes on every bar (practically speaking). This means that the line is changing slope on every bar. The LR value at a bar is the value of the LinearReg indicator, which is only the last value of the line. To find the beginning or any other point on the straight LR line, use y = mx + b

Run this and hopefully what I'm saying will be very clear.

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

denoving

#28
OK, fair enough.

How about this......

Can you tell me the proper call from the library routine in order to
retrieve the linear regression line point that is 90 bars prior to
the most recent bar? This is using day bars.

Maybe I'm just doing the library call incorrectly?

That would explain it all, because the LinReg function appears to be
plotting the data correctly.

regards,
Dan
profile picture

denoving

#29
regarding your post Cone.....

Thanks. I actually understand this concept well. But I agree with
you that most don't get it.

I very much understand that the slope of the LR line for a 90 day period
is quite different than the LR line for a 40 day period or a 150 day period.

What I seek is to obtain the LR data points(closing price, date) for
the start and end of the LR line, and then I'll compute my own slope
for those two data points using the Y=MX + b equation, or actually the
(price[bar]-price[bar-90])/(date2-date1)...that is the rise over the run.

So all I need is to be able to figure it out is the LR[90day] start point.

regards,
Dan
profile picture

Cone

#30
1. You don't need to compute the slope. That's what LinearRegSlope does. DrawLinRegLine() uses it.

2. The line is being drawn by DrawLinRegLine(), which uses DrawLine(). The beginning and end points are passed as parameters, otherwise the line wouldn't be drawn the way it is.

3. LR[bar-90] (which actually spans 91 bars) does not exist as the beginning of the LR[bar] line like you still think it does. Instead, LR[bar-90] is the end of the LR line 90 bars prior to the current bar. I explained and demonstrated it with the script.
profile picture

denoving

#31
Cone:

I know that i don't need to compute the slope to get the LR line(s).

I don't need to compute the LR slope, but I desire to get the raw slope number,
so I can look at it and use that raw number to compare one stock to another one,
and maybe make decisions in the scanner with that number.

I wanted the beginning point for the blue LR line so I could compute the slope.

If there is a better way to get that raw slope number for the Linear Regression
line for the most recent 90 day bars, then by all means, please show me how I can
get that data so I can print it out on the chart.

regards,
dan




profile picture

denoving

#32
Cone:

Thanks for your help.

Never mind that last post.

I figured it out. it works now.

Thanks again!

regards,
Dan
This website uses cookies to improve your experience. We'll assume you're ok with that, but you can opt-out if you wish (Read more).