ORB with ATR Trailing SL [Bluechip Algos] for ThinkOrSwim

samer800

Moderator - Expert
VIP
Lifetime
U0kUJtm.png


Author Message:

This is a simple ORB (Opening Range Breakout) Indicator that not only signals breakout directions based on the opening session range but also includes trailing stop levels to manage ongoing trades. Instead of regular fixed Stop loss, we use ATR indicator (ATR based SL) to trail the stop loss that might help in maximizing the profitable trades. This helps especially during the trending days where market moves unidirectionally.

CODE:

CSS:
#// Indicator for TOS
# @BlueChip_Algos
# ORB with ATR Trailing SL [Bluechip Algos]
# indicator("ORB Indicator", overlay = true)
# Converted by Sam4Cok@Samer800     - 11/2024

input orbTimeframe = {"Custom", default "15 Minutes", "30 Minutes", "45 Minutes", "1 Hour", "1.5 Hours", "2 Hours", "3 Hours", "4 Hours"}; # "ORB Timeframe"
input customSessionTimeStart = 0930; #"Custom Session"
input customSessionEndTime = 1000;
input SignalConfigure = {default "CLOSE", "TOUCH"}; # "Signal Configure"
input showTrailingLines = yes;
input longTrailPercentage = 0.38; # "ATR (%)"


def na = Double.NaN;
def last = IsNaN(close);
def timeframe = GetAggregationPeriod();
def longTrailPerc = longTrailPercentage * 0.01;
def dayofmonth = GetYYYYMMDD()!=GetYYYYMMDD()[1];

#f_insession(_session) =>
script inSession {
    input Start = 0930;
    input End  = 1000;
    def insession = SecondsFromTime(Start) >= 0 and SecondsTillTime(End) > 0;
    plot return = insession;
}

def in_session;
switch (orbTimeframe) {
case "Custom"     :
    in_session = inSession(customSessionTimeStart, customSessionEndTime);
case "30 Minutes" :
    in_session = inSession(0930, 1000);
case "45 Minutes" :
    in_session = inSession(0930, 1015);
case "1 Hour"     :
    in_session = inSession(0930, 1030);
case "1.5 Hours"  :
    in_session = inSession(0930, 1100);
case "2 Hours" :
    in_session = inSession(0930, 1130);
case "3 Hours" :
    in_session = inSession(0930, 1230);
case "4 Hours" :
    in_session = inSession(0930, 1330);
default :
    in_session = inSession(0930, 0945);

}
def hide = timeframe < AggregationPeriod.DAY;
def is_first = in_session and !in_session[1];

def orb_high;
def orb_low;

if is_first {
    orb_high = high;
    orb_low = low;
} else {
    orb_high = if high > orb_high[1] and in_session then high else orb_high[1];
    orb_low  = if low < orb_low[1] and in_session then low else orb_low[1];
}
#-- Stop Loss

def crossOver;
def crossUnder;
switch (SignalConfigure) {
case "TOUCH" :
    crossOver = (high > orb_high) and (high[1] <= orb_high[1]) or
                (high > orb_high) and isNaN(orb_high[1]);
    crossUnder = (low < orb_low) and (low[1] >= orb_low[1]) or
            (low < orb_low) and isNaN(orb_low[1]);
default :
    crossOver = (close > orb_high) and (close[1] <= orb_high[1]) or
            (close > orb_high) and isNaN(orb_high[1]);
    crossUnder = (close < orb_low) and (close[1] >= orb_low[1]) or
            (close < orb_low) and isNaN(orb_low[1]);
}

def new_flag = if is_first then yes else if dayofmonth then no else new_flag[1];

def bflag;
def sflag;
def long1_bo;
def short1_bo;
def buy; def sell;
if dayofmonth {
    bflag = yes;
    sflag = yes;
    long1_bo = no;
    short1_bo = no;
    buy       = no;
    sell      = no;
} else {
    buy  = crossOver and bflag[1] and new_flag;
    sell = crossUnder and sflag[1] and new_flag;
    if crossOver and bflag[1] and new_flag {
        bflag = no;
        sflag = sflag[1];
        long1_bo = yes;
        short1_bo = no;
    } else if crossUnder and sflag[1] and new_flag {
        bflag = bflag[1];
        sflag = no;
        long1_bo = no;
        short1_bo = yes;
    } else {
        bflag = bflag[1];
        sflag = sflag[1];
        long1_bo = long1_bo[1];
        short1_bo = short1_bo[1];
    }
}
def stopValueL = close[1] * (1 - longTrailPerc);
def stopValueS = close[1] * (1 + longTrailPerc);
def longStopPrice = if (long1_bo) then Max(stopValueL, longStopPrice[1]) else Double.NEGATIVE_INFINITY;
def shortStopPrice = if (short1_bo) then Min(stopValueS, shortStopPrice[1]) else Double.POSITIVE_INFINITY;
def LongORB = long1_bo and longStopPrice;
def ShortORB =short1_bo and shortStopPrice;
def col = if LongORB then 1 else if ShortORB then -1 else if dayofmonth then 0 else col[1];

plot stopLong = if showTrailingLines and LongORB then longStopPrice else na;
plot stopShort = if showTrailingLines and ShortORB then shortStopPrice else na;

stopLong.SetPaintingStrategy(PaintingStrategy.LINE);
stopShort.SetPaintingStrategy(PaintingStrategy.LINE);
stopLong.SetDefaultColor(Color.GREEN);
stopShort.SetDefaultColor(Color.RED);

AddChartBubble(buy, low, "B", Color.GREEN, no);
AddChartBubble(sell, high, "S", Color.RED);

#-- ORB
plot orbHi = if hide and !in_session then orb_high else na; # "ORB High"
plot orbLo = if hide and !in_session then orb_low else na;  # "ORB Low"
plot midHL = (orbHi + orbLo) / 2;
orbHi.SetPaintingStrategy(PaintingStrategy.DASHES);
orbLo.SetPaintingStrategy(PaintingStrategy.DASHES);
midHL.SetStyle(Curve.SHORT_DASH);
orbHi.AssignValueColor(if col>0 then Color.CYAN else if col < 0 then Color.MAGENTA else Color.GRAY);
orbLo.AssignValueColor(if col<0 then Color.MAGENTA else if col > 0 then Color.CYAN else Color.GRAY);
midHL.SetDefaultColor(Color.GRAY);

#-- cloud

AddCloud(if LongORB[1] then orbHi else na, orbLo, CreateColor(0, 67, 67));
AddCloud(if ShortORB[1] then orbHi else na, orbLo, CreateColor(67, 0, 67));

#-- 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
217 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