Code referencing other stocks not working properly

Chemmy

Active member
Hi all, I am trying to start a new indicator, and one aspect of it requires taking N number of the SPDR sectors and looking at whether they all flip green or red for the day. However, when I finished this step I noticed that it doesn't seem to show any of the green-flipped results on different stocks, but those results do appear when looking at the first symbol in the code, XLI. I've attached an example of what I mean here, where XLI shows a green arrow off open that does not appear for QQQ -- does anybody know what is going on with this?

1725767174058.png
1725767084193.png

Code:
# Input ETF symbols
input etf1 = "XLE";
input etf2 = "XLV";
input etf3 = "XLY";
input etf4 = "XLU";
input etf5 = "XLI";

# Number of ETFs that need to be green/red to trigger a signal
input tolerance = 4;
input agg = aggregationperiod.day;

# Script to check if a symbol is green for the day
script isGreenDay {
    input symbol = "";
    input aggy = aggregationperiod.day;
    def isGreen = close(symbol = symbol) > open(symbol = symbol, period=aggy);
    plot result = isGreen;
}

# Script to check if a symbol is red for the day
script isRedDay {
    input symbol = "";
    input aggy = aggregationperiod.day;
    def isRed = close(symbol = symbol) < open(symbol = symbol, period=aggy);
    plot result = isRed;
}

# Count green and red ETFs
def greenCount =
    isGreenDay(etf1, agg).result +
    isGreenDay(etf2, agg).result +
    isGreenDay(etf3, agg).result +
    isGreenDay(etf4, agg).result +
    isGreenDay(etf5, agg).result;

def redCount =
    isRedDay(etf1, agg).result +
    isRedDay(etf2, agg).result +
    isRedDay(etf3, agg).result +
    isRedDay(etf4, agg).result +
    isRedDay(etf5, agg).result;

# Plot arrows based on the count
def up = greenCount >= tolerance;
def dn =  redCount >= tolerance;

plot UpArrow = up and !up[1];
plot DownArrow = dn and !dn[1];

UpArrow.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
UpArrow.SetDefaultColor(Color.GREEN);
UpArrow.SetLineWeight(3);

DownArrow.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_DOWN);
DownArrow.SetDefaultColor(Color.RED);
DownArrow.SetLineWeight(3);
 
Solution
Does this work if your primary chart aggregation is set to Day as well? There seems to be something funny going on with the aggregationperiod.day thing at the moment. THere is another thread around here about things not working well on intra-day charts when trying to reference day aggregation data.

Not a solution, but at least an idea.

-mashume
Does this work if your primary chart aggregation is set to Day as well? There seems to be something funny going on with the aggregationperiod.day thing at the moment. THere is another thread around here about things not working well on intra-day charts when trying to reference day aggregation data.

Not a solution, but at least an idea.

-mashume
 
Solution

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

Hi all, I am trying to start a new indicator, and one aspect of it requires taking N number of the SPDR sectors and looking at whether they all flip green or red for the day. However, when I finished this step I noticed that it doesn't seem to show any of the green-flipped results on different stocks, but those results do appear when looking at the first symbol in the code, XLI. I've attached an example of what I mean here, where XLI shows a green arrow off open that does not appear for QQQ -- does anybody know what is going on with this?

View attachment 22854View attachment 22853
Code:
# Input ETF symbols
input etf1 = "XLE";
input etf2 = "XLV";
input etf3 = "XLY";
input etf4 = "XLU";
input etf5 = "XLI";

# Number of ETFs that need to be green/red to trigger a signal
input tolerance = 4;
input agg = aggregationperiod.day;

# Script to check if a symbol is green for the day
script isGreenDay {
    input symbol = "";
    input aggy = aggregationperiod.day;
    def isGreen = close(symbol = symbol) > open(symbol = symbol, period=aggy);
    plot result = isGreen;
}

# Script to check if a symbol is red for the day
script isRedDay {
    input symbol = "";
    input aggy = aggregationperiod.day;
    def isRed = close(symbol = symbol) < open(symbol = symbol, period=aggy);
    plot result = isRed;
}

# Count green and red ETFs
def greenCount =
    isGreenDay(etf1, agg).result +
    isGreenDay(etf2, agg).result +
    isGreenDay(etf3, agg).result +
    isGreenDay(etf4, agg).result +
    isGreenDay(etf5, agg).result;

def redCount =
    isRedDay(etf1, agg).result +
    isRedDay(etf2, agg).result +
    isRedDay(etf3, agg).result +
    isRedDay(etf4, agg).result +
    isRedDay(etf5, agg).result;

# Plot arrows based on the count
def up = greenCount >= tolerance;
def dn =  redCount >= tolerance;

plot UpArrow = up and !up[1];
plot DownArrow = dn and !dn[1];

UpArrow.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
UpArrow.SetDefaultColor(Color.GREEN);
UpArrow.SetLineWeight(3);

DownArrow.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_DOWN);
DownArrow.SetDefaultColor(Color.RED);
DownArrow.SetLineWeight(3);


sometimes issues happen when defining 2 prices, with different aggregation times, in a formula.
like this in the script, which has chart time and day time,
. input aggy = aggregationperiod.day;
. def isGreen = close(symbol = symbol) > open(symbol = symbol, period=aggy);

it is better to define the different agg value (with openz), then reference the variable in the formula.
. def openz = open(symbol = symbol, period=aggy);
. def isGreen = close(symbol = symbol) > openz;

------------------------

with extended hours on, the first bar of day will not show any arrows.

during after hours , up is equal to NAN, so this plot is never true on the first bar of day,
. plot UpArrow = up and !up[1];

updated formula that works on 1st bar of day
. plot UpArrow = if isnan(up[1]) and up then 1 else up and !up[1];

this formula is true for just the first in a series of true values of up.

-----------------------

removed the scripts and 2nd aggregation.
on the first bar of the day, this reads the open and keeps it, for comparing.

changed the green/red bar counting.
each of the 5 stocks is either +1, -1, or 0. then the 5 values are added up, and compared to tolerance.

can show horizontal lines from the day open.
they only show up if one of the five stocks is loaded on the chart.


Code:
#sector_grnred
#https://usethinkscript.com/threads/code-referencing-other-stocks-not-working-properly.19605/
#Code referencing other stocks not working properly

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

# Input ETF symbols
input etf1 = "XLE";
input etf2 = "XLV";
input etf3 = "XLY";
input etf4 = "XLU";
input etf5 = "XLI";

def s1cls = close(etf1);
def s2cls = close(etf2);
def s3cls = close(etf3);
def s4cls = close(etf4);
def s5cls = close(etf5);

# Number of ETFs that need to be green/red to trigger a signal
input tolerance = 4;

input start = 0930;
input end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
def startbar = secondsfromTime(start) == 0;


def s1opn;
def s2opn;
def s3opn;
def s4opn;
def s5opn;
if !daytime then {
 s1opn = na;
 s2opn = na;
 s3opn = na;
 s4opn = na;
 s5opn = na;
} else if startbar then {
 s1opn = open(etf1);
 s2opn = open(etf2);
 s3opn = open(etf3);
 s4opn = open(etf4);
 s5opn = open(etf5);
} else {
 s1opn = s1opn[1];
 s2opn = s2opn[1];
 s3opn = s3opn[1];
 s4opn = s4opn[1];
 s5opn = s5opn[1];
}


# count the green and red bars
def is1 = if s1cls > s1opn then 1 else if s1cls < s1opn then -1 else 0;
def is2 = if s2cls > s2opn then 1 else if s2cls < s2opn then -1 else 0;
def is3 = if s3cls > s3opn then 1 else if s3cls < s3opn then -1 else 0;
def is4 = if s4cls > s4opn then 1 else if s4cls < s4opn then -1 else 0;
def is5 = if s5cls > s5opn then 1 else if s5cls < s5opn then -1 else 0;
def cnt = is1 + is2 + is3 + is4 + is5;


# Plot arrows based on the count
def up = cnt >= tolerance;
def dn = cnt <= -tolerance;

plot UpArrow = if isnan(up[1]) and up then 1 else up and !up[1];
plot DownArrow = if isnan(dn[1]) and dn then 1 else dn and !dn[1];

UpArrow.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_UP);
UpArrow.SetDefaultColor(Color.GREEN);
UpArrow.SetLineWeight(3);

DownArrow.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_DOWN);
DownArrow.SetDefaultColor(Color.RED);
DownArrow.SetLineWeight(3);


input open_lines = no;
plot o1 = if open_lines and GetSymbol() == etf1 then s1opn else na;
plot o2 = if open_lines and GetSymbol() == etf2 then s2opn else na;
plot o3 = if open_lines and GetSymbol() == etf3 then s3opn else na;
plot o4 = if open_lines and GetSymbol() == etf4 then s4opn else na;
plot o5 = if open_lines and GetSymbol() == etf5 then s5opn else na;
o1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
o2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
o3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
o4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
o5.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
o1.SetDefaultColor(Color.gray);
o2.SetDefaultColor(Color.gray);
o3.SetDefaultColor(Color.gray);
o4.SetDefaultColor(Color.gray);
o5.SetDefaultColor(Color.gray);
#
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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