Opening Range Gaps [TFO] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
nD41mGH.png


Author Message:
This indicator displays Opening Range Gaps with an adjustable time window. Its intention is to capture the discrepancy between the close price of previous and new Real Trading Hours (RTH) sessions, i.e. yesterday's close compared to today's open. A gap will be drawn from this area with a solid line denoting its midpoint, and dashed lines denoting the upper and lower quartiles of its range. Its color is determined by whether the new session open price is above or below the previous session close.

CODE:

CSS:
#// https://www.tradingview.com/v/jAYmTrqZ/
#// This source code is subject to the terms of the Mozilla Public License 2.0
#// © tradeforopp
#indicator("Opening Range Gaps [TFO]", "Opening Range Gaps [TFO]", true, max_boxes_c
# Converted by Sam4Cok@Samer800    - 12/2023

input timeframe = {Default "Chart", "Custom"};
input customTimeframe = AggregationPeriod.FOUR_HOURS;
input startSessionDate = {Default "Previous Day Close", "Custom Session"};
input CustomSessionStartTime = 0930;       # "The start time from which the gap will be derived"
input CustomSessionEndTime   = 945;        # "The end time to which the gap will be derived"
input SessionDelineations = yes;    # "Vertical lines will mark the gap session start and end times"
input TrackStartPrice = yes;  # "Track the price that indicates the beginning of a gap (previous session close)"
input extendLine = no;        # "Extend gaps the right of the chart"
input keep_last = 5;          # "Keep Last"

def na = Double.NaN;
def last = isNaN(Close);
def dayAgg = AggregationPeriod.DAY;
def extend = if extendLine then no else last[1];
def intraDay = startSessionDate==startSessionDate."Custom Session";
def chartTime = GetAggregationPeriod();
def chartLimit = chartTime < dayAgg;
def date = GetYYYYMMDD();
def dayChange = date-date[1];
def SessionStart = SecondsFromTime(CustomSessionStartTime) >= 0;
def SessionEnd   = SecondsTillTime(CustomSessionEndTime) > 0;
def t = SessionStart and SessionEnd;
#-- Color
DefineGlobalColor("up", CreateColor(41, 98, 255));
DefineGlobalColor("dup", CreateColor(0, 27, 100));
DefineGlobalColor("dn", CreateColor(156, 39, 176));
DefineGlobalColor("ddn", CreateColor(85, 21, 96));

def dCl = close(Period = dayAgg);
def tfO = open;
def tfC = close;
def mtfO = open(Period = customTimeframe);
def mtfC = close(Period = customTimeframe);
def o_;
def c_;
Switch (timeframe) {
Case "Custom" :
    o_ = mtfO;
    c_ = mtfC;
Default :
    o_ = tfO;
    c_ = tfC;
}
def o = o_;
def c = c_;
def top; def bot;def Vert; def hor;def start; def counter; def cntCond;
if (if intraDay then (t and !t[1]) else dayChange) {
    cntCond = cntCond[1];
    top = if intraDay then c[1] else dCl[1];
    bot = if intraDay then c[1] else dCl[1];
    Vert  = if SessionDelineations and !cntCond then yes else na;
    hor   = if TrackStartPrice then yes else na;
    start = if intraDay then c[1] else dCl[1];
    counter = counter[1] + 1;
    } else
if (!t and t[1]) {
    cntCond = cntCond[1];
    top   = if o > top[1] then o else top[1];
    bot   = if o < bot[1] then o else bot[1];
    Vert  = if SessionDelineations and !cntCond then yes else no;
    hor   =  no;
    start =  start[1];
    counter = counter[1];
    } else {
    cntCond = highestAll(counter[1]) - counter[1] > keep_last;
    top = if (cntCond or !chartLimit) then na else if top[1] then top[1] else c[1];
    bot = if (cntCond or !chartLimit) then na else if bot[1] then bot[1] else c[1];
    Vert = no;
    hor  = hor[1];
    start  = start[1];
    counter = counter[1];
}
def topbot = (top - bot);
def h = if hor then na else topbot;
def qr = h / 4;
def q1 = (top - qr * 1);
def q2 = (top - qr * 2);
def q3 = (top - qr * 3);
def up = top > start;
def tpoLine = if (hor or extend) then na else top;
def botLine = if (hor or extend) then na else bot;

plot trackPrice = if hor then top else na;
plot q11 = if extend then na else q1;
plot q22 = if extend then na else q2;
plot q33 = if extend then na else q3;

trackPrice.SetDefaultColor(Color.GRAY);
trackPrice.SetPaintingStrategy(PaintingStrategy.DASHES);
q11.AssignValueColor(if up then GlobalColor("up") else GlobalColor("dn"));
q22.AssignValueColor(if up then GlobalColor("up") else GlobalColor("dn"));
q33.AssignValueColor(if up then GlobalColor("up") else GlobalColor("dn"));

q11.SetStyle(Curve.SHORT_DASH);
q22.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
q33.SetStyle(Curve.SHORT_DASH);

AddCloud(if up then na else tpoLine, botLine, GlobalColor("ddn"), GlobalColor("ddn"), yes);
AddCloud(if up then tpoLine else na, botLine, GlobalColor("dup"), GlobalColor("dup"), yes);

AddVerticalLine(Vert, "", Color.DARK_GRAY, Curve.SHORT_DASH);

#-- END OF CODE
 

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
432 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