 # ThinkorSwim showing N/A on a bubble or label

#### cabe1332

##### Active member
Hi All,

I usually find what I look for or the solution to my problem, but I getting a headache searching or working on the small problem. This is my first asking/posting for help.

When trading or scalping, I usually focus on the current candle. I thought it would be easier if I see a live/real-time bubble on the current candle with the price, p/l, and my target percent gain/exit in mind. I also added a momentum indicator that would help me exit and determine to stay long. The bubble turns green if buy and red if sell on the last trade sale. I am happy with it and it does help.

Now, my issue is the pisky "N/A". This only shows on the percent column when I do not own the position. The data is filled when I own the position. Can someone give me some ideas to fix or solutions for my issue, please?

Below is my code for P/L and some screenshots. If you have any questions, let me know. Thank you in advance.

Code:
``````# Bubble Profiting or Losing
# cabe1332

# start of code

def GetOpenPL = GetOpenPL();
def GetAveragePrice = if GetOpenPL() != 0 then GetAveragePrice() else double.NaN;
def AvgPrice = if GetAveragePrice != 0 then close - GetAveragePrice else double.NaN;
def plpct = if round((AvgPrice/GetAveragePrice)*100,2) != 0 then round((AvgPrice/GetAveragePrice)*100,2) else double.NaN;

# Chart Bubble
addChartBubble(bn == currentBar - barsBack, close,
if Highstaterange then "Price " + round(close,4)+ " | Momo " + round(MoMo) * 100 + " | " + round(Pctawayhi,0) + "% near 52Wk High" + " | \$ " + Round(GetOpenPL)  + " | " + plpct + " %"
else "Price " + round(close,4) + " | Momo " + round(MoMo)* 100 + " | \$ " + Round(GetOpenPL) + " | " + plpct + " %",
# background color
if price >= price then color.green else color.light_red);

# end of code``````   ##### Well-known member
VIP
@cabe1332 The code you posted isn't even valid and throws errors... If you post some valid code we might be able to help...

#### cabe1332

##### Active member
@cabe1332 The code you posted isn't even valid and throws errors... If you post some valid code we might be able to help...
Hi @rad14733, try the code below which produces a label of P/L. This shows N/A if you don't own the position, want it to be 0. Thanks in advance.

# Label if Profiting or Losing
# cabe1332

def GetOpenPL = GetOpenPL();
def GetAveragePrice = GetAveragePrice();
def AvgPrice = if GetAveragePrice != 0 then close - GetAveragePrice else 0;

AddLabel(yes, " P/L: \$ " + Round(GetOpenPL) + " | "
+ (round((AvgPrice/GetAveragePrice)*100,2) + " %") # This shows N/A if you don't own the position, want it to be 0.
, if GetOpenPL >= 0 then Color.green else Color.red);

#### Pensar

##### Well-known member
VIP
@cabe1332 - Here's something that shows zero, you can delete the quantity value if you wish.

Code:
``````def PL = GetOpenPL();
def Entry = if IsNaN(GetAveragePrice()) then Entry else GetAveragePrice();
def LastEntryBar = if Entry != Entry then BarNumber() else LastEntryBar;
def PercentChg = if Entry then (close - Entry) / Entry else 0;

AddLabel(1, "Qty: " + GetQuantity() +
" | P/L: " + AsDollars(PL) +
" | " + AsPercent(PercentChg),
if PL == 0 then Color.CYAN
else if PL > 0 then Color.GREEN
else Color.DARK_ORANGE);``````

#### cabe1332

##### Active member
@cabe1332 - Here's something that shows zero, you can delete the quantity value if you wish.

Code:
``````def PL = GetOpenPL();
def Entry = if IsNaN(GetAveragePrice()) then Entry else GetAveragePrice();
def LastEntryBar = if Entry != Entry then BarNumber() else LastEntryBar;
def PercentChg = if Entry then (close - Entry) / Entry else 0;

AddLabel(1, "Qty: " + GetQuantity() +
" | P/L: " + AsDollars(PL) +
" | " + AsPercent(PercentChg),
if PL == 0 then Color.CYAN
else if PL > 0 then Color.GREEN
else Color.DARK_ORANGE);``````

@Pensar Thank you for the updated code. It worked. A "0%" is better to look at than "N/A". I updated my chart bubble with the change. Sharing some screenshots, a sell-off day showed down position. Thanks again. cabe1332  Last edited:
• Pensar

#### Therival

##### New member
Hey @cabe1332 would you mind sharing the complete code for this? I cant for the life of me get the add chart bubble to pull from the defs. This is what I have so far using what @Pensar posted as a base.

Code:
``````#Profit or Loss Current Candle

#Start of Code

def PL = GetOpenPL();
def Entry = if IsNaN(GetAveragePrice()) then Entry else GetAveragePrice();
def LastEntryBar = if Entry != Entry then BarNumber() else LastEntryBar;
def PercentChg = if Entry then (close - Entry) / Entry else 0;

AddLabel(1, "Qty: " + GetQuantity() +
" | P/L: " + AsDollars(PL) +
" | " + AsPercent(PercentChg),
if PL == 0 then Color.CYAN
else if PL > 0 then Color.GREEN
else Color.DARK_ORANGE);

def LastBar = !IsNaN(close) and IsNaN(close[-1] ) ;

#End of Code``````

Last edited by a moderator:

#### cabe1332

##### Active member
Hey @cabe1332 would you mind sharing the complete code for this? I cant for the life of me get the add chart bubble to pull from the defs. This is what I have so far using what @Pensar posted as a base.

Code:
``````#Profit or Loss Current Candle

#Start of Code

def PL = GetOpenPL();
def Entry = if IsNaN(GetAveragePrice()) then Entry else GetAveragePrice();
def LastEntryBar = if Entry != Entry then BarNumber() else LastEntryBar;
def PercentChg = if Entry then (close - Entry) / Entry else 0;

AddLabel(1, "Qty: " + GetQuantity() +
" | P/L: " + AsDollars(PL) +
" | " + AsPercent(PercentChg),
if PL == 0 then Color.CYAN
else if PL > 0 then Color.GREEN
else Color.DARK_ORANGE);

def LastBar = !IsNaN(close) and IsNaN(close[-1] ) ;

#End of Code``````

Hi @Therival, sure I can share. Below you'll find a strip-down code and screenshot I use for my futures bubble chart. You'll find the momentum "MoMo" indicator value is very useful where it can help with decisions either to stay long on a stock or looking for a swing trade. On a daily chart, use it with Blast Off and the MoMo value will help you how active the stock on that day.

Good luck!

cabe1332

# FuturesMoMOBubbleChart
# Bubble on the chart with current bar candle and price with momentum value
# cabe1332 20210303

# Start code

# bubble for current bar
input barsBack = 0;
input price = close;
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(price) then bn else Double.NaN);

#MOMENTUM
input lengthmo = 5;
def c = close;
def Momentum = c - c[lengthmo];
def NegMomentum = c - c[lengthmo];;
def mopos = if Momentum >= 0 then 3 else 2;
def moneg = if Momentum < 0 then 3 else 2;
def MoMo = if Momentum >= 0 then momentum else if Momentum < 0 then NegMomentum else double.nan;
def MoMoInd = round(MoMo)* 100;

addChartBubble(bn == currentBar - barsBack, close, "\$ " + round(close,4) + " | Momo " + MoMoInd,
# background color
if price >= price then color.green else color.light_red);

# end of code   Last edited:
• Michael Kim and Therival

#### Therival

##### New member
Hi @Therival, sure I can share. Below you'll find a strip-down code and screenshot I use for my futures bubble chart. You'll find the momentum "MoMo" indicator value is very useful where it can help with decisions either to stay long on a stock or looking for a swing trade. On a daily chart, use it with Blast Off and the MoMo value will help you how active the stock on that day.

Good luck!

cabe1332

# FuturesMoMOBubbleChart
# Bubble on the chart with current bar candle and price with momentum value
# cabe1332 20210303

# Start code

# bubble for current bar
input barsBack = 0;
input price = close;
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(price) then bn else Double.NaN);

#MOMENTUM
input lengthmo = 5;
def c = close;
def Momentum = c - c[lengthmo];
def NegMomentum = c - c[lengthmo];;
def mopos = if Momentum >= 0 then 3 else 2;
def moneg = if Momentum < 0 then 3 else 2;
def MoMo = if Momentum >= 0 then momentum else if Momentum < 0 then NegMomentum else double.nan;
def MoMoInd = round(MoMo)* 100;

addChartBubble(bn == currentBar - barsBack, close, "\$ " + round(close,4) + " | Momo " + MoMoInd,
# background color
if price >= price then color.green else color.light_red);

# end of code   Awesome, thank you very much it looks great. Now I just need to decode the addchartbubble that thing is my bane haha.

#### Therival

##### New member
Here is the code me and my buddy came up with for anyone that stumbles on this and wants the same thing.

Since @rad14733 asked my general strategy is using the HMA, along with EMA 8 and 21 cross overs that I have set to a buy or sell indicator along with cloud strength to show buying and selling trend. I have been scalping GME the past week with this and any time 2/3 indicators show buy or sell I enter or exit a trade. For a long time I only used the HMA and have been very successful with scalping but since I have added these other two indicators it allows me to "confirm" the trend so to speak.

Code:
``````# Profit or Loss Current Candle
#Base Code provided by Pensar
#Modified by Therival and DoubleJinx

# Start of Code

# defs
def PL = GetOpenPL();
def Entry = if IsNaN(GetAveragePrice()) then Entry else GetAveragePrice();
def LastEntryBar = if Entry != Entry then BarNumber() else LastEntryBar;
def PercentChg = if Entry then (close - Entry) / Entry else 0;
def qty = GetQuantity();

# label
AddLabel(1, "Qty: " + GetQuantity() +
" | P/L: " + AsDollars(PL) +
" | " + AsPercent(PercentChg),
if PL == 0 then Color.CYAN
else if PL > 0 then Color.GREEN
else Color.DARK_ORANGE);

# bubble for current bar
input barsBack = 0;
input price = close;
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(price) then bn else Double.NaN);

#P/L Chart Bubble
addChartBubble(bn == currentBar - barsBack, close, AsDollars(PL) + "| " + AsPercent(PercentChg) + "|" + qty,

# background color
if PercentChg >= 0 then color.green else color.light_red);``````

Last edited:
• Pensar

#### Michael Kim

##### New member
Hi @Therival, sure I can share. Below you'll find a strip-down code and screenshot I use for my futures bubble chart. You'll find the momentum "MoMo" indicator value is very useful where it can help with decisions either to stay long on a stock or looking for a swing trade. On a daily chart, use it with Blast Off and the MoMo value will help you how active the stock on that day.

Good luck!

cabe1332

# FuturesMoMOBubbleChart
# Bubble on the chart with current bar candle and price with momentum value
# cabe1332 20210303

# Start code

# bubble for current bar
input barsBack = 0;
input price = close;
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(price) then bn else Double.NaN);

#MOMENTUM
input lengthmo = 5;
def c = close;
def Momentum = c - c[lengthmo];
def NegMomentum = c - c[lengthmo];;
def mopos = if Momentum >= 0 then 3 else 2;
def moneg = if Momentum < 0 then 3 else 2;
def MoMo = if Momentum >= 0 then momentum else if Momentum < 0 then NegMomentum else double.nan;
def MoMoInd = round(MoMo)* 100;

addChartBubble(bn == currentBar - barsBack, close, "\$ " + round(close,4) + " | Momo " + MoMoInd,
# background color
if price >= price then color.green else color.light_red);

# end of code   How to make red and orange color when trend up and red when down?
Is this using EMA?

#### cabe1332

##### Active member
How to make red and orange color when trend up and red when down?
Is this using EMA?
@Michael Kim try the ema50 code. below It will provide you the answer and get your feet wet in coding.

# EMA50 plot and label
# @cab1332

#code start
input price = close;
input length = 50;
input displace = 0;

plot EMA50 = MovingAverage(AverageType.eXPONENTIAL, price, length)[-displace];

EMA50.DefineColor("Up", color.green);
EMA50.DefineColor("Down", color.red);
EMA50.AssignValueColor(if EMA50 > EMA50 then EMA50.color("Up") else EMA50.color("Down"));

addlabel(Yes,"50EMA = " + round(emA50,2), if price > ema50 then Color.GREEN else Color.RED);
# code end

#### Wizedon

##### New member
VIP
Hi! I am testing this MOMO and EMA. What are the meaning of the numbers on the MOMO and EMA bubble? What are their range and significance? This will help me to better maximize its use. Thanks in advance for your response.

Last edited by a moderator: