Arup Rakshit
Arup Rakshit

Reputation: 118289

How can I keep lines my levels of two days not joined like it is now?

I am plotting camarilla levels using my own code. But the lines of two days are getting joined and it is looks very weird. Also the label text I want to be present at the beginning of each line. Right now it is on top of the first bar. How can I fix this?

//@version=4

study("Camarilla 1-6", overlay=true) 

[previous_day_high, previous_day_low, previous_day_close] = security(syminfo.tickerid, "D", [high[1], low[1], close[1]])

previous_day_range = previous_day_high - previous_day_low

// Resistances 
r4 = round(previous_day_close + (previous_day_range) * 1.1 / 2, 2)
r3 = round(previous_day_close + (previous_day_range) * 1.1 / 4, 2)
r2 = round(previous_day_close + (previous_day_range) * 1.1 / 6, 2)
r1 = round(previous_day_close + (previous_day_range) * 1.1 / 12, 2)

r5 = round(r4 + 1.168 * (r4 - r3), 2)
r6 = round((previous_day_high / previous_day_low) * previous_day_close, 2)

// Supports 
s4 = round(previous_day_close - (previous_day_range) * 1.1 / 2, 2)
s3 = round(previous_day_close - (previous_day_range) * 1.1 / 4, 2)
s2 = round(previous_day_close - (previous_day_range) * 1.1 / 6, 2)
s1 = round(previous_day_close - (previous_day_range) * 1.1 / 12, 2)

s5 = round(s4 - 1.168 * (s3 - s4), 2)
s6 = round(previous_day_close - (r6 - previous_day_close), 2)

// Plots
plot(r3 , title="H3: Go Short",       style=plot.style_stepline, color=color.red,   linewidth=2)
plot(r4 , title="H4: Long Breakout",  style=plot.style_stepline, color=color.green, linewidth=2)
plot(r5 , title="H5: Target 1",       style=plot.style_stepline, color=color.green, linewidth=2)
plot(r6 , title="H6: Target 2",       style=plot.style_stepline, color=color.green, linewidth=2)

plot(s3 , title="L3: Go Long",        style=plot.style_stepline, color=color.green, linewidth=2)
plot(s4 , title="L4: Short Breakout", style=plot.style_stepline, color=color.red,   linewidth=2)
plot(s5 , title="L5: Target 1",       style=plot.style_stepline, color=color.red,   linewidth=2)
plot(s6 , title="L6: Target 2",       style=plot.style_stepline, color=color.red,   linewidth=2)

// Labels
if (change(r3))
    label.new(bar_index, r3, text="H3: " + tostring(r3), style=label.style_none, textalign=text.align_left, yloc=yloc.abovebar)

if (change(r4))
    label.new(bar_index, r4, text="H4: " + tostring(r4), style=label.style_none)

if (change(r5))
    label.new(bar_index, r5, text="H5: " + tostring(r5), style=label.style_none)

if (change(r6))
    label.new(bar_index, r6, text="H6: " + tostring(r6), style=label.style_none)

It looks like now:

enter image description here

But I want it to looks like as this example code screenshot looks like https://www.tradingview.com/pine-script-docs/en/v4/essential/Drawings.html#pivot-points-standard. What things I need to change?

I was trying the options textalign=text.align_left, yloc=yloc.abovebar, but they are not the solution of my problem here. I kept them as it is there for now, that's why you see it in my code.

Upvotes: 0

Views: 198

Answers (1)

Bjorn Mistiaen
Bjorn Mistiaen

Reputation: 6905

The joined lines can be fixed like this.

//@version=4

study("Camarilla 1-6", overlay=true) 

[previous_day_high, previous_day_low, previous_day_close] = security(syminfo.tickerid, "D", [high[1], low[1], close[1]])

previous_day_range = previous_day_high - previous_day_low

// Resistances 
r4 = round(previous_day_close + (previous_day_range) * 1.1 / 2, 2)
r3 = round(previous_day_close + (previous_day_range) * 1.1 / 4, 2)
r2 = round(previous_day_close + (previous_day_range) * 1.1 / 6, 2)
r1 = round(previous_day_close + (previous_day_range) * 1.1 / 12, 2)

r5 = round(r4 + 1.168 * (r4 - r3), 2)
r6 = round((previous_day_high / previous_day_low) * previous_day_close, 2)

// Supports 
s4 = round(previous_day_close - (previous_day_range) * 1.1 / 2, 2)
s3 = round(previous_day_close - (previous_day_range) * 1.1 / 4, 2)
s2 = round(previous_day_close - (previous_day_range) * 1.1 / 6, 2)
s1 = round(previous_day_close - (previous_day_range) * 1.1 / 12, 2)

s5 = round(s4 - 1.168 * (s3 - s4), 2)
s6 = round(previous_day_close - (r6 - previous_day_close), 2)

// Plots
plot(r3 , title="H3: Go Short",       style=plot.style_linebr, color=change(r3)?na:color.red,   linewidth=2)
plot(r4 , title="H4: Long Breakout",  style=plot.style_linebr, color=change(r4)?na:color.green, linewidth=2)
plot(r5 , title="H5: Target 1",       style=plot.style_linebr, color=change(r5)?na:color.green, linewidth=2)
plot(r6 , title="H6: Target 2",       style=plot.style_linebr, color=change(r6)?na:color.green, linewidth=2)

plot(s3 , title="L3: Go Long",        style=plot.style_linebr, color=change(s3)?na:color.green, linewidth=2)
plot(s4 , title="L4: Short Breakout", style=plot.style_linebr, color=change(s4)?na:color.red,   linewidth=2)
plot(s5 , title="L5: Target 1",       style=plot.style_linebr, color=change(s5)?na:color.red,   linewidth=2)
plot(s6 , title="L6: Target 2",       style=plot.style_linebr, color=change(s6)?na:color.red,   linewidth=2)

// Labels
if (change(r3))
    label.new(bar_index, r3, text="H3: " + tostring(r3), style=label.style_none, textalign=text.align_left, yloc=yloc.abovebar)

if (change(r4))
    label.new(bar_index, r4, text="H4: " + tostring(r4), style=label.style_none)

if (change(r5))
    label.new(bar_index, r5, text="H5: " + tostring(r5), style=label.style_none)

if (change(r6))
    label.new(bar_index, r6, text="H6: " + tostring(r6), style=label.style_none)

Which yields

Example

Not sure what you mean by

Also the label text I want to be present at the beginning of each line.
Right now it is on top of the first bar.

Edit 1 in response to this comment

The ?: is a ternary conditional operator.
This is a short-hand form of an if-then-else syntax.

This statement

color=change(s3)?na:color.green

Is identical to

if change(s3)
    color := na
else
    color := color.green

What it does, is to set the color of the line to na (=no color) when the value changes.

Normally, when a plot line changes value, Pine draws a connecting line from the old to the new value, in the same color as the line, as can be seen in this example in yellow:

Example

The code for that 'connecting' yellow line is

plot(s6 , title="L6: Target 2",       style=plot.style_linebr, color=color.yellow)

So, by setting the color to na (=no color) when the value changes, the connecting line becomes invisible, as can be seen with the other (non-yellow) lines.

When we would substitute na with another color, then the connecting lines would show up as that color instead of invisible.

For example, if we change the code for s4 to show color white instead of na:

plot(s4 , title="L4: Short Breakout", style=plot.style_linebr, color=change(s4)?color.white:color.red,   linewidth=2)

That would make the connecting lines white instead of invisible.

Example

Upvotes: 1

Related Questions