Help Identifying Bars Where A Given Value Is True

bri8htf1y

New member
Hi all,

I often trade with price as percentage and need my charts set with RTH open as 0%. It seems the only option is to select a bar with the nearest closing price and Set Bar To 0% - and this must be done every time the timeframe changes. Usually not a big deal, but can be distracting in fast markets. Sooo...

I'm trying to add a script that paints the most proximate bar a bright color ( the bar that closes nearest to the RTH open price ). But for this method to work, the "zero bar" obviously can't be the RTH open bar , and it should select from all bars with a close on the chart including premarket bars.

Any help would be greatly appreciated!

Here's where I am:

input ShowZeroBars = yes;

def NA = Double.NaN;
def Bar = Barnumber();
def Today = GetLastDay() == GetDay();

def RTHOpen = if SecondsTillTime(0930) == 0 then open else RTHOpen[1];
def RTHO = RTHOpen;


def AllDay = Today && SecondsFromTime(0400) >= 0 && SecondsTillTime(1600) > 0;
def DayBar = if AllDay && !IsNaN(close) then Bar else NA;

def Proximity = if DayBar then AbsValue(close - RTHO) else Proximity[1];
def Proximate = LowestAll(Proximity);
def ProxBar = if Proximate == Proximity then Bar else NA;

def ZeroBar = close == RTHO;

AssignPriceColor(if ShowZeroBars && ProxBar then Color.CYAN else if close > open then Color.UPTICK else Color.DOWNTICK);
 
Solution
@bri8htf1y I keep rereading your post trying to understand what you are looking for.
Took a shot at it, came up with two bars, but they do have the same close.

Ruby:
input ShowZeroBars = yes;

def NA = Double.NaN;
def Bar = if !IsNaN(close) and BarNumber()>0 then BarNumber() else Bar[1];
def HBar = HighestAll(Bar);
def Today = GetLastDay() == GetDay();
def RTHOpen = if SecondsTillTime(0930) == 0 then open else RTHOpen[1];
def RTHOpenBN = if SecondsTillTime(0930) == 0 then Bar else RTHOpen[1];
def NotOpenBar = Bar <> RTHOpenBN;
def NotCurrentBar = Bar <> HBar;
def RTHO = RTHOpen;
def AllDay = Today && SecondsFromTime(0400) >= 0 && SecondsTillTime(1600) > 0;
def DayBar = if AllDay && !IsNaN(close) then Bar else NA;
def...
@bri8htf1y I keep rereading your post trying to understand what you are looking for.
Took a shot at it, came up with two bars, but they do have the same close.

Ruby:
input ShowZeroBars = yes;

def NA = Double.NaN;
def Bar = if !IsNaN(close) and BarNumber()>0 then BarNumber() else Bar[1];
def HBar = HighestAll(Bar);
def Today = GetLastDay() == GetDay();
def RTHOpen = if SecondsTillTime(0930) == 0 then open else RTHOpen[1];
def RTHOpenBN = if SecondsTillTime(0930) == 0 then Bar else RTHOpen[1];
def NotOpenBar = Bar <> RTHOpenBN;
def NotCurrentBar = Bar <> HBar;
def RTHO = RTHOpen;
def AllDay = Today && SecondsFromTime(0400) >= 0 && SecondsTillTime(1600) > 0;
def DayBar = if AllDay && !IsNaN(close) then Bar else NA;
def Proximity = if DayBar and NotOpenBar then AbsValue(close - RTHO) else Proximity[1];
def Proximate = LowestAll(Proximity);
def ProxBar = if Proximate == Proximity then Bar else NA;
def ZeroBar = close == RTHO && NotCurrentBar;

Addchartbubble(Bar == RTHOpenBn, high, "Open Bar", color.white);
Addchartbubble(ProxBar && NotOpenBar && NotCurrentBar, high, "Proximate Bar",color.white);
Addchartbubble(ShowZeroBars && ZeroBar, high, "Zero Bar",color.white);

AssignPriceColor(if ProxBar && NotOpenBar && NotCurrentBar then Color.PINK else Color.CURRENT);
AssignPriceColor(if Bar == RTHOpenBn then Color.CYAN else Color.CURRENT);
AssignPriceColor(if ShowZeroBars && ZeroBar then Color.BLUE else Color.CURRENT);

def OpenBar = fold c = 0 to HBar while !IsNaN(GetValue(RTHOpen, -c)) do GetValue(RTHOpen, -c);
plot BarOpen = if OpenBar > 0 then OpenBar else Double.NaN;
BarOpen.SetStyle(Curve.LONG_DASH);
BarOpen.AssignValueColor(Color.LIME);
 
Solution
@Svanoy Thanks for taking a look at this - that's definitely the idea. One last piece to this-

