Counter Increment

Hi all,

I am trying to create an indicator to look at price action for a basket of stocks and see if the candle close was up or down over the previous one. I am trying to get to the total number of stocks that closed up out of the entire basket.

So, for "Stock1" I have

Price1 = close("Stock1");
Price2 = close("Stock1")[1];
DeltaPrice1 = Price2 - Price1;
..
.. and so on for the rest of the basket....

Then I have a counter to catch how many of the bar closes were bullish (closed above previous close) and to update only if the trend changes.

DeltaCntr = 0;

And this is where I am stuck trying to figure out how to increment/decrement the counter. What I am looking to do is the following.

if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then add 1 to DeltaCntr
if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then do nothing - Trend maintained

if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then subtract 1 from DeltaCntr
if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then do nothing - Trend maintained

if DeltaPrice = 0 do nothing - Trend maintained.

I can code the above for each stock separately but that would make for very ugly code. I wondering if someone can recommend a more simpler more efficient way of coding this.

Many thanks advance for your help ....

/Baba
 
Solution
if they have been moving down, then they aren't counted.
only new changes are counted

this ver,
i added 2 labels,
to count stocks moving up and stocks moving down

Ruby:
# 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
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 =...
You can give this a try. Can't test it but I think it's what you want. Might need some tweaking if not.

def DeltaCntr = compoundValue(1, if DeltaPrice > 0 and DeltaPrice[1] < 0 then DeltaCntr[1]+1 else if DeltaPrice > 0 and DeltaPrice[1] > 0 then DeltaCntr[1] else if DeltaPrice < 0 and DeltaPrice[1] > 0 then DeltaPrice[1] -1 else if DeltaPrice < 0 and DeltaPrice[1] < 0 then DeltaPrice[1] else DeltaPrice[1],0);
 
Hi all,

I am trying to create an indicator to look at price action for a basket of stocks and see if the candle close was up or down over the previous one. I am trying to get to the total number of stocks that closed up out of the entire basket.

So, for "Stock1" I have

Price1 = close("Stock1");
Price2 = close("Stock1")[1];
DeltaPrice1 = Price2 - Price1;
..
.. and so on for the rest of the basket....

Then I have a counter to catch how many of the bar closes were bullish (closed above previous close) and to update only if the trend changes.

DeltaCntr = 0;

And this is where I am stuck trying to figure out how to increment/decrement the counter. What I am looking to do is the following.

if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then add 1 to DeltaCntr
if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then do nothing - Trend maintained

if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then subtract 1 from DeltaCntr
if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then do nothing - Trend maintained

if DeltaPrice = 0 do nothing - Trend maintained.

I can code the above for each stock separately but that would make for very ugly code. I wondering if someone can recommend a more simpler more efficient way of coding this.

Many thanks advance for your help ....

/Baba
how many stocks?
 
You can give this a try. Can't test it but I think it's what you want. Might need some tweaking if not.

def DeltaCntr = compoundValue(1, if DeltaPrice > 0 and DeltaPrice[1] < 0 then DeltaCntr[1]+1 else if DeltaPrice > 0 and DeltaPrice[1] > 0 then DeltaCntr[1] else if DeltaPrice < 0 and DeltaPrice[1] > 0 then DeltaPrice[1] -1 else if DeltaPrice < 0 and DeltaPrice[1] < 0 then DeltaPrice[1] else DeltaPrice[1],0);
This is returning a decimal number for some reason. Not able to figure out why since all we are doing is adding and subtracting 1s....
 
This is returning a decimal number for some reason. Not able to figure out why since all we are doing is adding and subtracting 1s....

I reformatted the code and found what might have been a typo. So I changed the bold from DeltaPrice to DeltaCntr but now the label shows "N/A".

Would appreciate if you can look into it


def DeltaCntr = compoundValue(1,
if DeltaPrice > 0 and DeltaPrice[1] < 0 then DeltaCntr[1]+1
else if DeltaPrice > 0 and DeltaPrice[1] > 0 then DeltaCntr[1]
else if DeltaPrice < 0 and DeltaPrice[1] > 0 then DeltaPrice[1] -1
else if DeltaPrice < 0 and DeltaPrice[1] < 0 then DeltaPrice[1]
else DeltaPrice[1],0);


def DeltaCntr = compoundValue(1,
if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then DeltaCntr[1]+1
else if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then DeltaCntr[1]
else if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then DeltaCntr[1] -1
else if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then DeltaCntr[1]
else DeltaCntr[1],1);
 
Currently trying to get the results right for 1 stock then will add the rest. Here is the full code so far.

declare lower;

def Price1 = close(symbol = "AAPL")[1];
def Price2 = close(symbol = "AAPL");
def DeltaPrice1 = Price2 - Price1;


def DeltaCntr = compoundValue(1,
if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then DeltaCntr[1]+1
else if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then DeltaCntr[1]
else if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then DeltaCntr[1] -1
else if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then DeltaCntr[1]
else DeltaCntr[1],1);


AddLabel(yes, "label: "+DeltaCntr, Color.light_gray);
 
Hi all,

I am trying to create an indicator to look at price action for a basket of stocks and see if the candle close was up or down over the previous one. I am trying to get to the total number of stocks that closed up out of the entire basket.

So, for "Stock1" I have

Price1 = close("Stock1");
Price2 = close("Stock1")[1];
DeltaPrice1 = Price2 - Price1;
..
.. and so on for the rest of the basket....

Then I have a counter to catch how many of the bar closes were bullish (closed above previous close) and to update only if the trend changes.

DeltaCntr = 0;

And this is where I am stuck trying to figure out how to increment/decrement the counter. What I am looking to do is the following.

if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then add 1 to DeltaCntr
if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then do nothing - Trend maintained

if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then subtract 1 from DeltaCntr
if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then do nothing - Trend maintained

if DeltaPrice = 0 do nothing - Trend maintained.

I can code the above for each stock separately but that would make for very ugly code. I wondering if someone can recommend a more simpler more efficient way of coding this.

Many thanks advance for your help ....

/Baba

here is a version that looks at 5 stocks
i will make another version next for 15


here is a study that looks at the close price change of 5 stocks, from bar to bar.
if a price changes in a different direction than the previous change, then it is counted, in the increasing or decreasing count.
if a change is in the same direction as the previous change, it is ignored.

draw 2 labels,
. green one shows the count of increasing stocks,
. red one shows the count of decreasing stocks.

draw 5 labels,
. a label for each stock: C , F , SPY , T , Z (5 stocks i picked at random)
. each one shows the symbol and price change
. each one is colored based on the sign of the price change number, green up, red down, gray flat.



Ruby:
# stkgroup_upordown_0

# track/count bar to bar price changes of other stocks

def na = double.nan;
def bn = barnumber();

# s# = stock symbol
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;

# quantity of stock symbols.  change this if more code is added above
def stkqty = 5;


# this uses branchless boolean math. value in the ( ) is either 0 or 1
#  count stocks that have changed up
def stks_chg_pos = (n1>0) + (n2>0) + (n3>0) + (n4>0) + (n5>0);
#  count stocks that have changed down
def stks_chg_neg = (n1<0) + (n2<0) + (n3<0) + (n4<0) + (n5<0);


addlabel(1, "----", color.black);
addlabel(1, stks_chg_pos + " / " + stkqty + " stocks increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks decreased", color.red);
addlabel(1, "----", color.black);

# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));

# ----------------------------------------

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));
#


2 count labels
5 labels showing up or down for each stock
D3X7yEL.jpg



https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sign
 
here is a version that looks at 5 stocks
i will make another version next for 15


here is a study that looks at the close price change of 5 stocks, from bar to bar.
if a price changes in a different direction than the previous change, then it is counted, in the increasing or decreasing count.
if a change is in the same direction as the previous change, it is ignored.

