Thank you guys. Could you tell me if the scanner code also runs bar by bar like the indicators on the chart? If that's the case, then a scanner to check if current price is greater than 30 day moving average would be expensive as the scanner loads the last year's daily chart and runs bar by bar for the last 250 days, only to compare the current price with the last 30 day moving average. In that case, can I add an if block to say, only run the scanner if this bar is at most 30 bars from the most recent bar?
i don't scan, so this info is from what i have read, not experimented with.
yes, code can be added to limit the data used by formulas, but not needed.
the scanner can read from all the bars, but the output comes from the last bar.
if i am going to make a scan or watchlist study, i start with a lower chart study. that way i can see and verify the math is correct (by adding bubbles and labels and lines).
here is a lower study, that should work in a scanner.
it will be true when the current bar (last bar) close is > an average.
default is simple average
set scan time period to day
Code:
#declare lower;
input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 30;
def avg = MovingAverage(avg1_type, avg1_price, avg1_len);
plot z = close > avg;
---------------------------
watchlist (column) study
copy the lower study and change it to be a column study
it displays above or below , green or red cell color.
i add a z to the beginning of my custom column study names, so they sort together at the end of the list.
zaboveavg
http://tos.mx/6YGaBmy
Code:
# zaboveavg
# watchlist , day
input avg1_type = AverageType.SIMPLE;
input avg1_price = close;
input avg1_len = 30;
def avg = MovingAverage(avg1_type, avg1_price, avg1_len);
def z = close > avg;
addlabel(1, (if z then "above" else "below"), color.black);
assignbackgroundcolor(if z then color.green else color.red);
#
--------------------
=====================
=====================
not related, but topics that might be of interest,
------------
click on post number to get a url to that post
------------
think of all variables as a 1 dimensional array.
but variable addressing is relative, not absolute.
you can't address a specific variable, with an absolute index.
if the 10th bar on the chart is the current bar, and this is run,
def b = average(close, 10);
def a = b[1];
it won't read b at location 1. it reads b from 1 bar ago, the previous bar.
positive offsets read data in the past.
negative offsets read data from a future bar.
def c = b[-1];
https://tlc.thinkorswim.com/center/...nced/Chapter-10---Referencing-Historical-Data
https://tlc.thinkorswim.com/center/...dvanced/Chapter-12---Past-Offset-and-Prefetch
------------
sorting/ranking data.
this thread has several examples
sorting data from the last x bars
https://usethinkscript.com/threads/rank-volume-1-10.9504/#post-108403
------------
read the aggregation time of a chart ( or column,.. )
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
addlabel(1, "min " + chartmin, color.magenta);
------------
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AverageType
..AverageTypes..
EXPONENTIAL
HULL
SIMPLE
WEIGHTED
WILDERS