• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

Display "no position" as label of GetOpenPL

lmk99

Member
VIP
I want to display a simple P/L open label on an upper chart and have the following code:

Code:
def PLOpen =  GetOpenPL();

AddLabel (yes, "P/L: " + if ( PLOpen >= 0 ) then PLOpen else PLOpen, if PLOpen > 0 then Color.Black else Color.Red);
# if PL > 0 then CreateColor(0,0,0) else CreateColor(255,0,0);

Instead of displaying "0" if there is no open position, the chart label doesn't show up at all and in its place there is a small error icon (an exclamation point in a circle) on the chart. This is going to be visually distracting to have the label pop in and out of existence based on the existence of a temporarily open position. Is there a simple way to structure an if/then/else or other type of conditional to cause "0" or "No Position" to display when GetOpenPL is returning a null value (ideally, in this case I'd also like the color of the label to be gray instead of red or black - I haven't figured out a way to have three potential label colors either, only two based on if/then/else)?
 
Last edited:

lmk99

Member
VIP
@lmk99 Have you checked the "if" conditional clause documentation for help...???

Thanks, I don't understand how "else if" functions but I'd assume that should be what I need to use, I'll google and read about it.

The other component to this problem is how to define whether there is any open position or not (i.e. if I type "if ____ then" how would the lack of a position be defined, as something like " ! = PlOpen"? I haven't been able to find an answer yet).
 

rad14733

Well-known member
VIP
Thanks, I don't understand how "else if" functions but I'd assume that should be what I need to use, I'll google and read about it.

The other component to this problem is how to define whether there is any open position or not (i.e. if I type "if ____ then" how would the lack of a position be defined, as something like " ! = PlOpen"? I haven't been able to find an answer yet).
If you are trading Stocks, and some other securities, you can use EntryPrice() but it doesn't work with everything... For example, Options won't return EntryPrice(), unfortunately...

Also, for your "if" issue, for color assignment I usually use if <condition> then <do this> else if <condition> then <do this> else <do this>... The following is an example...

Code:
if Downtrend then Color.DOWNTICK else if Uptrend then Color.UPTICK else Color.GRAY
 
Last edited:

lmk99

Member
VIP
Thanks for your helpful advice, I tried this:

Code:
def PLOpen =  GetOpenPL();

Addlabel (yes, "P/L:" + if (EntryPrice() > 0) then PLOpen else "No Position", if PLOpen > 0 then Color.Green else if PLOpen < 0 then Color.Red else Color.Gray);


However, this script is still not resulting in a label appearing that says "P/L: No Position" when there is no open position. So I'm guessing that the condition "EntryPrice() > 0" is not being recognized as meaning the condition of "the existence of any entry or position" (such that when that condition is not met "No Position" can appear after the "else" in that clause).

Does anyone know how it would be possible to define the absence of a position for the purpose of having the label be constant, displaying "No Position" when there isn't an open position?

PS: Also tried this to make the logic more explicit:

Code:
def PLOpen =  GetOpenPL();
def HasPosition = EntryPrice() > 0;

Addlabel (yes, "P/L:" + if HasPosition then PLOpen else "No Position", if PLopen > 0 then Color.Green else if PLOpen < 0 then Color.Red else Color.Gray);

Although I can save the script, there does appear to be an error showing: "Wrong type cast: different types after then and else: class java.lang.String vs double"

OK, I think this could be related to the problem. For the chart that I have using this label, I need to use an 8min aggregation period. But the documentation for the "Portfolio" functions states:

Note also that Portfolio functions can only be used with the following aggregation periods: 1 min, 2 min, 3 min, 4 min, 5 min, 10 min, 15 min, 20 min, 30 min, 1h, or 1 day. Time period for the aggregation of 1 day is limited to 1 year.

Currently, the following Portfolio functions are available:

Kind of a bummer. Not sure if even without this confounding problem the current code would work on a different chart with a supported aggregation period, haven't tested that.

EDIT: I tried to get this to work on a different 5min chart instead. The aggregation period turned out to be the problem in fact. Because this code is working now on the 5min chart (the label just displays "PL: 0" if there is no open position):

Code:
def PLOpen =  GetOpenPL();

AddLabel (yes, "P/L: " + PLOpen, if PLOpen > 0 then Color.GREEN else if PLOpen < 0 then Color.RED else Color.GRAY);
 
Last edited by a moderator:

rad14733

Well-known member
VIP
@lmk99 Is this more what you are looking for...??? The label should only display if there is an open trade...

Code:
def PLOpen =  GetOpenPL();

AddLabel (GetQuantity() > 0, "P/L: " + AsText(PLOpen, NumberFormat.DOLLAR), if PLOpen > 0 then Color.GREEN else if PLOpen < 0 then Color.RED else Color.GRAY);
 
Last edited:

lmk99

Member
VIP
@rad14733 The label wasn't displaying before because the 8 minute aggregation period wasn't supported for Portfolio functions. It works on the 5min chart. I want the label to indicate no position instead of disappearing when there's no open position, so it's OK for my purposes that it currently shows "P/L Open: 0" in that case. However I really appreciate your improvement of using "NumberFormat.DOLLAR" - thanks for that, I have made that update to my code!
 
Top