Alternative to “within” using a variable


Hi Pete, the within reserved word only functions with an integer or length. Do you think it would be possible to write an expression that would achieve the same results as the within function while using a variable in place of an integer or length? Thanks.

Marked as spam
Posted by (Questions: 4, Answers: 6)
Asked on June 19, 2020 2:15 pm
Private answer

"within" is not a function. It is merely a "reserved word".

It is inserted into the code when using the Condition Wizard. I never use it directly when I write code so I have no experience that would enable me to assist you. I use a completely different type of code structure to accomplish the same thing.

Without a real world example I can't even perform basic trouble shooting techniques to answer your question. However using the example provided in the link above I can tell you I would write that same example like this:

plot IsDoji3 = Highest(Doji(), 3) > 0;

Marked as spam
Posted by (Questions: 37, Answers: 4097)
Answered on June 19, 2020 3:02 pm
Thanks Pete, sorry for not providing an example. Here is one (you can see I’m at a beginner level): input length = 10; def x = f(n); def avg = Average(close, length); def cross = Crosses(close, Avg); plot crosscheck = if cross == 1 within 5 bars then 1 else 0; crosscheck.SetPaintingStrategy(PaintingStrategy.BOOLEAN_POINTS); Is there a way for the script to look back x number of bars, where the variable x is calculated by a function f(n), rather than 5 in the example? I don’t have any specific function in mind for x, it could be anything. Thanks again
( at June 20, 2020 7:39 pm)
Not directly, no. This method you are using requires a static value and not a computed value. If you want to try using a computed value then you probably want to work with the GetValue() function:
( at June 20, 2020 9:58 pm)
Thanks Pete, I used GetValue in the example above and it seems to work. I wish TOS would make their examples easier to understand. The GetValue example uses the GetMaxValueOffset function, which purpose isn’t immediately obvious at least to me, and then looking at the GetMaxValueOffset example was just as unclear in explaining what that does, so little hope in understanding GetValue really clearly. Are you aware of a thinkscript wiki that describes these functions in layman’s terms? As always thanks again for your quick responses.
( at June 22, 2020 5:56 am)

def previousClose = close[1];

Same exact statement as

def previousClose = GetValue(close, 1);

( at June 22, 2020 7:26 am)