Previous Periods H/L drawn at Candle

cngo

New member
Hi,

looking for some help on modifying on an indicator that would draw the previous period's (month & week) high & low with a horizontal line beginning from that specified candle to the right of the chart.

The current indicator only shows the high and low levels drawn beginning at the current month (second screengrab & code below).

Desired output (white lines there for reference)
1744904011804.png




Current
1744904848883.png



Code:
input aggregationPeriod1 = aggregationPeriod.WEEK;
input aggregationPeriod2 = aggregationPeriod.MONTH;
input show_lines   = yes;
input show_bubbles = yes;
input showOnlyLastPeriod = yes;
input length = 1;
input displace = -1;
input bubble_displace = 2;



def prevOpen2 = open(period = AggregationPeriod.WEEK)[-1];
def priceOpen2 = open(period = AggregationPeriod.WEEK);
plot currentOpenWeek = if !IsNaN(prevOpen2) then Double.NaN else priceOpen2;
currentOpenWeek.SetDefaultColor(Color.CYAN);
currentOpenWeek.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


plot aggPer1High;
plot aggPer1Low;


#fill next next AggPeriod1 with previous period High
def DH1 = HighestAll(if IsNaN(close(period = aggregationPeriod1)[-1]) and !IsNaN(close(period=aggregationPeriod1))
    then high(period = aggregationPeriod1)[length]
    else Double.NaN);

#fill next next AggPeriod1 with previous period Low
def DL1 = LowestAll(if IsNaN(close[-1]) and !IsNaN(close)
    then low(period = aggregationPeriod1)[length]
    else Double.NaN);


if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod1)[-1]) {
    aggPer1High = Double.NaN;
    aggPer1Low  = Double.NaN;
} else {
    aggPer1High = DH1;
    aggPer1Low  = DL1;
}

aggPer1High.SetDefaultColor(GetColor(4));
aggPer1High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer1High.SetHiding(!show_lines);
aggPer1Low.SetDefaultColor(GetColor(4));
aggPer1Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer1Low.SetHiding(!show_lines);

def bn = BarNumber();


#AddChartBubble(show_bubbles and bn == HighestAll(bn - bubble_displace),  aggPer1High, "w " + Astext(aggPer1High), Color.Light_GREEN);
#AddChartBubble(show_bubbles and bn == HighestAll(bn - bubble_displace), aggPer1Low, "w " + Astext(aggPer1Low), Color.light_RED);



def prevOpen1 = open(period = AggregationPeriod.MONTH)[-1];
def priceOpen1 = open(period = AggregationPeriod.MONTH);
plot currentOpenMonth = if !IsNaN(prevOpen1) then Double.NaN else priceOpen1;
currentOpenMonth.SetDefaultColor(Color.YELLOW);
currentOpenMonth.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


plot aggPer2High;
plot aggPer2Low;

def DH2 = if IsNaN(close(period = aggregationPeriod2)[-1])
    then DH2[1]
    else Highest(high(period = aggregationPeriod2)[-displace], length);


def DL2 = if IsNaN(close(period = aggregationPeriod2)[-1])
    then DL2[1]
    else Lowest(low(period = aggregationPeriod2)[-displace], length);


if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod2)[-1])
    {
        aggPer2High = Double.NaN;
        aggPer2Low  = Double.NaN;
    }
    else {
        aggPer2High = DH2;
        aggPer2Low  = DL2;
          }

aggPer2High.SetDefaultColor(GetColor(4));
aggPer2High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer2High.SetHiding(!show_lines);

aggPer2Low.SetDefaultColor(GetColor(4));
aggPer2Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer2Low.SetHiding(!show_lines);

#

AddChartBubble(
    show_bubbles and bn == HighestAll(bn - bubble_displace),
    aggPer2High,
    "m" + AsText(aggPer2High),
    createColor(0,153,255),yes
);


#AddChartBubble(
#    show_bubbles and bn == HighestAll(bn - bubble_displace),
#    aggPer1High,
#    "w" + AsText(aggPer1High),
#    createColor(0,153,153),no
#);


#AddChartBubble(
#    show_bubbles and bn == HighestAll(bn - bubble_displace),
#    aggPer1low,
#    "w" + AsText(aggPer1Low),
#    createColor(153,0,255),yes
#);


AddChartBubble(
    show_bubbles and bn == HighestAll(bn - bubble_displace),
    aggPer2Low,
    "m " + AsText(aggPer2Low),
    createColor(153,0,255),no
);
 
Solution
Hi,

looking for some help on modifying on an indicator that would draw the previous period's (month & week) high & low with a horizontal line beginning from that specified candle to the right of the chart.

The current indicator only shows the high and low levels drawn beginning at the current month (second screengrab & code below).

Desired output (white lines there for reference)
View attachment 24588



Current
View attachment 24589



