Realized profit/loss % label on chart?


I know you can do an unrealized profit/loss % label but what about a realized one?  i cobbled together a lame attempt using the input function but, of course, that won’t work because changing the input for a different symbol changes it for all.  can you see any way to do this?

input buyprice = 0;
input sellprice = 0;
def buy = buyprice;
def sell = sellprice;
def diff = buy – sell;
def gain = (diff / buy) * 100;
addlabel (yes, “realized ” + gain + ” %”, color.white);

Marked as spam
Posted by (Questions: 6, Answers: 3)
Asked on January 12, 2022 1:50 pm
Private answer

If it is possible the solution is way beyond the scope of what I can provide free of charge in the Q&A Forum. Perhaps someone else has worked this out though and will come along and post their solution as a new answer to this topic.

There are so many issue with this it makes my head spin. For example the code will need to check for how many shares were purchased and at what price. That's pretty straight forward using existing language tools in Thinkorswim. However the code will only know a trade took place when the number of shares changes. There is just no way to write code in Thinkorswim that can keep track of buys and sells and amounts with any degree of accuracy. Not within a chart environment.

And what you purchased shares 5 years ago? And your chart only has 2 years of historical data loaded? Busted. There are so many ways this can go wrong and I strongly encourage you to drop this and move on to something more productive. This is a black whole that can drain away weeks of productivity with nothing to show for it.

And on top of all of this, the data you are seeking to display on a chart is already available elsewhere on the platform. Just use what's available and focus on making profitable trades.

Marked as spam
Posted by (Questions: 37, Answers: 3786)
Answered on January 12, 2022 2:24 pm
would it make a difference if we assumed just one buy and one sell, since i’d be using this only for very short term trades where that’s going to be the most likely case? i guess there’s no way for me to do it manually as i tried to do above. i know that there is a built-in ToS watchlist column for average price paid but i’m not sure if it can be referenced elsewhere or be recalled after the trade is closed to a zero share balance.
( at January 12, 2022 2:30 pm)
The only tools we have to work with are described here: You can plot GetQuantity() and GeAveragePrice() on the lower subgraph of the chart to see how those values change on the date each trade is executed. Trying to wire things up so that it keeps track of those changes over time is the complicated bit. And as I mentioned previously, this solution is beyond the scope of something I can provide free of charge in the forum.
( at January 12, 2022 2:38 pm)
okay, thanks.
( at January 12, 2022 2:58 pm)
found a study elsewhere that plots arrows for all buys and sells going back i don't know how far but perhaps to your two year mark. kinda cool but kinda useless, too, because it only puts an arrow on the day that you bought and doesn't give the actual price you bought it at. 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); 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); here's a pic of the study in action:
( at January 13, 2022 6:30 am)