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:
# 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);
 
Last edited by a moderator:
@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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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 - 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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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". Thanks again. cabe1332
 
Last edited by a moderator:
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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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] ) ;

addChartBubble( LastBar, close, "PL");

#End of Code
 
Last edited by a moderator:
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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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] ) ;

addChartBubble( LastBar, close, "PL");

#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[1] then color.green else color.light_red);

# end of code
 
Last edited by a moderator:
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[1] then color.green else color.light_red);

# end of code


CfD0N02.png


1YiXEs7.png


2o4MER3.png

Awesome, thank you very much it looks great. Now I just need to decode the addchartbubble that thing is my bane haha.
 
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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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:
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[1] then color.green else color.light_red);

# end of code


CfD0N02.png


1YiXEs7.png


2o4MER3.png
How to make red and orange color when trend up and red when down?
Is this using EMA?
 
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 *** 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[1] then EMA50.color("Up") else EMA50.color("Down"));

addlabel(Yes," ", Color.orange);
addlabel(Yes,"50EMA = " + round(emA50,2), if price > ema50 then Color.GREEN else Color.RED);
# code end
 
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:
I think Wizedon may have been referring to was, what unit of measure does a momo of say 650 represent. Or is it simply one number on a scale of say, 1000? I'd like to know that myself. Thanks.
 
I think Wizedon may have been referring to was, what unit of measure does a momo of say 650 represent. Or is it simply one number on a scale of say, 1000? I'd like to know that myself. Thanks.
It is not a unit of measurement. It is the standard definition of momentum. It is result of the equation: close - close from 5 bars ago.
The bigger the number the greater the momentum.
 
Last edited:
@Michael Kim try the ema50 code. below It will provide you the answer and get your feet *** 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[1] then EMA50.color("Up") else EMA50.color("Down"));

addlabel(Yes," ", Color.orange);
addlabel(Yes,"50EMA = " + round(emA50,2), if price > ema50 then Color.GREEN else Color.RED);
# code end
How would you make this same thing for the SMA? Thanks!

Andy
 
How would you make this same thing for the SMA? Thanks!

Andy
Ruby:
# SMA plot and label

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

plot SMA1 = MovingAverage(AverageType.simple, price, length)[-displace];

SMA1.DefineColor("Up", color.green);
SMA1.DefineColor("Down", color.red);
SMA1.AssignValueColor(if SMA1 > SMA1[1] then SMA1.color("Up") else SMA1.color("Down"));

addlabel(Yes," ", Color.orange);
addlabel(Yes,"50EMA = " + round(SMA1,2), if price > SMA1 then Color.GREEN else Color.RED);
# code end
 
@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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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);
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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
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);
Hi Guys @Pensar @Therival @DoubleJinx. I have been using your code (see below) and I find this invaluable, however I recently sold a position because I saw a Green background however it was -0.4% down, (no biggy because I am still testing stuff along with your code). I understand the delay of broker, market maker, etc. can play a big role but wondered if you have improved this great label you guys have worked on? I did a (small 1.2mb) annotated movie showing the problems, but I am not sure how to put it up here if I can.

Here's the 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[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then BarNumber() else LastEntryBar[1];
def PercentChg = if Entry then (close - Entry) / Entry else 0;
def qty = GetQuantity();

# label
AddLabel(1, "Qty: " + GetQuantity() +
" | Avg$ " + GetAveragePrice()+
" | P/L: " + AsDollars(PL) +
" | " + AsPercent(PercentChg),
if PL == 0 then Color.WHITE
else if PL > 0.01 then Color.GREEN
else Color.Light_Red);


# bubble for current bar
input barsBack = 0;
input price = close;
def bn = BarNumber();
def currentBar = HighestAll(if !IsNaN(price) then bn else Double.NaN);
 
Last edited by a moderator:
Hi Guys @Pensar @Therival @DoubleJinx. I have been using your code (see below) and I find this invaluable, however I recently sold a position because I saw a Green background however it was -0.4% down, (no biggy because I am still testing stuff along with your code). I understand the delay of broker, market maker, etc. can play a big role but wondered if you have improved this great label you guys have worked on?

You didn't provide enough information to say where you went astray.
The color of the label denoting gain or loss in the script comes from the function GetOpenPL() which
Returns the Open Profit/Loss value for a specified symbol

If your loss was due to an adjustment in your cost basis; you can rectify that as follows:
replace this statement:
Ruby:
def PL = GetOpenPL();
with these statements:
Ruby:
input PLMode = ProfitLossMode.COST_BASIS;
plot PL = GetOpenPL(profitLossMode = PLMode);
read more: https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Portfolio/GetOpenPL
 
Last edited:

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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