Getting Current Index direction

Mocnarf1

Member
how would a ThinkScript to test if an index (ie NDX) is down or up for the current day and time?
An example would be great.
 
how would a ThinkScript to test if an index (ie NDX) is down or up for the current day and time?
An example would be great.

this allows a user to enter a symbol,
then display 2 labels, the symbol and if it is up or down for the day.
and another label that shows by how much

in the watchlist, click on the header name and look for the group indices, and choose the symbol you want.

it uses 2nd aggregation to read the open of the day , and compare it to the close.
an alternative would be to check if the current bar is at the day open, then read the open, then keep it until tomorrows open.

Code:
# sym1_other_00

input Symbol1 = "SPY";
def s1_cls = close(Symbol1);

# compare close to the day open
def dayagg = AggregationPeriod.DAY;
def s1day_open = open(Symbol1, period = dayagg);

def isdayup = s1_cls > s1day_open;
def isdaydwn = s1_cls > s1day_open;

AddLabel(1,
  (Symbol1 + " is " + (if isdayup then "UP" else if isdaydwn then "DOWN" else "flat")),
  (if isdayup then Color.GREEN else if isdaydwn then Color.RED else Color.WHITE));

AddLabel(1,
  (s1_cls - s1day_open),
  (if isdayup then Color.GREEN else if isdaydwn then Color.RED else Color.WHITE));

input show_day_open_close = no;
AddLabel( show_day_open_close , "open " + s1day_open, Color.YELLOW);
AddLabel( show_day_open_close , "close " + s1_cls, Color.YELLOW);


input show_2nd_symbol_lines = no;
plot zs1 = s1_cls;
zs1.SetDefaultColor(Color.CYAN);
zs1.SetLineWeight(1);
zs1.HideBubble();
zs1.SetHiding(!show_2nd_symbol_lines);
#
 
this allows a user to enter a symbol,
then display 2 labels, the symbol and if it is up or down for the day.
......
I downloaded and created and IndexDirection() Study with that code. Thanks help the help.

What I was hoping to get is to compare the Index's Todays Open with the Index"s Todays Current price at any particular point in the day. So I can test at any time in the day whether the Index's Todays Open Price is greater than or less than Today's Index's Todays Current price.
Is it possible to get a Boolean answer to that question with this study? If so how?
 
I downloaded and created and IndexDirection() Study with that code. Thanks help the help.

What I was hoping to get is to compare the Index's Todays Open with the Index"s Todays Current price at any particular point in the day. So I can test at any time in the day whether the Index's Todays Open Price is greater than or less than Today's Index's Todays Current price.
Is it possible to get a Boolean answer to that question with this study? If so how?

the 'current' price occurs only once, on the last bar.

do you mean, compare the close, from a previous bar, from today?

is this what you are asking for?
one possibility,
the user could enter a 24 hour time, like 1045, and it would look for the close from the bar, from that time period.
 
the 'current' price occurs only once, on the last bar.

do you mean, compare the close, from a previous bar, from today?

is this what you are asking for?
one possibility,
the user could enter a 24 hour time, like 1045, and it would look for the close from the bar, from that time period.

The opening price for SPY at 9:30AM ET compared to the closing price of SPY at say 11:05AM or whatever time on a 15 minute chart. I want to compare if my midday closing price for SPY on a 15 minute chart is > or < SPY's opening price at 9:30AM
 
The opening price for SPY at 9:30AM ET compared to the closing price of SPY at say 11:05AM or whatever time on a 15 minute chart. I want to compare if my midday closing price for SPY on a 15 minute chart is > or < SPY's opening price at 9:30AM

i think this one will tell you if a secondary symbol, is up or down, from day open, to the desired time of day.
there is a blue dot on the desired bar

if you load the same symbol on the chart, then you can turn on open and close lines to verify the numbers.


it's a little messy and long.
there is debugging stuff at the end

Code:
# sym1_other_01

def na = double.nan;
input Symbol1 = "SPY";

# compare close to the day open
def dayagg = AggregationPeriod.DAY;
def s1day_open = open(Symbol1, period = dayagg);

def diffday = if getday() != getday()[1] then 1 else 0;

# get close from some time of the day
input time_24hour = 1330;

def hr = floor(time_24hour / 100);
def min = time_24hour - (hr * 100);
#def mins = (hr * 60) + min;
#def hrs = mins/60;
addlabel(1, symbol1 + " compare time " + hr + ":" + min, color.yellow);


