Welcome Guest, please sign in to participate in a discussion. | Search | Active Topics | |
Registered User Joined: 11/18/2005 Posts: 29
|
Is there any way to create this visual basic formula for Average True Range Bands in Blocks: w=2.
the UpperATR band is computed by adding a computed width to a simple moving average of the High; the LowerATR band subtracts the width from a simple moving avergate of the low.
The width is computed by taking a running moving average of the the difference between the highs and lows
w = ATRBandWidth(high, low, close, period, width)
upperATR = StdMA(low, period) + w
lowerATR = StdMA(high, period) - w
sub formula - atr band width
---------------------------------------
ATRBandWidth = RunMA(TrueRange(high, low, close), period) * width
True Range
----------------
Dim tr, yclose, d
TrueRange = null
If IsNull(high) Or IsNull(low) Then Exit Function
tr = high - low
If Not IsNull(close) Then
yclose = Delay(close, 1)
If Not IsNull(yclose) Then
d = Abs(high - yclose)
If d > tr Then tr = d
d = Abs(low - yclose)
If d > tr Then tr = d
End If
End If
TrueRange = tr
RunMA
-----------
Dim r, iflag
RunMA = null
If (period < 1) Or IsNull(value) Then Exit Function
If iflag = 0 Then
r = StdMA(value,period)
Else
r = (r * (period - 1) + value) / period
End if
iflag = Not IsNull(r)
RunMA = r
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
RealCode Indicators can currently output only a single Line or Bar. That means you will need to set the HighOrLow setting to High for one Indicator and Low for another Indicator to Plot both Lines when using the following RealCode:
'ATR Band
'# SMAPeriod = UserInput.Integer = 10
'# ATRPeriod = UserInput.Single = 14
'# Width = UserInput.Single = 2
'# HighOrLow = UserInput.String = High
'HighOrLow needs to be either High or Low
Static TR As Single
Static ATR As Single
Static termRatio As Single
Static sumWeight As Single
Static Weight As Single
Static Start As Integer
Static SMA As Single
Static HL(1) As Single
If isFirstBar Then
ATR = 0
termRatio = (ATRPeriod-1)/ATRPeriod
sumWeight = 1
TR = Price.High - Price.Low
Start = System.Math.Max(SMAPeriod-1,ATRPeriod*7)
SMA = 0
Else
TR = System.Math.Max(Price.High,Price.Last(1)) - _
System.Math.Min(Price.Low,Price.Last(1))
End If
Weight = 1 / sumWeight
If HighOrLow = "High" Then
HL(0) = Price.High
HL(1) = Price.High(SMAPeriod)
ATR = ATR * (1 - Weight) + Weight * TR
Else If HighOrLow = "Low" Then
HL(0) = Price.Low
HL(1) = Price.Low(SMAPeriod)
ATR = ATR * (1 - Weight) - Weight * TR
Else
HL(0) = Price.Last
HL(1) = Price.Last(SMAPeriod)
End If
sumWeight = sumWeight * termRatio + 1
SMA += HL(0) / SMAPeriod
If CurrentIndex >= SMAPeriod Then SMA -= HL(1) / SMAPeriod
If CurrentIndex >= Start Then
Plot = SMA + ATR * Width
Else
Plot = Single.NaN
End If
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/18/2005 Posts: 29
|
Bruce,Thank you very much. I think i screwed something up. In the formula the w which I said is equal to 2 in the band calculations:upperATR = StdMA(low, period) + wlowerATR = StdMA(high, period) - w is actually the computed value of the band width: ATRBandWidth = RunMA(TrueRange(high, low, close), period) * widthThis above line is actually multiplied by 2 (or an inputed value) NOT the width as it indicates. The above line should actually be:ATRBandWidth = RunMA(TrueRange(high, low, close), period) * 2 (or a multiplier inputed by the user)I am a little confused as to how to make those changes.
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
I must be misunderstanding something because I'm not sure how that is different from the code you provided earlier or the RealCode Indicator I created in an attempt to duplicate that code.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/18/2005 Posts: 29
|
Hi Bruce,I am sorry, i didn't see your response. What I was trying to explain that in the formula I gave you, The upper band is upperATR = StdMA(low, period) + wbut the w is NOT the user input value (which you have starting at 2)The w there is actually the ATRBandWidth, the formula for which is:ATRBandWidth = RunMA(TrueRange(high, low, close), period) * 2[or the user input value]the lower band is the same, but minus the ATRBandWidth:lowerATR = StdMA(high, period) - ATRBandWidth
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
If I'm understanding correctly, that's exactly what I did. The Plot is not the Simple Moving Average of the High or Low plus or minus the Width but the Simple Moving Average of the High or Low plus or minus the Width times the Average True Range.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/18/2005 Posts: 29
|
Bruce, I am not totally sure if your formula is doing this but the way that I understand it is the routine should first calculate the TrueRange then take the weighted moving average of the TrueRange and multiplies by 2 (the user input value). Then it subtracts it from the simple moving average of the highs(over the period) and adds it to the simple moving average of the lows (over the period).It might be easier if I email you a stock chart from my other program and you can see the difference.In your version, the bands seem to contain all the stock movement and in my other chart, there are times when the price moves above or below the upper and lower bands. It is these movements that I am interested in.How can I email this to you?steve
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
OK. I was basing the RealCode in part on your original description, "the UpperATR band is computed by adding a computed width to a simple moving average of the High; the LowerATR band subtracts the width from a simple moving avergate of the low." You corrected this in your Monday, September 08, 2008 7:44:24 PM ET post, but I focused on the the "w is NOT the user input value" portion. If you need to reverse the addition and subtraction, please try the following RealCode instead:
'ATR Band
'# SMAPeriod = UserInput.Integer = 10
'# ATRPeriod = UserInput.Single = 14
'# Width = UserInput.Single = 2
'# HighOrLow = UserInput.String = High
'HighOrLow needs to be either High or Low
Static TR As Single
Static ATR As Single
Static termRatio As Single
Static sumWeight As Single
Static Weight As Single
Static Start As Integer
Static SMA As Single
Static HL(1) As Single
If isFirstBar Then
ATR = 0
termRatio = (ATRPeriod-1)/ATRPeriod
sumWeight = 1
TR = Price.High - Price.Low
Start = System.Math.Max(SMAPeriod-1,ATRPeriod*7)
SMA = 0
Else
TR = System.Math.Max(Price.High,Price.Last(1)) - _
System.Math.Min(Price.Low,Price.Last(1))
End If
Weight = 1 / sumWeight
If HighOrLow = "High" Then
HL(0) = Price.High
HL(1) = Price.High(SMAPeriod)
ATR = ATR * (1 - Weight) - Weight * TR
Else If HighOrLow = "Low" Then
HL(0) = Price.Low
HL(1) = Price.Low(SMAPeriod)
ATR = ATR * (1 - Weight) + Weight * TR
Else
HL(0) = Price.Last
HL(1) = Price.Last(SMAPeriod)
End If
sumWeight = sumWeight * termRatio + 1
SMA += HL(0) / SMAPeriod
If CurrentIndex >= SMAPeriod Then SMA -= HL(1) / SMAPeriod
If CurrentIndex >= Start Then
Plot = SMA + ATR * Width
Else
Plot = Single.NaN
End If
On another note, just looking at a Plot isn't generally going to be enough for me to duplicate it in RealCode, so sending an image probably wouldn't help any.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Registered User Joined: 11/18/2005 Posts: 29
|
This works great. Thank you very much!
|
|
Worden Trainer
Joined: 10/7/2004 Posts: 65,138
|
You're welcome.
-Bruce Personal Criteria Formulas TC2000 Support Articles
|
|
Guest-1 |