Automatic Opening Range and Fibonacci Levels for Think or Swim

UPDATE 4 May 2009: Modified code so user can specify the open time. This is useful for trading the OR in other markets such as the Globex futures session.

UPDATE 11 Feb 2009: Added second fib extension option per Jim’s comment.


Trader-X has returned to blogging recently (and there was much rejoicing)! He is sharing his new, simplified setups on his blog. I decided to do some Thinkscript code to automate the plotting of the opening range and the Fibonacci retracements and extensions that Trader-X now uses. This is all my own interpretation of what X is doing, so any errors are my own.

I decided that the plot behavior I would want is to plot these lines starting from when the OR is defined, showing nothing at any time earlier than that. This helps to visually cue you to NOT take a trade before the OR is completed! If you want to see the levels for each day, set the input “ShowTodayOnly” to “No”. You also input the time (EST) that the OR begins, and the time the OR is “completed”. If your market opened at 9:30 EST, and you wanted a 30 minute opening range, you would enter “0930” for ORBegin, and “1000” for OREnd (and that is the default). A 60 minute OR would be “0930” and “1030”, respectively. Finally, you can input the fib extension you want plotted as well.

Here is the code:

# Automatic Opening Range and Fibonacci Levels
# By Prospectus @
# Inspired by Trader-X @
# This Thinkscript is designed to plot the OR high, low,
# 50% fib retrace, and fib extensions for the current day.
# This will only work correctly on time-based charts,
# where the OR timeframe is divisible by the bar period
# e.g. 30 minute OR, 10 min bars. An extra fib extension
# may be used if desired to create a target zone.
def na=double.nan;
# Define time that OR begins (in hhmm format,
# 0930 is the default):
input ORBegin = 0930;
# Define time that OR is finished (in hhmm format,
# 10:00 is the default):
input OREnd = 1000;
# Input first and second fib extension levels
# (default 1.382, 1.621):
Input FibExt1=1.382;
Input FibExt2=1.621;
# Show Today only? (Default Yes)
input ShowTodayOnly={"No", default "Yes"};
def s=ShowTodayOnly;
# Show Second fib extension? (Default No)
input ShowFibExt2={default "No", "Yes"};
def sf2=ShowFibExt2;
# Create logic for OR definition:
Def ORActive = if secondstilltime(OREnd)>0 AND secondsfromtime(ORBegin)>=0 then 1 else 0;
# Create logic to paint only current day post-open:
def today=if s==0 OR getday()==getlastday() AND secondsfromtime(ORBegin)>=0 then 1 else 0;
# Track OR High:
Rec ORHigh = if ORHigh[1]==0 or ORActive[1]==0 AND ORActive==1 then high else if ORActive AND high>ORHigh[1] then high else ORHigh[1];
# Track OR Low:
Rec ORLow = if ORLow[1]==0 or ORActive[1]==0 AND ORActive==1 then low else if ORActive AND low<ORLow[1] then low else ORLow[1];
# Calculate OR width:
Def ORWidth = ORHigh - ORLow;
# Calculate fib levels:
Def fib_mid = (ORHigh+ORLow)/2;
Def fib_ext_up1 = ORHigh + ORWidth*(FibExt1 - 1);
Def fib_ext_down1 = ORLow - ORWidth*(FibExt1 - 1);
Def fib_ext_up2= ORHigh + ORWidth*(FibExt2 - 1);
Def fib_ext_down2 = ORLow - ORWidth*(FibExt2 - 1);
# Define all the plots:
Plot ORH=if ORActive OR today<1 then na else ORHigh;
Plot ORL=if ORActive OR today<1 then na else ORLow;
Plot FibMid=if ORActive OR today<1 then na else fib_mid;
Plot FibExtUp1=if ORActive OR today<1 then na else fib_ext_up1;
Plot FibExtDown1=if ORActive OR today<1 then na else fib_ext_down1;
Plot FibExtUp2=if ORActive OR today<1 OR sf2<1 then na else fib_ext_up2;
Plot FibExtDown2=if ORActive OR today<1 OR sf2<1 then na else fib_ext_down2;
# Formatting:

