# IPO Date or First Trading Date

#### _Merch_Man_

##### Member
Hey gang. I have been using highest(volume,365) to get the highest volume in the past year but then I hit a NaN when I looked at a stock that had not been trading a full year. I have been trying to figure out how to avoid this NaN. One way is to check the IPO date. Psedo code would be

Code:
``````if (today - first trading date >= 365)
then
else
highest(volume,365)``````

Is there a way to get the IPO date or first trading day of a stock? Do you have any other ideas on how to solve this problem?

Thanks - Matt.

##### Well-known member
VIP
@_Merch_Man_ In reality there are only 252 trading days in a year... That being said, Thinkscript doesn't provide us a means of extrapolating IPO date...

#### _Merch_Man_

##### Member
@_Merch_Man_ In reality there are only 252 trading days in a year... That being said, Thinkscript doesn't provide us a means of extrapolating IPO date...
Thanks @rad14733 . Can you think of any other way to avoid the NaN?

##### Well-known member
VIP
Thanks @rad14733 . Can you think of any other way to avoid the NaN?

We would need to see the entire code script in order to debug properly...

#### _Merch_Man_

##### Member
OK. Here is the relevant code:

Code:
``````declare lower;

plot enhancedVolume = volume;

enhancedVolume.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

def highestVolYear = highest(volume,252);

enhancedVolume.assignValueColor(if volume == highestVolYear then COLOR.WHITE);``````

##### Well-known member
VIP
@_Merch_Man_ You'll need to refine your logic further... Below is the corrected "relevant" code as well as the results on a 2Y 1D chart... As you can see, every time there is a new 252D volume it paints the volume bar white, otherwise gray...

Ruby:
``````declare lower;

plot enhancedVolume = volume;

enhancedVolume.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

def highestVolYear = highest(volume,252);

enhancedVolume.assignValueColor(if volume == highestVolYear then COLOR.WHITE else COLOR.GRAY);``````

#### _Merch_Man_

##### Member
Sorry @rad14733 I clearly did not do a good job explaining the problem. The code does what I want except that it does not work for stocks that have not been trading for at least 252 days. For example, if I change the symbol to SLQT then the plot goes blank because highest(volume,252) is returning a NaN.

#### _Merch_Man_

##### Member
Spent another hour on this and have determined that the NaN is not the problem. I am still investigating. Thanks for your help thus far.

Edit 4/2 13:47 EST - I am going to start a new thread with a new problem description.

Last edited:

##### Well-known member
VIP
@_Merch_Man_ While it seems almost inconceivable that Thinkscript doesn't possess a means of determining IPO date or a way to determine the index value of the results of First(), that is our reality... I went and scoured the Learning Center for a potential function but nothing exists that I could find... I even went so far as to check several other resources without any luck...

All that said, one idea I came up with would be to use a fold loop to count back until price returns Double.Nan using IsNaN() and then use that result in associated calculations... Perhaps that is your best course of action... Food for thought...

#### XeoNoX

##### Well-known member
VIP
its not a number so you must use ISNAN
remember to leave a thumbs up if you found this post useful

here is what you originally requested:

Highest Volume in the past XYZ bars (version 1.0)
Works with recently listed IPOs and Trading Instruments
Default is 252 Days which is Highest Volume in past Year
Set to Day Aggregation

Code:
``````## Highest Volume in the past XYZ bars v1.0
## Works with recently listed IPOs and Trading Instruments
## Default is 252 Days which is Highest Volume in past Year
## Set to Day Agreggation
## By XeoNoX via usethinkscript.com April 02,2021
declare lower;
Input BarsAgo = 252;
def hv = if volume > hv[1] then volume else hv[1];
def highestVolYear = if isnan(highest(volume,barsago)) then hv else highest(volume,barsago);
AddLabel (yes, "Highest Volume " +  (highestVolYear )  );
plot scan = highestVolYear;``````

explanation:
if volume is greater then the previous volume then use that volume else keep searching
def hv = if volume > hv[1] then volume else hv[1];

if the highest volume from the past 252 bars is not a number then use HV else if it is a number then use highest volume from past 252 bars
def highestVolYear = if isnan(highest(volume,barsago)) then hv else highest(volume,barsago);

Last edited:

#### _Merch_Man_

##### Member
its not a number so you must use ISNAN
remember to leave a thumbs up if you found this post useful

here is what you originally requested:

Highest Volume in the past XYZ bars (version 1.0)
Works with recently listed IPOs and Trading Instruments
Default is 252 Days which is Highest Volume in past Year
Set to Day Aggregation

Code:
``````## Highest Volume in the past XYZ bars v1.0
## Works with recently listed IPOs and Trading Instruments
## Default is 252 Days which is Highest Volume in past Year
## Set to Day Agreggation
## By XeoNoX via usethinkscript.com April 02,2021
declare lower;
Input BarsAgo = 252;
def hv = if volume > hv[1] then volume else hv[1];
def highestVolYear = if isnan(highest(volume,barsago)) then hv else highest(volume,barsago);
AddLabel (yes, "Highest Volume " +  (highestVolYear )  );
plot scan = highestVolYear;``````

explanation:
if volume is greater then the previous volume then use that volume else keep searching
def hv = if volume > hv[1] then volume else hv[1];

if the highest volume from the past 252 bars is not a number then use HV else if it is a number then use highest volume from past 252 bars
def highestVolYear = if isnan(highest(volume,barsago)) then hv else highest(volume,barsago);

