Day of Week Gap Analytics For ThinkOrSwim

Ommni007

New member
VIP
Day of Week Gap Analytics is generally understudied,
This Gap Analytics study complement's @mashume's work by tracking the cumulative ticks gained/lost on gap bars segmented by DAY OF WEEK.
https://usethinkscript.com/threads/...forms-well-on-a-particular-day-of-year.16929/

3 types of analysis can be run: Close-Close, Close-Open, Open-Close. Clarifying equity expansion/contraction by DAY OF WEEK is it's primary value - perhaps use it to complement your other trading tools. Example use: $AMD Daily, 1 Year chart - Close-Close analysis. Significant equity expansion is from Wednesday to Monday. > *Buy near Wednesday's Close - *Sell the following Monday before the Close IF consistent with your primary indicators. Hope the community finds some value in the study.
2023-11-03-TOS_CHARTS_AMD.png


Ruby:
#Gap Analytics Study

#Ommni007, 11/3/23 Free to share within UseThinkscript Community
#Change log: 11/3/23 Trimmed to basic version

#hint: Tracks the cumulative ticks gained/lost on gap bars segmented by DAY OF WEEK.
#hint i_type: The type of analysis to run. Options are: <b>CloseOpen</b> (current open minus previous close), <b>OpenClose</b> (current close versus current open), and <b>CloseClose</b> (current close versus previous close)


declare lower;

#INPUTS
input i_print_day_labels = YES;
input i_type = {default CloseOpen, OpenClose, CloseClose};
input timeFrame = {default DAY, WEEK, MONTH};


#LOGIC
def v_tick_size = TickSize();
def v_contract_roll = HasContractChangeEvent();
def v_weekday = GetDayOfWeek(GetYYYYMMDD());

def v_gap_up;
def v_gap_down;
switch (i_type)
{
case CloseOpen:
if open(period = timeFrame) > close(period = timeFrame)[1] and !v_contract_roll
then {
v_gap_up = 1;
}
else {
v_gap_up = 0;
}
if open(period = timeFrame) < close(period = timeFrame)[1] and !v_contract_roll
then {
v_gap_down = 1;
}
else {
v_gap_down = 0;
}
case OpenClose:
if close(period = timeFrame) > open(period = timeFrame)
then {
v_gap_up = 1;
}
else {
v_gap_up = 0;
}
if close(period = timeFrame) < open(period = timeFrame)
then {
v_gap_down = 1;
}
else {
v_gap_down = 0;
}
case CloseClose:
if close(period = timeFrame) > close(period = timeFrame)[1] and !v_contract_roll
then {
v_gap_up = 1;
}
else {
v_gap_up = 0;
}
if close(period = timeFrame) < close(period = timeFrame)[1] and !v_contract_roll
then {
v_gap_down = 1;
}
else {
v_gap_down = 0;
}
}

def v_gap_size;
switch (i_type)
{
case CloseOpen:
if (v_gap_up or v_gap_down)
then {
v_gap_size = (open(period = timeFrame) - close(period = timeFrame)[1]) / v_tick_size;
}
else {
v_gap_size = 0;
}
case OpenClose:
if (v_gap_up or v_gap_down)
then {
v_gap_size = (close(period = timeFrame) - open(period = timeFrame)) / v_tick_size;
}
else {
v_gap_size = 0;
}
case CloseClose:
if (v_gap_up or v_gap_down)
then {
v_gap_size = (close(period = timeFrame) - close(period = timeFrame)[1]) / v_tick_size;
}
else {
v_gap_size = 0;
}
}

#ACCUMULATE THE GAPS THEMSELVES (IN TICKS). GAPS UP ARE ADDITIVE, GAPS DOWN ARE SUBTRACTIVE.
def v_mon_gap;
def v_tue_gap;
def v_wed_gap;
def v_thu_gap;
def v_fri_gap;

if v_weekday == 1 and (v_gap_up or v_gap_down)
then {v_mon_gap = v_mon_gap[1] + v_gap_size;}
else {v_mon_gap = v_mon_gap[1];}
if v_weekday == 2 and (v_gap_up or v_gap_down)
then {v_tue_gap = v_tue_gap[1] + v_gap_size;}
else {v_tue_gap = v_tue_gap[1];}
if v_weekday == 3 and (v_gap_up or v_gap_down)
then {v_wed_gap = v_wed_gap[1] + v_gap_size;}
else {v_wed_gap = v_wed_gap[1];}
if v_weekday == 4 and (v_gap_up or v_gap_down)
then {v_thu_gap = v_thu_gap[1] + v_gap_size;}
else {v_thu_gap = v_thu_gap[1];}
if v_weekday == 5 and (v_gap_up or v_gap_down)
then {v_fri_gap = v_fri_gap[1] + v_gap_size;}
else {v_fri_gap = v_fri_gap[1];}

