• Get $40 off VIP by signing up for a free account! Sign Up

Whats wrong with my code (Open High Low indicator)

Trigun1127

Member
So I have my code set to display yesterdays High And Low while also displaying just the days open price for that current day. However the open price has been inaccurate for the past two days either by a tick or a whole point. I'm also using this on the 5 minute chart.

declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};

plot high = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, high(period = timeFrame)[1], Double.NaN);
plot Low = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, low(period = timeFrame)[1], Double.NaN);
plot Open = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, open(period = timeFrame)[0], Double.NaN);

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
Open.SetDefaultColor (Color.Yellow);
Open.SetPaintingStrategy(PaintingStrategy.DASHES);

 
Solution
Its actually very odd be cause orginally the study was working fine. Now its always off by a tick. I have no clue whats causing the issue.

Heres a picture of the databox

I adjusted your code to not use open(period=timeframe)[0], but to just open, which is how I usually do it, when it is for today's open. This seems to work consistently. I did see the former worked before at times.

That is probably what was causing the slight differences. I would not recommend the plot names the way you did and then used a different version of the same plot name within your logic. For example, using plot high = ... high(period=timeframe)[1]... would be better if it were def hi = high(period=timeframe)[1]... and plot high =...
So I have my code set to display yesterdays High And Low while also displaying just the days open price for that current day. However the open price has been inaccurate for the past two days either by a tick or a whole point. I'm also using this on the 5 minute chart.




TOS uses a different method for futures (in this case /ES) to determine a timeframe. For /ES the DAY starts at 1800 hours, which is what was plotted for your definition of open(period = timeFrame)[0] as that was the open for the DAY. For stocks, DAY relates to regular trading hours, which is what you are requesting.

Here is code you can add to code to plot Today's regular trading hour open

Ruby:
def to = if getday()==getlastday() and gettime() crosses above regularTradingStart(getyyyYMMDD()) then open() else to[1];
plot todayopen = to;
todayopen.setpaintingStrategy(paintingStrategy.DASHES);
todayopen.setdefaultColor(color.white);
 
I am trading RTH on /MES. Not sure exactly where to put the code. I tried in a couple different areas but didn't change. Still shows the first bar off by a couple ticks or a whole point. Thank you
 
I am trading RTH on /MES. Not sure exactly where to put the code. I tried in a couple different areas but didn't change. Still shows the first bar off by a couple ticks or a whole point. Thank you

/MES is a futures so your code as posted displays today's open as the open at 1800.

Here is the code I posted above added to your code to display the RTH open.

Capture.jpg
Ruby:
declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};

plot high = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, high(period = timeFrame)[1], Double.NaN);
plot Low = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, low(period = timeFrame)[1], Double.NaN);
plot Open = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, open(period = timeFrame)[0], Double.NaN);

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
Open.SetDefaultColor (Color.Yellow);
Open.SetPaintingStrategy(PaintingStrategy.DASHES);

def to = if getday()==getlastday() and gettime() crosses above regularTradingStart(getyyyYMMDD()) then open() else to[1];
plot todayopen = to;
todayopen.setpaintingStrategy(paintingStrategy.DASHES);
todayopen.setdefaultColor(color.white);
 
Last edited:
I still get the issue for some reason. Here's my screen. I dont display the Globex session as an aside.


That code assumed you had extended hours showing in the chart settings for futures. If you are stating that you don't display the Globex session means you changed the chart settings, then your chart would only be displaying regular trading hours, Then your original code should work. See image below.

I cannot tell what timeframe chart you are using in the screenshot above nor the type of candles. If you are using heikin ashi candles then that could be what the problem may be.

To be able to assist you further, I need to know what timeframe chart you are using, whether extended hours have been unchecked in chart settings for futures and what type of candles you are using.

Here is your orignal code with /MES, 15m candle chart and the chart settings for futures that I referenced above.

Capture.jpg
Here is the chart settings for futures I mentioned above
Capture.jpg
 

Attachments

  • Capture.jpg
    Capture.jpg
    103.2 KB · Views: 229
Its actually very odd be cause orginally the study was working fine. Now its always off by a tick. I have no clue whats causing the issue.

Heres a picture of the databox

I adjusted your code to not use open(period=timeframe)[0], but to just open, which is how I usually do it, when it is for today's open. This seems to work consistently. I did see the former worked before at times.

That is probably what was causing the slight differences. I would not recommend the plot names the way you did and then used a different version of the same plot name within your logic. For example, using plot high = ... high(period=timeframe)[1]... would be better if it were def hi = high(period=timeframe)[1]... and plot high = hi;

Hope this works consistently for you now. TOS can be quirky.

Capture.jpg
Ruby:
declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};

def hi = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, high(period = timeFrame)[1], hi[1]);
plot high = hi;
def lo = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN, low(period = timeFrame)[1], lo[1]);
plot Low = lo;
def op = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and GetDay() != GetDay()[1], open, op[1]);
plot Open = op;

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
Open.SetDefaultColor (Color.YELLOW);
Open.SetPaintingStrategy(PaintingStrategy.DASHES);
 
Solution
I actually have one more question. I also set this to plot the open of the week and to display that line until the week is over. It does so every Monday at open. It seems to only do so on the daily.
 
I actually have one more question. I also set this to plot the open of the week and to display that line until the week is over. It does so every Monday at open. It seems to only do so on the daily.

Sorry, I got so focused on the daily, I forgot about the timeframe input having week and month. The condition of 'x' should fix all 3 options.

Capture.jpg
Ruby:
declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};
def x      = if timeFrame == timeFrame.WEEK
             then GetWeek()
             else if timeFrame == timeFrame.MONTH   
             then GetMonth()
             else GetDay();
def hi     = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
             x != x[1],
             high(period = timeFrame)[1],
             hi[1]);
plot high  = hi;
def lo     = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
             x != x[1] ,
             low(period = timeFrame)[1],
             lo[1]);
plot Low   = lo;
def op     = If(GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
             x != x[1] ,
             open,
             op[1]);
plot Open  = op;

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
Open.SetDefaultColor (Color.YELLOW);
Open.SetPaintingStrategy(PaintingStrategy.DASHES);
 
I dont know why but all of a sudden for some reason when a bar closes the High and Low from 2 days ago are getting plotted instead of yestedays highs and low. I have no clue why the sudden change


It looks like your time axis may be set too many bars to the right and is bringing in another day into play.

I have modified the code to hopefully account for that by adding !isnan(close) to the hi and lo definitions so the code only recognizes bars/periods with data

Ruby:
declare hide_on_daily;
declare once_per_bar;

input timeFrame = {default DAY, WEEK, MONTH};
def x      = if timeFrame == timeFrame.WEEK
             then GetWeek()
             else if timeFrame == timeFrame.MONTH   
             then GetMonth()
             else GetDay();
def hi     = If(!isnan(close) and GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
             x != x[1],
             high(period = timeFrame)[1],
             hi[1]);
plot high  = hi;
def lo     = If(!isnan(close) and GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
             x != x[1] ,
             low(period = timeFrame)[1],
             lo[1]);
plot Low   = lo;
def op     = If(!isnan(close) and GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
             x != x[1] ,
             open,
             op[1]);
plot Open  = op;

high.SetDefaultColor (Color.GREEN);
high.SetPaintingStrategy(PaintingStrategy.DASHES);
Low.SetDefaultColor(Color.RED);
Low.SetPaintingStrategy(PaintingStrategy.DASHES);
Open.SetDefaultColor (Color.YELLOW);
Open.SetPaintingStrategy(PaintingStrategy.DASHES);
 

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