gjbkdunn |
Gold User, Member, Platinum User, TeleChart
|
Registered User |
|
|
|
|
Unsure |
|
Thursday, March 10, 2005 |
Friday, October 26, 2012 7:49:14 PM |
13 [0.00% of all post / 0.00 posts per day] |
|
In Van Tharp's review of the overall market he uses his "System Quality Number" SQN to measure market performance for various issues.
I'd like to create the SQN plot for stocks but I am not a programmer.
The SQN basically consists of the following formula:
N= the lookback period
SquareRoot N * [Average (daily % change over N periods) / (Std Dev daily % change over N periods) ]
The daily % change would be the close to the previous close.
Can someone create an indicator that I can drop on a price chart and change the lookback period.
Thanks
Greg
|
Bruce,
I've been thinking about this 3 day price spike business and trying to wrap my mind around the logic of how
to do this. Then it dawned on me that I can use the code we already have for the 1 day and just change the chart type to a 3 day chart instead of a 1 day. I checked that out with what I've programmed in Excell.
The numbers don't match up exactly because the excell uses the open three days ago ... but the shape of the curve is very close.
I'm looking for large magnitude moves and once they've reached a certain threshold that is THE signal not if it off by a few decimals. So I will use the code from the previous discussion in Dec 09 and use both 1 day and three day charts as needed.
Hopefully this saves some of your troubleshooting time for others.
Thanks for you help....
|
OK... after hacking around for a while.... I decided to combine the above code with the one you created for the 3 day price spikes.
I came up with the following:
'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Price Spike
'|******************************************************************
'# Period = UserInput.Single = 20
'# Spike = UserInput.Single = 3
Static Sum(1) As Single
Static OneStdDev As Single
If CurrentIndex > (Period + Spike) Then
Plot = (Price.Last - Price.Last(Spike)) / OneStdDev
Else
Plot = Single.NaN
End If
If isFirstBar Then
Sum(0) = 0
Sum(1) = 0
Else If CurrentIndex > Period Then
Sum(0) += System.Math.Log(Price.Last) - System.Math.Log(Price.Last(Spike)) - _
System.Math.Log(Price.Last(Period)) + System.Math.Log(Price.Last(Period + Spike))
Sum(1) += (System.Math.Log(Price.Last) - System.Math.Log(Price.Last(Spike))) ^ 2 - _
(System.Math.Log(Price.Last(Period)) - System.Math.Log(Price.Last(Period + Spike))) ^ 2
OneStdDev = Price.Last * (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5)
Else
Sum(0) += System.Math.Log(Price.Last) - System.Math.Log(Price.Last(Spike))
Sum(1) += (System.Math.Log(Price.Last) - System.Math.Log(Price.Last(Spike))) ^ 2
If CurrentIndex = Period Then
OneStdDev = Price.Last * (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5)
End If
End If
The problem is that this code will not plot out anything. In trying to figure this out I went in and changed
the Spike period. If I bring it down to 1 instead of 3 it plots the exact same as the code without the spikes. I had to change the spike to single instead of integer. As I increased the spike up to 1.5 it had no effect on the plot. Over 1.5 the plot goes blank.
Can you help on correcting the code for the multi day price spikes ???
Thanks.
I found the discussion under "Standard Deviation Price Spike Charts" started 12/8/09
|
Bruce,
I found a similar post in another forum from last December I think... (my first search on Std dev in the forum didn't find it)
Your reply at that time I posted below. I tried that one and it got a closer value for the higher price spike... (The 4/21/10 AAPL price spike is calculated at 7.14 vs Excell @ 6.95.
I'll have to review and make sure it is what I need.
Haven't had a chance to review to see what is differnet.
'# Period = UserInput.Integer = 20
Static Sum(1) As Single
Static OneStdDev As Single
If CurrentIndex > Period Then
Plot = (Price.Last - Price.Last(1)) / OneStdDev
Else
Plot = Single.NaN
End If
If isFirstBar Then
Sum(0) = 0
Sum(1) = 0
Else If CurrentIndex > Period Then
Sum(0) += System.Math.Log(Price.Last) - System.Math.Log(Price.Last(1)) - _
System.Math.Log(Price.Last(Period)) + System.Math.Log(Price.Last(Period + 1))
Sum(1) += (System.Math.Log(Price.Last) - System.Math.Log(Price.Last(1))) ^ 2 - _
(System.Math.Log(Price.Last(Period)) - System.Math.Log(Price.Last(Period + 1))) ^ 2
OneStdDev = Price.Last * (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5)
Else
Sum(0) += System.Math.Log(Price.Last) - System.Math.Log(Price.Last(1))
Sum(1) += (System.Math.Log(Price.Last) - System.Math.Log(Price.Last(1))) ^ 2
If CurrentIndex = Period Then
OneStdDev = Price.Last * (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5)
End If
End If
|
opps.... I have a problem with the way the price spikes are calculated (I had the problem with the spreadsheet too but that was an easy fix). I can't get my mind around the code to figure out how to change it.
I want to calculate the std dev of the 20 price changes immediately preceeding todays price change.
So for 20 days we calc the log normal of those 20 price changes. Then I want to multiply that number by the close on day 21 to determine what the magnitude of 1 std dev is.
Then divide the day 22 price change by that day 21 value of 1 std dev to get the price spike value.
In most calcs there is just a small error with what we've got now. However on days with large price swings we are figureing that large swing into the calcs and the numbers are way off.
I can explain it via spreadsheet terms:
Row 1 Col D has the first days close
Row 2 Col D has the second days close : Col E calcs D2 - D1 : Col F is Ln (D2/D1)
Skip down to
Row 21 where we add Col G = StdDev (F2:F21)
Row 22 we add Col H - G22 * D22
Row 23 we calc todays price spike using yesterdays value for 1 Std dev Col I = E23/H22
performing all the calc on Appl computer with closing prices from 3/19/10 to 4/21/10.
The price spike on 4/21/10 is significant, the price jumps about 14+ points from the previous day. Using the orginal calcs we get the 4/21/10 value of that spike at about 3.95. i.e. a price spike equal to 3.95 std deviations. But if we elimninate the price spike on that date in the std dev calculations and do it the way I've tried to explain here we get a price spike of 6.95 std deviations.
Thanks
|
On the three bar spike..... It appears that what we have is close today minus close 3 days ago.
What I'd like to do is have all the calcs based on the close today minus the OPEN 3 days ago.
Is that just a matter of substituting price.open(spike) in the formulas ?
Thanks
|
Yeah....that needs to be changed as the calcs are based on the new spike which in this case is a 3 day spike.In the spreadsheet :I get the 3 day price change today close - open three days ago. = "A"Then I take the LN of that. = "B"Then the 20 period std dev of that. (In the spreadsheet i don't do the calc on every day only every three days.... so a 20 period std dev is actually taking in about 60 days of data. But i guess a daily running 3 day price spike would work also.) = "C"Then I multiply that result by that day's close. That basically tells me what a 1 std dev price spike equals. ="D"Then calc and plot the price spike in terms of Std devs i.e. back to the "A"/"D"Bottom line what this all does is give you an added signal to enter or exit something.i.e. occasionally on some stocks you can end up with a price spike, either daily or multiple days, whereit exceeds 3 std devs. On those days you can consider either entering on a neg price spike or exiting on a pos price spike. You can't use this as the only entry or exit triggers but it can be used in conjunction with other triggers.Thanks again
|
Excellent. I double checked the graph vs the Excel Spreadsheet I currently use and it what I am looking for.
I'll need to highlight extra large spikes and create a search for those on a daily basis.
Thank - a great help.
|
copy and pasted it and it runs..... like YOU knew it would.
I'll have to double check some results and make sure what I told you is what I want.
I will also add some highlights so when the price spike is above or below 3 std devs I can see
those in another color.
If I choose to also show a three day price spice the formula for A would be
A = Close today - Open three days ago.
How would I code that and insert it on which lines?
Thanks for your help.
|
No you are right ... I mistyped the correction.D = C * close i.e. the std dev times the closeYou are also correct in the fact I had StockFinder4 I'm currently downloading SF5 and its data. Thanks
|
|