declare lower;
# stkgroup15b_upordown
# add labels to count stocks moving up and stocks moving down
# track/count bar to bar price changes of other stocks
def na = Double.NaN;
def bn = BarNumber();
# s# = stock symbol
# Shows# = show stoc price label
# p# = stock close price
# delp# price change from previous close
# c# = price change, bar to bar
# m# = sign of the price change
# n# = adjusted sign of change. if same as prev, then 0, ignore it
def chground = 3;
input s1 = "AAPL";
input show_s1 = no;
def p1 = close(s1);
def delp1 = close(s1) - close(s1)[1];
def c1 = Round(p1 - p1[1], chground);
def m1 = Sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;
input s2 = "AMZN";
input show_s2 = no;
def p2 = close(s2);
def delp2 = close(s2) - close(s2)[1];
def c2 = Round(p2 - p2[1], chground);
def m2 = Sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;
input s3 = "TSLA";
input show_s3 = no;
def p3 = close(s3);
def delp3 = close(s3) - close(s3)[1];
def c3 = Round(p3 - p3[1], chground);
def m3 = Sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;
input s4 = "MSFT";
input show_s4 = no;
def p4 = close(s4);
def delp4 = close(s4) - close(s4)[1];
def c4 = Round(p4 - p4[1], chground);
def m4 = Sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;
input s5 = "GOOGL";
input show_s5 = no;
def p5 = close(s5);
def delp5 = close(s5) - close(s5)[1];
def c5 = Round(p5 - p5[1], chground);
def m5 = Sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;
input s6 = "GOOG";
input show_s6 = no;
def p6 = close(s6);
def delp6 = close(s6) - close(s6)[1];
def c6 = Round(p6 - p6[1], chground);
def m6 = Sign(c6);
def n6 = if m6 <> m6[1] then m6 else 0;
input s7 = "NVDA";
input show_s7 = no;
def p7 = close(s7);
def delp7 = close(s7) - close(s7)[1];
def c7 = Round(p7 - p7[1], chground);
def m7 = Sign(c7);
def n7 = if m7 <> m7[1] then m7 else 0;
input s8 = "FB";
input show_s8 = no;
def p8 = close(s8);
def delp8 = close(s8) - close(s8)[1];
def c8 = Round(p8 - p8[1], chground);
def m8 = Sign(c8);
def n8 = if m8 <> m8[1] then m8 else 0;
input s9 = "AVGO";
input show_s9 = no;
def p9 = close(s9);
def delp9 = close(s9) - close(s9)[1];
def c9 = Round(p9 - p9[1], chground);
def m9 = Sign(c9);
def n9 = if m9 <> m9[1] then m9 else 0;
input s10 = "PEP";
input show_s10 = no;
def p10 = close(s10);
def delp10 = close(s10) - close(s10)[1];
def c10 = Round(p10 - p10[1], chground);
def m10 = Sign(c10);
def n10 = if m10 <> m10[1] then m10 else 0;
input s11 = "BRK/B";
input show_s11 = no;
def p11 = close(s11);
def delp11 = close(s11) - close(s11)[1];
def c11 = Round(p11 - p11[1], chground);
def m11 = Sign(c11);
def n11 = if m11 <> m11[1] then m11 else 0;
input s12 = "JPM";
input show_s12 = no;
def p12 = close(s12);
def delp12 = close(s12) - close(s12)[1];
def c12 = Round(p12 - p12[1], chground);
def m12 = Sign(c12);
def n12 = if m12 <> m12[1] then m12 else 0;
#input s13 = "CVS";
#def p13 = close(s13);
#def p13 = close(s13);
#def c13 = round(p13 - p13[1], chground);
#def m13 = sign(c13);
#def n13 = if m13 <> m13[1] then m13 else 0;
#input s14 = "AAPL";
#def p14 = close(s14);
#def p14 = close(s14);
#def c14 = round(p14 - p14[1], chground);
#def m14 = sign(c14);
#def n14 = if m14 <> m14[1] then m14 else 0;
#input s15 = "X";
#def p15 = close(s15);
#def p15 = close(s15);
#def c15 = round(p15 - p15[1], chground);
#def m15 = sign(c15);
#def n15 = if m15 <> m15[1] then m15 else 0;
# quantity of stock symbols. change this if more code is added above
def stkqty = 12;#15
# -------------------------------------
# this uses branchless boolean math. value in the ( ) is either 0 or 1
def stks_pos = (m1 > 0) + (m2 > 0) + (m3 > 0) + (m4 > 0) + (m5 > 0) + (m6 > 0) + (m7 > 0) + (m8 > 0) + (m9 > 0) + (m10 > 0) + (m11 > 0) + (m12 > 0);# + (m13>0) + (m14>0) + (m15>0);
# count stocks that have changed down
def stks_neg = (m1 < 0) + (m2 < 0) + (m3 < 0) + (m4 < 0) + (m5 < 0) + (m6 < 0) + (m7 < 0) + (m8 < 0) + (m9 < 0) + (m10 < 0) + (m11 < 0) + (m12 < 0);# + (m13<0) + (m14<0) + (m15<0);
AddLabel(1, "----", Color.BLACK);
AddLabel(1, stks_pos + " / " + stkqty + " up", Color.GREEN);
AddLabel(1, AbsValue(stks_neg) + " / " + stkqty + " down", Color.RED);
AddLabel(1, "----", Color.BLACK);
# -------------------------------------
# count stocks that have changed up
def stks_chg_pos = (n1 > 0) + (n2 > 0) + (n3 > 0) + (n4 > 0) + (n5 > 0) + (n6 > 0) + (n7 > 0) + (n8 > 0) + (n9 > 0) + (n10 > 0) + (n11 > 0) + (n12 > 0);# + (n13>0) + (n14>0) + (n15>0);
# count stocks that have changed down
def stks_chg_neg = (n1 < 0) + (n2 < 0) + (n3 < 0) + (n4 < 0) + (n5 < 0) + (n6 < 0) + (n7 < 0) + (n8 < 0) + (n9 < 0) + (n10 < 0) + (n11 < 0) + (n12 < 0);# + (n13<0) + (n14<0) + (n15<0);
AddLabel(1, "----", Color.BLACK);
AddLabel(1, stks_chg_pos + " / " + stkqty + " changed to up", Color.GREEN);
AddLabel(1, AbsValue(stks_chg_neg) + " / " + stkqty + " changed to down", Color.RED);
AddLabel(1, "----", Color.BLACK);
# -------------------------------------
# show: symbol , $ change
AddLabel(show_s1, s1 + " |" + c1, (if n1 > 0 then Color.GREEN else if n1 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s2, s2 + " |" + c2, (if n2 > 0 then Color.GREEN else if n2 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s3, s3 + " |" + c3, (if n3 > 0 then Color.GREEN else if n3 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s4, s4 + " |" + c4, (if n4 > 0 then Color.GREEN else if n4 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s5, s5 + " |" + c5, (if n5 > 0 then Color.GREEN else if n5 < 0 then Color.RED else Color.GRAY));
AddLabel(1, "--", Color.BLACK);
AddLabel(show_s6, s6 + " |" + c6, (if n6 > 0 then Color.GREEN else if n6 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s7, s7 + " |" + c7, (if n7 > 0 then Color.GREEN else if n7 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s8, s8 + " |" + c8, (if n8 > 0 then Color.GREEN else if n8 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s9, s9 + " |" + c9, (if n9 > 0 then Color.GREEN else if n9 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s10, s10 + " |" + c10, (if n10 > 0 then Color.GREEN else if n10 < 0 then Color.RED else Color.GRAY));
AddLabel(1, "--", Color.BLACK);
AddLabel(show_s11, s11 + " |" + c11, (if n11 > 0 then Color.GREEN else if n11 < 0 then Color.RED else Color.GRAY));
AddLabel(show_s12, s12 + " |" + c12, (if n12 > 0 then Color.GREEN else if n12 < 0 then Color.RED else Color.GRAY));
#addlabel(1, s13 + " |" + c13, (if n13>0 then color.green else if n13<0 then color.red else color.gray));
#addlabel(1, s14 + " |" + c14, (if n14>0 then color.green else if n14<0 then color.red else color.gray));
#addlabel(1, s15 + " |" + c15, (if n15>0 then color.green else if n15<0 then color.red else color.gray));
#-------- Add Histogram for moves
plot zeroLine = 0;
plot percentChange = ((delp1 + delp2 + delp3 + delp4 + delp5 + delp6 + delp7 + delp8 + delp9 + delp10 + delp11 + delp12) / (p1 + p2 + p3 + p4 + p5 + p6 + p7 + p8 + p9 + p10 + p11 + p12) * 100);
#plot Hist = percentChange;
percentChange.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
percentChange.SetLineWeight(5);
percentChange.AssignValueColor(if percentChange >= 0 then Color.GREEN else Color.RED);
# ----------------------------------------
input test1 = no;
AddLabel(test1, "----", Color.BLACK);
AddLabel(test1, "signs of stock changes " + n1 + " " + n2 + " " + n3 + " " + n4 + " " + n5, Color.CYAN);
input test2 = no;
AddLabel(test2, "----", Color.BLACK);
AddLabel(test2, s1 + "|" + p1[1] + "/" + p1 + "|" + c1 + "|" + n1 , (if n1 > 0 then Color.GREEN else if n1 < 0 then Color.RED else Color.GRAY));
AddLabel(test2, s2 + "|" + p2[1] + "/" + p2 + "|" + c2 + "|" + n2 , (if n2 > 0 then Color.GREEN else if n2 < 0 then Color.RED else Color.GRAY));
AddLabel(test2, s3 + "|" + p3[1] + "/" + p3 + "|" + c3 + "|" + n3 , (if n3 > 0 then Color.GREEN else if n3 < 0 then Color.RED else Color.GRAY));
AddLabel(test2, s4 + "|" + p4[1] + "/" + p4 + "|" + c4 + "|" + n4 , (if n4 > 0 then Color.GREEN else if n4 < 0 then Color.RED else Color.GRAY));
AddLabel(test2, s5 + "|" + p5[1] + "/" + p5 + "|" + c5 + "|" + n5 , (if n5 > 0 then Color.GREEN else if n5 < 0 then Color.RED else Color.GRAY));
#