## 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
#
# 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:
#
ORH.setdefaultcolor(color.green);
ORH.setStyle(curve.Long_DASH);
ORH.setlineweight(3);
ORL.setdefaultcolor(color.red);
ORL.setStyle(curve.Long_DASH);
ORL.setlineweight(3);
FibMid.setdefaultcolor(color.gray);
FibMid.setStyle(curve.SHORT_DASH);
FibMid.setlineweight(3);
FibExtUp1.setdefaultcolor(color.green);
FibExtUp1.setStyle(curve.SHORT_DASH);
FibExtUp1.setlineweight(2);
FibExtDown1.setdefaultcolor(color.red);
FibExtDown1.setStyle(curve.SHORT_DASH);
FibExtDown1.setlineweight(2);
FibExtUp2.setdefaultcolor(color.green);
FibExtUp2.setStyle(curve.SHORT_DASH);
FibExtUp2.setlineweight(1);
FibExtDown2.setdefaultcolor(color.red);
FibExtDown2.setStyle(curve.SHORT_DASH);
FibExtDown2.setlineweight(1);

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

1. Jim Says:

Prospectus,

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:

Pupsor,

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:

Prospectus,

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.

Thanks,
Tyler

7. Prospectus Says:

Tyler,

8. Quentin Says:

can this be done in Quotetracker, too ? thanks

9. Prospectus Says:

Quentin:

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;

Thanks
Subramanik

12. Prospectus Says:

Subramanik,

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:

Prospectus,

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 ?

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

Hello,

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?

19. Prospectus Says:

You can plot the opening print like this:

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

Hi,

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

21. Dennis Says:

Hello,

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: