Entry, Target, Profit Loss (PnL) Watchlists, Labels of Owned Equities In ThinkOrSwim

Ok, I've done some more fiddling and now would like to store the maximum profit/loss percentage. Here's my code. I calculate a profit/loss percentage (PLPercent) and then use the variable MPL to save whatever the maximum profit/loss is.

My problem is it's not always saving the highest PLPercent value. Sometimes it does, but other times it drops back down to the current PLPercent. Is that because the previous bar has a range, and my code may be picking up something different than what I expect?

The last line accounts for long or short positions.

Code:
def PLPercent = if qty>0 then
(qty * close - qty * GetAveragePrice()) / (qty * GetAveragePrice()) else
(qty * GetAveragePrice() - qty * close) / (qty * GetAveragePrice());
AddLabel(qty<>0, "P/L: " + AsPercent(PLPercent),
if PLPercent==0 then Color.LIGHT_GRAY else if PLPercent>0 then Color.GREEN else Color.RED);

def MPL = if GetQuantity()==0 then 0 else if GetQuantity()[1]==0 then PLPercent else (if GetQuantity()>0 then Max(PLPercent, MPL[1]) else Min(PLPercent, MPL[1]));
 

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

Ok, I've done some more fiddling and now would like to store the maximum profit/loss percentage. Here's my code. I calculate a profit/loss percentage (PLPercent) and then use the variable MPL to save whatever the maximum profit/loss is.

My problem is it's not always saving the highest PLPercent value. Sometimes it does, but other times it drops back down to the current PLPercent. Is that because the previous bar has a range, and my code may be picking up something different than what I expect?

The last line accounts for long or short positions.

Code:
def PLPercent = if qty>0 then
(qty * close - qty * GetAveragePrice()) / (qty * GetAveragePrice()) else
(qty * GetAveragePrice() - qty * close) / (qty * GetAveragePrice());
AddLabel(qty<>0, "P/L: " + AsPercent(PLPercent),
if PLPercent==0 then Color.LIGHT_GRAY else if PLPercent>0 then Color.GREEN else Color.RED);

def MPL = if GetQuantity()==0 then 0 else if GetQuantity()[1]==0 then PLPercent else (if GetQuantity()>0 then Max(PLPercent, MPL[1]) else Min(PLPercent, MPL[1]));

looks like it should work....

when i have problems, i simplify things.
take your last formula and make several formulas from it. then use bubbles to display the values on each bar.
if you put multiple variables in 1 bubble, use this to force data on a new line.
+ "\n" + to

def getq = GetQuantity();
def getq0 = (GetQuantity()==0);
def getq1 = (GetQuantity()[1]==0);
def max1 = Max(PLPercent, MPL[1]);
def min1 = Min(PLPercent, MPL[1]);

addchartbubble(1, low,
getq + "\n" +
getq0 + "\n" +
getq1 + "\n" +
max1 + "\n" +
min1, color.cyan, no);
 
what is the difference in code for IsNaN(GetAveragePrice()) vs. GetAveragePrice() ?
IsNan() tests to insure that a number is present, meaning that there is an active trade... In the code above entry will either be filled with the value of the previous entry, entry[1], if GetAveragePrice() doesn't return a price or the current GetAveragePrice() if it does contain a price...

Ruby:
def entry = if IsNaN(GetAveragePrice()) then entry[1] else GetAveragePrice();
 
I am in the trade, Nothing will show up. no label will be added, the label should show the PRF as percentage
 
Last edited by a moderator:
@majidg This code is not ready for primetime.
@Pensar stated in post#5 that he was having some issues with some stocks as well.
 
Last edited:
Hi, came across this thread looking to plot previous trades. It seems most the codes in the thread plot live trades?

however using the snippet

Code:
plot tradePrice = if GetQuantity() > 0  then GetAveragePrice() else Double.NaN;

Seems to show "some" of my closed trades but not all. Just curious if anyone knows why and if I can get it to show all trades.
 
