Changes days to minutes in P/L label?

EddieM

New member
Plus
Can this be changed to minutes? When I put it on an interday chart, I am getting a day for each minute showing.
+ " | "+ " Bought: " +BuyBarsAgo +" days ago",

https://usethinkscript.com/threads/...quities-in-thinkorswim.7089/page-6#post-77993
Code:
# 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:
Can this be changed to minutes? When I put it on an interday chart, I am getting a day for each minute showing.
+ " | "+ " Bought: " +BuyBarsAgo +" days ago",

https://usethinkscript.com/threads/...quities-in-thinkorswim.7089/page-6#post-77993
Code:
# 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);

are you asking to convert the quantity of bars to minutes?
see if this is what you want.

Code:
#PL_label_days

#https://usethinkscript.com/threads/changes-days-to-minutes-in-p-l-label.19791/
#Changes days to minutes in P/L label?

# Current P/L Status
input value = .07;
def avg = getaveragePrice();
def AreYouIn = if getquantity() != 0 then yes else no;

# get chart agg time
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);

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,
#AddLabel(1,
         "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",
         +    " | "+ " Bought: " + (BuyBarsAgo * chartmin) + " minutes 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);
#
 
are you asking to convert the quantity of bars to minutes?
see if this is what you want.

Code:
#PL_label_days

#https://usethinkscript.com/threads/changes-days-to-minutes-in-p-l-label.19791/
#Changes days to minutes in P/L label?

# Current P/L Status
input value = .07;
def avg = getaveragePrice();
def AreYouIn = if getquantity() != 0 then yes else no;

# get chart agg time
def chartagg = getaggregationperiod();
def chartmin = chartagg/(1000*60);

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,
#AddLabel(1,
         "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",
         +    " | "+ " Bought: " + (BuyBarsAgo * chartmin) + " minutes 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);
#
Hi yes so it shows me how long i have been in a trade for mins.

How can I add this time part to another study I have been working on?

In my screen grab you can see I have a mix of what I have set up. Spending limit, how many shares I can buy with it - then green area of qty, avg and PL and so on. Then this code in the darker green that also shows the time of the trade.
I would like to take the time part and add it to my other study.
Here is the code I am using for the other study right now. A mix of things I have added together I found.
Also I do not know how to turn two items into one.
I would like to take the spending limit in green info and the You can buy shares and make them into just one item as white. No need for them to be separate from each other.



Screenshot 2024-10-08 at 1.05.23 PM.png



Code:
# Average Actual Entry
# Mobius
# 01.01.2018

# Removed portions of Mobius' code, added quantity and replaced Mobius' P/L
# calculation with GetOpenPL().
# Color-coded label, edited study look/colors for personal preference

input use_line_limits = yes;#Yes, plots line from/to; No, plot line across entire chart
input linefrom = 100;#Hint linefrom: limits how far line plots in candle area
input lineto = 12;#Hint lineto: limits how far into expansion the line will plot

def PL = GetOpenPL();
def c = if isNaN(close[-1]) then close else c[1];
def Entry = if isNaN(GetAveragePrice()) then Entry[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then barNumber() else LastEntryBar[1];

plot Entry_ = if barNumber() >= HighestAll(LastEntryBar) and Entry > 0
then highestAll(if isNaN(close[-1]) then Entry else double.nan) else double.nan;
Entry_.SetStyle(Curve.SHORT_DASH);
Entry_.SetLineWeight(3);
Entry_.SetDefaultColor(color.BLUE);
Entry_.HideBubble();
Entry_.HideTitle();

plot "5% Stop" = if Entry_ then Entry_ - .05*Entry else double.nan;
"5% Stop".SetDefaultColor(color.red);

plot "5% Target" = if Entry_ then Entry_ + .05*Entry else double.nan;
"5% Target".SetDefaultColor(color.green);

plot "10% Target" = if Entry_ then Entry_ + .10*Entry else double.nan;
"10% Target".SetDefaultColor(color.Yellow);

plot "15% Target" = if Entry_ then Entry_ + .15*Entry else double.nan;
"15% Target".SetDefaultColor(color.Magenta);

plot "20% Target" = if Entry_ then Entry_ + .20*Entry else double.nan;
"20% Target".SetDefaultColor(color.red);

input balance = 1000;
def current_price = close;
def limit = balance / current_price;

AddLabel(yes, Concat("Spending Limit = ", RoundDown(balance)), color.green);
AddLabel(yes, Concat("You can buy: " , RoundDown(limit)) +" Shares", color.white);

AddLabel(1, "Qty: " + GetQuantity() +
         "   Avg $" + Entry +
         "   P/L: " + AsDollars(PL) + "   Max L: " + AsDollars(Entry*GetQuantity()),
         if PL == 0 then color.gray
         else if PL > 0 then color.green
         else color.red);
#
 
Last edited by a moderator:
Hi yes so it shows me how long i have been in a trade for mins.

How can I add this time part to another study I have been working on?

In my screen grab you can see I have a mix of what I have set up. Spending limit, how many shares I can buy with it - then green area of qty, avg and PL and so on. Then this code in the darker green that also shows the time of the trade.
I would like to take the time part and add it to my other study.
Here is the code I am using for the other study right now. A mix of things I have added together I found.
Also I do not know how to turn two items into one.
I would like to take the spending limit in green info and the You can buy shares and make them into just one item as white. No need for them to be separate from each other.



View attachment 23096


Code:
# Average Actual Entry
# Mobius
# 01.01.2018

# Removed portions of Mobius' code, added quantity and replaced Mobius' P/L
# calculation with GetOpenPL().
# Color-coded label, edited study look/colors for personal preference

input use_line_limits = yes;#Yes, plots line from/to; No, plot line across entire chart
input linefrom = 100;#Hint linefrom: limits how far line plots in candle area
input lineto = 12;#Hint lineto: limits how far into expansion the line will plot

def PL = GetOpenPL();
def c = if isNaN(close[-1]) then close else c[1];
def Entry = if isNaN(GetAveragePrice()) then Entry[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then barNumber() else LastEntryBar[1];

plot Entry_ = if barNumber() >= HighestAll(LastEntryBar) and Entry > 0
then highestAll(if isNaN(close[-1]) then Entry else double.nan) else double.nan;
Entry_.SetStyle(Curve.SHORT_DASH);
Entry_.SetLineWeight(3);
Entry_.SetDefaultColor(color.BLUE);
Entry_.HideBubble();
Entry_.HideTitle();

plot "5% Stop" = if Entry_ then Entry_ - .05*Entry else double.nan;
"5% Stop".SetDefaultColor(color.red);

plot "5% Target" = if Entry_ then Entry_ + .05*Entry else double.nan;
"5% Target".SetDefaultColor(color.green);

plot "10% Target" = if Entry_ then Entry_ + .10*Entry else double.nan;
"10% Target".SetDefaultColor(color.Yellow);

plot "15% Target" = if Entry_ then Entry_ + .15*Entry else double.nan;
"15% Target".SetDefaultColor(color.Magenta);

plot "20% Target" = if Entry_ then Entry_ + .20*Entry else double.nan;
"20% Target".SetDefaultColor(color.red);

input balance = 1000;
def current_price = close;
def limit = balance / current_price;

AddLabel(yes, Concat("Spending Limit = ", RoundDown(balance)), color.green);
AddLabel(yes, Concat("You can buy: " , RoundDown(limit)) +" Shares", color.white);

AddLabel(1, "Qty: " + GetQuantity() +
         "   Avg $" + Entry +
         "   P/L: " + AsDollars(PL) + "   Max L: " + AsDollars(Entry*GetQuantity()),
         if PL == 0 then color.gray
         else if PL > 0 then color.green
         else color.red);
#

not sure what you are asking.
i added minutes since last entry,

Code:
#PL_label_days2

#https://usethinkscript.com/threads/changes-days-to-minutes-in-p-l-label.19791/#post-146834
# Average Actual Entry
# Mobius
# 01.01.2018

# Removed portions of Mobius' code, added quantity and replaced Mobius' P/L
# calculation with GetOpenPL().
# Color-coded label, edited study look/colors for personal preference

def na = double.nan;
def bn = barnumber();

# get chart agg time
def chartagg = GetAggregationPeriod();
def chartmin = chartagg / (1000 * 60);

input use_line_limits = yes;#Yes, plots line from/to; No, plot line across entire chart
input linefrom = 100;#Hint linefrom: limits how far line plots in candle area
input lineto = 12;#Hint lineto: limits how far into expansion the line will plot

def PL = GetOpenPL();
def c = if IsNaN(close[-1]) then close else c[1];
def Entry = if IsNaN(GetAveragePrice()) then Entry[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then bn else LastEntryBar[1];

def barssince = bn - lastentrybar;

plot Entry_ = if bn >= HighestAll(LastEntryBar) and Entry > 0
 then HighestAll(if IsNaN(close[-1]) then Entry else Double.NaN)
  else Double.NaN;
Entry_.SetStyle(Curve.SHORT_DASH);
Entry_.SetLineWeight(3);
Entry_.SetDefaultColor(Color.BLUE);
Entry_.HideBubble();
Entry_.HideTitle();

plot "5% Stop" = if Entry_ then Entry_ - .05 * Entry else Double.NaN;
"5% Stop".SetDefaultColor(Color.RED);

plot "5% Target" = if Entry_ then Entry_ + .05 * Entry else Double.NaN;
"5% Target".SetDefaultColor(Color.GREEN);

plot "10% Target" = if Entry_ then Entry_ + .10 * Entry else Double.NaN;
"10% Target".SetDefaultColor(Color.YELLOW);

plot "15% Target" = if Entry_ then Entry_ + .15 * Entry else Double.NaN;
"15% Target".SetDefaultColor(Color.MAGENTA);

plot "20% Target" = if Entry_ then Entry_ + .20 * Entry else Double.NaN;
"20% Target".SetDefaultColor(Color.RED);

input balance = 1000;
def current_price = close;
def limit = balance / current_price;

AddLabel(yes, Concat("Spending Limit = ", RoundDown(balance)), Color.GREEN);
AddLabel(yes, Concat("You can buy: " , RoundDown(limit)) + " Shares", Color.WHITE);

AddLabel(1, "Qty: " + GetQuantity() +
         "   Avg $" + Entry +
         "   P/L: " + AsDollars(PL) + "   Max L: " + AsDollars(Entry * GetQuantity()),
         if PL == 0 then Color.GRAY
         else if PL > 0 then Color.GREEN
         else Color.RED);

addlabel(1, 
 " Bought: " + ( barssince * chartmin) + " minutes ago", 
 if GetOpenPL() > 0 then Color.DARK_GREEN else Color.DARK_RED);
#
 
not sure what you are asking.
i added minutes since last entry,

Code:
#PL_label_days2

#https://usethinkscript.com/threads/changes-days-to-minutes-in-p-l-label.19791/#post-146834
# Average Actual Entry
# Mobius
# 01.01.2018

# Removed portions of Mobius' code, added quantity and replaced Mobius' P/L
# calculation with GetOpenPL().
# Color-coded label, edited study look/colors for personal preference

def na = double.nan;
def bn = barnumber();

# get chart agg time
def chartagg = GetAggregationPeriod();
def chartmin = chartagg / (1000 * 60);

input use_line_limits = yes;#Yes, plots line from/to; No, plot line across entire chart
input linefrom = 100;#Hint linefrom: limits how far line plots in candle area
input lineto = 12;#Hint lineto: limits how far into expansion the line will plot

def PL = GetOpenPL();
def c = if IsNaN(close[-1]) then close else c[1];
def Entry = if IsNaN(GetAveragePrice()) then Entry[1] else GetAveragePrice();
def LastEntryBar = if Entry != Entry[1] then bn else LastEntryBar[1];

def barssince = bn - lastentrybar;

plot Entry_ = if bn >= HighestAll(LastEntryBar) and Entry > 0
 then HighestAll(if IsNaN(close[-1]) then Entry else Double.NaN)
  else Double.NaN;
Entry_.SetStyle(Curve.SHORT_DASH);
Entry_.SetLineWeight(3);
Entry_.SetDefaultColor(Color.BLUE);
Entry_.HideBubble();
Entry_.HideTitle();

plot "5% Stop" = if Entry_ then Entry_ - .05 * Entry else Double.NaN;
"5% Stop".SetDefaultColor(Color.RED);

plot "5% Target" = if Entry_ then Entry_ + .05 * Entry else Double.NaN;
"5% Target".SetDefaultColor(Color.GREEN);

plot "10% Target" = if Entry_ then Entry_ + .10 * Entry else Double.NaN;
"10% Target".SetDefaultColor(Color.YELLOW);

plot "15% Target" = if Entry_ then Entry_ + .15 * Entry else Double.NaN;
"15% Target".SetDefaultColor(Color.MAGENTA);

plot "20% Target" = if Entry_ then Entry_ + .20 * Entry else Double.NaN;
"20% Target".SetDefaultColor(Color.RED);

input balance = 1000;
def current_price = close;
def limit = balance / current_price;

AddLabel(yes, Concat("Spending Limit = ", RoundDown(balance)), Color.GREEN);
AddLabel(yes, Concat("You can buy: " , RoundDown(limit)) + " Shares", Color.WHITE);

AddLabel(1, "Qty: " + GetQuantity() +
         "   Avg $" + Entry +
         "   P/L: " + AsDollars(PL) + "   Max L: " + AsDollars(Entry * GetQuantity()),
         if PL == 0 then Color.GRAY
         else if PL > 0 then Color.GREEN
         else Color.RED);

addlabel(1,
 " Bought: " + ( barssince * chartmin) + " minutes ago",
 if GetOpenPL() > 0 then Color.DARK_GREEN else Color.DARK_RED);
#
Ok thanks that works thanks
Screenshot 2024-10-09 at 12.40.21 PM.png
 

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
318 Online
Create Post

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