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.
# 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);
#
Join useThinkScript to post your question to a community of 21,000+ developers and traders.
I downloaded and created and IndexDirection() Study with that code. Thanks help the help.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?
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
# 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);
#
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.
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.
Start a new thread and receive assistance from our community.
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.
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.