Display position percent profit or loss in chart label


Category:
0
0

Hello!

I’ve got a code to show some information regard current position, unfortunately I haven’t been able to show the P&L as % in the chart, I’ve tried to play with the following code withouth luck since i have zero code experience, thanks for the help.

 

 

Attachments:
Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on March 5, 2024 2:47 pm
124 views
0
Private answer

We already have a similar solution posted here:

https://www.hahn-tech.com/ans/is-it-possible-to-monitor-pl-on-the-charts-tab-in-thinkorswim/

But yours is also requesting to show the value as a percent value and not just the dollar amount. So I decided to retain your question in the forum even though is sort of a duplicate question.

I only had to add one line of code to add the label you requested. But I also took some time to clean up the code a bit and will post the entire solution below:

input showDetails = yes;
input showBasis = yes;
input showProfitLoss = yes;
input showFills = no;
input netProfitLossPastTrades = 0.00;
# Entry Price
plot entryPrice = GetAveragePrice();
entryPrice.AssignValueColor(if close < entryPrice then Color.DOWNTICK else Color.UPTICK);
entryPrice.SetLineWeight(2); entryPrice.SetStyle(Curve.SHORT_DASH);
# Basis Calculation
def qty = GetQuantity();
def basis = (qty * entryPrice);
#Labels
AddLabel(showDetails, qty + "@" + AsDollars(entryPrice), Color.GRAY); AddLabel(showBasis and netProfitLossPastTrades == 0, "Basis: " + AsDollars(basis), if basis > (qty * close) then Color.DOWNTICK else Color.UPTICK);
AddLabel(showBasis and netProfitLossPastTrades != 0, "Basis (adj): " + AsDollars(basis), if basis > (qty * close) then Color.DOWNTICK else Color.UPTICK);
def pl = (close - entryPrice) * qty;
AddLabel(showProfitLoss, "P/L: " + AsDollars(pl), if pl > 0 then Color.UPTICK else if pl == 0 then Color.WHITE else Color.DOWNTICK);
AddLabel(showProfitLoss, "P/L/Share: " + AsDollars(close - entryPrice), if (close - entryPrice) > 0 then Color.UPTICK else if (close - entryPrice) == 0 then Color.WHITE else Color.DOWNTICK);
AddLabel(yes, "Prct P/L: " + AsPercent(pl / basis), Color.WHITE);

Marked as spam
Posted by (Questions: 37, Answers: 4117)
Answered on March 5, 2024 2:53 pm