ThinkorSwim showing N/A on a bubble or label

cabe1332

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

# end of code

JnZbANE.png


tZkuV6q.png


mnYC66H.png
 

cabe1332

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

Active 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[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

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[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". I updated my chart bubble with the change. Sharing some screenshots, a sell-off day showed down position. Thanks again. cabe1332

XobBgQP.png

TN0ktE7.png
 
Last edited:

rad14733

Well-known member
VIP
@cabe1332 I didn't realize that @Pensar had posted code before I got this completed... The following is a correction of the code you had posted... Notice how much cleaner the code looks, and how much easier it is to follow by simply doing as many calculations as possible before calling AddLabel()... It's a good coding habit to get into as it helps with debugging... No calculations are being done within AddLabel(), only the GetOpenPL comparison that could also have been done beforehand but doesn't make the code overly cumbersome...

Ruby:
# Label if Profiting or Losing
# cabe1332

def GetOpenPL = GetOpenPL();
def GetAveragePrice = GetAveragePrice();
def AvgPrice = if GetAveragePrice != 0 then close - GetAveragePrice else 0;
def result = if AvgPrice > 0 then Round(GetOpenPL + (AvgPrice/GetAveragePrice) * 100, 2) else 0;

AddLabel(yes, " P/L: $ " + result + " %", if GetOpenPL >= 0 then Color.green else Color.red);
 
Last edited:

cabe1332

Member
@cabe1332 I didn't realize that @Pensar had posted code before I got this completed... The following is a correction of the code you had posted... Notice how much cleaner the code looks, and how much easier it is to follow by simply doing as many calculations as possible before calling AddLabel()... It's a good coding habit to get into as it helps with debugging... No calculations are being done within AddLabel(), only the GetOpenPL comparison that could also have been done beforehand but doesn't make the code overly cumbersome...

Ruby:
# Label if Profiting or Losing
# cabe1332

def GetOpenPL = GetOpenPL();
def GetAveragePrice = GetAveragePrice();
def AvgPrice = if GetAveragePrice != 0 then close - GetAveragePrice else 0;
def result = if AvgPrice > 0 then Round(GetOpenPL + (AvgPrice/GetAveragePrice) * 100, 2) else 0;

AddLabel(yes, " P/L: $ " + result + " %", if GetOpenPL >= 0 then Color.green else Color.red);
Thanks, @rad14733 for pointing and suggestion. I knew it was something simple. It was a test, and you got an "A". Just kidding. Happy camper now. Hopefully, with all these indicators available, I get to flush out my PDT. Thanks again.
 

Therival

New member
Thanks, @rad14733 for pointing and suggestion. I knew it was something simple. It was a test, and you got an "A". Just kidding. Happy camper now. Hopefully, with all these indicators available, I get to flush out my PDT. Thanks again.
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:

cabe1332

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[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


CfD0N02.png


1YiXEs7.png


2o4MER3.png
 
Last edited:

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[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.
 

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[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:

Therival

New member
@Therival Please edit your post above to give some overview of your "strategy" that this is based on... You posted an image and code, but what's the trading "strategy"...???
This was in reply to having Chart Bubbles display on the current candle that show your P/L % and nothing to do with my "strategy." But since you asked I will post how I trade with my setup in case anyone else is interested.
 

Similar threads

Top