def v_avg_gap_mon = TotalSum(AbsValue(v_mon_gap-v_mon_gap[1]))/TotalSum(v_mon_gap <> v_mon_gap[1]);
def v_avg_gap_tue = TotalSum(AbsValue(v_tue_gap-v_tue_gap[1]))/TotalSum(v_tue_gap <> v_tue_gap[1]);
def v_avg_gap_wed = TotalSum(AbsValue(v_wed_gap-v_wed_gap[1]))/TotalSum(v_wed_gap <> v_wed_gap[1]);
def v_avg_gap_thu = TotalSum(AbsValue(v_thu_gap-v_thu_gap[1]))/TotalSum(v_thu_gap <> v_thu_gap[1]);
def v_avg_gap_fri = TotalSum(AbsValue(v_fri_gap-v_fri_gap[1]))/TotalSum(v_fri_gap <> v_fri_gap[1]);


#PLOTS
plot p_mon_gap = Round(v_mon_gap * v_tick_size,2);
plot p_tue_gap = Round(v_tue_gap * v_tick_size,2);
plot p_wed_gap = Round(v_wed_gap * v_tick_size,2);
plot p_thu_gap = Round(v_thu_gap * v_tick_size,2);
plot p_fri_gap = Round(v_fri_gap * v_tick_size,2);
plot p_zero = 0;

#PLOT STYLES & SETTINGS
p_mon_gap.SetDefaultColor(Color.MAGENTA);
p_tue_gap.SetDefaultColor(Color.CYAN);
p_wed_gap.SetDefaultColor(Color.LIGHT_GRAY);
p_thu_gap.SetDefaultColor(Color.GREEN);
p_fri_gap.SetDefaultColor(Color.YELLOW);
p_zero.SetStyle(Curve.SHORT_DASH);
p_zero.SetDefaultColor(Color.WHITE);
p_zero.SetLineWeight(3);
AddLabel(i_print_day_labels, "MON", Color.MAGENTA);
AddLabel(i_print_day_labels, "TUE", Color.CYAN);
AddLabel(i_print_day_labels, "WED", Color.LIGHT_GRAY);
AddLabel(i_print_day_labels, "THU", Color.GREEN);
AddLabel(i_print_day_labels, "FRI", Color.YELLOW);
 
Last edited by a moderator:
The Gap Analytics Study can be deployed in either Aggressive or Conservative mode. Aggressive mode is last bar open, whereas Conservative mode is last bar closed. The first version posted is Aggressive mode. The study below is Conservative mode. What's the difference? For example, Open-Close Aggressive the offsets are this:

case OpenClose:
if close(period = timeFrame) > open(period = timeFrame)

..and for Open-Close Conservative this:

case OpenClose:
if close(period = timeFrame)[1] > open(period = timeFrame)[1]

The difference between the two is clear on the comparative $AMD chart where Conservative mode is the lower half of the chart. Look at Tuesday on the Conservative chart. It's interpreted as "If Monday's Open-Close Gap is positive, then a LONG position will be taken at Tuesday’s open and flattened at Tuesday’s close". The difference in Tuesday equity gain is noteworthy. Will make sense the more you work with Conservative mode.

#Gap Analytics Study

#Ommni007 11/3/23 Free to share within UseThinkscript Community

#Change log: 11/3/23 Basic version Conservative Mode

#hint i_type: The type of analysis to run. Options are: <b>CloseOpen</b> (current open minus previous close), <b>OpenClose</b> (previous close versus previous open), and <b>CloseClose</b> (previous close versus 2nd previous close)


declare lower;

#INPUTS
input i_print_day_labels = YES;
input i_type = {default CloseOpen, OpenClose, CloseClose};
input timeFrame = {default DAY, WEEK, MONTH};


#LOGIC
def v_tick_size = TickSize();
def v_contract_roll = HasContractChangeEvent();
def v_weekday = GetDayOfWeek(GetYYYYMMDD());

def v_gap_up;
def v_gap_down;
switch (i_type)
{
case CloseOpen:
if open(period = timeFrame) > close(period = timeFrame)[1] and !v_contract_roll
then {
v_gap_up = 1;
}
else {
v_gap_up = 0;
}
if open(period = timeFrame) < close(period = timeFrame)[1] and !v_contract_roll
then {
v_gap_down = 1;
}
else {
v_gap_down = 0;
}
case OpenClose:
if close(period = timeFrame)[1] > open(period = timeFrame)[1]
then {
v_gap_up = 1;
}
else {
v_gap_up = 0;
}
if close(period = timeFrame)[1] < open(period = timeFrame)[1]
then {
v_gap_down = 1;
}
else {
v_gap_down = 0;
}
case CloseClose:
if close(period = timeFrame)[1] > close(period = timeFrame)[2] and !v_contract_roll
then {
v_gap_up = 1;
}
else {
v_gap_up = 0;
}
if close(period = timeFrame)[1] < close(period = timeFrame)[2] and !v_contract_roll
then {
v_gap_down = 1;
}
else {
v_gap_down = 0;
}
}