Code:
input aggregationPeriod1 = aggregationPeriod.WEEK;
input aggregationPeriod2 = aggregationPeriod.MONTH;
input show_lines   = yes;
input show_bubbles = yes;
input showOnlyLastPeriod = yes;
input length = 1;
input displace = -1;
input bubble_displace = 2;



def prevOpen2 = open(period =...
Hi,

looking for some help on modifying on an indicator that would draw the previous period's (month & week) high & low with a horizontal line beginning from that specified candle to the right of the chart.

The current indicator only shows the high and low levels drawn beginning at the current month (second screengrab & code below).

Desired output (white lines there for reference)
View attachment 24588



Current
View attachment 24589



Code:
input aggregationPeriod1 = aggregationPeriod.WEEK;
input aggregationPeriod2 = aggregationPeriod.MONTH;
input show_lines   = yes;
input show_bubbles = yes;
input showOnlyLastPeriod = yes;
input length = 1;
input displace = -1;
input bubble_displace = 2;



def prevOpen2 = open(period = AggregationPeriod.WEEK)[-1];
def priceOpen2 = open(period = AggregationPeriod.WEEK);
plot currentOpenWeek = if !IsNaN(prevOpen2) then Double.NaN else priceOpen2;
currentOpenWeek.SetDefaultColor(Color.CYAN);
currentOpenWeek.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


plot aggPer1High;
plot aggPer1Low;


#fill next next AggPeriod1 with previous period High
def DH1 = HighestAll(if IsNaN(close(period = aggregationPeriod1)[-1]) and !IsNaN(close(period=aggregationPeriod1))
    then high(period = aggregationPeriod1)[length]
    else Double.NaN);

#fill next next AggPeriod1 with previous period Low
def DL1 = LowestAll(if IsNaN(close[-1]) and !IsNaN(close)
    then low(period = aggregationPeriod1)[length]
    else Double.NaN);


if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod1)[-1]) {
    aggPer1High = Double.NaN;
    aggPer1Low  = Double.NaN;
} else {
    aggPer1High = DH1;
    aggPer1Low  = DL1;
}

aggPer1High.SetDefaultColor(GetColor(4));
aggPer1High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer1High.SetHiding(!show_lines);
aggPer1Low.SetDefaultColor(GetColor(4));
aggPer1Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer1Low.SetHiding(!show_lines);

def bn = BarNumber();


#AddChartBubble(show_bubbles and bn == HighestAll(bn - bubble_displace),  aggPer1High, "w " + Astext(aggPer1High), Color.Light_GREEN);
#AddChartBubble(show_bubbles and bn == HighestAll(bn - bubble_displace), aggPer1Low, "w " + Astext(aggPer1Low), Color.light_RED);



def prevOpen1 = open(period = AggregationPeriod.MONTH)[-1];
def priceOpen1 = open(period = AggregationPeriod.MONTH);
plot currentOpenMonth = if !IsNaN(prevOpen1) then Double.NaN else priceOpen1;
currentOpenMonth.SetDefaultColor(Color.YELLOW);
currentOpenMonth.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);


plot aggPer2High;
plot aggPer2Low;

def DH2 = if IsNaN(close(period = aggregationPeriod2)[-1])
    then DH2[1]
    else Highest(high(period = aggregationPeriod2)[-displace], length);


def DL2 = if IsNaN(close(period = aggregationPeriod2)[-1])
    then DL2[1]
    else Lowest(low(period = aggregationPeriod2)[-displace], length);


if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod2)[-1])
    {
        aggPer2High = Double.NaN;
        aggPer2Low  = Double.NaN;
    }
    else {
        aggPer2High = DH2;
        aggPer2Low  = DL2;
          }

aggPer2High.SetDefaultColor(GetColor(4));
aggPer2High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer2High.SetHiding(!show_lines);

aggPer2Low.SetDefaultColor(GetColor(4));
aggPer2Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
aggPer2Low.SetHiding(!show_lines);

#

AddChartBubble(
    show_bubbles and bn == HighestAll(bn - bubble_displace),
    aggPer2High,
    "m" + AsText(aggPer2High),
    createColor(0,153,255),yes
);


#AddChartBubble(
#    show_bubbles and bn == HighestAll(bn - bubble_displace),
#    aggPer1High,
#    "w" + AsText(aggPer1High),
#    createColor(0,153,153),no
#);


#AddChartBubble(
#    show_bubbles and bn == HighestAll(bn - bubble_displace),
#    aggPer1low,
#    "w" + AsText(aggPer1Low),
#    createColor(153,0,255),yes
#);


AddChartBubble(
    show_bubbles and bn == HighestAll(bn - bubble_displace),
    aggPer2Low,
    "m " + AsText(aggPer2Low),
    createColor(153,0,255),no
);

this plots lines from ohlc, from 2 different aggregation times, week and month
can choose which agg period, default is 1 agg bar back

Code:
#agg_prev_hilo_mo_week
#https://usethinkscript.com/threads/previous-periods-h-l-drawn-at-candle.20914/
#Previous Periods H/L drawn at Candle

