Show only the last occurrence of a condition

argim

New member
Hi, Say I have a study showing the highest high in the last X bars (drawings horizontal line)... That would fill my screen with many lines where the condition is satisfied. My question is what code should be added to show only the most recent line...

The highest high is only an example of course...

thanks
A
 
Here's an example. This keeps track of swing highs and lows, and there's a section (lines 52-55) that increments when it finds a new swing low or high, then keeps that value until a new one is plotted. Something like this could work well for what you're talking about.

The only downside is it uses the HighestAll() function, which means it will only update once per bar, so if you need something that updates while bars are forming I would not recommend this method.


Ruby:
declare upper;

# USER INPUTS
input show_points = no;
input dot_size = 1;
input dot_distance = 2;
input show_lines = yes;
input price_type = {default High_Low, Open_Close, Close_Only};
input swing_back = 9;
input swing_forward = 9;
input maxbars = 500;
input show_labels = no;
input show_only_last_swing = no;


# GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));


# CALCULATIONS
def sb = swing_back;
def sf = swing_forward;
def na = Double.NaN;

def PriceHigh =
    if price_type == price_type.High_Low then high
    else if price_type == price_type.Open_Close then Max(open, close)
    else if price_type == price_type.Close_Only then close
    else close
;
def PriceLow =
    if price_type == price_type.High_Low then low
    else if price_type == price_type.Open_Close then Min(open, close)
    else if price_type == price_type.Close_Only then close
    else close
;

def lfor = Lowest(PriceLow, sf)[-sf];
def lback = Lowest(PriceLow, sb)[1];
def hfor = Highest(PriceHigh, sf)[-sf];
def hback = Highest(PriceHigh, sb)[1];
def swinglow = if PriceLow < lfor and PriceLow <= lback then yes else no;
def swinghigh = if PriceHigh > hfor and PriceHigh >= hback then yes else no;

def lsl = if IsNaN(close[-sf]) then lsl[1] else if swinglow then PriceLow else lsl[1];
def lsh = if swinghigh then PriceHigh else lsh[1];
def lcount = if swinglow then 1 else lcount[1] + 1;
def hcount = if swinghigh then 1 else hcount[1] + 1;

def slcount = if swinglow then slcount[1] + 1 else slcount[1];
def shcount = if swinghigh then shcount[1] + 1 else shcount[1];
def onlylastslplotbar = slcount == HighestAll(slcount);
def onlylastshplotbar = shcount == HighestAll(shcount);


def lastlowdata =
    if lcount <= maxbars and IsNaN(close[-sf]) then lsl[1]
    else if lcount > maxbars then na
    else if lcount < 2 then na
    else lsl;
def lasthighdata =
    if hcount <= maxbars and IsNaN(close[-sf]) then lsh[1]
    else if hcount > maxbars then na
    else if hcount < 2 then na
    else lsh;


# PLOTS
plot sl = if swinglow then PriceLow - (dot_distance * TickSize()) else na;
plot sh = if swinghigh then PriceHigh + (dot_distance * TickSize()) else na;
plot lastlow =
    if show_only_last_swing then
        if onlylastslplotbar then
            lastlowdata
        else na
    else
        lastlowdata;
plot lasthigh =
    if show_only_last_swing then
        if onlylastshplotbar then
            lasthighdata
        else na
    else
        lasthighdata;


# FORMATTING
sh.SetStyle(curve.points);
sh.SetLineWeight(dot_size);
sh.assignvaluecolor(if SH > lasthigh[1] then GlobalColor("Green") else GlobalColor("Red"));
sh.SetHiding(show_points == No);
sh.HideBubble();
sh.HideTitle();

sl.SetStyle(curve.points);
sl.SetLineWeight(dot_size);
sl.assignvaluecolor(if SL < lastlow[1] then GlobalColor("Red") else GlobalColor("Green"));
sl.SetHiding(show_points == No);
sl.HideBubble();
sl.HideTitle();

lasthigh.SetStyle(curve.SHORT_DASH);
lasthigh.AssignValueColor(GlobalColor("Gray"));
lasthigh.SetHiding(show_lines == No);
lasthigh.HideBubble();
lasthigh.HideTitle();

lastlow.SetStyle(curve.sHORT_DASH);
lastlow.AssignValueColor(GlobalColor("Gray"));
lastlow.SetHiding(show_lines == No);
lastlow.HideBubble();
lastlow.HideTitle();
 
Here's an example. This keeps track of swing highs and lows, and there's a section (lines 52-55) that increments when it finds a new swing low or high, then keeps that value until a new one is plotted. Something like this could work well for what you're talking about.

The only downside is it uses the HighestAll() function, which means it will only update once per bar, so if you need something that updates while bars are forming I would not recommend this method.


Ruby:
declare upper;

# USER INPUTS
input show_points = no;
input dot_size = 1;
input dot_distance = 2;
input show_lines = yes;
input price_type = {default High_Low, Open_Close, Close_Only};
input swing_back = 9;
input swing_forward = 9;
input maxbars = 500;
input show_labels = no;
input show_only_last_swing = no;


# GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));


# CALCULATIONS
def sb = swing_back;
def sf = swing_forward;
def na = Double.NaN;

def PriceHigh =
    if price_type == price_type.High_Low then high
    else if price_type == price_type.Open_Close then Max(open, close)
    else if price_type == price_type.Close_Only then close
    else close
;
def PriceLow =
    if price_type == price_type.High_Low then low
    else if price_type == price_type.Open_Close then Min(open, close)
    else if price_type == price_type.Close_Only then close
    else close
;

def lfor = Lowest(PriceLow, sf)[-sf];
def lback = Lowest(PriceLow, sb)[1];
def hfor = Highest(PriceHigh, sf)[-sf];
def hback = Highest(PriceHigh, sb)[1];
def swinglow = if PriceLow < lfor and PriceLow <= lback then yes else no;
def swinghigh = if PriceHigh > hfor and PriceHigh >= hback then yes else no;

def lsl = if IsNaN(close[-sf]) then lsl[1] else if swinglow then PriceLow else lsl[1];
def lsh = if swinghigh then PriceHigh else lsh[1];
def lcount = if swinglow then 1 else lcount[1] + 1;
def hcount = if swinghigh then 1 else hcount[1] + 1;

def slcount = if swinglow then slcount[1] + 1 else slcount[1];
def shcount = if swinghigh then shcount[1] + 1 else shcount[1];
def onlylastslplotbar = slcount == HighestAll(slcount);
def onlylastshplotbar = shcount == HighestAll(shcount);


def lastlowdata =
    if lcount <= maxbars and IsNaN(close[-sf]) then lsl[1]
    else if lcount > maxbars then na
    else if lcount < 2 then na
    else lsl;
def lasthighdata =
    if hcount <= maxbars and IsNaN(close[-sf]) then lsh[1]
    else if hcount > maxbars then na
    else if hcount < 2 then na
    else lsh;


# PLOTS
plot sl = if swinglow then PriceLow - (dot_distance * TickSize()) else na;
plot sh = if swinghigh then PriceHigh + (dot_distance * TickSize()) else na;
plot lastlow =
    if show_only_last_swing then
        if onlylastslplotbar then
            lastlowdata
        else na
    else
        lastlowdata;
plot lasthigh =
    if show_only_last_swing then
        if onlylastshplotbar then
            lasthighdata
        else na
    else
        lasthighdata;


# FORMATTING
sh.SetStyle(curve.points);
sh.SetLineWeight(dot_size);
sh.assignvaluecolor(if SH > lasthigh[1] then GlobalColor("Green") else GlobalColor("Red"));
sh.SetHiding(show_points == No);
sh.HideBubble();
sh.HideTitle();

sl.SetStyle(curve.points);
sl.SetLineWeight(dot_size);
sl.assignvaluecolor(if SL < lastlow[1] then GlobalColor("Red") else GlobalColor("Green"));
sl.SetHiding(show_points == No);
sl.HideBubble();
sl.HideTitle();

lasthigh.SetStyle(curve.SHORT_DASH);
lasthigh.AssignValueColor(GlobalColor("Gray"));
lasthigh.SetHiding(show_lines == No);
lasthigh.HideBubble();
lasthigh.HideTitle();

lastlow.SetStyle(curve.sHORT_DASH);
lastlow.AssignValueColor(GlobalColor("Gray"));
lastlow.SetHiding(show_lines == No);
lastlow.HideBubble();
lastlow.HideTitle();
Unfortunately, it shows all the occurrences. At any time I want to show only the last one or "active"
1688204532759.png
 
Unfortunately, it shows all the occurrences. At any time I want to show only the last one or "active" View attachment 19012

The script is modified with the following to limit the plot of the lines

Code:
input limit_plot = yes;
input plot_limited_to_last_x = 1;
def cond_h      = if !IsNaN(sh) then 1 else Double.NaN;
def dataCount_h = CompoundValue(1, if !IsNaN(cond_h) then dataCount_h[1] + 1 else dataCount_h[1], 0);
def cond_l      = if !IsNaN(sl) then 1 else Double.NaN;
def dataCount_l = CompoundValue(1, if !IsNaN(cond_l) then dataCount_l[1] + 1 else dataCount_l[1], 0);

plot lastlow =
    if limit_plot then
       if HighestAll(dataCount_l) - dataCount_l <= plot_limited_to_last_x - 1 then
          lastlowdata
        else na
    else lsl;

plot lasthigh =
    if limit_plot then
       if HighestAll(dataCount_h) - dataCount_h <= plot_limited_to_last_x - 1 then
          lasthighdata
        else na       
    else
        lasthighdata;

Here is the full modified script

Screenshot 2023-07-15 141113.png
Code:
# USER INPUTS
input show_points = no;
input dot_size = 1;
input dot_distance = 2;
input show_lines = yes;
input price_type = {default High_Low, Open_Close, Close_Only};
input swing_back = 9;
input swing_forward = 9;
input maxbars = 500;
input show_labels = no;
input limit_plot = yes;
input plot_limited_to_last_x = 1;