def v_gap_size;
switch (i_type)
{
case CloseOpen:
if (v_gap_up or v_gap_down)
then {
v_gap_size = (open(period = timeFrame) - close(period = timeFrame)[1]) / v_tick_size;
}
else {
v_gap_size = 0;
}
case OpenClose:
if (v_gap_up or v_gap_down)
then {
v_gap_size = (close(period = timeFrame)[1] - open(period = timeFrame)[1]) / v_tick_size;
}
else {
v_gap_size = 0;
}
case CloseClose:
if (v_gap_up or v_gap_down)
then {
v_gap_size = (close(period = timeFrame)[1] - close(period = timeFrame)[2]) / v_tick_size;
}
else {
v_gap_size = 0;
}
}

#ACCUMULATE THE GAPS THEMSELVES (IN TICKS). GAPS UP ARE ADDITIVE, GAPS DOWN ARE SUBTRACTIVE.
def v_mon_gap;
def v_tue_gap;
def v_wed_gap;
def v_thu_gap;
def v_fri_gap;

if v_weekday == 1 and (v_gap_up or v_gap_down)
then {v_mon_gap = v_mon_gap[1] + v_gap_size;}
else {v_mon_gap = v_mon_gap[1];}
if v_weekday == 2 and (v_gap_up or v_gap_down)
then {v_tue_gap = v_tue_gap[1] + v_gap_size;}
else {v_tue_gap = v_tue_gap[1];}
if v_weekday == 3 and (v_gap_up or v_gap_down)
then {v_wed_gap = v_wed_gap[1] + v_gap_size;}
else {v_wed_gap = v_wed_gap[1];}
if v_weekday == 4 and (v_gap_up or v_gap_down)
then {v_thu_gap = v_thu_gap[1] + v_gap_size;}
else {v_thu_gap = v_thu_gap[1];}
if v_weekday == 5 and (v_gap_up or v_gap_down)
then {v_fri_gap = v_fri_gap[1] + v_gap_size;}
else {v_fri_gap = v_fri_gap[1];}

def v_avg_gap_mon = TotalSum(AbsValue(v_mon_gap-v_mon_gap[1]))/TotalSum(v_mon_gap <> v_mon_gap[1]);
def v_avg_gap_tue = TotalSum(AbsValue(v_tue_gap-v_tue_gap[1]))/TotalSum(v_tue_gap <> v_tue_gap[1]);
def v_avg_gap_wed = TotalSum(AbsValue(v_wed_gap-v_wed_gap[1]))/TotalSum(v_wed_gap <> v_wed_gap[1]);
def v_avg_gap_thu = TotalSum(AbsValue(v_thu_gap-v_thu_gap[1]))/TotalSum(v_thu_gap <> v_thu_gap[1]);
def v_avg_gap_fri = TotalSum(AbsValue(v_fri_gap-v_fri_gap[1]))/TotalSum(v_fri_gap <> v_fri_gap[1]);


#PLOTS
plot p_mon_gap = Round(v_mon_gap * v_tick_size,2);
plot p_tue_gap = Round(v_tue_gap * v_tick_size,2);
plot p_wed_gap = Round(v_wed_gap * v_tick_size,2);
plot p_thu_gap = Round(v_thu_gap * v_tick_size,2);
plot p_fri_gap = Round(v_fri_gap * v_tick_size,2);
plot p_zero = 0;

#PLOT STYLES & SETTINGS
p_mon_gap.SetDefaultColor(Color.MAGENTA);
p_tue_gap.SetDefaultColor(Color.CYAN);
p_wed_gap.SetDefaultColor(Color.LIGHT_GRAY);
p_thu_gap.SetDefaultColor(Color.GREEN);
p_fri_gap.SetDefaultColor(Color.YELLOW);
p_zero.SetStyle(Curve.SHORT_DASH);
p_zero.SetDefaultColor(Color.WHITE);
p_zero.SetLineWeight(3);
AddLabel(i_print_day_labels, "MON", Color.MAGENTA);
AddLabel(i_print_day_labels, "TUE", Color.CYAN);
AddLabel(i_print_day_labels, "WED", Color.LIGHT_GRAY);
AddLabel(i_print_day_labels, "THU", Color.GREEN);
AddLabel(i_print_day_labels, "FRI", Color.YELLOW);

#END CODE

2023-11-03-TOS_CHARTS_amd_OC_compare.png
 

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