@XeoNoX - Thanks for taking the time to reply. Did you try this code on a chart with less than 252 bars? When I try this code I see the same problem - if there are less than 252 bars on the chart then the plot does not show up.

#### XeoNoX

##### Well-known member
VIP
yes i tried it, can you screenshot what you are seeing? the labels seem to return value

#### _Merch_Man_

##### Member
Yes, I get a label, but no volume histogram. You can see where the cursor is over the NewStudy2 the value is N/A over on the right axis.
Thanks for your help on this.

#### SleepyZ

##### Active member
Yes, I get a label, but no volume histogram. You can see where the cursor is over the NewStudy2 the value is N/A over on the right axis.
Thanks for your help on this.

See if this helps. This is a workaround some of thinkscript's limitations when dealing with symbols opened less than the 'barsago' days. As mentioned, there may be simpler solutions, but this may be what you are requesting. The attached chart shows the same study in both the upper and lower panes.
Code:
``````#Example Highest Volume within Last barsago
#Usethinkscript request
#Workaround to handle charts with less than barsago input bars
#Sleepyz

#Use at least on a 2 YEAR DAILY CHART

input barsago   = 252;
input showlabel = yes;

def lastbar = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def range   = CompoundValue(1, if Between(BarNumber(), HighestAll(lastbar) - barsago, HighestAll(lastbar)) then 1 else 0, 1);

#Highest Volume on a chart with more bars than barsago
def vol       = if range[1] == 0 and range == 1
then volume
else if volume > vol[1]
then volume
else vol[1];
def volext    = if BarNumber() > HighestAll(lastbar) then volext[1] else vol;
def volb      = if range == 1 and volume == vol
then BarNumber()
else Double.NaN;
plot v252_end = if HighestAll(lastbar) > barsago and BarNumber() >= HighestAll(volb)
then volext
else Double.NaN;

v252_end.setdefaultColor(color.yellow);
v252_end.setpaintingStrategy(paintingStrategy.HORIZONTAL);

#Highest Volume on a chart with less bars than barsago
def r       = if HighestAll(lastbar) < barsago then 1 else 0;
def volh    = if BarNumber() == 1 and !IsNaN(range)
then volume
else if  volume > volh[1]
then volume
else volh[1];
def volhext = if BarNumber() > HighestAll(lastbar) then volhext[1] else volh;
def volb1   = if volume == volh
then barnumber()
else Double.NaN;
plot v1_end = if isnan(r) or r then volhext else double.nan;
v1_end.setdefaultColor(color.yellow);
v1_end.setpaintingStrategy(paintingStrategy.HORIZONTAL);

AddLabel(showlabel, "Highest Volume" + (if r==1 then " Less than "  else "  ") + barsago + " days ago == " + (if r==1 then volh else vol), Color.YELLOW);

addverticalLine(if r==1 then barnumber()==highestall(volb1) else barnumber()==highestall(volb),"",color.yellow);``````

#### _Merch_Man_

##### Member
See if this helps. This is a workaround some of thinkscript's limitations when dealing with symbols opened less than the 'barsago' days. As mentioned, there may be simpler solutions, but this may be what you are requesting. The attached chart shows the same study in both the upper and lower panes.
Code:
``````#Example Highest Volume within Last barsago
#Usethinkscript request
#Workaround to handle charts with less than barsago input bars
#Sleepyz

#Use at least on a 2 YEAR DAILY CHART

input barsago   = 252;
input showlabel = yes;

def lastbar = if IsNaN(close[-1]) and !IsNaN(close) then BarNumber() else Double.NaN;
def range   = CompoundValue(1, if Between(BarNumber(), HighestAll(lastbar) - barsago, HighestAll(lastbar)) then 1 else 0, 1);

#Highest Volume on a chart with more bars than barsago
def vol       = if range[1] == 0 and range == 1
then volume
else if volume > vol[1]
then volume
else vol[1];
def volext    = if BarNumber() > HighestAll(lastbar) then volext[1] else vol;
def volb      = if range == 1 and volume == vol
then BarNumber()
else Double.NaN;
plot v252_end = if HighestAll(lastbar) > barsago and BarNumber() >= HighestAll(volb)
then volext
else Double.NaN;

v252_end.setdefaultColor(color.yellow);
v252_end.setpaintingStrategy(paintingStrategy.HORIZONTAL);

#Highest Volume on a chart with less bars than barsago
def r       = if HighestAll(lastbar) < barsago then 1 else 0;
def volh    = if BarNumber() == 1 and !IsNaN(range)
then volume
else if  volume > volh[1]
then volume
else volh[1];
def volhext = if BarNumber() > HighestAll(lastbar) then volhext[1] else volh;
def volb1   = if volume == volh
then barnumber()
else Double.NaN;
plot v1_end = if isnan(r) or r then volhext else double.nan;
v1_end.setdefaultColor(color.yellow);
v1_end.setpaintingStrategy(paintingStrategy.HORIZONTAL);

AddLabel(showlabel, "Highest Volume" + (if r==1 then " Less than "  else "  ") + barsago + " days ago == " + (if r==1 then volh else vol), Color.YELLOW);

addverticalLine(if r==1 then barnumber()==highestall(volb1) else barnumber()==highestall(volb),"",color.yellow);``````
Thanks for taking the time to respond @SleepyZ. I am still trying to fully understand the code and how I could use it to get the desired bar color effects. Thanks again.