Draw candles from 1 day a week, onto 5 sequential days
Motivation: an internet rumor states that there are identifiable trading patterns on certain days of the week for certain stocks.
IE: Trading on Fridays for Amazon
pick a day of the week. default is monday.
draw new candles with the data from the first 5 desired days on the chart.
. (find all mondays, and pull data from just those days)
coded to find 5 days of data.
can turn off the primary candles.
show date of original days.
show shading on original days
uses addchart() to draw candles
has old commented out code, so others can see what i tried when creating.
INTC 15min
identify wednesdays and redraw the candles from those days, in cyan
hal_dayz hal_shift
i forgot about this, till now. original post.
https://usethinkscript.com/threads/only-show-friday-charts.5588/
Motivation: an internet rumor states that there are identifiable trading patterns on certain days of the week for certain stocks.
IE: Trading on Fridays for Amazon
pick a day of the week. default is monday.
draw new candles with the data from the first 5 desired days on the chart.
. (find all mondays, and pull data from just those days)
coded to find 5 days of data.
can turn off the primary candles.
show date of original days.
show shading on original days
uses addchart() to draw candles
has old commented out code, so others can see what i tried when creating.
INTC 15min
identify wednesdays and redraw the candles from those days, in cyan
hal_dayz hal_shift
i forgot about this, till now. original post.
https://usethinkscript.com/threads/only-show-friday-charts.5588/
Ruby:
# plot_same_day_data_01
# https://usethinkscript.com/threads/only-show-friday-charts.5588/
declare hide_on_daily;
declare once_per_bar;
#-------------------------
# date script
script date1 {
#from tos chat - mobius
def data = getYYYYMMDD();
def year = Round(data/10000, 0);
def month = Round((data % 10000) / 100, 0);
def day = (data % 100);
plot mo = month;
plot dy = day;
}
#addLabel(1, "date: " + month + "/" + day + "/" + AsPrice(year), color.white);
#----------------------
def bn = barnumber();
def na = double.nan;
def maxdays = 5;
input turn_off_main_candles = no;
HidePricePlot(turn_off_main_candles);
input day_of_week = {default Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
#def dow = day_of_week;
input show_day_name_bubbles = no;
input show_day_clouds = no;
input show_day_close_lines = yes;
input show_dates_of_days = yes;
input show_weekly_vert_lines = yes;
addlabel(1, day_of_week, color.orange);
# separater
input OOOOOOOOOO = 0;
input show_day_count = yes;
input show_all_barnumbers = no;
#input show_day_count_bubbles2 = no;
def dow = GetDayofWeek(GetYYYYMMDD());
def vert2 = 0.02;
# this displays the day name, on each bar during that day of the week
def daywken = (GetDayofWeek(GetYYYYMMDD()) == day_of_week + 1);
# AddChartBubble(GetDayofWeek(GetYYYYMMDD()) == day_of_week + 1, high, dow);
#AddChartBubble( show_day_name_bubbles and daywken, high * 1.003, dow, color.magenta, yes);
AddChartBubble( show_day_name_bubbles and daywken and !daywken[1], high * (1 + vert2), day_of_week, color.orange, yes);
# high*1.003
#------------------------------------------------------
#input turn_off_main_candles = no;
# coded for 5 max cdays, so 5 is max to display
def daycntz = if (daywken and !daywken[1]) then daycntz[1] + 1 else daycntz[1];
def dayz_en = daycntz <= maxdays;
addchartbubble( (show_day_count and daycntz and daywken and !daywken[1] and dayz_en), high * (1 + vert2), daycntz, color.orange, yes);
# -----------------------
# test_show_day_lines
#input show_day_close_lines = yes;
#def vert2 = 0.03;
#plot daylines = if (test_show_day_lines and daywken) then (high * (1 + vert2)) else na;
plot daylines = if (show_day_close_lines and daywken and dayz_en) then close else na;
daylines.setlineweight(2);
daylines.SetDefaultColor(Color.orange);
daylines.hidebubble();
# -----------------------
def cldtop = if (show_day_clouds and daywken and dayz_en) then high * (1 + vert2) else na;
addcloud(cldtop, low * (1 - vert2), color.light_gray, color.light_gray);
# -----------------------
# show dates of desired days
#input show_dates_of_days = yes;
#def date_en = if (!turn_off_main_candles and show_dates_of_days and daywken and !daywken[1]) then 1 else 0;
def date_en = if (show_dates_of_days and daywken and !daywken[1] and dayz_en) then 1 else 0;
addchartbubble(date_en, high * (1 + vert2), date1().mo + "/" + date1().dy, color.orange, yes);
# --------------------------
# show vert lines per week
#input show_weekly_vert_lines = yes;
#def week_vert = if (show_weekly_vert_lines and dow == 1 and daywken and !daywken[1]) then 1 else 0;
#def week_vert = if (!turn_off_main_candles and show_weekly_vert_lines and dow == 1 and dow[1] != 1) then 1 else 0;
# find start of a week. check for monday, friday holidays
def week_vert = if (show_weekly_vert_lines and ((dow == 1 and dow[1] != 1) or ( dow[1] == 5 and dow[0] != 5 and dow[0] != 1 ))) then 1 else 0;
addverticalline(week_vert,"-", color.violet);
# --------------------------
#------------------------------------------------------
# --------------------------
input day_count_total_days = no;
# count qty of desired days are on chart
def daywkcnt = if (!daywken[1] and daywken and !isnan(close) ) then daywkcnt[1] + 1 else daywkcnt[1];
# total count of desired days ( available to bar 1 )
def daycntmax = highestall(daywkcnt);
AddChartBubble(day_count_total_days , high * 1.005, daywkcnt + "\n" + daycntmax, color.green, yes);
input show_stats = no;
addlabel( show_stats, "qty of days " + daycntmax, color.yellow);
# ==========================
# get chart agg
def chartagg = getAggregationPeriod();
def chartmin = (chartagg/1000)/60;
# qty of bars in a day , 6.5 hours , 390 minutes
def daybars2 = 390/chartmin;
def daybars = roundup(daybars2,0);
addlabel(show_stats, "chartmin= " + chartmin + " daybars= " + daybars, color.yellow);
# last bar (most recent) , when script gets to this bar
def lastbar = !isnan(close[0]) and isnan(close[-1]);
# last bar (most recent) , usuable at all times in script
def lastbar2 = HighestAll(If(IsNaN(close), 0, bn));
addlabel( show_stats, "lastbar5 " + lastbar2, color.yellow);
# right side of chart
def lastbar3 = highestall(bn);
#addchartbubble(lastbar, low, bn + "\n" + lastbar3, color.cyan, no );
#addchartbubble(show_barnumber_bubbles, low, bn + "\n" + lastbar2, color.cyan, no );
addchartbubble(show_all_barnumbers, low, bn , color.cyan, no );
#addchartbubble(bn == lastbar2, high * 1.02, "xxxx", color.pink, yes);
#addchartbubble(bn == lastbar2, high * 1.01, date1().mo + "/" + date1().dy, color.pink, yes);
# =====================================
# these dont work if a day is missing bars
# find last bar of each day
# !! assumes all days have same qty of bars
#def daylastbar = rounddown(bn / daybars, 0) == (bn / daybars);
#addchartbubble(show_barnumber_bubbles and daylastbar, low * 0.99, bn , color.yellow, no );
# find 1st bar of each day
# !! assumes all days have same qty of bars
#def daybar = 1;
#def db = (daybars - daybar);
#def dayfirstbar = rounddown((bn + db) / daybars, 0) == ((bn + db) / daybars);
# addchartbubble(show_barnumber_bubbles and dayfirstbar, low * 0.99, bn , color.yellow, no );
# addchartbubble(1 , low * 0.99, dayfirstbar , color.yellow, no );
# =====================================
# find first / last bar of day
# GetDayValues
#
#https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
#Hi korygill,
#Your code is great but how do we find one before the last bar. I am looking to close intraday position before the #EOD close. If we use last bar of the day thinkscript will close on next bar open which is next day.
#@SimpleStock, we can add this easily by using an offset on a couple calculations.
#
# GetDayValues
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20190823-1400-KG - Created.
# 20200109-1300-KG - Added Offset to squish bars inward.
# ...
#declare hide_on_daily;
#declare once_per_bar;
# disable onupper var
#input onUpper = yes;
#input Offset = 0; #hint Offset: use this to squeeze the Nth bar inwards.
def Offset = 0;
#def rth = (RegularTradingEnd(GetYYYYMMDD()) - RegularTradingStart(GetYYYYMMDD())) / AggregationPeriod.HOUR;
#AddLabel(yes, "RTH duration (hrs): " + rth);
input day_first_last_bars = no;
#
# logic
#
def nan = Double.NaN;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if
(beforeStart[1+offset] == 1 and beforeStart[offset] == 0) or
(isRollover[offset] and beforeStart[offset] == 0)
then 1
else 0;
def lastBarOfDay = if
(afterEnd[-1-offset] == 1 and afterEnd[offset] == 0) or
(isRollover[-1-offset] and firstBarOfDay[-1-offset])
then 1
else 0;
# test
#addchartbubble( firstBarOfDay , low, firstBarOfDay , color.cyan, no);
# Identify first bar of day and last bar of day on chart
#AddChartBubble(show_first_last_bubbles and firstBarOfDay and onUpper,
AddChartBubble(day_first_last_bars and firstBarOfDay, high, "F", Color.GREEN, yes);
#AddChartBubble(show_first_last_bubbles and lastBarOfDay and lastBarOfDay[1] == 0 and onUpper,
AddChartBubble(day_first_last_bars and lastBarOfDay and lastBarOfDay[1] == 0, high, "L", Color.GREEN, yes);
#
# plots
#
#plot p1 = if !onUpper then isRollover else nan;
#plot p2 = if !onUpper then beforeStart else nan;
#plot p3 = if !onUpper then afterEnd else nan;
#plot p4 = if !onUpper then firstBarOfDay else nan;
#plot p5 = if !onUpper then lastBarOfDay else nan;
#p1.SetDefaultColor(GetColor(1));
#p2.SetDefaultColor(GetColor(2));
#p3.SetDefaultColor(GetColor(3));
#p4.SetDefaultColor(GetColor(4));
#p5.SetDefaultColor(GetColor(5));
#AddLabel(!onUpper, "isRollOver", GetColor(1));
#AddLabel(!onUpper, "beforeStart", GetColor(2));
#AddLabel(!onUpper, "afterEnd", GetColor(3));
#AddLabel(!onUpper, "firstBarOfDay", GetColor(4));
#AddLabel(!onUpper, "lastBarOfDay", GetColor(5));
# add bn
#def daybarfirst = if firstBarOfDay then bn else 0;
#def daybarlast = if lastBarOfDay then bn else 0;
def daybarfirst = if firstBarOfDay then bn else daybarfirst[1];
#def daybarlast = if lastBarOfDay then bn else lastBarOfDay[1];
#input show_dayfirst_bubble = no;
# show the bar # of first bar of the day, on all bars for that day
#addchartbubble(show_dayfirst_bubble, high * 1.02, daybarfirst, color.yellow,yes);
# add highestall() to find the start and stop bn's of specific days ,
# look back
# look at pareto code , highestall ( prev data)
#input show_barnumber_bubbles2 = no;
input day_first_last_barnumbers = no;
addchartbubble(day_first_last_barnumbers and ( firstBarOfDay or lastBarOfDay), low, bn , color.cyan, no );
# ===============================================
#input how_many_days_to_show = 5;
#def dayz = how_many_days_to_show;
# calc how many days can be shown.
# is dayz
def chartdays = lastbar2 / daybars;
addlabel(show_stats, "chartdays= " + chartdays, color.yellow);
# setup several counters
# count the days as drawn on new chart
# is bar1 at day open ?
# count newly positioned weekday days
# this has errors when halts, skipped bars
#def daycnt = round(bn/daybars,0) + 1;
# def dayx = dayz -
# as bn counts up, need to find source data
# find recent day , highest bn
# find day 5x ago
# find start - stop bn of source days
# these lines can have errors if a day has a halt or is missing a bar(s)
# AAL has errors in may 27 , from missing bars , halt ??
input label_first_last_barnumbers = no;
def lflb = label_first_last_barnumbers;
# chg this ...!?
# calc how many desired days on chart
# pick qty of days to show (up to the qty of sets of formulas below)
#
# find days, starting at left side of chart, using lowestall()
def big = 99999;
# chg to lowestall() to find 1st,2nd,3rd...
# chg back to highest , to find last, 2nd last, .... to show the days at end of chart, not beginning
def daya_start = lowestAll( If((daywken and firstBarOfDay), bn, big) );
def daya_end = lowestAll( If((daywken and lastBarOfDay), bn, big) );
addlabel(lflb,"daya " + daya_start + " to " + daya_end, color.cyan);
def dayb_start = lowestAll( If((daywken and firstBarOfDay and bn > daya_start), bn, big));
def dayb_end = lowestAll( If((daywken and lastBarOfDay and bn > daya_end), bn, big));
addlabel(lflb,"dayb " + dayb_start + " to " + dayb_end, color.cyan);
def dayc_start = lowestAll( If((daywken and firstBarOfDay and bn > dayb_start), bn, big));
def dayc_end = lowestAll( If((daywken and lastBarOfDay and bn > dayb_end), bn, big));
addlabel(lflb,"dayc " + dayc_start + " to " + dayc_end, color.cyan);
def dayd_start = lowestAll( If((daywken and firstBarOfDay and bn > dayc_start), bn, big));
def dayd_end = lowestAll( If((daywken and lastBarOfDay and bn > dayc_end), bn, big));
addlabel(lflb,"dayd " + dayd_start + " to " + dayd_end, color.cyan);
def daye_start = lowestAll( If((daywken and firstBarOfDay and bn > dayd_start), bn, big));
def daye_end = lowestAll( If((daywken and lastBarOfDay and bn > dayd_end), bn, big));
addlabel(lflb,"daye " + daye_start + " to " + daye_end, color.cyan);
# need to find open bar of each day
# ------------
input show_day_letter_bn = no;
addchartbubble( show_day_letter_bn and (bn == daya_start), low, "A" + "\n" + bn, color.cyan,no);
addchartbubble( show_day_letter_bn and (bn == dayb_start), low, "B" + "\n" + bn, color.cyan,no);
addchartbubble( show_day_letter_bn and (bn == dayc_start), low, "C" + "\n" + bn, color.cyan,no);
addchartbubble( show_day_letter_bn and (bn == dayd_start), low, "D" + "\n" + bn, color.cyan,no);
addchartbubble( show_day_letter_bn and (bn == daye_start), low, "E" + "\n" + bn, color.cyan,no);
# ================================
# chg to highest , find last day, 2nd last day,.... from right to left
# and < lastbar2
#def small = 0;
#def daya2_start = highestAll( If((daywken and firstBarOfDay and bn < lastbar2), bn, small) );
#def daya2_end = highestAll( If((daywken and lastBarOfDay and bn < lastbar2), bn, small) );
#addlabel(lflb,"daya2 " + daya2_start + " to " + daya2_end, color.magenta);
#def dayb2_start = highestAll( If((daywken and firstBarOfDay and bn < daya2_start), bn, small) );
#def dayb2_end = highestAll( If((daywken and lastBarOfDay and bn < daya2_start), bn, small) );
#addlabel(lflb,"dayb2 " + dayb2_start + " to " + dayb2_end, color.magenta);
#--
#def dayc2_start = highestAll( If((daywken and firstBarOfDay and bn < dayb2_start), bn, small) );
#def dayc2_end = highestAll( If((daywken and lastBarOfDay and bn < dayb2_start), bn, small) );
#addlabel(lflb,"dayc2 " + dayc2_start + " to " + dayc2_end, color.magenta);
#def dayd2_start = highestAll( If((daywken and firstBarOfDay and bn < dayc2_start), bn, small) );
#def dayd2_end = highestAll( If((daywken and lastBarOfDay and bn < dayc2_start), bn, small) );
#addlabel(lflb,"dayd2 " + dayd2_start + " to " + dayd2_end, color.magenta);
#def daye2_start = highestAll( If((daywken and firstBarOfDay and bn < dayd2_start), bn, small) );
#def daye2_end = highestAll( If((daywken and lastBarOfDay and bn < dayd2_start), bn, small) );
#addlabel(lflb,"dayeb2 " + daye2_start + " to " + daye2_end, color.magenta);
# ------------
#input day_letter_bn = no;
#addchartbubble( day_letter_bn and (bn == daya2_start), low*0.997, "A" + "\n" + bn, color.green,no);
#addchartbubble( day_letter_bn and (bn == dayb2_start), low*0.997, "B" + "\n" + bn, color.green,no);
#addchartbubble( day_letter_bn and (bn == dayc2_start), low*0.997, "C" + "\n" + bn, color.green,no);
#addchartbubble( day_letter_bn and (bn == dayd2_start), low*0.997, "D" + "\n" + bn, color.green,no);
#addchartbubble( day_letter_bn and (bn == daye2_start), low*0.997, "E" + "\n" + bn, color.green,no);
# ====================================
#def daya_start = daya2_start;
#def dayb_start = dayb2_start;
#def dayc_start = dayc2_start;
#def dayd_start = dayd2_start;
#def daye_start = daye2_start;
# ========================
# calc offsets for the days bars
# daybars , bars in a day
# create a new var to count days
# not accurate . if a halt , then missing bars
#def daycnt2 = floor((bn - 1) / daybars)+1;
# count the days from bar1
def daycnt = if bn == 1 then 1 else if firstBarOfDay then daycnt[1] + 1 else daycnt[1];
# find even #s of the daycnt
def daycnteven = (daycnt/2) == floor(daycnt/2 );
#addchartbubble(1,high * 1.004, daycnt + "\n" + daycnteven, (if daycnteven then color.green else color.magenta), yes);
# need to check if source range = daybarcnt , no missing bars
# //////// add more, up to? 15 , 20 ?
# diff of the 1st bars #s
def offset3 =
if daycnt == 1 then ( daya_start - daybarfirst)
else if daycnt == 2 then ( dayb_start - daybarfirst)
else if daycnt == 3 then ( dayc_start - daybarfirst)
else if daycnt == 4 then ( dayd_start - daybarfirst)
else if daycnt == 5 then ( daye_start - daybarfirst)
else na;
input test_barnumber_offsets = no;
addchartbubble(test_barnumber_offsets, high * 1.004, offset3 + "\n" + (daya_start - daybarfirst), color.white, yes);
def offset2 = offset3;
# shift candles vertically
input vertoff = 0.0;
# redraw same days candles
def o = GetValue(open, -offset2) + vertoff;
def h = GetValue(high, -offset2) + vertoff;
def l = GetValue(low, -offset2) + vertoff;
def c = GetValue(close, -offset2) + vertoff;
# Plot the new Chart
#AddChart(high = h, low = l, open = o, close = c, type = ChartType.CANDLE, growcolor = color.black);
AddChart(high = h, low = l, open = o, close = c, type = ChartType.CANDLE, growcolor = color.cyan);
#AddChart(high = h, low = l, open = o, close = c, type = ChartType.CANDLE, growcolor = color.cyan);
#------------------------------------
#
Last edited by a moderator: