AddChartBubble Tutorial For ThinkOrSwim

Sabs

New member
AddChartBubble overlaps bars when used with current prices as shown in my picture below. How would I go about moving the bubble 2 bars to the right, for example, so it doesn't overlap?

Code:
input EMAValue = 30;

def lastPrice = close(priceType = PriceType.LAST);
def _ChosenMovingAverage = MovingAverage(AverageType.Exponential, close, EMAValue);

AddChartBubble(IsNaN(close[-1]) and !IsNAN(close), close, AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1)), Color.GREEN);

H02ouEE.jpg
 
Last edited:
Solution
AddChartBubble overlaps bars when used with current prices as shown in my picture below. How would I go about moving the bubble 2 bars to the right, for example, so it doesn't overlap?

I don't believe you can shift a bubble in the way you want. However, you can move it slightly to get it out of your way by placing it at the high or low and then having it point up or down.

Place it at the high and above:
AddChartBubble(IsNaN(close[-1]) and !IsNAN(close), high, AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1)), Color.GREEN);

Place it at the low and below (notice the 0 on the end):
AddChartBubble(IsNaN(close[-1]) and !IsNAN(close), low...
AddChartBubble overlaps bars when used with current prices as shown in my picture below. How would I go about moving the bubble 2 bars to the right, for example, so it doesn't overlap?

I don't believe you can shift a bubble in the way you want. However, you can move it slightly to get it out of your way by placing it at the high or low and then having it point up or down.

Place it at the high and above:
AddChartBubble(IsNaN(close[-1]) and !IsNAN(close), high, AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1)), Color.GREEN);

Place it at the low and below (notice the 0 on the end):
AddChartBubble(IsNaN(close[-1]) and !IsNAN(close), low, AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1)), Color.GREEN,0);

Another alternative would be to add it as a label instead of a bubble, that way it's completely out of the way:
def PChange = AbsValue((lastPrice/_ChosenMovingAverage) - 1);
AddLabel(if IsNaN(close[-1]) and !IsNAN(close) then 1 else 0, "Change: "+AsPercent(PChange), color.green);


Edit:
Since I found this just a minute after replying, you could do something like this apparently (just change for your condition):

Ruby:
# plots a bubble x bars after last bar
input bars_in_future = 3;
def bif = bars_in_future;
def cls = close;
def x = isNaN(cls[bif]) and !isNaN(cls[bif+1]);
def vert = cls[bif+1];
AddChartBubble(x, vert, vert, color.white, yes);
 
Last edited:
Solution

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

Thank you for the response. I had found similar code for adding bars that I didn't have success with. I tried the code you posted in your edit and had the same issue.

The code works as posted for displaying the closing price in the desired position. However, if I attempt to replace the text that the bubble displays, "N/A" appears. It does move the bubble to the desired position.

Code:
AddChartBubble(x, vert, vert, color.white, yes);

For instance, if I replace the 2nd vert with something simple like cls then N/A is displayed. In my case, I'm trying to display the % from the EMA with "AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1))" and it displays N/A.

vaNd1Ii.png


Full code with the N/A issue:

Code:
input EMAValue = 30;

def lastPrice = close(priceType = PriceType.LAST);
def ChosenMovingAverage = ExpAverage(close, EMAValue);

# plots a bubble x bars after last bar
input bars_in_future = 2;
def bif = bars_in_future;
def cls = close;
def x = isNaN(cls[bif]) and !isNaN(cls[bif+1]);
def vert = cls[bif+1];

#Bubble
AddchartBubble(x, vert, AsPercent(AbsValue((lastPrice/ChosenMovingAverage) - 1)), Color.white);

I appreciate your help.
 
Try this

Ruby:
input EMAValue = 30;


# plots a bubble x bars after last bar
input bars_in_future = 2;
def bif = bars_in_future;
def cls = close;
def x = isNaN(cls[bif]) and !isNaN(cls[bif+1]);
def vert = cls[bif+1];
plot lastPrice = close(priceType = PriceType.LAST)[bif];
plot ChosenMovingAverage = ExpAverage(close[bif], EMAValue);

#Bubble
AddchartBubble(x, vert, AsPercent(AbsValue((lastPrice[1]/ChosenMovingAverage[1]) - 1)), Color.white);
 
Thank you for the response. I had found similar code for adding bars that I didn't have success with. I tried the code you posted in your edit and had the same issue.

The code works as posted for displaying the closing price in the desired position. However, if I attempt to replace the text that the bubble displays, "N/A" appears. It does move the bubble to the desired position.

Code:
AddChartBubble(x, vert, vert, color.white, yes);

For instance, if I replace the 2nd vert with something simple like cls then N/A is displayed. In my case, I'm trying to display the % from the EMA with "AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1))" and it displays N/A.

vaNd1Ii.png


Full code with the N/A issue:

Code:
input EMAValue = 30;

def lastPrice = close(priceType = PriceType.LAST);
def ChosenMovingAverage = ExpAverage(close, EMAValue);

# plots a bubble x bars after last bar
input bars_in_future = 2;
def bif = bars_in_future;
def cls = close;
def x = isNaN(cls[bif]) and !isNaN(cls[bif+1]);
def vert = cls[bif+1];

#Bubble
AddchartBubble(x, vert, AsPercent(AbsValue((lastPrice/ChosenMovingAverage) - 1)), Color.white);

I appreciate your help.
This should help. I changed def vert to be a number defining how many bars the data from the last candle has to be offset to display in the bubble. In other words the x displays the bubble one bar to the right of the last bar. The input bars_in_future moves it 2 more to the right. Vert is then defined to be the 2 bars plus the 1 that the bubble was already moved to the right by x.

Capture.jpg
Ruby:
input EMAValue = 30;

def lastPrice = close(priceType = PriceType.LAST);
def ChosenMovingAverage = ExpAverage(close, EMAValue);

# plots a bubble x bars after last bar
input bars_in_future = 2;
def bif = bars_in_future;
def cls = close;
def x = isNaN(cls[bif]) and !isNaN(cls[bif+1]);
def vert = bif+1;

#Bubble
AddchartBubble(x, close[vert], AsPercent(AbsValue((lastPrice[vert]/ChosenMovingAverage[vert]) - 1)), Color.white);
 
Each reference to [vert] within AddChartBubble returns the following error: "Only constants expected here: vert CL function indexer of" close/lastPrice/ChosenMovingAverage
 
Are you using the code I posted above? I am not having that problem. Here is a share link to the code above. Try that. https://tos.mx/ewHD8F4

Thanks for the link - I must've entered something incorrectly because it works great for me now!

Separately, I'm working on assigning different colors to the bubble based on values but I will post separately about that. Thanks again!
 
Thank you for the response. I had found similar code for adding bars that I didn't have success with. I tried the code you posted in your edit and had the same issue.

The code works as posted for displaying the closing price in the desired position. However, if I attempt to replace the text that the bubble displays, "N/A" appears. It does move the bubble to the desired position.

Code:
AddChartBubble(x, vert, vert, color.white, yes);

For instance, if I replace the 2nd vert with something simple like cls then N/A is displayed. In my case, I'm trying to display the % from the EMA with "AsPercent(AbsValue((lastPrice/_ChosenMovingAverage) - 1))" and it displays N/A.

vaNd1Ii.png


Full code with the N/A issue:

Code:
input EMAValue = 30;

def lastPrice = close(priceType = PriceType.LAST);
def ChosenMovingAverage = ExpAverage(close, EMAValue);

# plots a bubble x bars after last bar
input bars_in_future = 2;
def bif = bars_in_future;
def cls = close;
def x = isNaN(cls[bif]) and !isNaN(cls[bif+1]);
def vert = cls[bif+1];

#Bubble
AddchartBubble(x, vert, AsPercent(AbsValue((lastPrice/ChosenMovingAverage) - 1)), Color.white);

I appreciate your help.

that is some code i posted awhile ago, post2

if you change one of the first 3 parameters, (time, price, text) you have to make sure the variable has data.
if the time data causes the bubble to be placed after the last bar then the other 2 parameters, price and text, may need an offset to look back at older bars , where valid price data is.
If you just put close as one of the parameters, and there is no price data , then it will be an NA.
 
that is some code i posted awhile ago, post2

if you change one of the first 3 parameters, (time, price, text) you have to make sure the variable has data.
if the time data causes the bubble to be placed after the last bar then the other 2 parameters, price and text, may need an offset to look back at older bars , where valid price data is.
If you just put close as one of the parameters, and there is no price data , then it will be an NA.

Got it, thanks so much for the explanation! Makes sense now, and that helped me fix my if-then-else color parameter within the same line.

You guys are code wizards!
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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