@PlusEVTrades
GetQuantity() is a portfolio function which provides quantities for instruments currently in trade.
Closed trades are not available.
 
Current P/L Status
I tweaked the 1st post to include how many days ago that I bought a stock.
Helps me see the dogs in my portfolio
sVlHij5.png

Ruby:
# Crude Buy Sell Zone indicator by ANTHDKN 06.04.2021
# Free to use to all
# First posted on usethinkscript.com
# Auto entries from positions
input value = .07;
def avg = getaveragePrice();
def AreYouIn = if getquantity() != 0 then yes else no;

def targetprice02 = (avg) - (value*2);
def TargetPrice01 = (avg) - value;
def TargetPrice1 = (avg) + value;
def TargetPrice2 = (avg) + (value * 2);
def TargetPrice3 = (avg) + (value * 3);

def Entry = if isNaN(avg) then Entry[1] else avg;
def LastEntryBar = if Entry != Entry[1] then barNumber() else LastEntryBar[1];
def BuyBarsAgo = if barNumber() != LastEntryBar
                 then barNumber() - LastEntryBar
                 else if barNumber() == LastEntryBar
                      then Double.NaN
                 else BuyBarsAgo[1];


AddLabel(AreYouIn,
         "Cost: " +round(avg,2) +
         (if GetOpenPL() > 0
         then " | "+ "P/L: $" + GetOpenPL() +
              " | "+ aspercent(getopenPL() / (avg *  getquantity()))
         else " | "+ "P/L: -$" + GetOpenPL() +
              " | "+ aspercent(getopenPL() / (avg *  getquantity())))
         +    " | "+ " Bought: " +BuyBarsAgo +" days ago",
                 if GetOpenPL() > 0
         then Color.DARK_GREEN
         else Color.DARK_RED);

plot downside2 = if avg != 0  then TargetPrice02 else double.nan;
downside2.setstyle(curve.FIRM);
downside2.setdefaultColor(color.DARK_RED);

plot downside1 = if avg != 0  then TargetPrice01 else double.nan;
downside1.setstyle(curve.SHORT_DASH);
downside1.setdefaultColor(color.red);

plot avgprice = if avg != 0  then avg else double.nan;
avgprice.setstyle(curve.SHORT_DASH);
avgprice.setdefaultColor(color.Yellow);

plot upside1 = if avg != 0  then TargetPrice1 else double.nan;
upside1.setstyle(curve.SHORT_DASH);
upside1.setdefaultColor(color.light_GREEN);

plot upside2 = if avg != 0  then TargetPrice2 else double.nan;
upside2.setstyle(curve.SHORT_DASH);
upside2.setdefaultColor(color.light_GREEN);

plot upside3 = if avg != 0  then TargetPrice3 else double.nan;
upside3.setstyle(curve.FIRM);
upside3.setdefaultColor(color.dark_GREEN);
 
Last edited:
Is there a way to get the value of "P/L Day" so I can display it as a label on a chart.
 
Last edited by a moderator:
@BenTen Is something like this possible for TOS? The boxes mark when you entered\exited a position and it tracks how long you were in it. (How many candles you were in it and it shows how many hours). Box is green if it's positive, and red if it's negative.
unknown.png
 
@Angry_Raven All portfolio information resets to isNAN (none) after exiting a trade (except p/l ytd).
Which means we can't analyze our trade history :(

Members have mentioned that they download trades to Excel to do all their post-trade analysis.
 
Last edited:
Hello, this worked great. Is there a way to make it work for the week and 4 hours intervals?
Code:
def EP = Round(GetAveragePrice(), 2);
def PrLs = GetOpenPL();
def UPDN = EP + GetOpenPL();
AddLabel (yes, "Entry: " + EP, Color.CYAN);
AddLabel (if UPDN <> EP then yes else no, "PrLs: $" + PrLs , if UPDN > EP then Color.GREEN else Color.RED);
t
 
Good afternoon all,

