Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce
This is my Volatility Stop that you wrote for Blocks 3.0 that I saved or transferred to SF when SF was introduced.
I am assuming something needs to be adjusted because I can't plot it on Cumulative Custom Indexes.
For example I created Average of Advance and Decline Volume and plotted their running total as a cumulative indicator but when try to plot the Volatility stop on that it doesn't work.
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
'#Cumulative
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
TR = Price.High - Price.Low
termRatio = (period - 1) / period
ATR = TR
sumweight = termratio + 1
weight = 1 / sumweight
If Price.Last(-1) - System.Math.Min(Price.Low,Price.Low(-1)) < _
System.Math.Max(Price.High,Price.High(-1)) - Price.Last(-1) Then
extreme = Price.Last
tstop = extreme + factor * TR
state = False
Else
extreme = Price.Last
tstop = extreme - factor * TR
state = True
End If
Plot = tstop
Else
TR = (Price.High-Price.Low+ _
System.Math.Abs(Price.High-Price.Last(1))+ _
System.Math.Abs(Price.Last(1)-Price.Low))/2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state
If Price.Last < tstop
state = False
extreme = Price.Last
tstop = extreme + factor * ATR
End If
Else
If Price.Last > tstop
state = True
extreme = Price.Last
tstop = extreme - factor * ATR
End If
End If
Plot = tstop
If state
extreme = System.Math.Max(extreme,Price.Last)
tstop = extreme - factor * ATR
Else
extreme = System.Math.Min(extreme,Price.Last)
tstop = extreme + factor * ATR
End If
End If
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
It doesn't work on Cumulative Average of Advance and Decline Volume because it is based on Price.
If you want to use it on something else, you need to Drag and Drop the Indicator into the RealCode Editor to create something like the first line of the following RealCode Indicator and then change the Price references to match the variable named assigned to the Dragged and Dropped Indicator:
'# MI = indicator.MyIndicator
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
'# Cumulative
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
TR = MI.High - MI.Low
termRatio = (period - 1) / period
ATR = TR
sumweight = termratio + 1
weight = 1 / sumweight
If MI.Last(-1) - System.Math.Min(MI.Low, MI.Low(-1)) < _
System.Math.Max(MI.High, MI.High(-1)) - MI.Last(-1) Then
extreme = MI.Last
tstop = extreme + factor * TR
state = False
Else
extreme = MI.Last
tstop = extreme - factor * TR
state = True
End If
Plot = tstop
Else
TR = (MI.High - MI.Low + _
System.Math.Abs(MI.High - MI.Last(1)) + _
System.Math.Abs(MI.Last(1) - MI.Low)) / 2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state
If MI.Last < tstop
state = False
extreme = MI.Last
tstop = extreme + factor * ATR
End If
Else
If MI.Last > tstop
state = True
extreme = MI.Last
tstop = extreme - factor * ATR
End If
End If
Plot = tstop
If state
extreme = System.Math.Max(extreme, MI.Last)
tstop = extreme - factor * ATR
Else
extreme = System.Math.Min(extreme, MI.Last)
tstop = extreme + factor * ATR
End If
End If
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce is the Volatility Stop above looking at the ATR as just HIgh - Low or is it the greatest value of
1. High minus low
2. The absolute value of the present high minus the previous close
3. The absolute value of the present low minus the previous close
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The latter.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Ok thanks
There is one issue with this volatility Stop and let see if we can do anything to solve it.
Whats good about this is that as the stock becomes more volatile the stop backs away giving the stock more room to move with out creating whipsaws but at the same time sometimes it backs away too much.
look at MANT in SEP, OCT of 2008 with a 14 and 4 volatility stop and you will see what I am talking about. I like the fact that it backs away but sometimes it backs away too much.
What I am trying to do is to limit how much it can back away from the highest level it reached since price closed above it.
In this case on 7/22 price closed above it and the Volatility stop was placed at 47.13 and the highest level it reached was 53.77 on 8/29 is it possible that we can add in that it can not back away more than 1 ATR from the highest level reached since price closed above and opposite for the shorts?
So since 53.77 is the highest and if you look at MANT's ATR 14 the highest value it had was little over 4 points therefore the lowest value the Volatility stop can drop to was 53.77 - 4.08 = 49.69 rather than 45.47 that it dropped to.
thanks
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Realize that the only retreat is the result of an expanding ATR, so there are limits to what this particular technique of retreat moderation will accomplish.
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
'# retreat = UserInput.Single = 1
'# Cumulative
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme(1) As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
TR = Price.High - Price.Low
termRatio = (period - 1) / period
ATR = TR
sumweight = termratio + 1
weight = 1 / sumweight
If Price.Last(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
System.Math.Max(Price.High, Price.High(-1)) - Price.Last(-1) Then
extreme(0) = Price.Last
tstop = extreme(0) + factor * TR
extreme(1) = tstop
state = False
Else
extreme(0) = Price.Last
tstop = extreme(0) - factor * TR
extreme(1) = tstop
state = True
End If
Plot = tstop
Else
TR = (Price.High - Price.Low + _
System.Math.Abs(Price.High - Price.Last(1)) + _
System.Math.Abs(Price.Last(1) - Price.Low)) / 2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state
If Price.Last < tstop
state = False
extreme(0) = Price.Last
tstop = extreme(0) + factor * ATR
extreme(1) = tstop
End If
Else
If Price.Last > tstop
state = True
extreme(0) = Price.Last
tstop = extreme(0) - factor * ATR
extreme(1) = tstop
End If
End If
Plot = tstop
If state
extreme(0) = System.Math.Max(extreme(0), Price.Last)
tstop = extreme(0) - factor * ATR
extreme(1) = System.Math.Max(extreme(1), tstop)
tstop = System.Math.Max(tstop, extreme(1) - retreat * ATR)
Else
extreme(0) = System.Math.Min(extreme(0), Price.Last)
tstop = extreme(0) + factor * ATR
extreme(1) = System.Math.Min(extreme(1), tstop)
tstop = System.Math.Min(tstop, extreme(1) + retreat * ATR)
End If
End If
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Wow you are absolutely amazing looks pretty good
Thank you
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce what do I need to do on the code you wrote on Tuesday, May 19, 2009 4:59:34 PM so that I can plot it on an indicator?
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
I think I need to drag and drop the indicator in the real code and change its name to something like MI and change any where you have Price in the real code to MI?
Let me know if I am correct please
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Yeah, that's pretty much what you need to do:
'# MI = indicator.MyIndicator
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
'# retreat = UserInput.Single = 1
'# Cumulative
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme(1) As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
TR = MI.High - MI.Low
termRatio = (period - 1) / period
ATR = TR
sumweight = termratio + 1
weight = 1 / sumweight
If MI.Last(-1) - System.Math.Min(MI.Low, MI.Low(-1)) < _
System.Math.Max(MI.High, MI.High(-1)) - MI.Last(-1) Then
extreme(0) = MI.Last
tstop = extreme(0) + factor * TR
extreme(1) = tstop
state = False
Else
extreme(0) = MI.Last
tstop = extreme(0) - factor * TR
extreme(1) = tstop
state = True
End If
Plot = tstop
Else
TR = (MI.High - MI.Low + _
System.Math.Abs(MI.High - MI.Last(1)) + _
System.Math.Abs(MI.Last(1) - MI.Low)) / 2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state
If MI.Last < tstop
state = False
extreme(0) = MI.Last
tstop = extreme(0) + factor * ATR
extreme(1) = tstop
End If
Else
If MI.Last > tstop
state = True
extreme(0) = MI.Last
tstop = extreme(0) - factor * ATR
extreme(1) = tstop
End If
End If
Plot = tstop
If state
extreme(0) = System.Math.Max(extreme(0), MI.Last)
tstop = extreme(0) - factor * ATR
extreme(1) = System.Math.Max(extreme(1), tstop)
tstop = System.Math.Max(tstop, extreme(1) - retreat * ATR)
Else
extreme(0) = System.Math.Min(extreme(0), MI.Last)
tstop = extreme(0) + factor * ATR
extreme(1) = System.Math.Min(extreme(1), tstop)
tstop = System.Math.Min(tstop, extreme(1) + retreat * ATR)
End If
End If
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Thanks
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce,
I need to offset my Volayility Stop by 1 Bar so I went to the Block diagram and put a Bar Offset Block right before the numeric plot but that will not save. If I close and reopen the layout it goes back to normal again.
I am assuming that is because the indicator is in real code and I have no idea how to adjust it so that I can offset it by a bar.
Let me know please
Thanks
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The easy way would be to add a 1-Period Simple Moving Average as a Child Indicator with the Offset set to 1 or edit the Moving Average Indicator so it is just an Offset.
Another option that would not involve particularly complicated modifications to the RealCode would be to replace Plot = tstop line in the If isFirst Bar section of the RealCode with Plot = Single.NaN and move the other Plot = tstop line from outside the If Then Else structure to right after the Else that is all the way to the left (not indented) in the same If Then Else structure.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Moving average doesn't work it doesn't look right on the chart.
How come I can't save the block diagram?
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
QUOTE (thnkbigr) Moving average doesn't work it doesn't look right on the chart.
I'm finding this assertion quite odd. When you add a 1-Period Simple Moving Average as a Child Indicator to an Indicator it should overlap that Indicator exactly (and does so on my computer when applied to your the RealCode Indicators given above). Changing the Offset from 0 to 1 just shifts the line 1-Bar to the right (since it is displaying the previous Value). I have no idea why it would not behave the same way for you as this would seem to indicate that the Moving Average is not being calculated or scaled correctly.
QUOTE (thnkbigr) How come I can't save the block diagram?
Because the Block Diagram for a RealCode Indicator is dynamically generated each time it is compiled (such as when you select Apply or initially load a saved version of the Indicator - be it on its own or as part of a Chart or Layout). This means any edits you make to a dynamically generated Block Diagram will disappear when StockFinder is restarted (as this re-loads and re-compiles the Indicator).
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
I can swear I did this 3 times yesterday and it didn't look right and it didn't make sense to me either and then I tried to put a Bar offset in the block diagram. No idea what happened.
Thanks
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Does adding a 1-Period Moving Average and adjusting the Offset work now?
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Yea
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Good. I just wanted to make sure it was still working incorrectly for you.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 8/13/2009 Posts: 4
|
Hi
How can I define the Volatility Stop formula, posted Tuesday, May 19, 2009 4:59:34 PM to be a child plot.
I tried adding it as a new indicator, saving it, then it appears in a new pane, I drag it to price pane and there it does all sorts of unwanted things, since it seems to use its own scaling. It should use price scaling like a MA. It bounces up and down independently when moving the chart back in time.
Cheers
Tim
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
ralfrichter4,
It's only going to be on the same scale as Price History if you tell it. Right-click on the Custom Indicator and select Scaling | Scale With | Price History.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 8/13/2009 Posts: 4
|
Thank you
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce,
Here is a quick 2 min video on the issue with the Volatility stop being plotted on an indicator.
The RealCode is from your post above on Monday, July 13, 2009 12:55:03 PM
let mw know
thx
http://www.screencast.com/t/YzM5NzdmNT
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
It's not a bug. AutoLoop will only start once all of the data being input is available and it does so because it is the right thing to do. The Price for the Active Symbol is always being input into a RealCode Indicator.
You would need to use manual looping to get around this. Replacing everything after the Inherits line in the Class tab of the RealCode Editor should duplicate the AutoLoop based Indicator from my Monday, July 13, 2009 12:55:03 PM ET post using manual looping instead (the indicator.MyIndicator reference was change to chart.MyIndicator to be compatible with SF5).
Sub New
AutoLoop = False
'# MI = chart.MyIndicator
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
'# retreat = UserInput.Single = 1
'# Cumulative
End Sub
Public Overrides Function Plot() As System.Single
Dim TR As Single = MI.Bar.HighValue(0) - MI.Bar.LowValue(0)
Dim ATR As Single = TR
Dim termRatio As Single = (period - 1) / period
Dim sumWeight As Single = termratio + 1
Dim weight As Single = 1 / sumWeight
Dim extreme(1) As Single
Dim tstop As Single
Dim state As Boolean
If MI.Bar.Value(1) - Math.Min(MI.Bar.LowValue(0), MI.Bar.LowValue(1)) < _
Math.Max(MI.Bar.HighValue(0), MI.Bar.HighValue(1)) - MI.Bar.Value(1) Then
extreme(0) = MI.Bar.Value(0)
tstop = extreme(0) + factor * ATR
extreme(1) = tstop
state = False
Else
extreme(0) = MI.Bar.Value(0)
tstop = extreme(0) - factor * ATR
extreme(1) = tstop
state = True
End If
AddToOutput(MI.Bar.DateValue(0), tstop)
For i As Integer = 1 To MI.Bar.Count - 1
TR = (MI.Bar.HighValue(i) - MI.Bar.LowValue(i) + _
Math.Abs(MI.Bar.HighValue(i) - MI.Bar.Value(i - 1)) + _
Math.Abs(MI.Bar.Value(i - 1) - MI.Bar.LowValue(i))) / 2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state = True Then
If MI.Bar.Value(i) < tstop
state = False
extreme(0) = MI.Bar.Value(i)
tstop = extreme(0) + factor * ATR
extreme(1) = tstop
End If
Else
If MI.Bar.Value(i) > tstop
state = True
extreme(0) = MI.Bar.Value(i)
tstop = extreme(0) - factor * ATR
extreme(1) = tstop
End If
End If
AddToOutput(MI.Bar.DateValue(i), tstop)
If state = True Then
extreme(0) = Math.Max(extreme(0), MI.Bar.Value(i))
tstop = extreme(0) - factor * ATR
extreme(1) = Math.Max(extreme(1), tstop)
tstop = Math.Max(tstop, extreme(1) - retreat * ATR)
Else
extreme(0) = Math.Min(extreme(0), MI.Bar.Value(i))
tstop = extreme(0) + factor * ATR
extreme(1) = Math.Min(extreme(1), tstop)
tstop = Math.Min(tstop, extreme(1) + retreat * ATR)
End If
Next
End Function
End Class
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce,
I get math is not declared
Also I am in V4 i don't know if that makes a difference
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Yes, it makes a difference. Add System. to the front of every instance of Math.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
thx
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce,
In your Friday, July 17, 2009 11:04:45 AM post in order to offset volatility stop you gave 2 options one was to plot a MA with a 1 bar offset and the 2nd was
Another option that would not involve particularly complicated modifications to the RealCode would be to replace Plot = tstop line in the If isFirst Bar section of the RealCode with Plot = Single.NaN and move the other Plot = tstop line from outside the If Then Else structure to right after the Else that is all the way to the left (not indented) in the same If Then Else structure.
In one of my layouts I have the volatility stop dragged into so many other realcodes so if I plot a MA I have to one by one go to every other RC and drag the MA of Volatility Stop rather than volatility stop.
Is this what you mean by the above
'# period = UserInput.Single = 14
'# factor = UserInput.Single = 3
'# retreat = UserInput.Single = 1
'# Cumulative
Static TR As Single
Static ATR As Single
Static termRatio As Single
static Weight As Single
Static sumWeight As Single
Static extreme(1) As Single
Static tstop As Single
Static state As Boolean
If isFirstBar Then
TR = Price.High - Price.Low
termRatio = (period - 1) / period
ATR = TR
sumweight = termratio + 1
weight = 1 / sumweight
If Price.Last(-1) - System.Math.Min(Price.Low, Price.Low(-1)) < _
System.Math.Max(Price.High, Price.High(-1)) - Price.Last(-1) Then
extreme(0) = Price.Last
tstop = extreme(0) + factor * TR
extreme(1) = tstop
state = False
Else
extreme(0) = Price.Last
tstop = extreme(0) - factor * TR
extreme(1) = tstop
state = True
End If
Plot = tstop Change to Single.NaN
Else
Here
TR = (Price.High - Price.Low + _
System.Math.Abs(Price.High - Price.Last(1)) + _
System.Math.Abs(Price.Last(1) - Price.Low)) / 2
ATR = ATR * (1 - weight) + weight * TR
sumweight = sumweight * termratio + 1
weight = 1 / sumweight
If state
If Price.Last < tstop
state = False
extreme(0) = Price.Last
tstop = extreme(0) + factor * ATR
extreme(1) = tstop
End If
Else
If Price.Last > tstop
state = True
extreme(0) = Price.Last
tstop = extreme(0) - factor * ATR
extreme(1) = tstop
End If
End If
Plot = tstop move to
If state
extreme(0) = System.Math.Max(extreme(0), Price.Last)
tstop = extreme(0) - factor * ATR
extreme(1) = System.Math.Max(extreme(1), tstop)
tstop = System.Math.Max(tstop, extreme(1) - retreat * ATR)
Else
extreme(0) = System.Math.Min(extreme(0), Price.Last)
tstop = extreme(0) + factor * ATR
extreme(1) = System.Math.Min(extreme(1), tstop)
tstop = System.Math.Min(tstop, extreme(1) + retreat * ATR)
End If
End If
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
Yes, that is what I mean. That way it does the Plot based on the calculations from the last Bar before doing the calculations for the new Bar.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Platinum Customer
Joined: 3/31/2006 Posts: 3,207
|
Bruce
In Volatility Stop are we plotting the stop at 3 * ATR below the Highest High or the Highest Close that the stock has reached since last cross?
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
The versions in this particular topic appear to use the extremes of the Close.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Guest-1 |