Relocate AddChartBubble to outside of chart

tlee404

New member
How do I get a Bubble to appear outside the chart like other Bubbles for studies such as MACD, SMA,etc? I have did some research but can't find any code to relocate the Bubble outside the chart. I created code to show the Close with a Bubble. But the Bubble is inside the chart.

Code:
#Daily Close
input aggregationPeriod = AggregationPeriod.DAY;
input showOnlyLastPeriod = yes;

def prevPrice = close(period = aggregationPeriod)[-1];
def price = close(period = aggregationPeriod);

plot DailyClose = if showOnlyLastPeriod and !IsNaN(prevPrice) then Double.NaN else price;
def x = DailyClose;
DailyClose.SetDefaultColor(GetColor(9));
DailyClose.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

AddChartBubble((x + 8) > DailyClose, close, close, Color.yellow, yes);

It appears my Question was truncated or changed. It should have been the same as the 1st sentence of my post.

After further research found the following.
  • The (x +8) > Daily Close, was not necessary, it only needed an x, you cannot use Close for it .
  • AddChartBubble((x + 8) > DailyClose, close,: if you add or subtract such as close +5, it will move the vertical position of the bubble.
But the amount moved seems to be dependent on the price of the stock and is not constant. It appears the bubble will appear on the right side of the candle, however if you don't have enough space on the right side of the chart to fit it, it will appear on the left side. This causes a problem as the close is the last candle on the chart so if you don't leave at least 6 extra candles spaces after the close the bubble will appear on the left side of the close. This makes it a problem when the previous prices are above the closing price of the day. As the bubble may conflict with other candles or bubbles. That is the reason I would have liked to have the bubble on the outside edge of the chart such as MACD, SMA,etc.

If your wondering, I coded this because I use Heikin Ashi candles, and as you know the closing price it shown is not the real closing price.
And I get tired of having to take the time to move my head to see the real closing price at the far left side of the chart. Also, I have a limited coding experience., but I can patch things together, which is how I created this after doing some research and sending hours to create it. You can be sure I will be asking for advice in the future. Hope this can help someone in the future.
 
Last edited by a moderator:
Solution
@tlee404 Noticed that your code definition for the variable "prevPrice", you're indexing 1 bar into the future. To index this as a bar before, your definition needs to be changed to

Code:
def prevPrice = close(period = aggregationPeriod)[1];

Your other comment about locating the bubble outside the chart - I take it you mean you'd like to display this in the expansion space?
Note that bubbles are located using an X, Y axis where X is the first variable usually denoted by a barNumber() or horizontal location and Y is the vertical or price location. Looking at your first variable for AddChartBubble() - that evaluates to a boolean rather than a horizontal location.

Hope that helps
@mourningwood4521 Your code is doing exactly what you are asking it to do... If you don't want a bubble for each candle then you need to use if . . . then . . . else Double.Nan on failure... That Double.NaN stops the constant painting from happening...

Ruby:
plot UpStep = if upStep1 or upStep1[-1] or upStep2 or upStep2[-1] and zigZag == low then 1 else Double.NaN;
plot DownStep = if downStep1 or downStep1[-1] or downStep2 or downStep2[-1] and zigZag == high then 1 else Double.NaN;
 

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

Hello All

I have a thinkscript that plots chart bubbles based on certain market conditions, however, I only want the most recent highest or lowest chart bubble plotted - is this possible?

For example, if I were to pull up an hour chart there would be 10 or so chart bubbles because the criteria was meet that many times, however, I would only like to see the most recent chart bubble because the other 9 (in this case) I don't care about anymore.

Code:
input displace = 0;
input length = 20;
input price=close;

input length1 = 14;
input averageType = AverageType.WILDERS;

plot ADX = DMI(length1, averageType).ADX;
ADX.setDefaultColor(GetColor(5));
ADX.hide();

plot ATR = MovingAverage(averageType, TrueRange(high, close, low), length1);
ATR.hide();

plot LowerBand = Lowest(low[-displace + 1], length);
LowerBand.SetDefaultColor(GetColor(8));

plot UpperBand = Highest(high[-displace + 1], length);
UpperBand.SetDefaultColor(GetColor(1));

def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def UpperWick = high - Max(open, close);
def LowerWick = Min(open, close) - low;

plot h=high;
h.hide();
plot l=low;
l.hide();

input length4 = 200;
input displace5 = 0;
input showBreakoutSignals = no;

plot AvgExp = ExpAverage(price[-displace5], length4);

AddChartBubble(if BarNumber() and price > upperBand and price >=AvgExp then 1 else 0, high, "ENTRY", Color.RED, yes);
AddChartBubble(BarNumber() and price < lowerband and price <AvgExp, bodymin, "ENTRY", Color.GREEN, no);

Thanks
 
@british43

One approach I've used is to count the number of times the condition is true and then only show the last one by finding when that count is at its highest. You can also use this to mark the first, by using count == 1.

Hope this helps,
Jay

Code:
def sellCond = if BarNumber() and price > upperBand and price >=AvgExp then 1 else 0;
def sellCondCount = if sellCond then sellCondCount[1] + 1 else sellCondCount[1];

AddChartBubble(sellCond and sellCondCount == HighestAll(sellCondCount), high, "ENTRY", Color.RED, yes);
 
I can't get the addchartbubble to plot only on the first bar of the line.

Here is what I'm dealing with.
s0Edby1.png


So all I want is for this one to show the wavelength which is 13, like it's doing, but only that very first one that is above the #1 in blue.
I've tried a bunch of different formats to try to get it to stick there, and the ones that I used after doing a command F search on the entire document for "addchartbubble" over on this super informative document, I've been using various combinations on this all morning.

I'm just going to link the very part I'm having trouble with, which is the part that I mourningwood4521 wrote, because all im doing is adding to the code which is copyrighted I'm pretty sure.


Note*** I was changing the order of the very bottom addchartbubble line as I went along, so I don't have all the combos written out.
Code:
plot bear13 = if !v1[-1] and av13 then vl13 else Double.NaN;
     bear13.SetDefaultColor(color.pink);

#1st try
#Input Offset_1 = 0;
#def LastBar_1 = !IsNaN(bear13) and IsNaN(bear13 [-1] ) ;
#def barNum = barNumber();
#def FirstBar = if barNum == 1 then 1 else 0;
#Def BubbleLocation = firstbar[Offset_1];


#2nd try
#Define variables used to place a bubble
#========================================
#Input Offset = BarNumber() / 2;
def barNum = BarNumber();
def offset_1 = 0;
def LastBar_1 = !IsNaN(bear13) and IsNaN(bear13 [-1] ) ;
def BubbleLocation = LastBar_1[offset_1];
def FirstBar = if barNum == 1 then 1 else 0;
def FirstBarValue = if barNum == 1 then 1 else 0;
def LastBarValue = if LastBar_1 then barNum else 0;
def MidBar = if LastBar_1 then barNum == (BarNumber() / 2)  else 0;
#example
#addchartbubble(LastBar_1,45, "This is a last bar bubble", color.White);
#-----------------------

#3rd and 4th try
#AddLabel( yes, "Total bars = " + barNum, ), Color.pink );
#addchartbubble(BubbleLocation , close, "Last Bar_1", color.white);
#addchartbubble(FirstBar, close, "FirstBar", color.white);

#def open_1 = open(bear13);


#the label as shown in the picture.
addchartBubble(labelpoints and bear13,  bear13, wavelength, color.blue, yes);
 
@mourningwood4521 The bubble needs to be locked along the x-axis, so perhaps try something like
Code:
def lock = !isnan(close) and isnan(close[-1]);
addchartBubble(labelpoints and lock, bear13, wavelength, color.blue, yes);
That's just an example, of course. If you use it, it would plot the bubble in line with the current bar.
 
@XeoNoX the 13 is the wavelength, and I'm trying to make it so it shows me the wavelength for each of the different wavelengths that return values, so, I will be adding this study with a wavelength of 5, 10, 13, 21, 29, etc, and this will all be on 1 time frame say 1D:1M, and I want to see which wavelength is being plotted without having to change the color for different wavelengths.

@Pensar ohhh that's awesome, i'm going to try to see if I can add first bar instead of close, and incorporate an open of bear 13 as being the only place it can do it.
 
@XeoNoX the 13 is the wavelength, and I'm trying to make it so it shows me the wavelength for each of the different wavelengths that return values, so, I will be adding this study with a wavelength of 5, 10, 13, 21, 29, etc, and this will all be on 1 time frame say 1D:1M, and I want to see which wavelength is being plotted without having to change the color for different wavelengths.

@Pensar ohhh that's awesome, i'm going to try to see if I can add first bar instead of close, and incorporate an open of bear 13 as being the only place it can do it.
@mourningwood4521 Look at the indicator code and find the AddChartBubble() that plots the "1", then use the variables coded into the "1" bubble to place your new bubble at the same spot. That should probably do the trick.
 
@mourningwood4521 Look at the indicator code and find the AddChartBubble() that plots the "1", then use the variables coded into the "1" bubble to place your new bubble at the same spot. That should probably do the trick.
The issue is, it's a complex code, and this guy who wrote it did so very well and his way of thinking is very different than ours, he did this for the label section, followed by the add chart bubble start location with the color. I'll post it below cuz this section is on line 80 something and how he defines the variables above that is something not many people could recreate. pic at the bottom as well.

I was trying to add the 13 in with the 1 without making it add the 1 and 13 together, but I couldn't do it without making it not do the math. I was about to give up and then realized there is also a plot line along those label points which are also reversal points, and I am now exploring my options there.

Code:
def bullc = if p1 then 1 else if p2 then 2 else if p3 then 3 else if p4 then 4 else if p5 then 5 else 0;
def bearc = if v1 then 1 else if v2 then 2 else if v3 then 3 else if v4 then 4 else if v5 then 5 else 0;

AddChartBubble(labelPoints and bullc > 0, if peak then high else low, bullc,   createcolor(51, 238, 16)   , if peak then 1 else 0);  # was color.white
AddChartBubble(labelPoints and bearc > 0, if peak then high else low, bearc, createcolor(0, 0, 255), if peak then 1 else 0);     # was color.pink


so p1 is the def behind 15 or so lines of def's, and is the area where the 1 is labeled.

K02OKY0.png
 
update1: I changed it to this and got this
Code:
Addchartbubble(labelpoints and bearc > 0, if peak then high else low, wavelength,   color.red, yes);
DyyVXM9.png



update2:

the middle section-- if peakhighlow then ________ is the location that the plot is starting at. V1 plots it very close to zero.

I need to figure out how to say the open of a study, the current format I have is:

Code:
def peakhighlow = if peak then high else low;
def firstbar = open;
#firstbar(bear13);
Addchartbubble(labelpoints and bearc > 0, if peakhighlow then firstbar(bear13) else double.NaN, wavelength,   color.red, if peak then 1  else 0 );

I know I've had studies reference the open, but i'm unsure as the format to use because the open was inside the ()-- (open);, and ive changed the word open to whatever study I had defined further above that point.


update 3: thank you to @Pensar for all the help!
final product looks like this
Code:
addchartBubble(labelpoints and v1, bear13, wavelength, color.red, if peak then 1 else 0);
addchartBubble(labelpoints and p1, bull13, wavelength, color.red, if peak then 1 else 0);

lfEa7bh.png
 
Last edited:
update1: I changed it to this and got this
Code:
Addchartbubble(labelpoints and bearc > 0, if peak then high else low, wavelength,   color.red, yes);
DyyVXM9.png
@mourningwood4521 After looking at the samples you provided, try this -

Code:
addchartBubble(labelpoints and v1, bear13, wavelength, color.blue, yes);

Alternatively, you can do something like this to add more text to a bubble -
Code:
AddChartBubble(labelPoints and bearc > 0, if peak then high else low, bearc + " " + wavelength, createcolor(0, 0, 255), if peak then 1 else 0);

I have no idea if that will work for you, but if you have'nt figured it out yet, maybe it can help.
 
First Post and request for help.

Looking for a very simple addchartBubble solution.
Not simple for me, newbe...

Want to add a custom text at right end of Moving ave.

Code:
#Ver02: Testing on TOS
# Want to add text bubble on the right of the SMA Line, "I want to "Customize" The Text string"

input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 12;
input averageType = AverageType.SIMPLE;
input  String = "12D SMA";
plot MovAvg = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod), length);
#Have error on this line----  AddChartBubble ("String", Color.LIGHT_GREEN, no);
# I have no idea how to do this...
 
