Download software Tutorial videos
Subscription & data-feed pricing Class schedule


New account application Trading resources
Margin rates Stock & option commissions

Attention: Discussion forums are read-only for extended maintenance until further notice.
Welcome Guest, please sign in to participate in a discussion. Search | Active Topics |

Price Spike Rate this Topic:
Previous Topic · Next Topic Watch this topic · Print this topic ·
gjbkdunn
Posted : Friday, October 8, 2010 8:03:41 PM
Registered User
Joined: 3/10/2005
Posts: 13
I would like to chart price spikes that are a function of volatilty.  I don't know how/where to find how to do a log normal function and standard deviation.  Not sure if an array is needed or how to do that.
The calculations I can easily do in excel but I'm brand new to StockFinder and this type of programming.
Right now I download the data from TC2000 to excell but it sure would be nice to have it plotted in StockFinder.
Calcs required are:
A = close today - close yesterday
B= ln close today / close yesterday     ->  ln = log normal
C= 20 day standard deviation of B
D = A * C
E = A / D
What I want to plot is E
Any help would be appreciated.
Thanks
Bruce_L
Posted : Monday, October 11, 2010 12:05:25 PM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm not sure I understand the question, but maybe something like the following RealCode Indicator?

'|******************************************************************
'|*** 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
Static Sum(1) As Single
If isFirstBar Then
    Sum(0) = 0
    Sum(1) = 0
Else If CurrentIndex > Period Then
    Sum(0) += Math.Log(Price.Last / Price.Last(1)) - _
        Math.Log(Price.Last(Period) / Price.Last(Period + 1))
    Sum(1) += Math.Log(Price.Last / Price.Last(1)) ^ 2 - _
        Math.Log(Price.Last(Period) / Price.Last(Period + 1)) ^ 2
Else
    Sum(0) += Math.Log(Price.Last / Price.Last(1))
    Sum(1) += Math.Log(Price.Last / Price.Last(1)) ^ 2
End If
If CurrentIndex >= Period AndAlso Price.Last <> Price.Last(1) Then
    Plot = (Price.Last - Price.Last(1)) / _
        ((Price.Last - Price.Last(1)) * _
        (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5))
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gjbkdunn
Posted : Monday, October 11, 2010 6:48:01 PM
Registered User
Joined: 3/10/2005
Posts: 13
To clarify I want plot daily price spikes that are a function of the 20 day std dev of price change i.e. a measure of volatility.To do that I have 5 things that need to be calculated.I made a mistake in my first post it is the formula for the "D" variable.A = close today minus close yesterdayB= log normal of close today divided by close yesterdayC= then calculate the 20 day standard deviation of the result in BD = A * Close today i.e. this tells me what 1 std dev of the volatility equates to for this closing price. E = A divided by D. i.e. this is the price spike in volatility units. This is the value I want to plot. It can be positive or negative and should range from -3 to +3 basically a 6 std dev range.At some point in the future I may also want to plot the 3 day price spike so I'd start offwith A= close today minus the open three days ago Thanks again.
gjbkdunn
Posted : Monday, October 11, 2010 7:07:09 PM
Registered User
Joined: 3/10/2005
Posts: 13

copied the indicator you posted just to see what it looks like and got an error message saying
Name 'Math' is not declared in lines 12, 13, 14, 15, 17, 18

Thanks

 

Bruce_L
Posted : Tuesday, October 12, 2010 9:50:01 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
QUOTE (gjbkdunn)
copied the indicator you posted just to see what it looks like and got an error message saying
Name 'Math' is not declared in lines 12, 13, 14, 15, 17, 18

It reads like you might be using StockFinder 4 instead of the current version of StockFinder (SF5). A similar indicator for SF4 could be written as:

'# Period = UserInput.Single = 20
Static Sum(1) As Single
If isFirstBar Then
    Sum(0) = 0
    Sum(1) = 0
Else If CurrentIndex > Period Then
    Sum(0) += System.Math.Log(Price.Last / Price.Last(1)) - _
        System.Math.Log(Price.Last(Period) / Price.Last(Period + 1))
    Sum(1) += System.Math.Log(Price.Last / Price.Last(1)) ^ 2 - _
        System.Math.Log(Price.Last(Period) / Price.Last(Period + 1)) ^ 2
Else
    Sum(0) += System.Math.Log(Price.Last / Price.Last(1))
    Sum(1) += System.Math.Log(Price.Last / Price.Last(1)) ^ 2
End If
If CurrentIndex >= Period AndAlso Price.Last <> Price.Last(1) Then
    Plot = (Price.Last - Price.Last(1)) / _
        ((Price.Last - Price.Last(1)) * _
        (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5))
Else
    Plot = Single.NaN
End If

QUOTE (gjbkdunn)
I made a mistake in my first post it is the formula for the "D" variable.
...
D = A * Close today i.e. this tells me what 1 std dev of the volatility equates to for this closing price.

