New High found on a current week

shakib3585

Active member
VIP
Greetings all,

I need help building a script that allows you to check if the current daily high of a given week has passed the previous daily high of the same week. It will display a label box with the following information: if broken, "New high achieved", otherwise, "No New high achieved". I wish to run it on a daily aggregation period. I have tried to write a script as follows :

"
def newWeek = GetWeek()== GetWeek()[1];

def newday = GetDay()<>GetDay()[1];
def CDH = if !newWeek then Double.NaN
else if newday then high else if high > CDH[1] then high else CDH[1];
AddLabel(yes, Concat("DH: ", CDH), Color.green);
AddLabel(yes, if CDH>CDH[1] then “New high achieved” else “No New high achieved”, if CDH>CDH[1] then color.greeN else color.red); "

The script only compares with the previous day, not with the earlier days of the same week.

Any help is much appreciated.

Thanks,

Fais
 
Last edited:
Solution
I just found a code that worked. If there are loopholes, please feel free to comment,

#BHAI_BHAI_CODE

def newWeek = GetWeek() <> GetWeek()[1];

rec weeklyHigh = if newWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > weeklyHigh[1] then High(period=aggregationPeriod.DAY) else weeklyHigh[1];

def CWH = weeklyHigh;

def WH = if NewWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > WH[1] then High(period=aggregationPeriod.DAY) else WH[1];

def PWH = if NewWeek then WH[1] else PWH[1];

def h_d = High(period=aggregationPeriod.DAY) ;

# as a reminder, the h_d is based off a Daily Aggregation only

def p1 = CWH > PWH;

def p2 = h_d == CWH;

AddLabel(yes, if p1 then...
I just found a code that worked. If there are loopholes, please feel free to comment,

#BHAI_BHAI_CODE

def newWeek = GetWeek() <> GetWeek()[1];

rec weeklyHigh = if newWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > weeklyHigh[1] then High(period=aggregationPeriod.DAY) else weeklyHigh[1];

def CWH = weeklyHigh;

def WH = if NewWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > WH[1] then High(period=aggregationPeriod.DAY) else WH[1];

def PWH = if NewWeek then WH[1] else PWH[1];

def h_d = High(period=aggregationPeriod.DAY) ;

# as a reminder, the h_d is based off a Daily Aggregation only

def p1 = CWH > PWH;

def p2 = h_d == CWH;

AddLabel(yes, if p1 then if p2 then “New DH achieved and PWH surpassed” else “ Only PWH surpassed” else “PWH NOT Surpassed “, if p1 then if p2 then color.green else color.Dark_Green else Color.Lime);

[/CODE]
 
Last edited:
Solution
I just found a code that worked. If there are loopholes, please feel free to comment,

#BHAI_BHAI_CODE

def newWeek = GetWeek() <> GetWeek()[1];

rec weeklyHigh = if newWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > weeklyHigh[1] then High(period=aggregationPeriod.DAY) else weeklyHigh[1];

def CWH = weeklyHigh;

def WH = if NewWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > WH[1] then High(period=aggregationPeriod.DAY) else WH[1];

def PWH = if NewWeek then WH[1] else PWH[1];

def h_d = High(period=aggregationPeriod.DAY) ;

# as a reminder, the h_d is based off a Daily Aggregation only

def p1 = CWH > PWH;

def p2 = h_d == CWH;

AddLabel(yes, if p1 then if p2 then “New DH achieved and PWH surpassed” else “ Only PWH surpassed” else “PWH NOT Surpassed “, if p1 then if p2 then color.green else color.Dark_Green else Color.Lime);

[/CODE]


the code in post2 doesn't do what your words describe.

i added some code to the end of yours, to verify some values,

Code:
def t = p1 and p2;
input test1 = no;
addchartbubble(test1, low,
CWH + " cwh\n" +
PWH + " pwh\n" +
p1 + " p1=CWH>PWH\n" +
" " + "\n" +
h_d + " hd\n" +
CWH + " cwh\n" +
p2 + " p2=hd=cwh"
, (if t then color.yellow else color.gray), no);


plot y = wh;

def d = High(period=aggregationPeriod.day); 
plot zd = d;
zd.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def w = High(period=aggregationPeriod.week); 
plot zw = w;
zw.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
#




these 2 formulas are the same, so the 2nd one isn't needed,

rec weeklyHigh = if newWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > weeklyHigh[1] then High(period=aggregationPeriod.DAY) else weeklyHigh[1];

def WH = if NewWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > WH[1] then High(period=aggregationPeriod.DAY) else WH[1];


on a monday, this is reading the previous value of wh, the high on friday, which is last weeks high,
def PWH = if NewWeek then WH[1] else PWH[1];

so this is comparing a weekly high (cwh) to last weeks high (pwh), not the current week high
def p1 = CWH > PWH;


----------------------------------


this will place a bubble on the days with a higher high of the week.
can choose to mark mondays as a high or not.
can choose to show bubbles on mondays.


Code:
def WH = if NewWeek then High(period=aggregationPeriod.DAY) else if High(period=aggregationPeriod.DAY) > WH[1] then High(period=aggregationPeriod.DAY) else WH[1];

input include_mondays_as_a_high = yes;
def newhigh = if newweek then include_mondays_as_a_high 
 else if wh != wh[1] then 1
 else 0;

addchartbubble(newhigh, wh,
(if newweek then "monday" else "higher")
, (if newweek then color.yellow else if newhigh then color.green else color.gray), yes);


def lo = low(period=aggregationPeriod.DAY);
input show_mondays = yes;
addchartbubble(show_mondays and newweek, lo,
"monday" 
, color.yellow, no);

plot y = wh;

SJBzNaj.jpg
 
Thank you very much @halcyonguy. So if I understood clearly , the code for the previous week high was correct but the code for the current weekly high was incorrect which you fixed, am I right? Also, can you please help me with a code to find the weekly high of the previous day of the same week.

Thank you
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
350 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