Expecting Constant Error -Please help me find workaround


Category:
0
0

In something I have been working on, I have to (a) determine the closing price x bars ago – ie. close[x]

(b) find the average closing price  since that date .  average(close,x)

x was determined through my code and so is not a constant, which Think Script needs in these situations.

From your answers to others I could learn that using getValue() solves my problem(a). But I could not find a solution for problem (b) so far – i.e.for average(close,x).

Hope you can also refer me to some of your indicators where you solved this issue, so that I can learn using your examples

 

Thanks

Arun.

Marked as spam
Posted by (Questions: 11, Answers: 16)
Asked on May 12, 2018 7:19 am
97 views
0
Private answer

There may not be a solution to this. But I have an idea. I will need to see the rest of your code in order to know if my solution will work. I understand you probably don’t want to make that public. So you may have to submit a project request and handle this as a paid solution.

I haven’t solved this before so I have not examples to offer for you to review.

About the only thing I can imagine that we could use is the fold statement. Which is Thinkorswim’s low tech way of looping through bars on the chart. You can read about it here: http://toslc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/fold.html

If we could perform real loop statements in Thinkorswim this would be easily solved.

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 12, 2018 10:01 am
0
Thanks, Pete. Kept trying after posting request. Found a solution using compoundValue but have additional questions. ( Fold statement always seemed like a beast and so avoided reading about it so far. ). Rec SumClose = compoundvalue(1,if x==1 then close else if x>1 and !isNan(close) then SumClose[1]+close else 0,0); plot xavg = SumClose/x; My questions regarding compound value: I don’t know when the last parameter 0 becomes significant. Can I use close as the last parameter in some way to initialize the computation instead of initializing Sumclose through the” if x=1 then Sumclose=close”? Or this last parameter is only used to initialize the Sumclose[1] for the first bar ? Secondly, Does this compound statement always get computed on a 2 year data by the scan? can I use a larger number than 1 as the first parameter in this compound value example? Does it reduce the computing load if I use a larger number? Thanks Arun Thanks. Arun
( at May 12, 2018 11:24 am)
0
Additionally, I was able to get to the solution only because I used your Plot-the-variable debugging method. Without your technique, I would not have gotten the answer. Though elementary, I really still do not remember that the code is executed for every bar and the values of variables at the last bar are not the same through out the chart. It is only when I used plots, I could see the obvious and modify my thought process and the code. Thanks.
( at May 12, 2018 11:51 am)
0

Yes, the last parameter of CompoundValue initializes the value of the first bar, but only if the first parameter is set to 1 (first bar). You may also use an if statement as the last parameter. You will find there is a lot more to this if you experiment. So you can set the first parameter to 5 and use a recursive if statement in the third parameter to determine the value of those first 5 bars.

Very happy to hear you remembered to use the debugging method I described earlier. I use that for every single project I do. Nice work. You should post that as a New Answer instead of just a comment. Since you did come up with a solution. Have fun!

( at May 12, 2018 12:48 pm)