# What line of code to use for recording the highs of each bar marked in the script?

#### jngy2k

##### Member
Code:
``````declare hide_on_daily;
declare once_per_bar;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
AddChartBubble(firstBarOfDay , close, "Fb", Color.white, yes);``````

The above script registers each bar at the start of each trading day on the chart as firstBarofDay
Whether the chart is in 5min, 15min, hourly, etc. the firstbar of each trading day will be captured.

Now, I'm trying to capture the value of each bar, specifically the High and the Low.

My feeble attempt below
Code:
``````def MOHigh = if firstbarofday[1] == 0 AND firstbarofday == 1 then high else MOHigh[1];
def MOLow = if firstbarofday[1] == 0 AND firstbarofday == 1 then low else MOLow[1];``````
This only captures the high and low of the firstbaroftheday for the latest bar.
Question: How would I capture the high and low of each firstbarofday bars in the chart?

The Goal: My purpose for doing so is to find the range (high-low) of each firstbarofday and compare it to today's firstbarofday.
What I will have to do after registering the High and Low values in the script is to find the Average Range of all the firstbarofday in the chart.
So thats Question 2: How do I find the Average Range of all the firstbarofday?
My feeble attempt below
Code:
``````def avgmo = average(mohigh,2)-average(molow,2);
AddLabel (yes, "AvgOR: " + round(avgmo,2), color.white);``````

Code:
``````declare hide_on_daily;
declare once_per_bar;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
AddChartBubble(firstBarOfDay , close, "Fb", Color.white, yes);``````

The above script registers each bar at the start of each trading day on the chart as firstBarofDay
Whether the chart is in 5min, 15min, hourly, etc. the firstbar of each trading day will be captured.

Now, I'm trying to capture the value of each bar, specifically the High and the Low.

My feeble attempt below
Code:
``````def MOHigh = if firstbarofday[1] == 0 AND firstbarofday == 1 then high else MOHigh[1];
def MOLow = if firstbarofday[1] == 0 AND firstbarofday == 1 then low else MOLow[1];``````
This only captures the high and low of the firstbaroftheday for the latest bar.
Question: How would I capture the high and low of each firstbarofday bars in the chart?

The Goal: My purpose for doing so is to find the range (high-low) of each firstbarofday and compare it to today's firstbarofday.
What I will have to do after registering the High and Low values in the script is to find the Average Range of all the firstbarofday in the chart.
So thats Question 2: How do I find the Average Range of all the firstbarofday?
My feeble attempt below
Code:
``````def avgmo = average(mohigh,2)-average(molow,2);
AddLabel (yes, "AvgOR: " + round(avgmo,2), color.white);``````

See if this helps

Code:
``````declare hide_on_daily;
declare once_per_bar;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else Double.NaN;
def firstbarcount = if !IsNaN(firstBarOfDay) then firstbarcount[1] + 1 else firstbarcount[1];
AddChartBubble(firstBarOfDay , close, "Fb: " + firstbarcount, Color.WHITE, yes);
def mohigh = if !IsNaN(firstBarOfDay) then mohigh[1] + high else mohigh[1];
AddLabel(1, "H: " + mohigh, color.green);
def molow = if !IsNaN(firstBarOfDay) then molow[1] + low else molow[1];
AddLabel(1, "L: " + molow, color.red);
def avgmo = (mohigh - molow) / firstbarcount;
AddLabel(1, "AvgOR: " + AsText(avgmo), Color.WHITE);``````

@SleepyZ I'll lok at it in the morning. Currently at work. Thanks for the help.
@SleepyZ Just reading thru it and imaging the code work
I can't believe I didn't think of registering the bar count.
That's great work.

edit: my revision and how i'll use it
https://books.mec.biz/tmp/books/KSPF578WWBQYG4VRFYV6.pdf
toby crabel is a billionaire hedge fund manager

Code:
``````#Average First Bar of Day range on chart
declare hide_on_daily;
declare once_per_bar;
input showMO = yes;
input showBubble = no;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());

def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else Double.NaN;

def firstbarcount = if !IsNaN(firstBarOfDay) then firstbarcount[1] + 1 else firstbarcount[1];

def mohightotal = if !IsNaN(firstBarOfDay) then mohightotal[1] + high else mohightotal[1];
def molowtotal = if !IsNaN(firstBarOfDay) then molowtotal[1] + low else molowtotal[1];

def avgmo = (mohightotal - molowtotal) / firstbarcount;
AddLabel(showmo, "MO: " + AsText(avgmo), Color.yellow);
AddChartBubble(if showbubble then firstBarOfDay else double.nan, low, firstbarcount, Color.yellow, no);``````

Last edited:

87k+ Posts
364 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?