Tried somemore today and came up with a little bit of a start... I have some more specific questions.
Here is my code thusfar:
Code:
declare lower;
input comparisonStyle = {"CANDLE", "BAR", default "LINE"};
input secondarySecurity = "SPX";
input startdate = 20210101;
def CloseonStartDate = getvalue(close, startdate);
def SecondaryCloseonStartDate = getvalue(close(symbol = secondarySecurity), startdate);
plot Bands = close(GetSymbol()) / CloseonStartDate - close(secondarySecurity) / SecondaryCloseonStartDate;
First question is, can you set up the time frame plotted using script?
Second question, why am I not seeing anything on the graph?
Third question is: is there a way to see what values are returned so I can trouble-shoot easier?
Thanks for your help in advance.
debugging
i added a bubble to your study, to display some variables.
\n , is used in a bubble, to start a new line of characters.
the \n can be within quotes of text or on its own like this , + "\n" +
all 3 variables are NA. so something in your formulas is wrong.
Code:
declare lower;
input comparisonStyle = {"CANDLE", "BAR", default "LINE"};
input secondarySecurity = "SPX";
input startdate = 20210101;
def CloseonStartDate = getvalue(close, startdate);
def SecondaryCloseonStartDate = getvalue(close(symbol = secondarySecurity), startdate);
plot Bands = close(GetSymbol()) / CloseonStartDate - close(secondarySecurity) / SecondaryCloseonStartDate;
#---------------------------
# have to have at least 1 line/shpare plotted on a chart before bubbles will appear
plot z = 0;
input test1 = yes;
addchartbubble(
test1, 0,
CloseonStartDate + " 1\n" +
SecondaryCloseonStartDate + " 2\n" +
Bands + " B"
, color.yellow, yes);
#
create bubbles to display variable values
all 3 variables are NA , so nothing is plotted
=============================================
when things don't work, copy a code line, disable it, then make it simpler.
i always copy lines, so i have the original code line to go back to if one variation doesn't work.
example,
start with this line,
copy it,
put a # in front of it to disable it.
then make it simpler, replace a variable with a constant. does it work ?
#def CloseonStartDate = getvalue(close, startdate);
def CloseonStartDate = getvalue(close, 1);
now the bubble display a number for CloseonStartDat.
so the original formula didn't work , why?
look up getvalue()
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue
Returns the value of data with the specified dynamic offset.
this function expects an offset , not a date.
an offset is a number that means, to look back (or forward) x number of bars and read a variable.
by using this as a parameter in getvalue()
startdate = 20210101;
this
getvalue( close , startdate )
is trying to read a value of close, 20 million bars back in the past.
that data doesn't exist, so it is equal to NA.
===============================
use GetYYYYMMDD() to compare each bar date to the desired date.
if they match then read the close from a bar.
if the date is not a trading day, then no match and no price will be found.
20210101 is a friday, but it is a holiday, so no trading, no price data.
20210104 is a trading day.
if the date is not visible on the chart, then no match and no price will be found.
change this,
#def CloseonStartDate = getvalue(close, startdate);
to this,
def CloseonStartDate = if bn == 1 then na else if GetYYYYMMDD() == startdate then close else CloseonStartDate[1];
i use if bn == 1 then na to initialize the variable, to NA, so it won't plot.
some people use compoundvalue(), i like to do it this way.
the last part,
else CloseonStartDate[1];
will keep the previous value, so after the date, the close price will exist in the variable.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/GetYYYYMMDD
set chart to day 3 year
change date to 1/4/2021 , a valid trading day.
comparing a date number to GetYYYYMMDD() has found a price
=====================
change the secondary price formula
from this,
#def SecondaryCloseonStartDate = getvalue(close(symbol = secondarySecurity), startdate);
to this,
def SecondaryCloseonStartDate = if bn == 1 then na else if GetYYYYMMDD() == startdate then close(symbol = secondarySecurity) else SecondaryCloseonStartDate[1];
now , after the desired date, we have a 3 numbers in the bubble and a line drawn on the lower chart.
change the 5th parameter for the bubble,
from ,yes) to , no),
so it will plot below the price level of 0, so it won't cover up the line.
Code:
declare lower;
def bn = barnumber();
def na = double.nan;
input comparisonStyle = {"CANDLE", "BAR", default "LINE"};
input secondarySecurity = "SPX";
#input startdate = 20210101;
input startdate = 20210104;
#def CloseonStartDate = getvalue(close, startdate);
#def CloseonStartDate = getvalue(close, 1);
def CloseonStartDate = if bn == 1 then na else if GetYYYYMMDD() == startdate then close else CloseonStartDate[1];
#def SecondaryCloseonStartDate = getvalue(close(symbol = secondarySecurity), startdate);
def SecondaryCloseonStartDate = if bn == 1 then na else if GetYYYYMMDD() == startdate then close(symbol = secondarySecurity) else SecondaryCloseonStartDate[1];
plot Bands = close(GetSymbol()) / CloseonStartDate - close(secondarySecurity) / SecondaryCloseonStartDate;
#---------------------------
# have to have at least 1 line/shpare plotted on a chart before bubbles will appear
plot z = 0;
input test1 = yes;
addchartbubble(
test1, 0,
CloseonStartDate + " 1\n" +
SecondaryCloseonStartDate + " 2\n" +
Bands + " B"
, color.yellow, no);
#
============================
turn off the bubble by changing the code, so the default for test1 is no instead of yes
stock is EMR
chart is day 3 year
hal_debug