Troubleshooting scanner code

nitrotrader

New member
Hello, I'm new to thinkscript and trying to build out a watchlist scanner with visual indicators for 2 criteria...(1) above/between/below the prior days range, and also (2) if price is above/below an hourly EMA. Here is a screenshot of what I'm attempting to visualize but running into issues:

vGgZ6N1.jpg



1. Previous Day Range ("PDR")

The code I'm using works as expected, but only during the pre-market session. Once the regular session starts all the values change (I'm sure it has to do with the -1 in the displace input). I'd like this to work so that regardless of premarket/regular session/after hours, it correctly colors.

Here's the code I mismashed together:

Code:
input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = yes;
def PrevDayHigh = Highest(high(period = aggregationPeriod)[-displace], length) ;
def PrevDayLow = Highest(low(period = aggregationPeriod)[-displace], length); ;
def PrevDayClose = Highest(close(period = aggregationPeriod)[-displace], length);

plot pdr = PrevDayHigh;

assignBackgroundColor(if bid > prevDayHigh then Color.GREEN else if bid < prevDayLow then Color.RED else Color.GRAY);
pdr.assignValueColor (if bid > prevDayHigh then Color.GREEN else if bid < prevDayLow then Color.LIGHT_RED else Color.GRAY);

2. EMA

Similar to range plot above, I'd like to display green/red if the price is above/below the EMA (hourly, length of 80, on close)

My code isn't even close to working so I'll spare you the headache of troubleshoot my stuff (although I'm sure I'm robbing you of a good laugh). I'm hoping the calculation of above/below an EMA is simple enough to quickly code that out.

Thanks in advance!
 
For PDR, try this code:

Code:
def prevDayHigh = high[1];
def prevDayLow = low[1];

plot static = 1;

assignBackgroundColor(if bid > prevDayHigh then Color.GREEN else if bid < prevDayLow then Color.RED else Color.GRAY);
static.assignValueColor (if bid > prevDayHigh then Color.GREEN else if bid < prevDayLow then Color.LIGHT_RED else Color.GRAY);

Make sure the aggregation period of your watchlist is set to D (daily).

8KoV8vH.png
 
Thanks @BenTen, this looks like it works for Previous Day Range. Also, I didn't realize you could/need to define the Aggregation period for the watchlist so thanks for bringing that up.

Now...for the second half of the battle of the EMA. And I assume that if since I set the Aggregation period to Daily, that an hourly EMA would have to be a derivative of a Daily time frame?
 
@nitrotrader You set the Aggregation period to whichever timeframe you want the data to be coming from. For your case, you want to put it to 1HR since you want the EMA to be from the Hourly chart.
 
So the scanner worked the first day, but for some reason it's not working today. It's incorrectly showing if the price in above/inside/below the previous days range. Here's a few examples (and its wrong for 90% of the tickers). I added other items to the watchlist scanner, could there be a conflict?

@BenTen , any ideas?

SAPOrHM.jpg



IY1cwH5.jpg



0sBcC1Q.jpg
 
Also, now that it's the regular session hours, the PDR is showing correctly....so it appears it's not properly working during premarket hours based on the code (and that's when I need it the most to scan for stocks in play prior to the open).
 
@BenTen , what is a good way to identify if you're in pre-market vs. regular session timing? The reason I ask is the code below works properly for the previous day range....but it only works properly during the pre-market when "displace = 0" and only works properly during the regular session when I update it to "displace = -1".

Is there a code snippet to identify the session, and then update the displace value accordingly?

Code:
def  aggregationPeriod = AggregationPeriod.DAY;
def length = 1;
def displace = 0;

def PrevDayHigh = Highest(high(period = aggregationPeriod)[-displace], length) ;
def PrevDayLow = Highest(low(period = aggregationPeriod)[-displace], length); ;
def PrevDayClose = Highest(close(period = aggregationPeriod)[-displace], length);

plot pdr = PrevDayHigh;

assignBackgroundColor(if bid > prevDayHigh then Color.dark_GREEN else if bid < prevDayLow then Color.RED else Color.light_GRAY);
pdr.assignValueColor (if bid > prevDayHigh then Color.GREEN else if bid < prevDayLow then Color.white else Color.GRAY);
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
428 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top