# scan_earn_4x_00_lower
#---------------------------------------
# test numbers and stocks found , day chart , 4 years
# stocks that have a signal somewhere in past 4 years
# TGT, MRK, MET, +1 , -3 , 0 , current earn beat, prev 3 were misses
# SO, SBUX, BAC, +3 , -1 , +1 , current run of 3 beats , prev 1 was miss , prev 1 was beat
# WMT, MET, BBUX, +2 , -2 , 0 , current run of 2 beats, prev 2 were misses
# BAC, BMY, COP, -1 , +2 , 0 , current 1 miss, prev 2 were beats
# stocks that have a signal on latest earnings
# COP, -1 , +2 , 0 , current 1 miss, prev 2 were beats
# SO, +3 , -1 , +1 , current run of 3 beats , prev 1 was miss , prev 1 was beat
declare lower;
def na = double.nan;
def bn = barnumber();
def getearn = if !isnan(GetActualEarnings()) then round(GetActualEarnings(),2) else 0;
def estearn = if !isnan(GetEstimatedEarnings()) then round(GetEstimatedEarnings(),2) else 0;
def earningsDay = HasEarnings() ;
def earningsBeat = (earningsDay and getearn > 0 and getearn >= estearn);
def earningsMiss = (earningsDay and getearn > 0 and GetEarn < EstEarn);
# sequence1 - count seq of same states , of beat or miss
# cnt1 has the current state, cnt2 has the prev, cnt3 has the cnt before cnt2
# state = 1 = beat , -1 = miss
def cnt1 = if bn == 1 then 0
else if cnt1[1] <= 0 and earningsBeat then 1
else if cnt1[1] >= 0 and earningsmiss then -1
else if cnt1[1] > 0 and earningsBeat then cnt1[1] + 1
else if cnt1[1] < 0 and earningsmiss then cnt1[1] - 1
else cnt1[1];
# sequence2 - when cnt1 has a diff state, then copy cnt1 to cnt2, and start new count in cnt1
def cnt2 = if bn == 1 then 0
else if sign(cnt1[1]) != sign(cnt1) then cnt1[1]
else cnt2[1];
# sequence3 - when cnt1 has a diff state, then copy cnt2 to cnt3 , and start new count in cnt2
def cnt3 = if bn == 1 then 0
else if sign(cnt1[1]) != sign(cnt1) then cnt2[1]
else cnt3[1];
# input sequence counts
input seq1_current = 1;
#hint seq1_current: sequence 1.\n positive number for a sequence of earnings that beat estimates\n negative number for a sequence of earnings that missed estimates
input seq2_prev1 = -3;
#hint seq2_prev1: sequence before seq1, opposite sign of seq1. if seq1 is positive, then this will be a negative number\nif 0, then ignore this parameter.
input seq3_prev2 = 0;
#hint seq3_prev2: sequence before seq2, opposite sign of seq2. if seq2 is negative, then this will be a positive number\nif 0, then ignore this parameter.
def signsok = if seq1_current == 0 then 0
else if (sign(seq1_current) != 0 and seq2_prev1 == 0 and sign(seq3_prev2) != 0) then 0
else if (sign(seq1_current) != sign(seq2_prev1) and seq3_prev2 == 0) then 1
else (sign(seq1_current) != sign(seq2_prev1) and sign(seq1_current) == sign(seq3_prev2));
def seq1;
def seq2;
def seq3;
if seq1_current > 0 then {
seq1 = (cnt1 == seq1_current);
seq2 = if (seq2_prev1 == 0) then 1 else (cnt2 <= seq2_prev1);
seq3 = if (seq3_prev2 == 0) then 1 else (cnt3 >= seq3_prev2);
} else if seq1_current < 0 then {
seq1 = (cnt1 == seq1_current);
seq2 = if (seq2_prev1 == 0) then 1 else (cnt2 >= seq2_prev1);
seq3 = if (seq3_prev2 == 0) then 1 else (cnt3 <= seq3_prev2);
} else {
seq1 = 0;
seq2 = 0;
seq3 = 0;
}
def seqs = signsok and seq1 and seq2 and seq3;
input signal_exist_for_quarter = no;
# no = signal exists on just 1 bar
def signal = if bn == 1 or isnan(close) then 0
else if signal_exist_for_quarter then seqs
else earningsDay and seqs;
# scan signal
plot z = signal;
#-------------------------------
# for a scan , delete everything after this line
addlabel(!signsok, " >>>>>>>>>>> numbers have wrongs polarity. need to be alternating <<<<<<<<<<<", color.cyan);
addlabel(1, " ", color.black);
addlabel(1, "Earnings", color.yellow);
addlabel(1, " ", color.black);
addlabel(1, "Seq3: " + absvalue(seq3_prev2) + (if seq3_prev2 > 0 then " beat" else if seq3_prev2 < 0 then " miss" else ""), (if seq3_prev2 > 0 then color.green else if seq3_prev2 < 0 then color.red else color.gray));
addlabel(1, "Seq2: " + absvalue(seq2_prev1) + (if seq2_prev1 > 0 then " beat" else if seq2_prev1 < 0 then " miss" else ""), (if seq2_prev1 > 0 then color.green else if seq2_prev1 < 0 then color.red else color.gray));
addlabel(1, "Seq1: " + absvalue(seq1_current) + (if seq1_current > 0 then " beat" else if seq1_current < 0 then " miss" else ""), (if seq1_current > 0 then color.green else if seq1_current < 0 then color.red else color.gray));
addlabel(1, " ", color.black);
plot z0 = 0;
input test1 = no;
addchartbubble(test1, 0,
earningsDay + "\n" +
# earningsAtAll + "\n" +
GetEarn + " ern\n" +
EstEarn + " est\n" +
earningsBeat + " beat\n" +
earningsMiss + " miss\n"
# beatTrack + "\n"
, (if earningsDay then color.yellow else color.gray), no);
input test2 = yes;
addchartbubble(test2 and !isnan(close) and earningsDay, 0,
# earningsDay + "\n" +
# earningsAtAll + "\n" +
GetEarn + " ern\n" +
EstEarn + " est\n" +
# earningsBeat + " beat\n" +
# earningsMiss + " miss\n" +
# beatTrack + "\n"
cnt1 + " seq1\n" +
cnt2 + " seq2\n" +
cnt3 + " seq3\n" +
signal + "\n"
# sign(seq1_current) + " s1\n" +
# sign(seq2_prev1) + " s2\n" +
# sign(seq3_prev2) + " s3\n"
, (if signal then color.yellow else color.gray), no);
#plot z3 = (seq3 * 1) + 1.5;
#plot z2 = (seq2 * 1) + 3;
#plot z1 = (seq1 * 1) + 4.5;
#