Tricking daily watchlist column to return prior months high

greco26

Active member
Hi All,

As the subject suggests, I'm tricking the daily watchlist column to identify last month's high. When I manually enter in the BeginDate and EndDate, the code works fine but I'm trying to automatically set the BeginDate and EndDate based on the current month. When I use a formula to calculate the dates, the result is 0. The code isn't recognizing the BeginDate and EndDate in the formula output for some reason. Any ideas why this is the case?


Code:
def H = high;
def L = low;
def C = close;
def O = open;
def bn = barnumber();
def na = double.nan;

def month = Getmonth();
def BeginDate = if
month == 1 then 20211201 else if
month == 2 then 20220101 else if
month == 3 then 20220201 else if
month == 4 then 20220301 else if
month == 5 then 20220401 else if
month == 6 then 20220501 else if
month == 7 then 20220601 else if
month == 8 then 20220701 else if
month == 9 then 20220801 else if
month == 10 then 20220901 else if
month == 11 then 20221001 else if
month == 12 then 20221101 else na;

def EndDate = if
month == 1 then 20211231 else if
month == 2 then 20220131 else if
month == 3 then 20220228 else if
month == 4 then 20220331 else if
month == 5 then 20220430 else if
month == 6 then 20220531 else if
month == 7 then 20220630 else if
month == 8 then 20220731 else if
month == 9 then 20220831 else if
month == 10 then 20220930 else if
month == 11 then 20221031 else if
month == 12 then 20221130 else na;



def ispriormonth = if DaysFromDate(BeginDate) >= 0 and
DaysTillDate(EndDate) >= 0 then 1 else 0;

def hi = if bn == 0 or !ispriormonth then 0
else if ispriormonth and high > hi[1] then high else hi[1];

def hihi = highestall(hi);
def z = if ispriormonth then hihi else z[1];
plot zz= z;
 
Hi All,

As the subject suggests, I'm tricking the daily watchlist column to identify last month's high. When I manually enter in the BeginDate and EndDate, the code works fine but I'm trying to automatically set the BeginDate and EndDate based on the current month. When I use a formula to calculate the dates, the result is 0. The code isn't recognizing the BeginDate and EndDate in the formula output for some reason. Any ideas why this is the case?


Code:
def H = high;
def L = low;
def C = close;
def O = open;
def bn = barnumber();
def na = double.nan;

def month = Getmonth();
def BeginDate = if
month == 1 then 20211201 else if
month == 2 then 20220101 else if
month == 3 then 20220201 else if
month == 4 then 20220301 else if
month == 5 then 20220401 else if
month == 6 then 20220501 else if
month == 7 then 20220601 else if
month == 8 then 20220701 else if
month == 9 then 20220801 else if
month == 10 then 20220901 else if
month == 11 then 20221001 else if
month == 12 then 20221101 else na;

def EndDate = if
month == 1 then 20211231 else if
month == 2 then 20220131 else if
month == 3 then 20220228 else if
month == 4 then 20220331 else if
month == 5 then 20220430 else if
month == 6 then 20220531 else if
month == 7 then 20220630 else if
month == 8 then 20220731 else if
month == 9 then 20220831 else if
month == 10 then 20220930 else if
month == 11 then 20221031 else if
month == 12 then 20221130 else na;



def ispriormonth = if DaysFromDate(BeginDate) >= 0 and
DaysTillDate(EndDate) >= 0 then 1 else 0;

def hi = if bn == 0 or !ispriormonth then 0
else if ispriormonth and high > hi[1] then high else hi[1];

def hihi = highestall(hi);
def z = if ispriormonth then hihi else z[1];
plot zz= z;

Date functions in TOS are difficult to use. So in this case, the following will work in a watchlist for the prior month's high.

Screenshot-2022-12-27-112046.png
 

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

Thx but I can't have the watchlist column set to month but instead, day. So that's the 'trick' as im getting the prior month's high based on the daily aggregation. Make sense?
 
Thx but I can't have the watchlist column set to month but instead, day. So that's the 'trick' as im getting the prior month's high based on the daily aggregation. Make sense?

Since you did not want to use a MONTH watchlist, I modified your script to be used on a DAY watchlist looking for the prior month's high. .

I modified your script as there were various problems such as with your defs for begindate and enddate outputs being used with daysfromdate (56) and daystilldate (-27) to define the prior month.

Here is the revised code with an image showing the result on a DAY chart, MONTH chart and a DAY Watchlist.

Screenshot-2022-12-27-121108.png
Ruby:
def H = high;
def L = low;
def C = close;
def O = open;
def bn = BarNumber();
def na = Double.NaN;

input monthsback = 1;
def hi = if getmonth()[monthsback + 1 ]!=getmonth()[monthsback]
         then high[1]
         else if getmonth()[monthsback] and high > hi[1]
         then high
         else hi[1];

def zz = if getmonth()!=getmonth()[monthsback] then hi[1] else zz[1];
AddLabel(1,zz, Color.YELLOW);
 
Last edited:
hmm, your code looks like its returning correct high for the prior month for SPY however it's not returning the correct prior month's high for other tickers such as AAPL, AMZN.
 
never mind! I just needed to offset the high as it wasn't counting the first day of the month. @SleepyZ You are amazing as usual!!
Code:
input monthsback = 1;
def hi = if getmonth()[monthsback + 1 ]!=getmonth()[monthsback]
         then high[1]
         else if getmonth()[monthsback] and high > hi[1]
         then high
         else hi[1];

def zz = if getmonth()!=getmonth()[monthsback] then hi[1] else zz[1];

plot yy = zz;
 
hmm, your code looks like its returning correct high for the prior month for SPY however it's not returning the correct prior month's high for other tickers such as AAPL, AMZN.

Sorry, I corrected the above code for those. It was not picking up the first day of the prior month.
 
never mind! I just needed to offset the high as it wasn't counting the first day of the month. @SleepyZ You are amazing as usual!!
Code:
input monthsback = 1;
def hi = if getmonth()[monthsback + 1 ]!=getmonth()[monthsback]
         then high[1]
         else if getmonth()[monthsback] and high > hi[1]
         then high
         else hi[1];

def zz = if getmonth()!=getmonth()[monthsback] then hi[1] else zz[1];

plot yy = zz;

One last correction so that you can just change monthsback and get the high of the month back it references.

Ruby:
def H = high;
def L = low;
def C = close;
def O = open;
def bn = BarNumber();
def na = Double.NaN;


def hi = if GetMonth()[2] != (GetMonth()[1])
         then H[1]
         else if getmonth()[2] and H > hi[1]
         then H
         else hi[1];

input monthsback = 1;
def zz = if GetMonth()== GetlastMonth()-monthsback then hi[1] else zz[1];
AddLabel(1, zz, Color.YELLOW);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
409 Online
Create Post

Similar threads

Similar threads

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