Explnation:
I made the indecator because it works on some multible time frames, does work on ones i use
for exsmple: agg period = 5min length 624 is my 8d ema.
so want to put bubble at end saying "8 dema"
Thanks for any help.

Code:
#2nd try:
#Ver02: Testing on TOS
# Want to add text bubble on the right of the SMA Line, "I want to "Customize" The Text string"

input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 12;
input averageType = AverageType.SIMPLE;
input variable1 = "12D SMA";
plot MovAvg = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod), length);
#AddChartBubble(MovAvg, variable1, Color.RED, yes); ---- Works great without this line and "input variable1" line
# I have no idea how to do this...
# Thanks
 
Explnation:
I made the indecator because it works on some multible time frames, does work on ones i use
for exsmple: agg period = 5min length 624 is my 8d ema.
so want to put bubble at end saying "8 dema"
Thanks for any help.

Code:
#2nd try:
#Ver02: Testing on TOS
# Want to add text bubble on the right of the SMA Line, "I want to "Customize" The Text string"

input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 12;
input averageType = AverageType.SIMPLE;
input variable1 = "12D SMA";
plot MovAvg = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod), length);
#AddChartBubble(MovAvg, variable1, Color.RED, yes); ---- Works great without this line and "input variable1" line
# I have no idea how to do this...
# Thanks

