ORB Conditional Order Not firing on correct time frame

Leysritt

New member
Plus
I have some conditional orders I use every day that are supposed to fire based on a study that I use which has a custom script. Here is that script that is creating the Study that I am using as a trigger condition:
#orb_colors_03
#Mobius original
#csricksdds 2/20
#I like your ORB indicator with the labels that show when above/below...so that the labels don't show only when above/below can you add one showing Yellow "Trading within ORB" when it's trading within?
#Thanks to halcyonguy I have made a few changes to suit myself.
#https://usethinkscript.com/threads/shaded-opening-range-no-breakout.17922/
#Shaded Opening Range (No Breakout)
# 2020-01
# halcyonguy
#Revamped by C. Ricks 2/12/24
def na = Double.NaN;

# pick an agg time
input orb_time = AggregationPeriod.fifteen_min;
def orb_min = orb_time/60000;
def per = orb_min;

input show_ORB_label = yes;
input show_arrows = yes;

# open/close times (ET)
input start = 0930;
input end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;

def getgg = getaggregationPeriod();
def getaggmin = round(getgg/60,0);

addlabel(0,getaggmin);

# test if agg time is > than chart time
def aggok = if getaggmin <= orb_min then 1 else 0;


def durationsec = per * 60;
def secondspassed = secondsfromTime(start);
# simulated firstbar , based on orb time
def firstbar = if secondspassed >= 0 and secondspassed < durationsec then 1 else 0;
#addchartbubble(yes,low,per + "" + firstbar + "" + agg);
def afterfirst = if ( daytime and !firstbar ) then 1 else 0;

# is this the first bar after open ?
def openbar = if secondspassed >= 0 and secondspassed <= getgg then 1 else 0;

def ehi = high(period = orb_time);
def elo = low(period = orb_time);

# looks at firstbar , which is orbtime , NOT bar[1]
def perhigh = if !daytime then na else if firstbar then ehi else perhigh[1];
def perlow = if !daytime then na else if firstbar then elo else perlow[1];

# .....................................................


addlabel(1, " ", color.Yellow);
addlabel((close > perhigh),"Price Above ORB", color.green);
addlabel((close < perlow),"Price Below ORB", color.red);
addlabel((close <= perhigh and close >= perlow),"Price Inside ORB", color.yellow);
addlabel(1, " ", color.Yellow);

# .....................................................

# plot dots for first bar timeframe
plot hidots = if firstbar then perhigh else na;
plot lodots = if firstbar then perlow else na;
hidots.setpaintingStrategy(paintingStrategy.Dashes);
lodots.setpaintingStrategy(paintingStrategy.Dashes);
hidots.setDefaultColor(color.yellow);
lodots.setDefaultColor(color.yellow);


# plot line after first bar
plot hiline = if !firstbar then perhigh else na;
plot loline = if !firstbar then perlow else na;
hiline.setpaintingStrategy(paintingStrategy.LINE);
loline.setpaintingStrategy(paintingStrategy.LINE);
hiline.setDefaultColor(color.yellow);
loline.setDefaultColor(color.yellow);

#DefineGlobalColor("orb",color.magenta);
#DefineGlobalColor("orb_after",color.gray);
DefineGlobalColor("orb",color.red);
DefineGlobalColor("orb_after",color.green);

plot static = hiline;
static.AssignValueColor(Color.Yellow);

def limit = !IsNaN(close) and IsNaN(close [-1] ) && LowestAll(BarNumber());

AddChartBubble(limit, static, "ORB HI", Color.Yellow);


plot static2 = loline;
static2.AssignValueColor(Color.Yellow);

def limit2 = !IsNaN(close) and IsNaN(close [-1] ) && LowestAll(BarNumber());

AddChartBubble(limit2, static2, "ORB LO", Color.YELLOW);

##End Code

I am setting a Conditional Order to trigger a buy if the 5 minute candle closes above the upper plotted line. See here for the Conditional Order window:
1744466053006.png


I have a series of "Chart" tool windows open at any given time, one on the minute time frame and one on the five minute. Would anyone be able to tell me why occasionally the above order fires based on a 1 minute time frame instead of the five minute time frame? Sometimes they don't even fire at all. I have not saved these, so there is none of the code cut off problem that I know is present in TOS, I draft these each morning. I want to try and rely on this each day, but when they fire inconsistently it is difficult. Are there parameters I am missing or should be looking out for? Any help is appreciated and I can add more photos if needed.
 
Solution
@Leysritt Try using the code below, that I named ORB_CO_Lite, which has been trimmed down to eliminate all of the display related code other than plots... Also, when you reference the Study in your Conditional Order Wizard, simply compare the hiline plot to close as you don't need any AggregationPeriod settings as you have specified that at the top of your condition with aggregation set to 5m... The code may be able to be trimmed even further if needed by removing any plots that aren't required for Conditional Order purposes...

RXyuCXW.png

Ruby:
# ORB_CO_Lite
def na = Double.NaN;

input orb_time =...
@Leysritt Try using the code below, that I named ORB_CO_Lite, which has been trimmed down to eliminate all of the display related code other than plots... Also, when you reference the Study in your Conditional Order Wizard, simply compare the hiline plot to close as you don't need any AggregationPeriod settings as you have specified that at the top of your condition with aggregation set to 5m... The code may be able to be trimmed even further if needed by removing any plots that aren't required for Conditional Order purposes...

RXyuCXW.png

Ruby:
# ORB_CO_Lite
def na = Double.NaN;

input orb_time = AggregationPeriod.fifteen_min;
def orb_min = orb_time/60000;
def per = orb_min;

input show_arrows = yes;

input start = 0930;
input end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;

def getgg = getaggregationPeriod();
def getaggmin = round(getgg/60,0);

def aggok = if getaggmin <= orb_min then 1 else 0;

def durationsec = per * 60;
def secondspassed = secondsfromTime(start);
def firstbar = if secondspassed >= 0 and secondspassed < durationsec then 1 else 0;
def afterfirst = if ( daytime and !firstbar ) then 1 else 0;

def openbar = if secondspassed >= 0 and secondspassed <= getgg then 1 else 0;

def ehi = high(period = orb_time);
def elo = low(period = orb_time);

def perhigh = if !daytime then na else if firstbar then ehi else perhigh[1];
def perlow = if !daytime then na else if firstbar then elo else perlow[1];

plot hidots = if firstbar then perhigh else na;
plot lodots = if firstbar then perlow else na;

plot hiline = if !firstbar then perhigh else na;
plot loline = if !firstbar then perlow else na;
plot static = hiline;

def limit = !IsNaN(close) and IsNaN(close [-1] ) && LowestAll(BarNumber());

plot static2 = loline;

def limit2 = !IsNaN(close) and IsNaN(close [-1] ) && LowestAll(BarNumber());
 
Last edited by a moderator:
Solution
@rad14733 Thank you very kindly for amending that script for me, I have replaced my old orders and will begin testing them out. I will attach two screenshots here as I am trying to figure something out, when loading the order from save using the new cut down script, this is what appears in the order condition window:
1744710941990.png


If I recreate the order entirely using the new script, the window appears as such:
1744710998823.png

One shows more code than the other, but are both acceptable and will work properly? This is what I was referring to in earlier posts about how saved conditional orders don't seem to load the full code of a coded study thats part of the order.
 
Last edited by a moderator:
@Leysritt I'm not understanding why your Order Condition window would have the new code in it at all... The code I provided for ORB_CO_Lite only needs to be saved as a Study and then referenced by your Conditional Order using the single line of code I provided...

Let us know how the code performs when used as I have described...

Edited to add: The reason that the code I provided is not the same size is because I removed all non-essential code, or most of it, that a Conditional Order does not require... The code only needs definitions and calculations while excluding any Chart painting functions...
 
Last edited:
@rad14733 Let me try and be clearer.
I did save your ORB_CO_Lite as a study that I reference.
When I create a whole new order using your study as the condition, and then I go to the the page see below:
1744728188033.png

Then click on the gear Icon i get this as the order condition:
1744728243201.png

HOWEVER, if I save the conditional order and load it up without touching it, the code for that same window looks like this:
1744728292860.png

Not sure why when loading from a saved order I get the shorter code vs loading it right up, but I do.

BTW, this is what the full condition page looks like using your shorter script:
1744728392471.png
 
@Leysritt Ok... I've never had reason to click on the gear icon before now but it simply shows what code is being referenced within the conditional criteria... Your Buy leg of your OCO appears to be correct... Those extended trigger spikes in the lower panel merely show how long your criteria would be valid, but are a moot point once the order has triggered... Once triggered, that leg is active and waiting for the additional legs of your OCO... Whether the Buy leg performs reliably, due to potential complexities within the associated ORB_CO_Lite Thinkscript code, remains to be seen...
 
So at least yesterday, when I did not update the saved order, (it was showing the single line of code vs the full code) it did not fire at all, either when it should have or shouldn't have. Today I will try with the full code and see where that lands me, it may fire early, which has been my problem, but I will give it a shot and see.
 

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