I find that the day often opens at the close of the last premarket bar, or on/near
a retest of a PM S/R. Also, in the first minutes of RTH when the action is
fastest and I could use this tool the most, almost all bars are still in the PM, and
the price often moves quickly away.

Is there a way to add the previous bars (premarket) into the mix?

( i.e., the 16th bar on the chart appears to close right on the RTHO plot.. )
 
@bri8htf1y yeah premarket was what I was struggling with, maybe got it worked out.
Ruby:
input ShowZeroBars = yes;

def NA = Double.NaN;
def Bar = if !IsNaN(close) and BarNumber()>0 then BarNumber() else Bar[1];
def HBar = HighestAll(Bar);
def Today = GetLastDay() == GetDay();
def RTHOpen = if SecondsFromTime(0930) >= 0 && SecondsTillTime(0930)[1] > 0 then open else RTHOpen[1];
def RTHOpenBN = if SecondsFromTime(0930) >= 0 && SecondsTillTime(0930)[1] > 0 then Bar else RTHOpen[1];
def NotOpenBar = Bar <> RTHOpenBN;
def NotCurrentBar = Bar <> HBar;
def AllDay = Today && SecondsFromTime(0400) >= 0 && SecondsTillTime(1600) > 0;
def DayBar = if AllDay && !IsNaN(close) then Bar else NA;
def OpenBarValue = fold c = 0 to HBar while !IsNaN(GetValue(RTHOpen, -c)) && DayBar do GetValue(RTHOpen, -c);
def RTHO = OpenBarValue;
def ZeroBar = close == RTHO && NotCurrentBar;
def Proximity = if DayBar && NotOpenBar && !ZeroBar then AbsValue(close - RTHO) else Proximity[1];
def Proximate = LowestAll(Proximity);
def ProxBar = if Proximate == Proximity then Bar else NA;

plot BarOpen = if OpenBarValue > 0 then OpenBarValue else NA;
BarOpen.SetStyle(Curve.LONG_DASH);
BarOpen.AssignValueColor(Color.LIME);

AddVerticalLine(!IsNaN(DayBar) && IsNaN(DayBar[1]), "PreMarket", Color.ORANGE, curve.SHORT_DASH);
AddVerticalLine(IsNaN(DayBar) && !IsNaN(DayBar[1]) && !IsNaN(close), "EOD", Color.ORANGE, curve.SHORT_DASH);

Addchartbubble(Bar == RTHOpenBn && DayBar, high, "Open Bar", color.white);
Addchartbubble(ProxBar && NotOpenBar && NotCurrentBar && !ZeroBar && RTHO > 0, high, "Proximate Bar",color.white);
Addchartbubble(ShowZeroBars && ZeroBar, high, "Zero Bar",color.white);

AssignPriceColor(if ProxBar && NotOpenBar && NotCurrentBar && RTHO > 0 then Color.PINK else Color.CURRENT);
AssignPriceColor(if Bar == RTHOpenBn && DayBar then Color.CYAN else Color.CURRENT);
AssignPriceColor(if ShowZeroBars && ZeroBar then Color.BLUE else Color.CURRENT);
 
That works. Its as easy as just scanning down the line for obvious candidates.

I actually tailored your first code a bit to include a manual input for the daily open price (normally try to automate everything but this only has to be entered once so no biggy)

Now it paints the most proximate bar anywhere on the chart. Here's an example with two choices, one uptrending (Cyan) and one
down (Blue):

2022-09-10-FLEXIBLE-GRID.png


Still stumped on how to get the coded value to act as the simple input, but this will work fine for trading. Thanks again for getting me unstuck my friend.

Here's the current code:


input ShowZeroBars = yes;
input ZeroPrice = 14.26; # For example (would only have to enter daily open once when known)

def NA = Double.NaN;
def Bar = if !IsNaN(close) and BarNumber() > 0 then BarNumber() else Bar[1];
def Today = GetLastDay() == GetDay();
def HBar = HighestAll(Bar);
def NotCurrentBar = Bar <> HBar;
def DayBar = if Today then Bar else NA;
def Proximity = if DayBar then AbsValue(close - ZeroPrice) else Proximity[1];
def Proximate = LowestAll(Proximity);
def ProxBar = if Proximate == Proximity then Bar else NA;
def ZeroBar = close == ZeroPrice && NotCurrentBar;

defineGlobalColor("UP", Color.WHITE);
defineGlobalColor("DOWN", Color.BLACK);

AssignPriceColor(if ShowZeroBars && ProxBar && close > open then Color.CYAN else if ShowZeroBars && ProxBar && close < open then Color.BLUE else if close > open then GlobalColor("UP") else GlobalColor("DOWN"));
 

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