
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