How to draw Horizontal line to the left instead of the right

Learnbot

Active member
Hello,
I have simple code as shown below, i wanted to know if there is a way for the horizontal line to extend to the left till the market open rather than extending to the right how it is shown in the image.
Code:
plot up = MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" is less than 0;

def bu_high = if up then high[1] else bu_high[1];
def bu_low = if up then low[1] else bu_low[1];


plot bu_hh = bu_high[1];
plot bu_ll = bu_low[1];


bu_hh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_ll.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_hh.SetDefaultColor(Color.CYAN);
bu_ll.SetDefaultColor(Color.CYAN);


AddCloud(bu_hh, bu_ll, Color.CYAN, Color.CYAN);

mwB6ZMR.png



Thank you :) hope it is easy fix
 
Last edited by a moderator:
Solution
left.png


Code:
plot up = MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" is less than 0;

def bu_high = if up then high[1] else bu_high[1];
def bu_low = if up then low[1] else bu_low[1];

plot bu_hh = bu_high[1];
plot bu_ll = bu_low[1];

bu_hh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_ll.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_hh.SetDefaultColor(Color.CYAN);
bu_ll.SetDefaultColor(Color.CYAN);

AddCloud(bu_hh, bu_ll, Color.CYAN, Color.CYAN);

def buh = fold h=0 to AbsValue(barnumber()) while !IsNaN(getvalue(close,-h)) do if bu_high!=bu_high[1] then GetValue(bu_high,-h) else GetValue(bu_high[1],-h) ;
def buhh = fold ih = 0 to 1 while !IsNaN(close) do buh;

def bul = fold l=0 to AbsValue(barnumber())...
left.png


Code:
plot up = MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" is less than 0;

def bu_high = if up then high[1] else bu_high[1];
def bu_low = if up then low[1] else bu_low[1];

plot bu_hh = bu_high[1];
plot bu_ll = bu_low[1];

bu_hh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_ll.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_hh.SetDefaultColor(Color.CYAN);
bu_ll.SetDefaultColor(Color.CYAN);

AddCloud(bu_hh, bu_ll, Color.CYAN, Color.CYAN);

def buh = fold h=0 to AbsValue(barnumber()) while !IsNaN(getvalue(close,-h)) do if bu_high!=bu_high[1] then GetValue(bu_high,-h) else GetValue(bu_high[1],-h) ;
def buhh = fold ih = 0 to 1 while !IsNaN(close) do buh;

def bul = fold l=0 to AbsValue(barnumber()) while !IsNaN(getvalue(close,-l)) do if bu_low!=bu_low[1] then GetValue(bu_low,-l) else GetValue(bu_low[1],-l) ;
def bull = fold il = 0 to 1 while !IsNaN(close) do bul;

plot buhhplot = if buhh > 1 then buhh else Double.NaN;
buhhplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
buhhplot.SetDefaultColor(Color.CYAN);

plot bullplot = if bull > 1 then bull else Double.NaN;
bullplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bullplot.SetDefaultColor(Color.CYAN);

AddCloud(buhh, bull, Color.CYAN, Color.CYAN);
 
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

left.png


Code:
plot up = MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" is less than 0;

def bu_high = if up then high[1] else bu_high[1];
def bu_low = if up then low[1] else bu_low[1];

plot bu_hh = bu_high[1];
plot bu_ll = bu_low[1];

bu_hh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_ll.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bu_hh.SetDefaultColor(Color.CYAN);
bu_ll.SetDefaultColor(Color.CYAN);

AddCloud(bu_hh, bu_ll, Color.CYAN, Color.CYAN);

def buh = fold h=0 to AbsValue(barnumber()) while !IsNaN(getvalue(close,-h)) do if bu_high!=bu_high[1] then GetValue(bu_high,-h) else GetValue(bu_high[1],-h) ;
def buhh = fold ih = 0 to 1 while !IsNaN(close) do buh;

def bul = fold l=0 to AbsValue(barnumber()) while !IsNaN(getvalue(close,-l)) do if bu_low!=bu_low[1] then GetValue(bu_low,-l) else GetValue(bu_low[1],-l) ;
def bull = fold il = 0 to 1 while !IsNaN(close) do bul;

plot buhhplot = if buhh > 1 then buhh else Double.NaN;
buhhplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
buhhplot.SetDefaultColor(Color.CYAN);

plot bullplot = if bull > 1 then bull else Double.NaN;
bullplot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
bullplot.SetDefaultColor(Color.CYAN);

AddCloud(buhh, bull, Color.CYAN, Color.CYAN);

This is amazing! Thank you so much for your help. However, there is an issue. So in the image below, cyan down arrows is where condition is met however, you can see lines are not extending to the left instead it is still extending to the right like how i had coded. (cyan cloud is my original code and red cloud is your updated cloud). I dont know much abt "FOLD" and other stuff you have coded so i am not sure what is causing the discrepency.
also is there a way to have the cloud and line not to extend to the right but just to the left (the blue scribble is something i dont want to see, i am using this script to look back rather than forward )

Thank you SO MUCH for your help and time!

wqUhlek.png
 
... is there a way to have the cloud and line not to extend to the right but just to the left

is this what you are looking for?
this will draw 2 horizontal lines, left of the signal bar, at the high and low, of the future bar with the up signal.
but using fold for this, creates data that isn't usable in addcloud, so this doesn't draw clouds.

Code:
# leftlines_01

# draw lines before a signal ( to the left)
# no cloud

#--------------------------
def na = double.nan;
def bn = BarNumber();
def len = 500;

