Daily 4hr OHLC - 6am to 10am

chris_dc

New member
Hello,

I was looking for help making a script that marks the daily 4hr candle from 6am-10am pst with the OHLC and the lines stay there until 9:59am of the next day, then the new levels would be marked out. If it could have the option of having the previous levels stay on the graph or only showing the most recent one it would be amazing

I've been looking around and have only found scripts that mark every 4 hour candle continuously, I would just want that specific candle.

Thank you in advance for any help provided! 🫡
 
Solution
this defines a time period, from start time to end time.
when the period starts, it looks ahead and finds the OHLC levels during the period.
it draws 4 lines, OHLC , from the first bar of the period to the first bar of the next period.
it draws bubbles to define the 4 lines.
draws 2 vertical lines for the start and end of the period.


this uses code that sleepyz used in a code, to find the bar closest to the desired time.
so if there is no bar at 6am, it will find the next bar.
https://usethinkscript.com/threads/vertical-line-and-opening-price.11725/#post-101368

Code:
#ohlc_lines_from_time_period

#https://usethinkscript.com/threads/daily-4hr-ohlc-6am-to-10am.21619/
#Daily 4hr OHLC - 6am to 10am
#chris_dc  10/9

# mark the daily...
this defines a time period, from start time to end time.
when the period starts, it looks ahead and finds the OHLC levels during the period.
it draws 4 lines, OHLC , from the first bar of the period to the first bar of the next period.
it draws bubbles to define the 4 lines.
draws 2 vertical lines for the start and end of the period.


this uses code that sleepyz used in a code, to find the bar closest to the desired time.
so if there is no bar at 6am, it will find the next bar.
https://usethinkscript.com/threads/vertical-line-and-opening-price.11725/#post-101368

Code:
#ohlc_lines_from_time_period

#https://usethinkscript.com/threads/daily-4hr-ohlc-6am-to-10am.21619/
#Daily 4hr OHLC - 6am to 10am
#chris_dc  10/9

# mark the daily 4hr candle,
#  from 6am - 10am pst
#   with the OHLC lines
#    and the lines stay there until 9:59am of the next day
#     then the new levels would be marked out.

# options:
#   the previous levels stay on the graph  or only show the most recent one


def na = double.nan;
def bn = barnumber();

input start = 0600;
input end = 1000;
#def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;

def period = (secondsfromTime(start) >= 0 and secondstillTime(end) > 0);

def n = 800;
def big = 99999;

def o;
def h;
def l;
def c;
def cnt;
if bn == 1 then {
 o = na;
 h = na;
 l = na;
 c = na;
 cnt = 0;
} else if (!period[1] and period) then {
# 1st bar of period, find price levels
o = open;

h = fold a2 = 0 to n
  with b2
  while getvalue(period, -a2)
  do max(b2, getvalue(high, -a2));

l = fold a3 = 0 to n
  with b3 = big
  while getvalue(period, -a3)
  do min(b3, getvalue(low, -a3));

c = fold a4 = 0 to n
  with b4 = big
  while getvalue(period, -a4)
  do getvalue(close, -a4);

cnt = cnt[1] + 1;

} else {
o = o[1];
h = h[1];
l = l[1];
c = c[1];
cnt = cnt[1];
}

input only_last_period = yes;
def cnthi = highestall(cnt);
def last = (only_last_period and cnthi == cnt);

def lines = if !only_last_period then 1
 else if last then 1
 else 0;
 

plot zo = if lines then o else na;
plot zh = if lines then h else na;
plot zl = if lines then l else na;
plot zc = if lines then c else na;

zo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
zc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

zo.SetDefaultColor(Color.orange);
zh.SetDefaultColor(Color.green);
zl.SetDefaultColor(Color.red);
zc.SetDefaultColor(Color.blue);


# bubbles , line labels
def bubble_off = 0;

addchartbubble(
(lines and period[bubble_off] and open[bubble_off] == o),
o,
"OPEN",
zo.TakeValueColor(),
(if close > open then no else yes)
);

addchartbubble(
(lines and period[bubble_off] and high[bubble_off] == h),
h,
"HIGH",
zh.TakeValueColor(),
yes
);


addchartbubble(
(lines and period[bubble_off] and low[bubble_off] == l),
l,
"LOW",
zl.TakeValueColor(),
no
);


addchartbubble(
(lines and period[bubble_off] and close[bubble_off] == c),
c,
"CLOSE",
zc.TakeValueColor(),
(if close > open then no else yes)
);


input show_start_stop_vert_lines = yes;
addverticalline(show_start_stop_vert_lines and (!period[1] and period), "-", color.cyan);
addverticalline(show_start_stop_vert_lines and (period and !period[-1]), "-", color.cyan);


# ----------------------------
# test stuff

#addchartbubble(1, low, c, color.yellow,no);
#addverticalline(period, "-");

#addverticalline((!period[1] and period), "first", color.yellow);


addchartbubble(
0, low,
lines + "\n" +
period[bubble_off] + "\n" +
(open[bubble_off] == o) + "\n"
, color.cyan, no);
#
 

Attachments

  • img1.JPG
    img1.JPG
    65 KB · Views: 14
Last edited:
Solution

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