Parametric indexing


Category:
0
0

Hi Pete,

I would like to refer to a previous bar value by using a parameter which is changable (is not constant).

But, TOS expects a constant value in the []. How can it be done?

For example, lets suppose I would like to go back X bars as the value of an Index:

rec Index=if (Index>0) then Index[1]+1 else 1;

rec  PreviousXBar = PreviousXBar[Index];

 

Unfortunately TOS doesn’t allow this: “PreviousXBar[Index]” (Invalid statement).

Can you assist?

Thanks,

Arik.

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on December 20, 2017 5:45 am
847 views
0
Private answer

I suspect we have much more going on in your source code, which you choose not to share. I understand that. From what I see, you are likely to have more questions. From the example you provided, I first need to point out an error.

rec Index=if (Index>0) then Index[1]+1 else 1;

The correct version of this is:

rec index = if index[1] > 0 then index[1] + 1 else 1;

Notice the test to see if index is greater than zero is incorrect. You cannot reference the current value of index without generating an exception that prevents the entire statement from being evaluated. We must reference the previous value of index and check if it is greater than zero.

Having corrected this, what we have here is a counter. One that keeps track of the number of viewable bars on the chart. Which seems like an attempt to create something that returns the bar number. However it does not, not exactly. You can read more about the BarNumber() function here: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/BarNumber.html

So I am guessing in your actual code you have some statement that grabs the price when a specific pattern occurs, and you want to count the number of bars that have occurred since then so you can refer back to it using the index value. (but there are simpler ways to achieve this) The only reason I bring this up is because the way your have that statement written, the initial value begins at 2, not 1 or 0 but 2. This needs to be accounted for when you try to use it.

Since I actually have no idea if that is your intent, I will provide the rest of the solution assuming you understand this. (hint: you may need to make adjustments to this)

Ok so here is how we make this work. Your original statement also produces an error:

rec PreviousXBar = PreviousXBar[Index];

You cannot reference PreviousXBar at any index whatsoever, even a static one. This is because PreviousXBar has never been fully initialized. (there is no starting value, and by extension there are no previous values, anywhere on the chart)

So here I make my own decision of what value to plot, selecting the close.

rec prviousXBar = GetValue(close, index);

We use the GetValue() function to apply your variable index and return the value of close X Bars ago. You can read more about the GetValue() function here: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetValue.html

If the original statement were rewritten so that it initialized at zero instead of two, this second statement would return the close of the first viewable bar on the chart. But we already have a built in function that does this for us: http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Others/First.html

I suspect there were several ah-hah moments while reading this post. Go back and read it again to make sure you didn’t miss anything.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on December 20, 2017 9:15 am
0
Private answer

Hi Pete, I am new to programming. I have read thru this post and have looked up, the GetValue() link. But I am still stuck. How can I write a simple code-  close is greater than high from 15 bars ago. If I write it like that -the program just looks at the close of the bar -15 bars ago. I want it to look at the last 15 bars.

close is greater than high from 1-15 bars ago. but this don’t work either. Any ideas or examples? thanks

 

Marked as spam
Posted by (Questions: 3, Answers: 6)
Answered on December 30, 2017 2:54 am
0

That is an entirely different solution than what is described in this post. So to avoid confusing other visitors, please be sure to post a new question in your own topic. Be sure to use a title that is brief, yet descriptive enough that others can understand the main context of your question.
Also, when you are not providing an answer to a topic, please don’t place your response in the Answer box. Use the comment section, as I have done here.

( at December 30, 2017 9:21 am)
0

Thanks Pete, I made a new post. And should have put it under the right section/category -thinkorswim-scans.
thanks again.

( at December 30, 2017 10:44 am)