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

i haven't used getaverageprice() , so this is theoretical
maybe using getquantity is a better variable to use, instead of GetAveragePrice() , as slippage used in his code.

I don't think there's really an advantage either way in this case. I just usually default to getting the quantity first because it tells me if the position is long or short. And the function name has fewer letters making for slightly shorter lines of code.
 
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]));
 
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();
 
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:
# Current P/L Status
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 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 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?
 
What I am looking to achieve is to plot a green arrow pointing up on the chart at the price I bought into the trade. No details about the number of shares or time or anything else. Just the arrow up at my entry on the chart, and when I sell I want to plot a red arrow pointing down at the price I sold. Think about it as the TOS price bubble/ pill but without any number of shares or price, just the red or green arrow.

Is this possible to code?
 
Last edited by a moderator:
@Jony Giant @MerryDay . . . maybe this? I believe I read once that portfolio functions are limited to one year of data (please don't quote me on that) so not certain how far back in time this code will show. It appears to work for recent trades.

Code:
def x = GetQuantity();

plot start = x[1] == 0 and x <> 0;
     start.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
     start.setdefaultcolor(color.cyan);
    
plot end = x[1] <> 0 and x == 0;
     end.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
     end.setdefaultcolor(color.magenta);

sample-code-plot.png
 
@Jony Giant @MerryDay . . . maybe this? I believe I read once that portfolio functions are limited to one year of data (please don't quote me on that) so not certain how far back in time this code will show. It appears to work for recent trades.
OMGOODNESS!
I was so focused on what was not there, that I never gave thought to the 'was there' and 'now is not there' state having meaning. It is so elegant! It has such potential.

This could be SUCH a game changer in our portfolio analysis capabilities. @Pensar you continually amaze!
Thank you! For sharing!
 
Last edited:
@Jony Giant @MerryDay . . . maybe this? I believe I read once that portfolio functions are limited to one year of data (please don't quote me on that) so not certain how far back in time this code will show. It appears to work for recent trades.

Code:
def x = GetQuantity();

plot start = x[1] == 0 and x <> 0;
     start.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
     start.setdefaultcolor(color.cyan);
   
plot end = x[1] <> 0 and x == 0;
     end.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
     end.setdefaultcolor(color.magenta);

sample-code-plot.png
You absolute LEGEND. This is exactly what I was looking for!! This should definitely be integrated in TOS by default. THANK YOU!
PS: It looks like it plots the arrows when I open the position and when I close it, but it doesn't plot anything if I add more shares after the trade is open. Is still a huge leap though! Thank you!
 
You absolute LEGEND. This is exactly what I was looking for!! This should definitely be integrated in TOS by default. THANK YOU!
PS: It looks like it plots the arrows when I open the position and when I close it, but it doesn't plot anything if I add more shares after the trade is open. Is still a huge leap though! Thank you!
@Jony Giant If you want to show the adds/profit-takings to your position (for long positions only) add this to the code -

Code:
plot add = x > 0 and x > x[1];
     add.setpaintingstrategy(paintingstrategy.boolean_arrow_up);
     add.setdefaultcolor(color.white);

plot subtract = x > 0 and x < x[1];
     subtract.setpaintingstrategy(paintingstrategy.boolean_arrow_down);
     subtract.setdefaultcolor(color.gray);
 

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