I was wondering if there is a label on the chart to see when the last time a ticker was traded, possibly even with the P/L of that result of the last time the ticker was traded.

I mainly want a quick reference for the wash sale rule basically.

Thanks for any help!
 
Is there is a label on the chart to see when the last time a ticker was traded, possibly even with the P/L of that result of the last time the ticker was traded.

I mainly want a quick reference for the wash sale rule basically.

Thanks for any help!
I moved your post here as this thread discusses the myriad of ways to display trades on charts.
For a specific answer to your question:
https://usethinkscript.com/threads/show-trades-on-thinkorswim-chart-screen.1857/page-4#post-78210
 
Last edited:
Current P/L Status
I tweaked the 1st post to include how many days ago that I bought a stock.
Helps me see the dogs in my portfolio
sVlHij5.png

Ruby:
# Crude Buy Sell Zone indicator by ANTHDKN 06.04.2021
# Free to use to all
# First posted on usethinkscript.com
# Auto entries from positions
input value = .07;
def avg = getaveragePrice();
def AreYouIn = if getquantity() != 0 then yes else no;

def targetprice02 = (avg) - (value*2);
def TargetPrice01 = (avg) - value;
def TargetPrice1 = (avg) + value;
def TargetPrice2 = (avg) + (value * 2);
def TargetPrice3 = (avg) + (value * 3);

def Entry = if isNaN(avg) then Entry[1] else avg;
def LastEntryBar = if Entry != Entry[1] then barNumber() else LastEntryBar[1];
def BuyBarsAgo = if barNumber() != LastEntryBar
                 then barNumber() - LastEntryBar
                 else if barNumber() == LastEntryBar
                      then Double.NaN
                 else BuyBarsAgo[1];


AddLabel(AreYouIn,
         "Cost: " +round(avg,2) +
         (if GetOpenPL() > 0
         then " | "+ "P/L: $" + GetOpenPL() +
              " | "+ aspercent(getopenPL() / (avg *  getquantity()))
         else " | "+ "P/L: -$" + GetOpenPL() +
              " | "+ aspercent(getopenPL() / (avg *  getquantity())))
         +    " | "+ " Bought: " +BuyBarsAgo +" days ago",
                 if GetOpenPL() > 0
         then Color.DARK_GREEN
         else Color.DARK_RED);

plot downside2 = if avg != 0  then TargetPrice02 else double.nan;
downside2.setstyle(curve.FIRM);
downside2.setdefaultColor(color.DARK_RED);

plot downside1 = if avg != 0  then TargetPrice01 else double.nan;
downside1.setstyle(curve.SHORT_DASH);
downside1.setdefaultColor(color.red);

plot avgprice = if avg != 0  then avg else double.nan;
avgprice.setstyle(curve.SHORT_DASH);
avgprice.setdefaultColor(color.Yellow);

plot upside1 = if avg != 0  then TargetPrice1 else double.nan;
upside1.setstyle(curve.SHORT_DASH);
upside1.setdefaultColor(color.light_GREEN);

plot upside2 = if avg != 0  then TargetPrice2 else double.nan;
upside2.setstyle(curve.SHORT_DASH);
upside2.setdefaultColor(color.light_GREEN);

plot upside3 = if avg != 0  then TargetPrice3 else double.nan;
upside3.setstyle(curve.FIRM);
upside3.setdefaultColor(color.dark_GREEN);
This is awesome!! I trade outside of TD as well since I'm under 25k and use multiple brokers to bypass the PDT rule and play more positions. I see this removes the manual input, which is great I love that. Thanks for this.
 
I'd like to use the following on a Forex chart (e.g., EUR/USD, or NZD/USD, etc.) But it's not working. Is there another way to get Quantity with Forex symbols?
Code:
def qty = GetQuantity();
AddLabel(yes, "Qty: " + qty, color.CYAN);
 
I would like to know when I place a position if I can have automatically add in my graph a line and a bubble with my entry price. It's this possible to be create in thinkscrip?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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