draw 2 labels,
. green one shows the count of increasing stocks,
. red one shows the count of decreasing stocks.

draw 5 labels,
. a label for each stock: C , F , SPY , T , Z (5 stocks i picked at random)
. each one shows the symbol and price change
. each one is colored based on the sign of the price change number, green up, red down, gray flat.



Ruby:
# stkgroup_upordown_0

# track/count bar to bar price changes of other stocks

def na = double.nan;
def bn = barnumber();

# s# = stock symbol
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;

# quantity of stock symbols.  change this if more code is added above
def stkqty = 5;


# this uses branchless boolean math. value in the ( ) is either 0 or 1
#  count stocks that have changed up
def stks_chg_pos = (n1>0) + (n2>0) + (n3>0) + (n4>0) + (n5>0);
#  count stocks that have changed down
def stks_chg_neg = (n1<0) + (n2<0) + (n3<0) + (n4<0) + (n5<0);


addlabel(1, "----", color.black);
addlabel(1, stks_chg_pos + " / " + stkqty + " stocks increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks decreased", color.red);
addlabel(1, "----", color.black);

# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));

# ----------------------------------------

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));
#


2 count labels
5 labels showing up or down for each stock
D3X7yEL.jpg



https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sign
This is awesome !! You da man !!

Much appreciated Looking forward to the 15 one
 
This is awesome !! You da man !!

Much appreciated Looking forward to the 15 one

another version, with code for 15 stocks.
i picked 15 random stocks.

i thought the amount of change was important, so i left it in the labels.
you could make the labels smaller by removing the c# variables , and just have the s#
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));

Ruby:
# stkgroup15_upordown

# https://usethinkscript.com/threads/counter-increment.10211/
# Counter Increment
# Babaji Ka Thullu  2/17 at 5:28 PM

#I am trying to create an indicator to look at price action for a basket of stocks and see if the candle close was up or down over the previous one. I am trying to get to the total number of stocks that closed up out of the entire basket.

# ===========================================


# track/count bar to bar price changes of other stocks

def na = double.nan;
def bn = barnumber();

# s# = stock symbol
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;



input s6 = "Z";
def p6 = close(s6);
def c6 = round(p6 - p6[1], chground);
def m6 = sign(c6);
def n6 = if m6 <> m6[1] then m6 else 0;

input s7 = "AMD";
def p7 = close(s7);
def c7 = round(p7 - p7[1], chground);
def m7 = sign(c7);
def n7 = if m7 <> m7[1] then m7 else 0;

input s8 = "KO";
def p8 = close(s8);
def c8 = round(p8 - p8[1], chground);
def m8 = sign(c8);
def n8 = if m8 <> m8[1] then m8 else 0;

input s9 = "MRK";
def p9 = close(s9);
def c9 = round(p9 - p9[1], chground);
def m9 = sign(c9);
def n9 = if m9 <> m9[1] then m9 else 0;

input s10 = "GE";
def p10 = close(s10);
def c10 = round(p10 - p10[1], chground);
def m10 = sign(c10);
def n10 = if m10 <> m10[1] then m10 else 0;



input s11 = "NIO";
def p11 = close(s11);
def c11 = round(p11 - p11[1], chground);
def m11 = sign(c11);
def n11 = if m11 <> m11[1] then m11 else 0;

input s12 = "INTC";
def p12 = close(s12);
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 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 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 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 = 15;


# this uses branchless boolean math. value in the ( ) is either 0 or 1
#  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 + " stocks increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks decreased", color.red);
addlabel(1, "----", color.black);

# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s6 + " |" + c6, (if n6>0 then color.green else if n6<0 then color.red else color.gray));
addlabel(1, s7 + " |" + c7, (if n7>0 then color.green else if n7<0 then color.red else color.gray));
addlabel(1, s8 + " |" + c8, (if n8>0 then color.green else if n8<0 then color.red else color.gray));
addlabel(1, s9 + " |" + c9, (if n9>0 then color.green else if n9<0 then color.red else color.gray));
addlabel(1, s10 + " |" + c10, (if n10>0 then color.green else if n10<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s11 + " |" + c11, (if n11>0 then color.green else if n11<0 then color.red else color.gray));
addlabel(1, 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));


# ----------------------------------------

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));
#
 
here is a version that looks at 5 stocks
i will make another version next for 15


here is a study that looks at the close price change of 5 stocks, from bar to bar.
if a price changes in a different direction than the previous change, then it is counted, in the increasing or decreasing count.
if a change is in the same direction as the previous change, it is ignored.

draw 2 labels,
. green one shows the count of increasing stocks,
. red one shows the count of decreasing stocks.

draw 5 labels,
. a label for each stock: C , F , SPY , T , Z (5 stocks i picked at random)
. each one shows the symbol and price change
. each one is colored based on the sign of the price change number, green up, red down, gray flat.



Ruby:
# stkgroup_upordown_0

# track/count bar to bar price changes of other stocks

def na = double.nan;
def bn = barnumber();

# s# = stock symbol
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;

# quantity of stock symbols.  change this if more code is added above
def stkqty = 5;


# this uses branchless boolean math. value in the ( ) is either 0 or 1
#  count stocks that have changed up
def stks_chg_pos = (n1>0) + (n2>0) + (n3>0) + (n4>0) + (n5>0);
#  count stocks that have changed down
def stks_chg_neg = (n1<0) + (n2<0) + (n3<0) + (n4<0) + (n5<0);


addlabel(1, "----", color.black);
addlabel(1, stks_chg_pos + " / " + stkqty + " stocks increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks decreased", color.red);
addlabel(1, "----", color.black);

# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));

# ----------------------------------------

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));
#


2 count labels
5 labels showing up or down for each stock
D3X7yEL.jpg



https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Math---Trig/Sign
Tested this out and seems like the "stocks decreased" label is not updating.... all 5 stocks are down but stocks increased behaves well and shows zero however stocks decreased also shows zero
 
another version, with code for 15 stocks.
i picked 15 random stocks.

i thought the amount of change was important, so i left it in the labels.
you could make the labels smaller by removing the c# variables , and just have the s#
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));

Ruby:
# stkgroup15_upordown

# https://usethinkscript.com/threads/counter-increment.10211/
# Counter Increment
# Babaji Ka Thullu  2/17 at 5:28 PM

#I am trying to create an indicator to look at price action for a basket of stocks and see if the candle close was up or down over the previous one. I am trying to get to the total number of stocks that closed up out of the entire basket.

# ===========================================


# track/count bar to bar price changes of other stocks

def na = double.nan;
def bn = barnumber();

# s# = stock symbol
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;



input s6 = "Z";
def p6 = close(s6);
def c6 = round(p6 - p6[1], chground);
def m6 = sign(c6);
def n6 = if m6 <> m6[1] then m6 else 0;

input s7 = "AMD";
def p7 = close(s7);
def c7 = round(p7 - p7[1], chground);
def m7 = sign(c7);
def n7 = if m7 <> m7[1] then m7 else 0;

input s8 = "KO";
def p8 = close(s8);
def c8 = round(p8 - p8[1], chground);
def m8 = sign(c8);
def n8 = if m8 <> m8[1] then m8 else 0;

input s9 = "MRK";
def p9 = close(s9);
def c9 = round(p9 - p9[1], chground);
def m9 = sign(c9);
def n9 = if m9 <> m9[1] then m9 else 0;

input s10 = "GE";
def p10 = close(s10);
def c10 = round(p10 - p10[1], chground);
def m10 = sign(c10);
def n10 = if m10 <> m10[1] then m10 else 0;



input s11 = "NIO";
def p11 = close(s11);
def c11 = round(p11 - p11[1], chground);
def m11 = sign(c11);
def n11 = if m11 <> m11[1] then m11 else 0;

input s12 = "INTC";
def p12 = close(s12);
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 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 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 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 = 15;


# this uses branchless boolean math. value in the ( ) is either 0 or 1
#  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 + " stocks increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks decreased", color.red);
addlabel(1, "----", color.black);

# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s6 + " |" + c6, (if n6>0 then color.green else if n6<0 then color.red else color.gray));
addlabel(1, s7 + " |" + c7, (if n7>0 then color.green else if n7<0 then color.red else color.gray));
addlabel(1, s8 + " |" + c8, (if n8>0 then color.green else if n8<0 then color.red else color.gray));
addlabel(1, s9 + " |" + c9, (if n9>0 then color.green else if n9<0 then color.red else color.gray));
addlabel(1, s10 + " |" + c10, (if n10>0 then color.green else if n10<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s11 + " |" + c11, (if n11>0 then color.green else if n11<0 then color.red else color.gray));
addlabel(1, 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));


# ----------------------------------------

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));
#
Thanks for the quick update.... tested this version out as well and the "stocks decreased" counter doesnt seem to be updating.
 
Tested this out and seems like the "stocks decreased" label is not updating.... all 5 stocks are down but stocks increased behaves well and shows zero however stocks decreased also shows zero
if they have been moving down, then they aren't counted.
only new changes are counted


your rules

And this is where I am stuck trying to figure out how to increment/decrement the counter. What I am looking to do is the following.

if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then add 1 to DeltaCntr
if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then do nothing - Trend maintained

if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then subtract 1 from DeltaCntr
if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then do nothing - Trend maintained
 
if they have been moving down, then they aren't counted.
only new changes are counted


your rules

And this is where I am stuck trying to figure out how to increment/decrement the counter. What I am looking to do is the following.

if DeltaPrice1 > 0 and DeltaPrice1[1] < 0 then add 1 to DeltaCntr
if DeltaPrice1 > 0 and DeltaPrice1[1] > 0 then do nothing - Trend maintained

if DeltaPrice1 < 0 and DeltaPrice1[1] > 0 then subtract 1 from DeltaCntr
if DeltaPrice1 < 0 and DeltaPrice1[1] < 0 then do nothing - Trend maintained
Aaah yes.... I get it. Thanks man... really appreciate your help
 
if they have been moving down, then they aren't counted.
only new changes are counted

this ver,
i added 2 labels,
to count stocks moving up and stocks moving down

Ruby:
# 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
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;



input s6 = "Z";
def p6 = close(s6);
def c6 = round(p6 - p6[1], chground);
def m6 = sign(c6);
def n6 = if m6 <> m6[1] then m6 else 0;

input s7 = "AMD";
def p7 = close(s7);
def c7 = round(p7 - p7[1], chground);
def m7 = sign(c7);
def n7 = if m7 <> m7[1] then m7 else 0;

input s8 = "KO";
def p8 = close(s8);
def c8 = round(p8 - p8[1], chground);
def m8 = sign(c8);
def n8 = if m8 <> m8[1] then m8 else 0;

input s9 = "MRK";
def p9 = close(s9);
def c9 = round(p9 - p9[1], chground);
def m9 = sign(c9);
def n9 = if m9 <> m9[1] then m9 else 0;

input s10 = "GE";
def p10 = close(s10);
def c10 = round(p10 - p10[1], chground);
def m10 = sign(c10);
def n10 = if m10 <> m10[1] then m10 else 0;



input s11 = "NIO";
def p11 = close(s11);
def c11 = round(p11 - p11[1], chground);
def m11 = sign(c11);
def n11 = if m11 <> m11[1] then m11 else 0;

input s12 = "INTC";
def p12 = close(s12);
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 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 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 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 = 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 + " stocks increased", color.green);
addlabel(1, absvalue(stks_neg) + " / " + stkqty + " stocks decreased", 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 + " stocks changed to increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks changed to decreased", color.red);
addlabel(1, "----", color.black);