Are you sure? I had my doubts about the original description of what you wanted (So far everything described has A in both the numerator and denominator of the Plot. A could actually be cancelled out, but I've left it in), but if D is A times the Close instead of A times C, then B and C aren't used in the calculation of E at all. Anyway, here it is:

'|******************************************************************
'|*** StockFinder RealCode Indicator - Version 5.0 www.worden.com
'|*** Copy and paste this header and code into StockFinder *********
'|*** Indicator:Price Spike
'|******************************************************************
If CurrentIndex > 0 AndAlso Price.Last <> Price.Last(1) Then
    Plot = (Price.Last - Price.Last(1)) / _
        ((Price.Last - Price.Last(1)) * Price.Last)
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gjbkdunn
Posted : Tuesday, October 12, 2010 10:23:45 AM
Registered User
Joined: 3/10/2005
Posts: 13
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
Bruce_L
Posted : Tuesday, October 12, 2010 10:38:12 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
That makes a lot more sense to me. Please try the following instead:

'|******************************************************************
'|*** 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
Static Sum(1) As Single
If isFirstBar Then
    Sum(0) = 0
    Sum(1) = 0
Else If CurrentIndex > Period Then
    Sum(0) += Math.Log(Price.Last / Price.Last(1)) - _
        Math.Log(Price.Last(Period) / Price.Last(Period + 1))
    Sum(1) += Math.Log(Price.Last / Price.Last(1)) ^ 2 - _
        Math.Log(Price.Last(Period) / Price.Last(Period + 1)) ^ 2
Else
    Sum(0) += Math.Log(Price.Last / Price.Last(1))
    Sum(1) += Math.Log(Price.Last / Price.Last(1)) ^ 2
End If
If CurrentIndex >= Period  Then
    Plot = (Price.Last - Price.Last(1)) / (Price.Last * _
        (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5))
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gjbkdunn
Posted : Tuesday, October 12, 2010 10:53:11 AM
Registered User
Joined: 3/10/2005
Posts: 13
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.
gjbkdunn
Posted : Tuesday, October 12, 2010 11:02:57 AM
Registered User
Joined: 3/10/2005
Posts: 13

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.

Bruce_L
Posted : Tuesday, October 12, 2010 11:14:16 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
Do you also want to change B to reflect ln close today / close three days ago or do you want to leave it as is?

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gjbkdunn
Posted : Tuesday, October 12, 2010 11:40:37 AM
Registered User
Joined: 3/10/2005
Posts: 13
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
Bruce_L
Posted : Tuesday, October 12, 2010 11:50:21 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
The following uses the three Bar spike every Bar to calculate the Standard Deviation as calculating the three Bar spike every three Bars would seem to require a far less efficient algorithm.

'|******************************************************************
'|*** 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.Integer = 3
Static Sum(1) As Single
If isFirstBar Then
    Sum(0) = 0
    Sum(1) = 0
Else If CurrentIndex >= Period + Spike Then
    Sum(0) += Math.Log(Price.Last / Price.Last(Spike)) - _
        Math.Log(Price.Last(Period) / Price.Last(Period + Spike))
    Sum(1) += Math.Log(Price.Last / Price.Last(Spike)) ^ 2 - _
        Math.Log(Price.Last(Period) / Price.Last(Period + Spike)) ^ 2
Else If CurrentIndex >= Spike
    Sum(0) += Math.Log(Price.Last / Price.Last(Spike))
    Sum(1) += Math.Log(Price.Last / Price.Last(Spike)) ^ 2
End If
If CurrentIndex >= Period + Spike - 1  Then
    Plot = (Price.Last - Price.Last(Spike)) / (Price.Last * _
        (((Sum(1) - Sum(0) ^ 2 / Period) / Period) ^ .5))
Else
    Plot = Single.NaN
End If

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
gjbkdunn
Posted : Thursday, October 14, 2010 9:44:21 AM
Registered User
Joined: 3/10/2005
Posts: 13
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
gjbkdunn
Posted : Thursday, October 14, 2010 3:30:29 PM
Registered User
Joined: 3/10/2005
Posts: 13
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

gjbkdunn
Posted : Thursday, October 14, 2010 8:09:51 PM
Registered User
Joined: 3/10/2005
Posts: 13
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
gjbkdunn
Posted : Thursday, October 14, 2010 9:02:42 PM
Registered User
Joined: 3/10/2005
Posts: 13
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

gjbkdunn
Posted : Friday, October 15, 2010 9:55:54 AM
Registered User
Joined: 3/10/2005
Posts: 13
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....
Bruce_L
Posted : Monday, October 25, 2010 9:34:39 AM


Worden Trainer

Joined: 10/7/2004
Posts: 65,138
I'm happy to read you were able to figure out a solution on your own.

-Bruce
Personal Criteria Formulas
TC2000 Support Articles
Users browsing this topic
Guest-1

Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.