Price Earnings Gap (PEG) Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
> "PEG" stands for price earnings gap and also known as the holy grail setup. looking for a pullback from a previous gap and the stock is in a consolidation between the EMA10-30. The candlesticks are important here to confirm the move. The insync code helps to confirm the move. Arrows will still be plotted if insync value does NOT confirm the move, but gray color instead of red or green. Consider a half size position on a gray signal.

Created by Drew Griffith.

VJWwtJk.png

Multi-Indicator "InSync" Calculation
  • Script defines a custom indicator called insync by summing the values of several normalized technical indicators:
    • Bollinger Percent B (BBP): Normalized to 0-100.
    • 2-period Relative Strength Index (RSI2).
    • 14-period Relative Strength Index (RSI14).
    • Money Flow Index (MFI).
    • Stochastic Full (STO).
  • The insync value is then rounded to the nearest multiple of 10 (numberofdigits = -1).
  • This score likely represents the overall market strength or weakness across multiple timeframes/metrics.
3. Candlestick Wick Reversal Patterns
  • It defines two complex patterns, LongWickReversal and ShortWickReversal, which look for candles with a long wick in the direction of the desired reversal and a small body.
    • The WickMultiplier (default 1.65) requires the reversal wick (e.g., lower wick for a bullish reversal) to be at least $1.65$ times the size of the candle body.
    • The BodyPercentage (default 0.40) requires the candle body to be no more than $40\%$ of the total bar range (hi - lo).
    • The specific logic is complex and handles both up and down candles (cl > op or cl < op) and Doji/Pin-bar cases (cl == op).
4. The Signal Logic
The script generates two main signals:

SignalCandlestick ConditionEMA ConditionColor Condition (Strong Signal)
signalUP (Bullish)LongWickReversal == 1Close is below EMA10 and above or at EMA30, with EMA10 > EMA30 (price is pulling back into the short-term trend).insync <= insync_bullpull (default 220, indicating strong oversold conditions).
signalDN (Bearish)ShortWickReversal == 1Close is above EMA10 and below or at EMA30, with EMA10 < EMA30 (price is rallying into the short-term downtrend).insync >= insync_bearpull (default 300, indicating strong overbought conditions).
  • The signals are plotted as up/down arrows (PaintingStrategy.ARROW_UP/ARROW_DOWN).
  • The arrow is colored Green/Red for the strong "InSync" condition, and Gray if the EMA and wick conditions are met, but the "InSync" condition is not met.
  • The script also includes audible alerts for both signals.

thinkScript Code
Rich (BB code):
declare upper;
def cl = close;
def lo = low;
def hi = high;
def op = open;

input ema_10 = 10;
input ema_30 = 30;
input WickMultiplier = 1.65; #2.0
input BodyPercentage = 0.40; #.25
input insync_bullpull=220;
input insync_bearpull=300;

plot EMA10 = MovAvgExponential(cl, ema_10);
EMA10.AssignValueColor(Color.CYAN);
EMA10.HideBubble();
EMA10.HideTitle();
EMA10.Hide();
EMA10.SetLineWeight(1);
EMA10.SetPaintingStrategy(PaintingStrategy.LINE);

plot EMA30 = MovAvgExponential(cl, ema_30);
EMA30.AssignValueColor(Color.MAGENTA);
EMA30.HideBubble();
EMA30.HideTitle();
EMA30.Hide();
EMA30.SetLineWeight(2);
EMA30.SetPaintingStrategy(PaintingStrategy.LINE);

# InSync calcs
# study definitions
def bbp_length = 20;
def mfi_length = 14;
def sto_length = 14;
def rsi2_length = 2;
def rsi14_length = 14;
def rsi2 = RSI(length = rsi2_length);
def rsi14 = RSI(length = rsi14_length);
def mfi = MoneyFlowIndex(length = mfi_length);
def sto = StochasticFull("k period" = sto_length);
def bp = BollingerPercentB(length = bbp_length, "average type" = "exponential");
def bbp = if bp >= 100 then 100 else if bp <= 0 then 0 else bp;
def insync = Round(bbp + rsi2 + rsi14 + mfi + sto, numberofdigits = -1);

def LongWickReversal = if cl > op
    and (op - lo) >= ((cl - op) * WickMultiplier)
    and (hi - cl) <= ((hi - lo) * BodyPercentage)
    or cl < op
    and (cl - lo) >= ((op - cl) * WickMultiplier)
    and (hi - cl) <= ((hi - lo) * BodyPercentage)
    or cl == op and cl != hi
    and hi - lo >= Average(hi - lo, 50) then 1
    else 0;

def ShortWickReversal = if cl < op
    and (hi - op) >= ((op - cl) * WickMultiplier)
    and (cl - lo) <= ((hi - lo) * BodyPercentage)
    or cl > op
    and (hi - cl) >= ((cl - op) * WickMultiplier)
    and (cl - lo) <= ((hi - lo) * BodyPercentage)
    or cl == op and cl != lo
    and (hi - lo) >= ((cl - lo) * WickMultiplier)
    and (cl - lo) <= ((hi - lo) * BodyPercentage)
    or op == lo and cl == lo
    and hi - lo >= Average(hi - lo, 50) then 1 else 0;

plot signalUP = if cl < EMA10 and cl >= EMA30 and EMA10 > EMA30
     and (LongWickReversal == 1)
     then lo else Double.NaN;
signalUP.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
signalUP.SetLineWeight(2);
signalUP.AssignValueColor(if cl < EMA10 and cl >= EMA30 and EMA10 > EMA30
     and (LongWickReversal == 1) and insync <= insync_bullpull
     then Color.GREEN else Color.gray);

plot signalDN = if cl > EMA10 and cl <= EMA30 and EMA10 < EMA30
     and (ShortWickReversal == 1)
     then hi else Double.NaN;
signalDN.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
signalDN.SetLineWeight(2);
signalDN.AssignValueColor(if cl > EMA10 and cl <= EMA30 and EMA10 < EMA30
     and (ShortWickReversal == 1) and insync >= insync_bearpull
     then Color.RED else Color.gray);

input audibleAlerts = yes;
Alert(If(signalUP, 1, 0) within 1 bars, "BULLISH PEG", Alert.BAR, Sound.Ding);
Alert(If(signalDN, 1, 0) within 1 bars, "BEARISH PEG", Alert.BAR, Sound.Ding);

Shareable Link
https://tos.mx/SnjZe1
 
Last edited by a moderator:

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

Dynamic StopLoss following an indicator I need your help ben to convert it to TOS . Thanks

 
Last edited:
@ukwildcat1998 Still works for me. Please check again.



@mo2020 Please create a new post with your request. Do not post it under any other topics.

 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
846 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