def na = double.nan;
def bn = barnumber();
def isyr = (GetYear() == GetLastYear());

addlabel(1, " ", color.black);
input agg1 = aggregationPeriod.WEEK;
def agg1min =  agg1/60000;
AddLabel(yes, "Agg1 " + (if agg1min < 60 then (agg1min + " m")
              else if agg1min < 1440 then ((agg1min/60) + " H")
              else if agg1min < 10080 then (agg1min/(60*24) + " D")
              else if agg1 == aggregationPeriod.WEEK then "Wk"
              else if agg1 == aggregationPeriod.MONTH then "Mo"
              else "") + " dot", Color.cyan);

input agg1_bars_back = 1;

def o1 = open(period = agg1);
def h1 = high(period = agg1);
def l1 = low(period = agg1);
def c1 = close(period = agg1);

def o2;
def h2;
def l2;
def c2;
if bn == 1 then {
 o2 = na;
 h2 = na;
 l2 = na;
 c2 = na;
} else if isyr and (getweek() + agg1_bars_back) == GetLastWeek() then {
 o2 = if open == o1 then open else o2[1];
 h2 = if high == h1 then high else h2[1];
 l2 = if low == l1 then low else l2[1];
 c2 = if close == c1 then close else c2[1];
} else {
 o2 = o2[1];
 h2 = h2[1];
 l2 = l2[1];
 c2 = c2[1];
}

input show_agg1_lines = yes;
# lines from candle matching agg price
plot yo2 = if show_agg1_lines then o2 else na;
plot yh2 = if show_agg1_lines then h2 else na;
plot yl2 = if show_agg1_lines then l2 else na;
plot yc2 = if show_agg1_lines then c2 else na;
yo2.SetDefaultColor(Color.cyan);
yh2.SetDefaultColor(Color.light_gray);
yl2.SetDefaultColor(Color.light_gray);
yc2.SetDefaultColor(Color.yellow);
yo2.SetStyle(Curve.short_DASH);
yh2.SetStyle(Curve.short_DASH);
yl2.SetStyle(Curve.short_DASH);
yc2.SetStyle(Curve.short_DASH);


#------------------
input agg2 = aggregationPeriod.MONTH;
def agg2min =  agg2/60000;
AddLabel(yes, "Agg2 " + (if agg2min < 60 then (agg2min + " m")
              else if agg2min < 1440 then ((agg2min/60) + " H")
              else if agg2min < 10080 then (agg2min/(60*24) + " D")
              else if agg2 == aggregationPeriod.WEEK then "Wk"
              else if agg2 == aggregationPeriod.MONTH then "Mo"
              else "") + " dash", Color.cyan);

input agg2_bars_back = 1;

def o3 = open(period = agg2);
def h3 = high(period = agg2);
def l3 = low(period = agg2);
def c3 = close(period = agg2);

def o4;
def h4;
def l4;
def c4;
if bn == 1 then {
 o4 = na;
 h4 = na;
 l4 = na;
 c4 = na;
} else if isyr and (getmonth() + agg2_bars_back) == GetLastmonth() then {
 o4 = if open == o3 then open else o4[1];
 h4 = if high == h3 then high else h4[1];
 l4 = if low == l3 then low else l4[1];
 c4 = if close == c3 then close else c4[1];
} else {
 o4 = o4[1];
 h4 = h4[1];
 l4 = l4[1];
 c4 = c4[1];
}

input show_agg2_lines = yes;
# lines from candle matching agg price
plot yo4 = if show_agg2_lines then o4 else na;
plot yh4 = if show_agg2_lines then h4 else na;
plot yl4 = if show_agg2_lines then l4 else na;
plot yc4 = if show_agg2_lines then c4 else na;
yo4.SetDefaultColor(Color.cyan);
yh4.SetDefaultColor(Color.light_gray);
yl4.SetDefaultColor(Color.light_gray);
yc4.SetDefaultColor(Color.yellow);
yo4.SetStyle(Curve.MEDIUM_DASH);
yh4.SetStyle(Curve.MEDIUM_DASH);
yl4.SetStyle(Curve.MEDIUM_DASH);
yc4.SetStyle(Curve.MEDIUM_DASH);


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

input test_agg1_hilo_lines = no;
plot z1 = if test_agg1_hilo_lines then h1 else na;
plot z2 = if test_agg1_hilo_lines then l1 else na;
z1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z1.SetDefaultColor(Color.cyan);
z2.SetDefaultColor(Color.cyan);
#z1.hidebubble();
#z2.hidebubble();

input test_agg2_hilo_lines = no;
plot z3 = if test_agg2_hilo_lines then h3 else na;
plot z4 = if test_agg2_hilo_lines then l3 else na;
z3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
z3.SetDefaultColor(Color.yellow);
z4.SetDefaultColor(Color.yellow);
#z3.hidebubble();
#z4.hidebubble();
#
 

Attachments

  • img1.JPG
    img1.JPG
    52 KB · Views: 20
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
365 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