| Registered User Joined: 4/21/2008
 Posts: 81
 
 | I'd like to keep a running count of bars during which the stock has been rangebound (e.g. 10% from 20-day high).  I tried something like this but it didn't work.  All I got was zero values.  I think there is a problem with price.MaxHigh(...) because it is not finding the 20-day high one day ago, two days ago, and so on... I think it is bad syntax to do this: MaxHigh(20)(x)?  Please point out what I'm doing wrong.  Thanks. 
 ------------------
 
 Dim count, x As Integer
 Count = 0
 If islastbar Then
 For x = 0 To 20
 If price.Close(x) > 0.9 * price.MaxHigh(20) And _
 price.Close <= Price.MaxHigh(20) Then count = count + 1
 Next
 End If
 | 
	
	| Registered User Joined: 12/31/2005
 Posts: 2,499
 
 | price.maxhigh(20,daysAgo) is the syntax 
 Problems:
 1. You never plotted a value
 2. as wrriten only the last day's value will be plotted
 
 Dim count, x As Integer
 Count = 0
 If islastbar Then
 For x = 0 To 20
 If price.Close(x) > 0.9 * price.MaxHigh(20) And _
 price.Close <= Price.MaxHigh(20) Then count = count + 1
 Next
 End If
 plot = count
 
 For a continuous plot you can keep a running total
 
 static count as integer
 if isfirstbar then
 count = 0
 end if
 '
 ' conditionally increment count
 '
 if price.close > price.maxhigh(20) * 0.09 andalso _
 price.close < price.maxhigh(20) then
 count += 1
 endif
 '
 ' potentially decrement count after 21 days have elapsed
 ' to keep count of last 20 days
 '
 if currentindex >= 21 then
 if price.close(20) > price.maxhigh(20,20) * 0.90 andalso _
 price.close(20) <= price.maxhigh(20,20) then
 count -= 1
 end if
 end if
 plot = count
 
 
 
 
 
 | 
	
	| Registered User Joined: 7/1/2009
 Posts: 97
 
 | I'd like to see what that looks like, jas0501, but when I enter the following I get errors in line 14 (plot not declared), line 17 (local variable 'count' is already declared in the current block), and line 38 (name 'plot' is not declared):
 '*********************************************
 '* if Price.Close > Price.Close(1) then Pass *
 '*********************************************
 Dim count, x As Integer
 Count = 0
 If islastbar Then
 For x = 0 To 20
 If price.Close(x) > 0.9 * price.MaxHigh(20) And _
 price.Close <= Price.MaxHigh(20) Then count = count + 1
 Next
 End If
 plot = count
 Static count As Integer
 If isfirstbar Then
 count = 0
 End If
 '
 ' conditionally increment count
 '
 If price.close > price.maxhigh(20) * 0.09 AndAlso _
 price.close < price.maxhigh(20) Then
 count += 1
 End If
 '
 ' potentially decrement count after 21 days have elapsed
 ' to keep count of last 20 days
 '
 if currentindex >= 21 then
 if price.close(20) > price.maxhigh(20, 20) * 0.90 andalso _
 price.close(20) <= price.maxhigh(20, 20) then
 count -= 1
 end if
 end if
 plot = count
 | 
	
	|  
  Worden Trainer
 
 Joined: 10/7/2004
 Posts: 65,138
 
 | Mark17, There are three issues I can see.
 
 You appear to be using this in a RealCode Rule. The RealCode needs to be used in a RealCode Indicator because that's the only place in StockFinder where the Plot syntax is currently valid.
 
 You also appear to be combining your RealCode with jas0501's suggested RealCode. You need to eliminate everything above the line (keep the following line and everything below it):
 
 Static count As Integer
 
 There also appears to be a mismatch between the code that increments up and the code that increments down. If I alter the increment up portion to match the increment down portion I end up with something like the following (this is the complete RealCode and should replace everything in the Code tab of a RealCode Indicator):
 
 Static count As Integer
 If isfirstbar Then
 count = 0
 End If
 '
 ' conditionally increment count
 '
 If currentindex >= 20 AndAlso _
 price.close > price.maxhigh(20) * .9 Then
 count += 1
 End If
 '
 ' potentially decrement count
 ' to keep count of last 20 days
 '
 If currentindex >= 40 AndAlso _
 price.close(20) > price.maxhigh(20, 20) * .9 Then
 count -= 1
 End If
 
 If currentindex >= 39 Then
 plot = count
 Else
 plot = Single.NaN
 End If
 
 -Bruce
 Personal Criteria Formulas
 TC2000 Support Articles
 |