How do I code this?
Conditions
1. Daily aggregation
2. Ticker is up at-least 4 days of the past 7 regular trading days.
I am looking to make a upper chart indicator and also a scan.
this will check if there were x up bars, in the previous y bars.
default is 4 of 7.
...draw an up arrow on all of these signals or just the first in a series of them.
...draw a green dot above just the up bars that are before the first arrow.
i started by using 2nd aggregation, but was getting odd results , so removed it.
set the chart to the desired time.
you didn't define what an up bar is, so i added an option to pick 1 of 2 methods,
bar close > previous bar close
close > open
upper chart
Ruby:
# up4in7bars_upper_00c
# Indicator Request: Stock is up at-least 4 days of the past 7 RTD
def bn = barnumber();
def na = double.nan;
input min_up_days = 4;
input time_span_days = 7;
input up_type = { default bar_to_bar_close , open_to_close };
def up;
switch (up_type){
case bar_to_bar_close:
up = if bn == 1 then 0 else if close > close[1] then 1 else 0;
case open_to_close:
up = if bn == 1 then 0 else if close > open then 1 else 0;
}
# up is true, if today close is > yesterday close
#def up = if bn == 1 then 0
# else if close > close[1] then 1
# else 0;
def isminup = (sum(up, time_span_days) >= min_up_days);
input show_only_first_arrow = yes;
def firstbar = (!isminup[1] and isminup);
def firstbn = if firstbar then bn else 0;
def arrow_bars = if bn == 1 then 0
else if !show_only_first_arrow then isminup
else if show_only_first_arrow and firstbar then 1
else 0;
plot z1 = arrow_bars;
z1.setdefaultcolor(color.cyan);
z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
z1.setlineweight(2);
# dots on up bars before the 1st bar of a series
def isfirst = (sum(firstbar[-(time_span_days-1)], time_span_days) >= 1);
def early_ups = up and isfirst;
input dots_on_up_bars_before_first = yes;
plot z2 = if dots_on_up_bars_before_first and early_ups then high*1.01 else na;
z2.setdefaultcolor(color.green);
z2.SetPaintingStrategy(PaintingStrategy.points);
z2.setlineweight(4);
#--------------------
# info labels
input show_description_labels = yes;
addlabel(show_description_labels, "find " + min_up_days + " up bars, in the past " + time_span_days, color.cyan);
addlabel(show_description_labels, up_type, color.yellow);
addlabel(show_description_labels and show_only_first_arrow, "show only first arrow in a series", color.cyan);
addlabel(show_description_labels and dots_on_up_bars_before_first, "show dots above the up bars, that are before the first up bar in a series", color.green);
#
BK day chart
set to show just the first arrow in a series
--------------------------
i didn't test this as a scan, just as a lower study.
with column or scan codes, the user has to edit the code to change an input.
to make it easier, i make a copy of an input line for each possible value. i set each of the copies to equal one of the possible values, then disable the code line copies with #
then to change an input , add and remove a #.
see below, where i added a copy of the input up_type = ... line, and set it to have a different default.
scan
Ruby:
# up4in7bars_lower_00c
# https://usethinkscript.com/threads/indicator-request-stock-is-up-at-least-4-days-of-the-past-7-rtd.12163/
# Indicator Request: Stock is up at-least 4 days of the past 7 RTD
#declare lower;
def bn = barnumber();
def na = double.nan;
input min_up_days = 4;
input time_span_days = 7;
# enable one of these lines
input up_type = { default bar_to_bar_close , open_to_close };
#input up_type = { bar_to_bar_close , default open_to_close };
def up;
switch (up_type){
case bar_to_bar_close:
up = if bn == 1 then 0 else if close > close[1] then 1 else 0;
case open_to_close:
up = if bn == 1 then 0 else if close > open then 1 else 0;
}
def isminup = (sum(up, time_span_days) >= min_up_days);
input show_only_first_arrow = yes;
def firstbar = (!isminup[1] and isminup);
def firstbn = if firstbar then bn else 0;
def arrow_bars = if bn == 1 then 0
else if !show_only_first_arrow then isminup
else if show_only_first_arrow and firstbar then 1
else 0;
plot z1 = arrow_bars;
#z1.setdefaultcolor(color.cyan);
#z1.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#z1.setlineweight(2);
#
used as a lower study, to verify output
spikes line up with arrows.
arrows set to show just the first one.
if all arrows are shown