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 =...
Thanks to @halcyonguy I was able to get this indicator in place. I am still tinkering with it but have made some updates.

1. Added ability to show hide labels for individual stocks.
2. Added a histogram with %age moves
3. Updated the wording to make the labels more concise.
4. Currently using 12 of the 15 stocks set up for the basket. I have commented the code for the rest so it can be un-comented and used as needed.

I looked at the stocks at the top 10 stocks based on weightage in the S&P and NASDAQ. These number 12. I am looking at 3 price points, current price, close of the candle before the current one, and the close of the candle before that. Next, I look at if they closes are positive or negative and then based on this I try to see which way the heavy weights like AAPL, MSFT, GOOGL, NVDA etc are moving.

The indicator labels show current trend over previous candle close and the trend before that to see if there is a likely direction change. The Histogram shows the %age change of current price over previous candle close.

Hope this makes sense and helps someone out there...


Here is the updated code.....

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

mfNzJBU.png
 
You will notice that the numbers of the labels do not add up. The reason is the labels on the left show how many are currently up or down as compared to the previous candle close and the pair of labels on the right shows the number of stocks that have changed direction....



pXKAjwq.png
 

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