Volatility BO indicator into Scan

MBF

Active member
2019 Donor
Hi guys, I am currently testing out a study we created. I am trying to turn it into a scan but having an issue with an input. First, is it possible to create a scan that references 5-minute data and also daily data from today and yesterday? As an indicator, it works. Here is the study as is.

Code:
``````def aggD = AggregationPeriod.DAY;
def agg5 = AggregationPeriod.FIVE_MIN;
def CloseFive = close(period = agg5);
def OpenD = open(period = aggD);
def HighD = high(period = aggD);
def LowD = low(period = aggD);
def pRange = HighD[1] - LowD[1];
plot condition1 = CloseFive[0] > OpenD[0] + (.9 * pRange);``````

@MBF I believe aggregation periods dont work well in scans. They have to be chosen from the dropdown tab on the scan editor. Also, shouldnt the current close of the price be the same across all timeframes? So you could simplify the scan to this -
Code:
``````def pRange = high[1] - low[1];
plot condition1 = close > open + (.9*pRange);``````

and then just choose a daily aggregation from the scan timeframe choices.

MBF
Thank you @Pensar however,
The current close of the 5 minute bar is different from the close of the prior day, hence the need for two timeframe data in this scan.

[1:59 PM]
How is def pRange = high[1] - low[1] as you have written referenced to the prior day high/low? That is the issue I am struggling with. As written it seems to reference the high/low of the last 5 minute bar.

@MBF You are correct that the current close of the 5 min bar is different from yesterday's close. In the code provided, the 5 min aggregation is only used to find the current close. The reason why I simply used "close" with no hard-coded aggregation is because the current close is the same across all timeframes, no matter if it is 1 min, 10 min, daily, etc. So, when the daily timeframe is selected in the scanner, "close" is the same as the 5 min "close". As a result, there is no need to specify that the current close be from the five minute aggregation.

How is def pRange = high[1] - low[1] as you have written referenced to the prior day high/low? That is the issue I am struggling with. As written it seems to reference the high/low of the last 5 minute bar.

This references the prior high and low by using "[1]" after each, which selects the value of 1 bar back. So the current price bar is "close" (this also can be written as "close[0]" ), the previous price bar is "close[1]", and so on back in time.

The key here is which time aggregation you select. Choosing the daily timeframe in the scanner will give you the daily "low[1]" and "high[1]" values. If you choose a different aggregation such as 1 min, 5 min, or weekly, it will scan using "close[1]" from the previous 1 min, 5 min, or weekly bars.

Here's what is happening:
def pRange = high[1] - low[1];

Using a user-selected daily aggregation, this code snippet takes the previous daily bar's high and low for its calculations

plot condition1 = close > open + (.9*pRange);

Again using a daily aggregation, this snippet uses the current close of the daily price (which is equal to the current close of the 5 min bar) and
the day's open price.

Edit: Hopefully this is explained clearly, but if it isnt, I'll try to change it to make it clearer.

Last edited:
MBF
@Pensar Very nice explanation. I will go try this out and see what happens. Thank you so much for taking a look and replying. I will return with an update. . Indeed.

@MBF This compares the current 5 min close and the current daily close, just for fun. It may be easier to see than explain.
Added a color change. This was interesting to see. It actually has momentary blips when they differ.

Code:
``````#compares the current 5 min and current daily_bar_close aggregation closes
#will color labels white if the same, and red if different

def five_min_bar_close = close(period = aggregationperiod.five_min);
def daily_bar_close = close(period = aggregationperiod.day);

addlabel(1, "current 5 min close = " + five_min_bar_close, if five_min_bar_close == daily_bar_close then color.white else color.red);
addlabel(1,"current daily close = " + daily_bar_close, if five_min_bar_close == daily_bar_close then color.white else color.red);``````

Last edited:
MBF
@Pensar Nice!!! I tried again on this script. Would something like this work? It seems so simple for asking a lot. Something isnt working. Im going to try the new one too! Ive been working on TS code too

so maybe I am confused.

Code:
``````def agg5 = AggregationPeriod.FIVE_MIN;
def CloseFive = close(period = agg5);
def pRange = High[1] - Low[1];
plot condition1 = CloseFive > Open + (.9 * pRange);``````

@Pensar Ah I missed something. Let me go back and check something.

I forgot to try the initial code you sent was the problem. Maybe should not be multi tasking while trading. Yah. Anyway, it worked! Thank you so much for your help. I will give it a whirl tomorrow. Cheers to you and yours mate!

Was just about to post this. These are simple troubleshooting codes I made for explaining, if you find some time.

Put these two lower studies on your chart, one below the other -
Code:
``````#This is the simplified scan code, with open, high, and low set to daily agg
#there is no specified agg setting for close

declare lower;

def o = open(period = aggregationperiod.day);
def h = high(period = aggregationperiod.day);
def l = low(period = aggregationperiod.day);

def pRange = h[1] - l[1];

plot condition1 = if close > o + (.9*pRange) then 1 else 0;``````
Code:
``````# original provided code that uses a daily agg for open, high, and low,
# and uses the five min agg for the close

declare lower;

def aggD = AggregationPeriod.DAY;
def agg5 = AggregationPeriod.FIVE_MIN;
def CloseFive = close(period = agg5);
def OpenD = open(period = aggD);
def HighD = high(period = aggD);
def LowD = low(period = aggD);
def pRange = HighD[1] - LowD[1];

plot condition1 = if CloseFive > OpenD + (.9 * pRange) then 1 else 0;``````

Run the scan code in post #2 against the SPY500, I received 34 results. When you go through the results of the scan on the chart, both troubleshooting studies should plot identically.
This should show that setting the current close to a 5 minute aggregation in the scanner is not needed. Using just "close" with the aggregation of the scanner set to daily should work fine.

MBF
@Pensar That was really handy. Wow, you know your stuff huh? I think I have a hold on this. I will know soon enough. Thank you again for your expertise, advice, and help. Great stuff. Have a wonderful weekend!

You're welcome, @MBF, and the same to you! I'm not an expert, though. There are many things in the thinkScript coding language that I havent yet learned.

MBF
@Pensar, is there a way to add color blocks to this for the watchlist instead of 1 and 0? If so, can you show me the formula for it? I have other columns I'd love to add it to.

def o = open(period = aggregationperiod.day);
def h = high(period = aggregationperiod.day);
def l = low(period = aggregationperiod.day);

def pRange = h[1] - l[1];

plot condition1 = if close > o + (.9*pRange) then 1 else 0;

@MBF Sure. AssignBackgroundColor() will give you a colored background, and AddLabel() plots whatever text you want the column to show. A daily aggregation also needs to be chosen.

Code:
``````#Choose a daily aggregation for the watchlist
#Change the quoted text in the label, as well as any colors, to whatever you like

def pRange = high[1] - low[1];
def condition1 = close > open + (.9*pRange);

assignbackgroundcolor(if condition1 then color.green else color.black);``````

MBF
@Pensar Thank you again! Saved it in my notebook. Appreciate ALL of your help. Outstanding!

Not the exact question you're looking for?

87k+ Posts
420 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?