Tags: , , ,

40 Responses to “Automatic Opening Range and Fibonacci Levels for Think or Swim”

  1. Jim Says:


    Nice job of coding. I like your color scheme.

    You might consider also adding the 1.62 extension level so that you have a target range 1.38-1.62. A market will good momentum will often move thru the 1.38 extension and tag the other side of the zone (1.62).

  2. Prospectus Says:

    Thanks, Jim. I’ll add in one more Fib extension level.

  3. Pupsor Says:

    Can we detect what timeframe is set and use it for plotting, because every time you need change ORTime?

  4. Pupsor Says:

    And of couse – good job, Prospectus!

  5. Prospectus Says:


    As I understand it, Trader-X sticks to a 30 minute opening range, regardless of the timeframe of the bars. If you put “1000” in as the time that the OR is finished, then my thinkscript is smart enough to figure out how many bars it needs to be for that time. I’m not sure why you would need to change the OR time based on chart timeframe.

    If that doesn’t clarify, then I don’t understand your question, I guess. If so, feel free to ask again!

  6. Tyler Says:


    I am glad you decided to make this study. I have been trying to make this for eSignal for a while, but have had a tough time with the code, etc. I was very excited when I read your comment on Trader X’s blog stating that you were going to work on it, but didn’t expect it that quick! This is very helpful.


  7. Prospectus Says:


    No problem. Good luck with your coding and trading!

  8. Quentin Says:

    can this be done in Quotetracker, too ? thanks

  9. Prospectus Says:


    QuoteTracker doesn’t let you create custom indicators. You could add some paintbar code to paint something like a dot when price crosses a fib level, but you would have to do a lot of hard-coded nested logic. Plus, you wouldn’t get the fib levels to plot on the chart. So yes and no, to answer your question. You can manually draw a fib retracement of course, but that could get tedious.

  10. Peter Says:

    Great script, thanks for sharing!! 🙂

  11. Subramanik Says:

    I am unable to make this code work in think or swim.

    Getting error ORWidth with error :- “No such variable ORWidth at 65:28”

    Def ORWidth = ORHigh – ORLow;

    Can some one help please.


  12. Prospectus Says:


    In your thinkscript editor, delete all the “-” in the code and type a new “-” from your keyboard. That should fix your problem. WordPress changed the “-” from the code I typed in into a fancy hyphen character that thinkscript chokes on. I’ve fixed it in the code, so future users can just copy/paste the code from the article above and it should work.

  13. MichaellaS Says:

    tks for the effort you put in here I appreciate it!

  14. Webby Says:


    Nice looking indicator. I had a question though.

    How can I use this in a 24 hrs market such as the currency futures ? I trade the Euro a lot and was wondering if the open range envelope should be started earlier to account for the 24 hrs nature of contract. If so what time would be the Start Time ?

    Your thoughts would be appreciated.

  15. Prospectus Says:

    I would think you want to align with an “open” somewhere, be it New York, London or Tokyo. But I’m not a forex expert by any means.

  16. Qing Says:

    Hi, I noticed the plots start at 10 instead of 9:30. Could we shift the lines left?

  17. Prospectus Says:

    I’ll work on it

  18. followmytrade Says:


    I want to thank you for your contribution to the trading community.. Was referred here today by someone in our chatroom…

    I made a few changes to this study so I could obtain the Initial Balance (first hour) of trading, which works very well thank you… I tried to make the changes to show me the previous days information but it does not want to show up. Can you help me with that? The only thing I need is the high and low for the first hour and able to see previous days information as well.

    Just as an FYI, I have used 4 seperate studies to give me the IB, ongoing midpoint, previuos days high, low and close .. really good info to have on there… The only other item I am looking for is to plot the opening print, is there an easy way to do that or to add it to the study?

    Thanks in advance,


  19. Prospectus Says:


    You can plot the opening print like this:

    def prioropen=open(period=”DAY”)[1];

  20. Gunawan Cahadi Says:


    Have you coding this OR Auto FIB levels in Ninja Trader Script? Can you share it here? Tx in advance…

  21. Dennis Says:


    Is there a way to plot the input values on top of each retracement line?
    Plot 138.2%, 162.1%, etc on the left edge on top of each retracement line and the $amount on the rigth edge, so that we know the value each line represents.
    Just like a normal fib drawing does.

    Thank you

  22. Prospectus Says:

    I’ll look into it

  23. Fibonacci Projection & Fibonacci Study. Forex | Forex Robot Review, Forex Robot Trading, Best Forex Robot Review, Forex Robots, Forex Trading Software, Forex Pips Says:

    […] 2011GBP/JPY Elliott wave count and Fibonacci levels for September 13, 2011Lesson 3: Fibonacci LevelsAutomatic Opening Range and Fibonacci Levels for Think or Swim div.socialicons{float:left;display:block;margin-right: 10px;}div.socialicons p{margin-bottom: […]

  24. Louis Says:

    love your code.
    what would be the trick to draw a set of fibs for the 16h00 – 09h30 globex session?

    thank you.

  25. Day trading Crude without indicators - Trading Journals - Page 267 Says:

    […] for Ninja trader. Thanks, using TOS, found a free OR indie that works if anyone else wants it: Automatic Opening Range and Fibonacci Levels for Think or Swim Read the Prospectus Set times and disabled all plots except ORH and ORL. So today's OR is 87.43 to 88.36, correct? […]

  26. Jean-Louis Says:

    Great Indicator!
    Could we have the Fib level number showing on the right side of of the chart just above the Fib levels? 1.621.. etc..

  27. Dollar Bounce Looks Corrective, SPX 500 at Risk of Deeper Losses | I am John Becker Says:

    […] Automatic Opening Range and Fibonacci Levels for Think or Swim … […]

  28. Vahan Says:

    Its an Excellent Script! THANKS for sharing!

  29. Dollar climbs to one-month high – Business Recorder | I am John Becker Says:

    […] Automatic Opening Range and Fibonacci Levels for Think or Swim … […]

  30. AUD/NZD: Bears and bulls lining up for battle – Futures Magazine | I am John Becker Says:

    […] Automatic Opening Range and Fibonacci Levels for Think or Swim … […]

  31. balvald Says:

    awesome, this study rocks. Thanks

  32. flag represent Says:

    Thank you for your site post. Jones and I happen to be saving for just a new e-book oon this issue and your writing hhas made
    people like us to save all of our money. Yourr ideaas really responded to all our issues.
    In fact, over what we had known just before wwe
    ran into your xcellent blog. We no longer nurture
    doubts along with a troubled mind because you have actually attended to
    each of our needs here. Thanks

  33. Paul Says:

    Great script, is there a way to set it for the most resent high/low instead calculating the hole day.


  34. PatrickFX Says:

    I am impressed with your considerate method, let’s connect some

  35. forex fury Says:

    I appreciate you for this, I’ll certainly share it with my favorite audience.

  36. forex fury ea Says:

    Your notions on trading are really extraordinary, I am going to throw several of my readers to your web site.

  37. John Kane Says:

    The foundation regarding your investing method is effortless to see.
    Many thanks for distributing this with us at this moment.

  38. Subhajit Sengupta Says:

    Excellent script but does not work on the thinkscript mobile platform. Have you noticed such differences between the mobile and desktop platforms

  39. Prospectus Says:

    Yes. I need to go back and make changes, but haven’t found time

  40. Forex Fury Says:

    Everything you come up with hits a nerve with me, thanks for baffling your followers.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: