calculate weekdays stats based on open vs close

pgupta1646

New member
How to calculate week day stats based on current day opn vs closed prices of the stocks? I want to see which day of the week is best to buy & sell SPY or QQQ on weekly basis?
 
How to calculate week day stats based on current day opn vs closed prices of the stocks? I want to see which day of the week is best to buy & sell SPY or QQQ on weekly basis?

'best to buy' is one of the most vague descriptions i have worked off....
i don't think there is any value to finding data on a day of the week, but...
i like a challenge,


this collects the price range of every day.
it adds up every monday range, then calculates an average. it does this for the 5 days in a week.
it finds the highest and lowest range averages.
it plots bubbles after the last bar, the biggest average is green. lowest is red. other 3 are gray.
my chart was 30D 1hr , so 7 weeks . works on any time ,


Code:
#day_stats_01

#https://usethinkscript.com/threads/calculate-weekdays-stats-based-on-current-day-opn-vs-closed-prices.20039/
#calculate weekdays stats based on current day opn vs closed prices

def o = open;
def h = high;
def l = low;
def c = close;

def na = double.nan;
def bn = barnumber();
def lastbar = !isnan(close[0]) and isnan(close[-1]);
def lastcls = if !isnan(close) and !lastbar then close else if lastbar then close else lastcls[1];

def dat = getYYYYMMDD();
def d = getday();
def day_start = if d != d[1] then 1 else 0;
def day_end = if d != d[-1] then 1 else 0;
def DOW = GetDayOfWeek(dat);

def week_cnt = if bn == 1 then 0
 else if isnan(c) then week_cnt[1]
 else if (day_end and DOW == 1) then week_cnt[1] + 1
 else week_cnt[1];
addlabel(1, "weeks " + week_cnt, color.yellow);

def s = 0;
def mon_open = if bn == 1 then s else if isnan(c) then mon_open[1] else if dow == 1 and day_start then o else mon_open[1];
def mon_cls = if bn == 1 then s else if isnan(c) then mon_cls[1] else if dow == 1 and day_end then c else mon_cls[1];
def tue_open = if bn == 1 then s else if isnan(c) then tue_open[1] else if dow == 2 and day_start then o else tue_open[1];
def tue_cls = if bn == 1 then s else if isnan(c) then tue_cls[1] else if dow == 2 and day_end then c else tue_cls[1];
def wed_open = if bn == 1 then s else if isnan(c) then wed_open[1] else if dow == 3 and day_start then o else wed_open[1];
def wed_cls = if bn == 1 then s else if isnan(c) then wed_cls[1] else if dow == 3 and day_end then c else wed_cls[1];
def thr_open = if bn == 1 then s else if isnan(c) then thr_open[1] else if dow == 4 and day_start then o else thr_open[1];
def thr_cls = if bn == 1 then s else if isnan(c) then thr_cls[1] else if dow == 4 and day_end then c else thr_cls[1];
def fri_open = if bn == 1 then s else if isnan(c) then fri_open[1] else if dow == 5 and day_start then o else fri_open[1];
def fri_cls = if bn == 1 then s else if isnan(c) then fri_cls[1] else if dow == 5 and day_end then c else fri_cls[1];


def t = 0;
def mon_rng = if bn == 1 then t else if isnan(c) then mon_rng[1] else if dow == 1 and day_end then (mon_cls - mon_open) + mon_rng[1] else mon_rng[1];
def mon_avg = (mon_rng/week_cnt);

def tue_rng = if bn == 1 then t else if isnan(c) then tue_rng[1] else if dow == 2 and day_end then (tue_cls - tue_open) + tue_rng[1] else tue_rng[1];
def tue_avg = (tue_rng/week_cnt);

def wed_rng = if bn == 1 then t else if isnan(c) then wed_rng[1] else if dow == 3 and day_end then (wed_cls - wed_open) + wed_rng[1] else wed_rng[1];
def wed_avg = (wed_rng/week_cnt);

def thr_rng = if bn == 1 then t else if isnan(c) then thr_rng[1] else if dow == 4 and day_end then (thr_cls - thr_open) + thr_rng[1] else thr_rng[1];
def thr_avg = (thr_rng/week_cnt);

def fri_rng = if bn == 1 then t else if isnan(c) then fri_rng[1] else if dow == 5 and day_end then (fri_cls - fri_open) + fri_rng[1] else fri_rng[1];
def fri_avg = (fri_rng/week_cnt);


def avgmax = max(mon_avg, max(tue_avg, max(wed_avg, max(thr_avg, fri_avg))));
def avgmin = min(mon_avg, min(tue_avg, min(wed_avg, min(thr_avg, fri_avg)))); 

input show_bubble = yes;
addchartbubble(show_bubble and lastbar[3], lastcls,
"Fri " + fri_avg
, (if avgmax == fri_avg then color.green else if avgmin == fri_avg then color.red else color.gray), yes);

addchartbubble(show_bubble and lastbar[3], lastcls,
"Thr " + thr_avg
, (if avgmax == thr_avg then color.green else if avgmin == thr_avg then color.red else color.gray), yes);

addchartbubble(show_bubble and lastbar[3], lastcls,
"Wed " + wed_avg
, (if avgmax == wed_avg then color.green else if avgmin == wed_avg then color.red else color.gray), yes);

addchartbubble(show_bubble and lastbar[3], lastcls,
"Tue " + tue_avg
, (if avgmax == tue_avg then color.green else if avgmin == tue_avg then color.red else color.gray), yes);

addchartbubble(show_bubble and lastbar[3], lastcls,
"Mon " + mon_avg
, (if avgmax == mon_avg then color.green else if avgmin == mon_avg then color.red else color.gray), yes);

addchartbubble(show_bubble and lastbar[3], lastcls, " ", color.black, yes);

addchartbubble(show_bubble and lastbar[3], lastcls,
("day avg ranges\n" +
" over " + week_cnt + " weeks" )
, color.yellow, yes);


#--------------------------------------
# test stuff

input test_day = no;
addchartbubble(test_day and day_start, 0,
    (if DOW == 1 then "Mon"
else if DOW == 2 then "Tue"
else if DOW == 3 then "Wed"
else if DOW == 4 then "Thur"
else if DOW == 5 then "Fri"
else "-")
, color.yellow, yes);



#addchartbubble(show_bubble and lastbar[3],
#lastcls,
#("day avg ranges\n" +
#" over " + week_cnt + " weeks\n" + 
#"Mon " + mon_avg + "\n" +
#"Tue " + tue_avg + "\n" +
#"Wed " + wed_avg + "\n" +
#"Thr " + thr_avg + "\n" +
#"Fri " + fri_avg ) 
#, color.yellow, yes);


#plot z1 = mon_open;
#plot z2 = mon_cls;
#plot z3 = if mon_cls > 0 then mon_rng2 else na;
#plot z4 = if mon_cls > 0 then mon_rng else na;
#plot z5 = mon_avg;
#plot z0 = 0;

input test2 = no;
addchartbubble(test2 and (dow == 1 and day_end), 0,
# dow + " dy\n" +
# day_end + " E\n" +
 mon_cls + " c\n" +
 mon_open + " o\n" +
# mon_rng2 + " r2\n" +
 mon_rng + " r\n" +
 week_cnt + " cnt\n" +
 mon_avg + " av"
, color.yellow, no);

#
 

Attachments

  • img1.JPG
    img1.JPG
    30.9 KB · Views: 36

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
451 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