Auto horizontal line at the close of the first 5m candle after the open

azakusa

Member
Hello fellow traders and coders.

Looking to create an simple indicator that automatically draws a horizontal line at the close of the first 5 minute candle after the open.

I have searched through everything related I could find on this site and most of the auto draw scripts for horizontal lines seem far more complicated than I think necessary. Often involving MAs and HL averages.

Could anyone out there point me in the right direction on this?

Many thanks in advance.
 
Last edited:
See if this works

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 cl = if IsNaN(close)
then cl[1]
else If(!IsNaN(close) and GetAggregationPeriod() <= AggregationPeriod.FIFTEEN_MIN and
x != x[1] ,
close,
cl[1]);
plot Close = cl;
Close.SetDefaultColor (Color.YELLOW);
Close.SetPaintingStrategy(PaintingStrategy.DASHES);
 
Hello fellow traders and coders.

Looking to create an simple indicator that automatically draws a horizontal line at the close of the first 5 minute candle after the open.

I have searched through everything related I could find on this site and most of the auto draw scripts for horizontal lines seem far more complicated than I think necessary. Often involving MAs and HL averages.


this will draw a line, from the close of 1 of the bars during normal trading hours
pick a 2nd agg time, then pick a number for agg bar multiple

ex.
pick 2nd agg time = 15min
chart time = 5 min
agg ratio is 15/5 = 3
pick a 2nd agg bar of day = 4
3 * 4 = 12th bar of day

there are labels to show the bar number calcs

Code:
# line_from_agg_bar_0

# https://usethinkscript.com/threads/auto-horizontal-line-at-the-close-of-the-first-5m-candle-after-the-open.13337/
# Auto horizontal line at the close of the first 5m candle after the open

def na = double.nan;
def bn = barnumber();
def diffday = if getday() != getday()[1] then 1 else 0;

def start = 0930;
input agg_time = AggregationPeriod.five_min;
def aggmin = agg_time/(1000*60);
def cls = close(period = agg_time);
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
def aggratio = (aggmin / chartmin);

input aggbar_of_day = 1;
def bar = ((aggbar_of_day - 0 ) * (aggmin / chartmin));
def aggbar2 = bar - 1;
def agg2min = aggbar2 * chartmin;
def time4 = if secondsfromTime(start) == (agg2min*60) then 1 else 0;

input vert_line = no;
addverticalline(vert_line and time4, "---", color.yellow);

input show_arrow = yes;
plot zz = if show_arrow and time4 then low*0.997 else na;
zz.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zz.SetDefaultColor(Color.cyan);
zz.setlineweight(3);
zz.hidebubble();

input price = close;
def level4 = if bn == 1 then na
  else if diffday and time4 then price
  else if diffday then na
  else if time4 then price
  else level4[1];

plot z = level4;
z.SetDefaultColor(Color.cyan);
#z.setlineweight(1);
z.hidebubble();
z.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#---------------------------

input test_labels = yes;
addlabel(test_labels, "  ", color.black);
addlabel(test_labels, "close from xth agg bar /day", color.yellow);
addlabel(test_labels, "agg min " + aggmin, color.yellow);
addlabel(test_labels, "/", color.yellow);
addlabel(test_labels, "chart min " + chartmin, color.yellow);
addlabel(test_labels, "=", color.yellow);
addlabel(test_labels, "agg ratio " + aggratio, color.yellow);
addlabel(test_labels, "*", color.yellow);
addlabel(test_labels, "aggbar_of_day " +  aggbar_of_day, color.yellow);
addlabel(test_labels, "=", color.yellow);
addlabel(test_labels, "bar " + bar, color.yellow);
addlabel(test_labels, "  ", color.black);


addchartbubble(0, low*0.995,
 aggbar_of_day + "\n" +
 aggmin + "\n" +
 chartmin + "\n" +
 bar + "\n" +
 (secondsfromTime(start)/60)
, color.yellow, no);
#


ODwixzp.jpg


JHxaD4U.jpg
 
Last edited:
@halcyonguy
I've been hacking around within the code to try to limit painting this horizontal line to current intraday session only, but no joy.

Could you possibly provide some clues as to which line to edit? I am not a coder so even adding a "second aggtime" as you mention is incomprehensible to my small brain. My guess is that its under line 14 getday syntax.
 
@halcyonguy
Thanks again for this script - its working well.
Wondering if there is a way to limit the drawing to the current intraday session only? Not in any previous days.

Many thanks again,

no need for duplicate posts, within a day

here is an updated study that,
...lets you pick to see things drawn on just the current day , or all days
...fixed , if desired bar was also bar #1 on chart, it didn't draw a line.


i added this
Code:
# is last day the current day
def istoday = if GetLastDay() == GetDay() then 1 else 0;
input show_only_today = yes;
# draw only on current day  yes/no
def days_enable = if show_only_today then istoday else 1;
then added the variable to the plot formulas


Code:
# line_from_agg_bar_01

# add - choose today only
# fix - first bar doesnt have a line

# https://usethinkscript.com/threads/auto-horizontal-line-at-the-close-of-the-first-5m-candle-after-the-open.13337/
# Auto horizontal line at the close of the first 5m candle after the open

#def aggbar = 0;

def na = double.nan;
def bn = barnumber();
def diffday = if getday() != getday()[1] then 1 else 0;


# is last day the current day
def istoday = if GetLastDay() == GetDay() then 1 else 0;
input show_only_today = yes;
# draw only on current day  yes/no
def days_enable = if show_only_today then istoday else 1;


