Power Earnings Gaps (PEG) Scanner for ThinkorSwim


Staff member
Power Earnings Gappers (PEG) is a strategy shared by @traderstewie at Art of Trading. If you haven't check out the original article, you can do so here.

This scanner for ThinkorSwim just makes it easier to find potential stocks that fit the criteria. It was developed by Drew Griffith.


PEG Scanner

Rich (BB code):
#hint: SCAN (only) for "PEG" stands for power earnings gap. looking for a pullback from a previous gap and the stock is in a consolidation phase.

declare upper;
input percentage = 0.5; #hint: % gap
input wthn = 30; # periods to look for gap
def x = 1 + percentage / 100;

def cl = close;
def lo = low;
def hi = high;
def op = open;
def vo = volume;
def barnumber = BarNumber();

def gapup = lo > hi[1] * x;
def sign_up = if gapup then barnumber else 0;
def cup = (barnumber - Highest(sign_up[1], wthn));
def count_up = if barnumber <= wthn then 0 else if cup > wthn then 0 else cup;

def gapdn = hi * x < lo[1];
def sign_dn = if gapdn then barnumber else 0;
def cdn = (barnumber - Highest(sign_dn[1], wthn));
def count_dn = if barnumber <= wthn then 0 else if cdn > wthn then 0 else cdn;

input ema_10 = 10;
input ema_30 = 30;
input ema_300 = 300;

def EMA10 = MovAvgExponential(cl, ema_10);

def EMA30 = MovAvgExponential(cl, ema_30);

#def EMA300 = MovAvgExponential(cl, ema_300);

plot signal =
if cl <= EMA10 and cl >= EMA30 and EMA10 >= EMA30 and count_up > 0 and average(vo,5) < average(vo,50) then 1 else
if cl >= EMA10 and cl <= EMA30 and EMA10 <= EMA30 and count_dn > 0 and average(vo,5) < average(vo,50) then 1 else 0;
Last edited:


New member
This is an interesting strategy. But, Im confused how to use this. As in, how do I set up the scanner - do I have to copy the script into the scanner part or do I have to copy this as an indicator/strategy code and set up the scanner separately ?
Thanks is advance


Active member
2019 Donor
Copy the script, go to studies in TOS, click Create, delete everything then paste script above into study, change the name at the top and hit okay. Go to Scan, hit Study Filter, go all the way down to custom, hit delete, add condition, scroll to study, find whatever you named the study, click it, select "is true" within "however many bars". Hit okay and scan.
Don't forget aggregation periods for your scans.


Well-known member
@MBF thank you for giving that very succinct explanation. It often takes me three paragraphs to say what you did in four sentences or so. 🙏
Much wine for you, my friend.
  • Like
Reactions: MBF


New member
I got the scan set up and it works great showing me the stocks that have gapped up on their past earnings but what do I need to add for it to show me stocks that have had earning gap ups within the past day or two? Thanks in advance!!


Staff member
@jacqshen Have you tried to use the "within X bars" feature? That will allows you to set a lookback period.


Well-known member
@jacqshen There may not be many stocks that have earnings yet. Or, gaps may not have happened yet.
Save it as a Dynamic Scan and it will update every 3 minutes so that when earnings come rolling in over the next few weeks you can see the gappers when they occur.
Go here: Marketwach > Calendar > Uncheck all > Check Earnings > now you can see that there were only 8 or so companies that reported today.


Well-known member
@jacqshen Put this in as a Watchlist Column. Then you'll know the scheduled date. I use this one. Origin unknown, no citation.
#Days_til_Earns Markos uTS
input length = 60;
def xx= -geteventOffset(events.EARNINGS);
def yy = sum(hasEarnings(type = earningTime.AFTER_MARKET),length)[-length +1] >0;
plot x=xx+yy*.5;
x.assignvaluecolor( if yy!=0 then color.LIGHT_RED else color.DARK_GRAY);

@BenTen please feel free to move or copy this to another thread.
Last edited:


Staff member
Hello all, thanks for sharing this valuable info. I tried getting this Scan set up using the custom study code provided here. I put it in the "All of the following tab". Then hit scan. Does not seem to work, the scan gives me a huge number of stocks. So something is not working. Can someone smart help me point out what's not working? I'd like to post a pic, but the system is not allowing me to.
@SamTrader77, What other filters are you using? Using minimal filters, I show 596 results on a Daily Aggregation.

With more aggressive filtering:

I get 26 results. Depends on what you are looking for. We are in earnings season; you will need to be exact in your definitions.


New member
Thanks VERY much for this info @MerryDay . I will use more filters to narrow down results. I also noted that that there are two ways to add such a study/filter. It can be done as a stored study OR as a custom filter by entering the code into the editor, as I set the filtering. I will also use 6 bars, not 3 like I did before.

Wondering, How has this code/strategy worked out for folks? Just curious as I am new to this. Thanks again for the help. It's MUCH appreciated.


New member
What would an ideal bar setting be for this? Is there a reference in stewies' writings? Any suggestions? Haven't seen any action so I'm curious what to put in for this.

Similar threads