# -------------------------------------
# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s6 + " |" + c6, (if n6>0 then color.green else if n6<0 then color.red else color.gray));
addlabel(1, s7 + " |" + c7, (if n7>0 then color.green else if n7<0 then color.red else color.gray));
addlabel(1, s8 + " |" + c8, (if n8>0 then color.green else if n8<0 then color.red else color.gray));
addlabel(1, s9 + " |" + c9, (if n9>0 then color.green else if n9<0 then color.red else color.gray));
addlabel(1, s10 + " |" + c10, (if n10>0 then color.green else if n10<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s11 + " |" + c11, (if n11>0 then color.green else if n11<0 then color.red else color.gray));
addlabel(1, 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));


# ----------------------------------------

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));
#
[code]
 
Solution
this ver,


side note
when you have this many labels on a chart you may want to space them away from other study labels , or space them out a little more in my program. i used a couple black labels to space them out some.

you can use this as is, or copy a line or 2 and add it to my program , if you want all 15 labels to wrap over to the next row.

when i am creating a study, i use this to add black space between my other studied and the last one, that i am working on, to make it easier to see the labels.

Code:
# label_shifter

input show_labels = no;
addlabel( show_labels,"label shifter", color.gray);

input qty_of_blank_labels = 3;
def qty = if qty_of_blank_labels > 10 then qty_of_blank_labels else if qty_of_blank_labels < 1 then 1 else qty_of_blank_labels;

# 20 chars
addlabel(show_labels and (qty >= 1), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 2), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 3), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 4), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 5), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 6), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 7), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 8), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 9), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 10), "-=========-=========" , color.black);
 
this ver,
i added 2 labels,
to count stocks moving up and stocks moving down

Ruby:
# 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
# p# = stock close price
# 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 = "C";
def p1 = close(s1);
def c1 = round(p1 - p1[1], chground);
def m1 = sign(c1);
def n1 = if m1 <> m1[1] then m1 else 0;

input s2 = "F";
def p2 = close(s2);
def c2 = round(p2 - p2[1], chground);
def m2 = sign(c2);
def n2 = if m2 <> m2[1] then m2 else 0;

input s3 = "SPY";
def p3 = close(s3);
def c3 = round(p3 - p3[1], chground);
def m3 = sign(c3);
def n3 = if m3 <> m3[1] then m3 else 0;

input s4 = "T";
def p4 = close(s4);
def c4 = round(p4 - p4[1], chground);
def m4 = sign(c4);
def n4 = if m4 <> m4[1] then m4 else 0;

input s5 = "Z";
def p5 = close(s5);
def c5 = round(p5 - p5[1], chground);
def m5 = sign(c5);
def n5 = if m5 <> m5[1] then m5 else 0;



input s6 = "Z";
def p6 = close(s6);
def c6 = round(p6 - p6[1], chground);
def m6 = sign(c6);
def n6 = if m6 <> m6[1] then m6 else 0;

input s7 = "AMD";
def p7 = close(s7);
def c7 = round(p7 - p7[1], chground);
def m7 = sign(c7);
def n7 = if m7 <> m7[1] then m7 else 0;

input s8 = "KO";
def p8 = close(s8);
def c8 = round(p8 - p8[1], chground);
def m8 = sign(c8);
def n8 = if m8 <> m8[1] then m8 else 0;

input s9 = "MRK";
def p9 = close(s9);
def c9 = round(p9 - p9[1], chground);
def m9 = sign(c9);
def n9 = if m9 <> m9[1] then m9 else 0;

input s10 = "GE";
def p10 = close(s10);
def c10 = round(p10 - p10[1], chground);
def m10 = sign(c10);
def n10 = if m10 <> m10[1] then m10 else 0;



input s11 = "NIO";
def p11 = close(s11);
def c11 = round(p11 - p11[1], chground);
def m11 = sign(c11);
def n11 = if m11 <> m11[1] then m11 else 0;

input s12 = "INTC";
def p12 = close(s12);
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 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 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 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 = 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 + " stocks increased", color.green);
addlabel(1, absvalue(stks_neg) + " / " + stkqty + " stocks decreased", 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 + " stocks changed to increased", color.green);
addlabel(1, absvalue(stks_chg_neg) + " / " + stkqty + " stocks changed to decreased", color.red);
addlabel(1, "----", color.black);

# -------------------------------------
# show: symbol , $ change
addlabel(1, s1 + " |" + c1, (if n1>0 then color.green else if n1<0 then color.red else color.gray));
addlabel(1, s2 + " |" + c2, (if n2>0 then color.green else if n2<0 then color.red else color.gray));
addlabel(1, s3 + " |" + c3, (if n3>0 then color.green else if n3<0 then color.red else color.gray));
addlabel(1, s4 + " |" + c4, (if n4>0 then color.green else if n4<0 then color.red else color.gray));
addlabel(1, s5 + " |" + c5, (if n5>0 then color.green else if n5<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s6 + " |" + c6, (if n6>0 then color.green else if n6<0 then color.red else color.gray));
addlabel(1, s7 + " |" + c7, (if n7>0 then color.green else if n7<0 then color.red else color.gray));
addlabel(1, s8 + " |" + c8, (if n8>0 then color.green else if n8<0 then color.red else color.gray));
addlabel(1, s9 + " |" + c9, (if n9>0 then color.green else if n9<0 then color.red else color.gray));
addlabel(1, s10 + " |" + c10, (if n10>0 then color.green else if n10<0 then color.red else color.gray));
addlabel(1, "--", color.black);
addlabel(1, s11 + " |" + c11, (if n11>0 then color.green else if n11<0 then color.red else color.gray));
addlabel(1, 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));


# ----------------------------------------

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));
#
[code]
SUPER !! perfect... This is what I wanted but didnt know it :)

Many thanks !!
 
side note
when you have this many labels on a chart you may want to space them away from other study labels , or space them out a little more in my program. i used a couple black labels to space them out some.

you can use this as is, or copy a line or 2 and add it to my program , if you want all 15 labels to wrap over to the next row.

when i am creating a study, i use this to add black space between my other studied and the last one, that i am working on, to make it easier to see the labels.

Code:
# label_shifter

input show_labels = no;
addlabel( show_labels,"label shifter", color.gray);

input qty_of_blank_labels = 3;
def qty = if qty_of_blank_labels > 10 then qty_of_blank_labels else if qty_of_blank_labels < 1 then 1 else qty_of_blank_labels;

# 20 chars
addlabel(show_labels and (qty >= 1), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 2), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 3), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 4), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 5), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 6), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 7), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 8), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 9), "-=========-=========" , color.black);
addlabel(show_labels and (qty >= 10), "-=========-=========" , color.black);
That is a neat trick ... I plan to have the summary labels on and a couple of the relevant basket stocks off and on. I can always comment out the labels that I do not want to show. I might even add a "show label" tag to the stocks if it gets too cumbersome to comment/uncomment :)

I can manage some simple code based on what you guys post here but cant handle these complex logics :)

Thanks again !!
 
Aaah yes.... I get it. Thanks man... really appreciate your help
I am assuming that the counters will get reset every time the study is reinitialized like a change on time frame or change in the stock on the chart itself.... so 2 instances of the study could potentially show different results if they are not started/initialized at the same time.

Please correct me if I am wrong....
 
I am assuming that the counters will get reset every time the study is reinitialized like a change on time frame or change in the stock on the chart itself.... so 2 instances of the study could potentially show different results if they are not started/initialized at the same time.

Please correct me if I am wrong....
all loaded studies look at the same price data. if 2 copies of the same study are loaded, they should show the same info.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Thread starter Similar threads Forum Replies Date
F Bar Counter & Bubble Questions 1
S Condition counter Questions 12
S Fold Function Counter Questions 3
G Bar Counter Label Questions 4
S Bar counter Questions 3

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
353 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