Here are 2 methods to place the bubble in the right expansion area. See annotations for explanations.

Screenshot 2023-10-10 092201.png
Code:
#2nd try:
#Ver02: Testing on TOS
# Want to add text bubble on the right of the SMA Line, "I want to "Customize" The Text string"

input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 12;
input averageType = AverageType.SIMPLE;
input variable1 = "12D SMA";
plot MovAvg = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod), length);
#AddChartBubble(MovAvg, variable1, Color.RED, yes); ---- Works great without this line and "input variable1" line
# I have no idea how to do this...
# Thanks

input bubble = yes;
input bubblemover = 3;
def   b = bubblemover;
def   b1 = b + 1;
AddChartBubble(bubble and IsNaN(close[b]) and !IsNaN(close[b1]), MovAvg[b1], variable1, Color.RED);
#To place a bubble in the right expansion area, we need to identify the location of the last candle displayed. The isnan(close[b]) and !isnan(close[b1]) defines last candle. The bubblemover moves the bubble sideways by using an input to accomplish it. The b and b1 identifies how many bars in the expansion area that the bubble is located.

#Therefore without the bubblemover method, then this would be another way to locate the bubble:
input test = yes;
AddChartBubble(test and IsNaN(close[3]) and !IsNaN(close[4]), MovAvg[4], variable1, Color.RED);
#The test bubble usses the same last candle, causing the test bubble to be stacked above the bubblemover version. The last candle defined by bars is isnan(close[-1]) and !isnan(close[0]). The IsNaN(close[3]) and !IsNaN(close[4]) moves the bubble 3 bars to the rightt of the last candle.  The MovAvg[4] references back 4 bars to the last movavg plot at the last candle.
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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