def start = 0930;
input agg_time = AggregationPeriod.five_min;
def aggmin = agg_time/(1000*60);
def cls = close(period = agg_time);
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
def aggratio = (aggmin / chartmin);

input aggbar_of_day = 1;
def bar = ((aggbar_of_day - 0 ) * (aggmin / chartmin));
def aggbar2 = bar - 1;
def agg2min = aggbar2 * chartmin;
def time4 = if secondsfromTime(start) == (agg2min*60) then 1 else 0;

input vert_line = no;
addverticalline(vert_line and time4, "---", color.yellow);

input show_arrow = yes;
plot zz = if show_arrow and days_enable and time4 then low*0.997 else na;
zz.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zz.SetDefaultColor(Color.cyan);
zz.setlineweight(3);
zz.hidebubble();

input price = close;
def level4 = if bn == 1 and !time4 then na
  else if diffday and time4 then price
  else if diffday then na
  else if time4 then price
  else level4[1];

plot z = if days_enable then level4 else na;
z.SetDefaultColor(Color.cyan);
#z.setlineweight(1);
z.hidebubble();
z.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#---------------------------

input test_labels = yes;
addlabel(test_labels, "  ", color.black);
addlabel(test_labels, "close from xth agg bar /day", color.yellow);
addlabel(test_labels, "agg min " + aggmin, color.yellow);
addlabel(test_labels, "/", color.yellow);
addlabel(test_labels, "chart min " + chartmin, color.yellow);
addlabel(test_labels, "=", color.yellow);
addlabel(test_labels, "agg ratio " + aggratio, color.yellow);
addlabel(test_labels, "*", color.yellow);
addlabel(test_labels, "aggbar_of_day " +  aggbar_of_day, color.yellow);
addlabel(test_labels, "=", color.yellow);
addlabel(test_labels, "bar " + bar, color.yellow);
addlabel(test_labels, "  ", color.black);


addchartbubble(0, low*0.995,
 aggbar_of_day + "\n" +
 aggmin + "\n" +
 chartmin + "\n" +
 bar + "\n" +
 (secondsfromTime(start)/60)
, color.yellow, no);

#
 
no need for duplicate posts, within a day

here is an updated study that,
...lets you pick to see things drawn on just the current day , or all days
...fixed , if desired bar was also bar #1 on chart, it didn't draw a line.


i added this
Code:
# is last day the current day
def istoday = if GetLastDay() == GetDay() then 1 else 0;
input show_only_today = yes;
# draw only on current day  yes/no
def days_enable = if show_only_today then istoday else 1;
then added the variable to the plot formulas


Code:
# line_from_agg_bar_01

# add - choose today only
# fix - first bar doesnt have a line

# https://usethinkscript.com/threads/auto-horizontal-line-at-the-close-of-the-first-5m-candle-after-the-open.13337/
# Auto horizontal line at the close of the first 5m candle after the open

#def aggbar = 0;

def na = double.nan;
def bn = barnumber();
def diffday = if getday() != getday()[1] then 1 else 0;


# is last day the current day
def istoday = if GetLastDay() == GetDay() then 1 else 0;
input show_only_today = yes;
# draw only on current day  yes/no
def days_enable = if show_only_today then istoday else 1;


def start = 0930;
input agg_time = AggregationPeriod.five_min;
def aggmin = agg_time/(1000*60);
def cls = close(period = agg_time);
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);
def aggratio = (aggmin / chartmin);

input aggbar_of_day = 1;
def bar = ((aggbar_of_day - 0 ) * (aggmin / chartmin));
def aggbar2 = bar - 1;
def agg2min = aggbar2 * chartmin;
def time4 = if secondsfromTime(start) == (agg2min*60) then 1 else 0;

input vert_line = no;
addverticalline(vert_line and time4, "---", color.yellow);

input show_arrow = yes;
plot zz = if show_arrow and days_enable and time4 then low*0.997 else na;
zz.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
zz.SetDefaultColor(Color.cyan);
zz.setlineweight(3);
zz.hidebubble();

input price = close;
def level4 = if bn == 1 and !time4 then na
  else if diffday and time4 then price
  else if diffday then na
  else if time4 then price
  else level4[1];

plot z = if days_enable then level4 else na;
z.SetDefaultColor(Color.cyan);
#z.setlineweight(1);
z.hidebubble();
z.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#---------------------------

input test_labels = yes;
addlabel(test_labels, "  ", color.black);
addlabel(test_labels, "close from xth agg bar /day", color.yellow);
addlabel(test_labels, "agg min " + aggmin, color.yellow);
addlabel(test_labels, "/", color.yellow);
addlabel(test_labels, "chart min " + chartmin, color.yellow);
addlabel(test_labels, "=", color.yellow);
addlabel(test_labels, "agg ratio " + aggratio, color.yellow);
addlabel(test_labels, "*", color.yellow);
addlabel(test_labels, "aggbar_of_day " +  aggbar_of_day, color.yellow);
addlabel(test_labels, "=", color.yellow);
addlabel(test_labels, "bar " + bar, color.yellow);
addlabel(test_labels, "  ", color.black);


addchartbubble(0, low*0.995,
 aggbar_of_day + "\n" +
 aggmin + "\n" +
 chartmin + "\n" +
 bar + "\n" +
 (secondsfromTime(start)/60)
, color.yellow, no);

#
@halcyonguy
Thanks tons!! Sorry for my persistence, I just thought you had missed it. Again, thank you thank you thank you!!!
 

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