def up = if (MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" < 0) then 1 else 0;

def top;
def bot;
if bn == 1 then {
  top = na;
  bot = na;
} else if up[1] then {
  top = fold i = 1 to len
  while !getvalue(up[1], -i)
  do getvalue(high, -i);

  bot = fold k = 1 to len
  while !getvalue(up[1], -k)
  do getvalue(low, -k);

} else {
  top = top[1];
  bot = bot[1];
}

plot t = top;
t.SetDefaultColor(Color.cyan);
t.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot b = bot;
b.SetDefaultColor(Color.cyan);
b.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

# errors
#AddCloud(top, bot, Color.CYAN, Color.CYAN);

# -------------------------

input test1_up_signals = no;
addchartbubble(test1_up_signals and up, top, "up " + up + "\nH " + high + "\nL " + low,  color.cyan, yes);

input test2 = no;
addchartbubble(test2, bot, "up " + up + "\nH " + high + "\nL " + low,  color.cyan, no);

addverticalline( ((test1_up_signals or test2) and up[0]), "    UP", color.gray);
addverticalline( ((test1_up_signals or test2) and up[1]), "    UP", color.gray);
#

lines up to the high and low of the bar when up is true
mPMSIOP.jpg


when test1_up_signals is set to yes, draws bubbles with high and low, and 2 vertical lines around the up bar.
h3FhrRW.jpg


------------------------------
i wonder if this code,
https://usethinkscript.com/threads/current-price-line-indicator-for-thinkorswim.8793/
and the barnumber of up bars, can be used to create price levels usable in addcloud()?
 
Last edited:
is this what you are looking for?
this will draw 2 horizontal lines, left of the signal bar, at the high and low, of the future bar with the up signal.
but using fold for this, creates data that isn't usable in addcloud, so this doesn't draw clouds.
DUDE THIS IS PERFECT!! exactly what i was looking for! thank you so much for your help. Thank you @SleepyZ as well
 
DUDE THIS IS PERFECT!! exactly what i was looking for! thank you so much for your help. Thank you @SleepyZ as well
here is the updated code


EDIT ----- 1/8
ver01b
added the suggested cloud code from @SleepyZ , thank you
AddCloud(t, b, Color.CYAN, Color.CYAN);
it references the plotted variables , instead of the source variables.

i used a big number, 500 , for the fold loop, because i wasn't sure how many bars would exist between signals. because of this , it is complex. this number could be reduced to a small number, to make it not complex, but only if it can be verified that it doesn't miss signals.

----------------------------------------

is this what you are looking for?
this will draw 2 horizontal lines, left of the signal bar, at the high and low, of the bar with the up signal.
but using fold for this, creates data that isn't usable in addcloud, so this doesn't draw clouds.

Code:
# leftlines_01b

# draw lines before a signal ( to the left)

def na = double.nan;
def bn = BarNumber();
def len = 500;

def up = if (MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" < 0) then 1 else 0;

def top;
def bot;
if bn == 1 then {
  top = na;
  bot = na;
} else if up[1] then {
  top = fold i = 1 to len
  while !getvalue(up[1], -i)
  do getvalue(high, -i);

  bot = fold k = 1 to len
  while !getvalue(up[1], -k)
  do getvalue(low, -k);

} else {
  top = top[1];
  bot = bot[1];
}

plot t = top;
t.SetDefaultColor(Color.cyan);
t.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot b = bot;
b.SetDefaultColor(Color.cyan);
b.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

# errors
#AddCloud(top, bot, Color.CYAN, Color.CYAN);

input show_cloud = yes;
def t2 = if show_cloud then t else na;
AddCloud(t2, b, Color.CYAN, Color.CYAN);

# -------------------------

input test1_up_signals = no;
addchartbubble(test1_up_signals and up, top, "up " + up + "\nH " + high + "\nL " + low,  color.cyan, yes);

input test2 = no;
addchartbubble(test2, bot, "up " + up + "\nH " + high + "\nL " + low,  color.cyan, no);

addverticalline( ((test1_up_signals or test2) and up[0]), "    UP", color.gray);
addverticalline( ((test1_up_signals or test2) and up[1]), "    UP", color.gray);
#

lines up to the high and low of the bar when up is true
mPMSIOP.jpg


can turn a cloud on or off
JMlVw15.jpg


when test1_up_signals is set to yes, draws bubbles with high and low, and 2 vertical lines around the up bar.
h3FhrRW.jpg
 
Last edited:
here is the updated code


EDIT ----- 1/8
ver01b
added the suggested cloud code from @SleepyZ , thank you
AddCloud(t, b, Color.CYAN, Color.CYAN);
it references the plotted variables , instead of the source variables.

i used a big number, 500 , for the fold loop, because i wasn't sure how many bars would exist between signals. because of this , it is complex. this number could be reduced to a small number, to make it not complex, but only if it can be verified that it doesn't miss signals.

----------------------------------------

is this what you are looking for?
this will draw 2 horizontal lines, left of the signal bar, at the high and low, of the bar with the up signal.
but using fold for this, creates data that isn't usable in addcloud, so this doesn't draw clouds.
This is super impressive to be honest! Thank you! Just out of curiosity is there a way to draw the line for high and low of the bar before the signal instead of the bar where the signal occurred?
 
This is super impressive to be honest! Thank you! Just out of curiosity is there a way to draw the line for high and low of the bar before the signal instead of the bar where the signal occurred?

updated again

ver 01c
can shift the signal bar, to be before or after, the original up bar.

added an offset for finding the signal. when
signal_offset = 0
horizontal lines come from the original signal bar.
if it is set to a negative number, it moves the data bar to the left. positive and it moves to the right.

when i made the first version, i didn't notice that the clouds were connected diagonally.
in this version, i changed the cloud formula, so that the first bar of a horizontal line, does not have a cloud. this void makes the cloud non continious. exception, if one of the horizontal lines is at the same price level as the previous one, the cloud seems to be continious.

since i added the ability to shift the signal sideways, i wanted to verify the original up is where i think it should be. i added a test up arrow, that will be below it.

changed some of the test code.
the vertical lines track the offset signal bar. the test bubble tracks the offset signal bar.


Ruby:
# leftlines_01c

# chg signal offset

# draw lines before a signal ( to the left)
#--------------------------
def na = double.nan;
def bn = BarNumber();
def len = 500;
input signal_offset = 0;

def up = if (MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" < 0) then 1 else 0;

# draw an up arrow under the original up signal bar
input test_arrow_up_original = yes;
plot u1 = (test_arrow_up_original and up);
u1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
u1.SetDefaultColor(Color.white);
u1.setlineweight(1);
u1.hidebubble();

def top;
def bot;
if bn == 1 then {
  top = na;
  bot = na;
} else if up[signal_offset+1] then {
  top = fold i = 1 to len
  while !getvalue(up[signal_offset+1], -i)
  do getvalue(high, -i);

  bot = fold k = 1 to len
  while !getvalue(up[signal_offset+1], -k)
  do getvalue(low, -k);

} else {
  top = top[1];
  bot = bot[1];
}

plot t = top;
t.SetDefaultColor(Color.cyan);
t.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot b = bot;
b.SetDefaultColor(Color.cyan);
b.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

# errors
#AddCloud(top, bot, Color.CYAN, Color.CYAN);

# find first bar of line
def first = if (t[1] <> t[0] and !isnan(close) ) then 1 else 0;

input show_cloud = yes;
def t2 = if first then na else if show_cloud then t else na;
AddCloud(t2, b, Color.CYAN, Color.CYAN);

# -------------------------

input test1_up_signals = no;
addchartbubble(test1_up_signals and up[signal_offset+0], top, "up " + up + "\nH " + high + "\nL " + low,   ( if up[0] then color.cyan else color.gray), yes);

#input test2 = no;
#addchartbubble(test2, bot, "up " + up + "\nH " + high + "\nL " + low,  color.cyan, no);
#addverticalline( ((test1_up_signals or test2) and up[signal_offset+0]), "    UP", color.gray);
#addverticalline( ((test1_up_signals or test2) and up[signal_offset+1]), "    UP", color.gray);
#


in this image, signal_offset = -2. the up signal is shifted to the left. the white arrow is under the original up signal. test bubbles shown are off by default.
eaFtYE1.jpg
 
@halcyonguy So i have another issue (so sorry ;( ) I tried to adapt your code for my other study but i am getting fake cloud and lines somehow idk why. do you have any idea as to why these fake lines and clouds are popping up and how to eliminate them?
Here is the my adaptation of your code for my different study:
Code:
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def Two1TwoDN =
    IsDown[3] and
    IsDown[2] and
    IsUp[1] and
    IsDown[0] and
    close[2] < open[1] and
    open[1] > close[0] and
    open[3] > open[2] and
    close[3] > close[2] and
    open[2] > close[1];
def Two1TwoUP =
    IsUp[3] and
    IsUp[2] and
    IsDown[1] and
    IsUp[0] and
    open[3] < open[2] and
    open[2] < close[1] and
    close[2] > open[1] and
    close[3] < close[2] and
    open[1] < close[0];
###########################
input price = close;
input displace = 0;

def AvgExp = ExpAverage(price[-displace], 9);
input Num_Dev_up = 0.250;
input Num_Dev_dn = -0.250;

#def sDev = StDev(data = price[-displace], length = 9);
#def LowerBand = AvgExp + Num_Dev_dn * sDev;
#def UpperBand = AvgExp + Num_Dev_up * sDev;
#AddCloud(AvgExp, LowerBand, Color.RED, Color.RED);
#AddCloud(AvgExp, UpperBand, Color.GREEN, Color.GREEN);
###########################
def Value = MovingAverage(AverageType.EXPONENTIAL, close, 8) - MovingAverage(AverageType.EXPONENTIAL, close, 21);
def Avg = MovingAverage(AverageType.EXPONENTIAL, Value, 5);
def BullMACD = Value is greater than Avg;
def BearMACD = Value is less than Avg;
###########################
input length = 14;
input averageType = AverageType.WILDERS;
def hiDiff = high - high[1];
def loDiff = low[1] - low;
def plusDM = if hiDiff > loDiff and hiDiff > 0 then hiDiff else 0;
def minusDM =  if loDiff > hiDiff and loDiff > 0 then loDiff else 0;

def ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
def "DI+" = 100 * MovingAverage(averageType, plusDM, length) / ATR;
def "DI-" = 100 * MovingAverage(averageType, minusDM, length) / ATR;
def BullDI =  "DI+" is greater than  "DI-";
def BearDI = "DI+" is less than  "DI-";
############################
def UPCON1 = close is greater than AvgExp;
def UPCON2 = BullMACD and BullDI is true;
def UPcon3 = UPCON1 and UPCON2 is true;
def UP = Two1TwoUP and UPcon3 is true;
AddChartBubble(UP, high, "UP" , Color.YELLOW, yes);
alert(up,"up",alert.bar,sound.bell);
def DNCON1 = close is less than AvgExp;
def DNCON2 = BearMACD and BearDI is true;
def DNcon3 = DNCON1 and DNCON2 is true;
def DN = Two1TwoDN and DNcon3 is true;
AddChartBubble(DN, high, "DN" , Color.WHITE, yes);
alert(dn,"DN",alert.bar,sound.bell);


# leftlines_01c

# chg signal offset

# draw lines before a signal ( to the left)
#--------------------------
def na = double.nan;
def bn = BarNumber();
input len = 500;
input signal_offset = -1;

#def up = if (MACD()."Value" crosses above MACD()."Avg" and MACD()."Value" < 0) then 1 else 0;

# draw an up arrow under the original up signal bar
input test_arrow_up_original = yes;
plot u1 = (test_arrow_up_original and up);
u1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
u1.SetDefaultColor(Color.white);
u1.setlineweight(1);
u1.hidebubble();

def top;
def bot;
if bn == 1 then {
  top = na;
  bot = na;
} else if up[signal_offset+1] then {
  top = fold i = 1 to len
  while !getvalue(up[signal_offset+1], -i)
  do getvalue(high, -i);

  bot = fold k = 1 to len
  while !getvalue(up[signal_offset+1], -k)
  do getvalue(low, -k);

} else {
  top = top[1];
  bot = bot[1];
}

plot t = top;
t.SetDefaultColor(Color.cyan);
t.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot b = bot;
b.SetDefaultColor(Color.cyan);
b.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

# errors
#AddCloud(top, bot, Color.CYAN, Color.CYAN);

# find first bar of line
def first = if (t[1] <> t[0] and !isnan(close) ) then 1 else 0;

input show_cloud = yes;
def t2 = if first then na else if show_cloud then t else na;
AddCloud(t2, b, Color.CYAN, Color.CYAN);

# -------------------------

input test1_up_signals = no;
addchartbubble(test1_up_signals and up[signal_offset+0], top, "up " + up + "\nH " + high + "\nL " + low,   ( if up[0] then color.cyan else color.gray), yes);






plot D1 = (test_arrow_up_original and dn);
d1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
d1.SetDefaultColor(Color.white);
d1.setlineweight(1);
d1.hidebubble();

def top2;
def bot2;
if bn == 1 then {
  top2 = na;
  bot2 = na;
} else if dn[signal_offset+1] then {
  top2 = fold i2 = 1 to len
  while !getvalue(dn[signal_offset+1], -i2)
  do getvalue(high, -i2);

  bot2 = fold k2 = 1 to len
  while !getvalue(dn[signal_offset+1], -k2)
  do getvalue(low, -k2);

} else {
  top2 = top2[1];
  bot2 = bot2[1];
}

plot td = top2;
td.SetDefaultColor(Color.red);
td.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

plot bd = bot2;
bd.SetDefaultColor(Color.red);
bd.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

# errors
#AddCloud(top, bot, Color.CYAN, Color.CYAN);

# find first bar of line
def firstd = if (t2[1] <> t2[0] and !isnan(close) ) then 1 else 0;


def t2D = if firstd then na else if show_cloud then td else na;
AddCloud(t2d, bd, Color.red, Color.red);

# -------------------------

#input test1_up_signals = no;
#addchartbubble(test1_up_signals and up[signal_offset+0], top, "up " + up + "\nH " + high + "\nL " + low,   ( if up[0] then color.cyan else color.gray), yes);

qA1EMEB.png


You see the "DN" Bubble? the red cloud to it's left is correct however, the teal cloud to the left and overlaping cloud to the right (blue zigzags) are incorrect. I dont understand that is going on or what is it using to plot that cloud since those candles havent formed yet to satify my condition for a plot.

Thank you so much for your time, i really really appreciate it.
 
here is the updated code


EDIT ----- 1/8
ver01b
added the suggested cloud code from @SleepyZ , thank you
AddCloud(t, b, Color.CYAN, Color.CYAN);
it references the plotted variables , instead of the source variables.

i used a big number, 500 , for the fold loop, because i wasn't sure how many bars would exist between signals. because of this , it is complex. this number could be reduced to a small number, to make it not complex, but only if it can be verified that it doesn't miss signals.

----------------------------------------

is this what you are looking for?
this will draw 2 horizontal lines, left of the signal bar, at the high and low, of the bar with the up signal.
but using fold for this, creates data that isn't usable in addcloud, so this doesn't draw clouds.
Always great work. What do I need to change to have the lines on the right side instead of the left?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
226 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