Page 1 of 1

Plot stepped Highs and Lows

Posted: Thu Jul 01, 2021 3:44 am
by Mitesh1002
Hi scripters !

I am developing an intraday breakout indicator and I am stuck for a few days.

Using the security function, I am able to plot a Days high and low on lower time frame, but each time the high or the low changes, my script plots a new horizontal line based on the latest high and low, erasing the old one. Due to this I am having back testing issues.

Can somebody help me with plotting current and all previous day's highs and lows as steps and as they have occurred please.

For example,

If subsequent days highs are made at say 10 am, 12 pm, 2 pm, 4 pm,
I need the chart to plot one horizontal line from 10 am to 12 pm ( indicating the high until 12 pm ) then another horizontal line from 12 pm to 2 pm, then another horizontal line from 2 pm to 4 pm, and so on without erasing the previous horizontal lines.

I've tried to explain this to the best of my ability. I hope I have conveyed it clear enough.

This is my first post. Thank you in advance :)

Re: Plot stepped Highs and Lows

Posted: Thu Jul 01, 2021 8:26 am
by CanYouCatchMe01
I'm not sure if I understood what you wanted to do. Is this the result you wanted?
You can paint a picture of the result you want, then it may be easier for us to understand.
Image

My script first checks where the pivot points are. The pivot points are displayed with "H" and "L".
It takes the most recent pivot point and draws a line to the right corner.
If a new pivot point appears. Then it stops drawing the previous line and start a new line.

First tried to use hline, but is not possible to change the start and stop. I used a normal line, where it is possible.

Here is the code:

Code: Select all

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © CanYouCatchMe

//@version=4
study("horizontal pivot lines", overlay=true)

prd = input(defval = 20, title="Pivot Point Period", minval = 10, maxval = 50) //How many pivot points. Lower value results in more points.
float ph = na, float pl = na //If there is no pivit points. Default value
ph := pivothigh(prd, prd) //Returns the price of the pivot point "prd" ago. If there was no pivot "prd" ago, then it returns 'na'.
pl := pivotlow(prd, prd)

//Plotting lables. NOTE: offset = -prd, Because it gets the price "prd" ago.
plotshape(ph, text="H",  style=shape.labeldown, color=color.new(color.white, transp = 0), textcolor=color.black, location=location.abovebar, offset = -prd)
plotshape(pl, text="L",  style=shape.labeldown, color=color.new(color.white, transp = 0), textcolor=color.black, location=location.belowbar, offset = -prd)

//Saving the referance of the lines, so the the "x2" position can be moved the right.
var line l1 = na, var line l2 = na

//High lines
if(ph) //if there was a pivot point "prd" ago.
    line.set_x2(l1, bar_index-prd) //Changing old line last time
    l1 := line.new(x1=bar_index-prd, y1=ph, x2=bar_index, y2=ph) //New line added.
line.set_x2(l1, bar_index) //Moving "x2" to the right corner.

//Low lines
if(pl)
    line.set_x2(l2, bar_index-prd)
    l2 := line.new(x1=bar_index-prd, y1=pl, x2=bar_index, y2=pl, color=color.yellow)
line.set_x2(l2, bar_index)

Re: Plot stepped Highs and Lows

Posted: Thu Jul 01, 2021 4:43 pm
by Mitesh1002
Dear CanYouCatchMe01,

Thank you very much. Yes, this is very similar to what I wanted.
You are drawing the most recent pivot high and low to the right side of the chart, I am looking to draw the most recent higher high and the most recent lower low on intraday timeframe, without erasing the previous horizontal lines ( exactly what you have achieved )
I think I will be able to take it from here.

Thank you again. If I still am not able to code it, I will come back to you.