• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

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!
 

BenTen

Administrative
Staff
VIP
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
 

nitrotrader

New member
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?
 

BenTen

Administrative
Staff
VIP
@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.
 

nitrotrader

New member
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
 

nitrotrader

New member
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).
 

nitrotrader

New member
@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);
 

Similar threads

Top