# GLOBAL COLOR DEFINITIONS
DefineGlobalColor("Green", CreateColor(0, 155, 0));
DefineGlobalColor("Red", CreateColor(225, 105, 105));
DefineGlobalColor("Gray", CreateColor(192, 192, 192));


# CALCULATIONS
def sb = swing_back;
def sf = swing_forward;
def na = Double.NaN;

def PriceHigh =
    if price_type == price_type.High_Low then high
    else if price_type == price_type.Open_Close then Max(open, close)
    else if price_type == price_type.Close_Only then close
    else close
;
def PriceLow =
    if price_type == price_type.High_Low then low
    else if price_type == price_type.Open_Close then Min(open, close)
    else if price_type == price_type.Close_Only then close
    else close
;

def lfor = Lowest(PriceLow, sf)[-sf];
def lback = Lowest(PriceLow, sb)[1];
def hfor = Highest(PriceHigh, sf)[-sf];
def hback = Highest(PriceHigh, sb)[1];
def swinglow = if PriceLow < lfor and PriceLow <= lback then yes else no;
def swinghigh = if PriceHigh > hfor and PriceHigh >= hback then yes else no;

def lsl = if IsNaN(close[-sf]) then lsl[1] else if swinglow then PriceLow else lsl[1];
def lsh = if swinghigh then PriceHigh else lsh[1];
def lcount = if swinglow then 1 else lcount[1] + 1;
def hcount = if swinghigh then 1 else hcount[1] + 1;

def slcount = if swinglow then slcount[1] + 1 else slcount[1];
def shcount = if swinghigh then shcount[1] + 1 else shcount[1];
def onlylastslplotbar = slcount == HighestAll(slcount);
def onlylastshplotbar = shcount == HighestAll(shcount);


def lastlowdata =
    if lcount <= maxbars and IsNaN(close[-sf]) then lsl[1]
    else if lcount > maxbars then na
    else if lcount < 2 then na
    else lsl;
def lasthighdata =
    if hcount <= maxbars and IsNaN(close[-sf]) then lsh[1]
    else if hcount > maxbars then na
    else if hcount < 2 then na
    else lsh;


# PLOTS
plot sl = if swinglow then PriceLow - (dot_distance * TickSize()) else na;
plot sh = if swinghigh then PriceHigh + (dot_distance * TickSize()) else na;

def cond_h      = if !IsNaN(sh) then 1 else Double.NaN;
def dataCount_h = CompoundValue(1, if !IsNaN(cond_h) then dataCount_h[1] + 1 else dataCount_h[1], 0);
def cond_l      = if !IsNaN(sl) then 1 else Double.NaN;
def dataCount_l = CompoundValue(1, if !IsNaN(cond_l) then dataCount_l[1] + 1 else dataCount_l[1], 0);

plot lastlow =
    if limit_plot then
       if HighestAll(dataCount_l) - dataCount_l <= plot_limited_to_last_x - 1 then
          lastlowdata
        else na
    else lsl;

plot lasthigh =
    if limit_plot then
       if HighestAll(dataCount_h) - dataCount_h <= plot_limited_to_last_x - 1 then
          lasthighdata
        else na       
    else
        lasthighdata;


# FORMATTING
sh.SetStyle(Curve.POINTS);
sh.SetLineWeight(dot_size);
sh.AssignValueColor(if sh > lasthigh[1] then GlobalColor("Green") else GlobalColor("Red"));
sh.SetHiding(show_points == no);
sh.HideBubble();
sh.HideTitle();

sl.SetStyle(Curve.POINTS);
sl.SetLineWeight(dot_size);
sl.AssignValueColor(if sl < lastlow[1] then GlobalColor("Red") else GlobalColor("Green"));
sl.SetHiding(show_points == no);
sl.HideBubble();
sl.HideTitle();

lasthigh.SetStyle(Curve.SHORT_DASH);
lasthigh.AssignValueColor(GlobalColor("Gray"));
lasthigh.SetHiding(show_lines == no);
lasthigh.HideBubble();
lasthigh.HideTitle();

lastlow.SetStyle(Curve.SHORT_DASH);
lastlow.AssignValueColor(GlobalColor("Gray"));
lastlow.SetHiding(show_lines == no);
lastlow.HideBubble();
lastlow.HideTitle();
 
Did you set the "show_only_last_swing" input to Yes? If not it's going to keep track of all of them.
when making a study for someone, it should work as requested with the default values. they shouldn't have to change anything. some users may be unfamiliar with changing an input value.
 
Hi, Say I have a study showing the highest high in the last X bars (drawings horizontal line)... That would fill my screen with many lines where the condition is satisfied. My question is what code should be added to show only the most recent line...

The highest high is only an example of course...

the studies above may be doing this, i didn't study them thoroughly.
thought i would list a couple steps

Code:
def signal = .... some true/false formula
# save the barnumbers of the signals
def sbn = if signal then barnumber() else 0;
# find the highest barnumber
def hibn = highestall(sbn);
# if a barnumber equals hibn then this is the most recent signal
def highest_signal = if barnumber() == hibn then 1 else 0;
 

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