def t = (secondsfromtime(time_24hour) == 0);
def x = if diffday then 0 else if t then close(Symbol1) else x[1];


def cls;
if diffday then {
cls = fold i = 0 to 500
with p
while  getday() == getvalue( getday(), -i)
do if getvalue(x, -i) > p then getvalue(x, -i) else p;
} else {
cls = cls[1];
}


# draw a point on the bar to read the close
plot z = if t then low*0.998 else na;
z.SetPaintingStrategy(PaintingStrategy.POINTS);
z.SetDefaultColor(Color.cyan);
#z.setlineweight(1);
z.hidebubble();


#def s1_cls = close(Symbol1);
def s1_cls = if t then close(Symbol1) else if diffday then na else s1_cls[1];


#def isdayup = s1_cls > s1day_open;
#def isdaydwn = s1_cls > s1day_open;
def isdayup = cls > s1day_open;
def isdaydwn = cls < s1day_open;


AddLabel(1,
  (Symbol1 + " is " + (if isdayup then "UP" else if isdaydwn then "DOWN" else "flat")),
  (if isdayup then Color.GREEN else if isdaydwn then Color.RED else Color.WHITE));

AddLabel(1,
  (s1_cls - s1day_open),
  (if isdayup then Color.GREEN else if isdaydwn then Color.RED else Color.WHITE));

input show_day_open_close = no;
AddLabel( show_day_open_close , "open " + s1day_open, Color.YELLOW);
AddLabel( show_day_open_close , "close " + s1_cls, Color.YELLOW);


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

# test stuff
input test_open_price_line = no;
plot s1o = if test_open_price_line then s1day_open else na;
s1o.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1o.SetDefaultColor(Color.gray);


# close line
input test_close_line = no;
plot ee = if test_close_line then cls else na;
ee.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ee.SetDefaultColor(Color.magenta);


input test2_close_price_line = no;
plot s1c = if test2_close_price_line then s1_cls else na;
s1c.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
s1c.SetDefaultColor(Color.pink);
#s1c.SetLineWeight(1);
s1c.HideBubble();


addchartbubble(0, low,
 s1day_open + "\n" +
 s1_cls  + "\n" +
cls + "\n" +
isdayup + "\n" +
isdaydwn
,color.yellow, no);
#

SPY
vdL3E2M.jpg
 
I want you to know that I appreciate your effort on this request. Thank you. It's unfortunate that TOS doesn't allow the setting of a global variable so that the opening Price of SPY at 9:30am could be captured and used in the comparison later in the day. It would make it a lot easier to simply know (boolean True or False) if Index is up or down at that point in the day. Don't really need to see a graph, a Boolean answer is all I need to know. This is all I want it to do:
Not plotting when it should or at all.

Code:
def dayagg = AggregationPeriod.DAY;
   # Maybe set this value only once at a specific time ???
def s1day_open = open("SPY", period = dayagg);       #at 9:30AM ET        

   # Is SPY opening price at 9:30am > current open price of SPY
plot condition = s1day_open > open("SPY") ;                 ### <--- Not plotting when it should or at all.
 
Last edited:
I want you to know that I appreciate your effort on this request. Thank you. It's unfortunate that TOS doesn't allow the setting of a global variable so that the opening Price of SPY at 9:30am could be captured and used in the comparison later in the day. It would make it a lot easier to simply know (boolean True or False) if Index is up or down at that point in the day. Don't really need to see a graph, a Boolean answer is all I need to know. This is all I want it to do:
Not plotting when it should or at all.

Code:
def dayagg = AggregationPeriod.DAY;
   # Maybe set this value only once at a specific time ???
def s1day_open = open("SPY", period = dayagg);       #at 9:30AM ET      

   # Is SPY opening price at 9:30am > current open price of SPY
plot condition = s1day_open > open("SPY") ;                 ### <--- Not plotting when it should or at all.

sorry, sometimes i try harder and make it more complicated
here is something that might help
https://usethinkscript.com/threads/...ed-time-on-the-current-day.11900/#post-107868
 
I never thought it would be so hard to test to see if an Index (ie SPY) was up or down from it's opening price at 9:30 AM.
Maybe someone know's do build something similar to Yahoo Finance's Index Report it show's the change in value of the index from the start of the day to the current moment by either % or dollar value. ei like this:

[image] [/image]
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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