Writing a script for linear regression scores?
Author: DIY
Creation Date: 5/9/2012 5:07 PM
profile picture

DIY

#1
I use a chart containing 5 LinearReg lines (W-L Standard Indicators) of various time spans to arrive at a daily/weekly hand written score in Excel. Is it possible, without going into details at this stage, to write a script which 'scores' each stock eg the last move is up = 1; level = 0.5; down = 0?
profile picture

Cone

#2
[Almost] anything is possible, and computers are pretty good at assigning values, i.e., scoring. The devil is in the 'details at this stage'.
profile picture

DIY

#3
OK I would like to start but can not find anything along these lines. The aim is have 5 regression lines eg 10, 12, 14, 16, 18 days with summation of scores for bar and bar-1. The scores for each line are: up=1; level=0.5 and down=0. Sell if the summation is <2.5 and buy if >2.5. A small start is:

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

Cone

#4
How do you define of "up", "down", and "level"?
profile picture

Cone

#5
Well heck, here's may take on it - should be a good lesson for you ;)

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

DIY

#6
Thanks Cone for your help to get started. It gives me something to play around with.



Looking at a linear regression chart if the line between the last and second last bar slopes up then = 1; if level then = 0.5; if down = 0. Down the track I am hoping to modify it such that the mean scores of the last 2 bars is > or <2.5.
profile picture

Cone

#7
Your criteria are inadequate because there is probably less than a 0.000001% chance of finding a bar where a regression line is "level", i.e., exactly zero slope. So, I called anything between -0.1% and 0.1% of slope "level" as you can see in the ScoreIt() function. You can change the values there however you like.
profile picture

DIY

#8
Cone, you are correct via stats. I like your alternate script.
profile picture

DIY

#9
I am trying to include some strategy parameters in order to run the Optimiser Monte Carlo but keep coming up with errors. Parameters include int days, days and hopefully scores for buy/sell. I used to have a webpage of error numbers but can not find it now.

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

Eugene

#10
Ian,

Open the WealthScript Programming Guide, Programming Trading Strategies > Strategy Parameters and follow an illustrative tutorial there for "Method 3". I'm sure the task of inserting a strategy parameter will be a piece of cake then. If something isn't clear, let us know.
profile picture

DIY

#11
Eugene, I have tried working through Method 3 and searched for any other info I could find but am none the wiser and need help. I thought it was part of the subscription. I am not a programmer, have no intention to be but at least I am trying to learn the basics. Unfortunately it is written in a foreign language to me and I note some others have the same sentiment. I am sure you would also have problems if you tried to learn my field of expertise.
profile picture

Eugene

#12
Ian, I understand your position and your emotional reply but this is meant to be a help too. Well, if you find trying to teach to fish not acceptable, let's stick to Plan B and simply give you that fish.

And do not change bar+1 to bar in Cone's script like you did. What it does is a blatant peeking into the future. For more information, see this KB article: Bars, Loops, and Bar + 1

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

DIY

#13
Thanks Eugene for the help. I only contact you people as a last resort after I have tried various things for hours but it still does not work. The manuals seem to be written in programmer's language and are not fully explanatory to us mere mortals!
profile picture

DIY

#14
Eugene, I am wondering what the top chart on the screen titled 'The Score' means as when I do the maths it does not always correlate with presumably the correct score on the bottom chart which gives the correct buy or sell at bar + 1? I am reading the 5 regression lines at bar & bar - 1. An example:

[IMG]BHP 22.7.11 - 29.7.12 Eugene original script 13.5.12
Weekly, 22.7.11 to 29.7.11
+ - 0.1% + - 1.0% (where + - 0.1% = 0.5 & >0.1% = 1 for the 1st column, see Cone above)
start finish score score
43.30 42.75 -1.3% 0 0
43.09 42.62 -1.1% 0 0
42.59 42.40 -0.4% 0 0.5
42.23 41.96 -0.6% 0 0.5
42.49 41.86 -1.5% 0 0

Sum 0 1
Chart reads 1 1
[/IMG]

Also is it possible to have another chart on the same screen which gives a running line of the mean of the previous n sales (volume x price) for the same stock?
profile picture

DIY

#15
Trying to send the example as txt. The 0.1% and 1.0% are examples of scoring.


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

Cone

#16
QUOTE:
I am wondering what the top chart on the screen titled 'The Score' means as when I do the maths it does not always correlate with presumably the correct score on the bottom chart which gives the correct buy or sell at bar + 1?
I'm going to remind you of something very important that Eugene already pointed out about peeking into the future. For more information, see this KB article: Bars, Loops, and Bar + 1

Just think of how your trade. You know what The Score is from "today" - from the bar that just completed on 16 May 2012 - because you know the Closing price. This is plotted on the chart. Now you have to decide what to do for the next day, i.e., bar + 1, in other words, 17 May 2012.

So if the score were 5.0 at the close of May 16, you will have an Alert to buy on the open of May 17. However, after the close of May 17 (in about 10 hours from now), the Score will change again.. maybe it's -2 when today closes, but you entered the Position at the beginning of the bar. Understand?
profile picture

DIY

#17
Yes Cone I read the article and I think I understand what you and Eugene are saying - just curious if the top chart was the score at cob on the day and how it was arrived at. However I will not lose any more sleep over it - the script works just fine, thanks, and has allowed me to spend precious time doing modelling and analysis instead of programming.

More importantly my second question was about charting mean turnover per day to allow suitable multiples eg 50:1 to purchase.

profile picture

Cone

#18
QUOTE:
and how it was arrived at
I'm not sure how to answer since the code shows exactly how it is derived. In plain English:

1. Each bar is processed one-by-one (that's the for loop). The important thing to know is that you only can process a bar after it is complete, i.e., when the OHLC/V have been determined and cannot change.

2. The processing for each bar calls the ScoreIt function, once for each regression line, and sums the results.

3. The resulting score is saved to a DataSeries caled score. The value for score for a particular bar is score[bar].

4. When the processing is complete, score DataSeries is plotted in the ChartPane called scorePane.
profile picture

DIY

#19
Yes I realise and understand what you are saying about the score but my previous 2 requests above about mean turnover has not been addressed. How do I include this in the script?
profile picture

Cone

#20
Turnover means a lot of different things to me. What's the exact formula and how do you want to use it? Be precise.
profile picture

DIY

#21
What I am interested in is the mean turnover in $ of a stock over the last n bars eg 20 d, 15 weeks. Turnover for 1 bar would be the price at cob x volume (which is sells + buys). My interest is to have some idea of the value traded over a period in order to purchase a stock. For example a stock in the top 100 may have a very low turnover therefore it may be hard to get in and out easily or in a hurry.
profile picture

Cone

#22
Well, that's simple enough.

CODE:
Please log in to see this code.
But let's not get too fragmented here. This topic was on Linear Reg Scoring, so if you have other specific questions, please start a new thread. Also, keep in mind Our Support Policy.