Reprogramming a Study to avoid Recursion for Conditional Order use


Category:
0
0

Since conditional order entry based on a study cannot have recursion used within it, is there a way of getting around this using one or more of (a) additional variables, (b) the GetValue()  function, and/or (c) the Fold (looping) feature, or other thinkscript features to access historical data?

So in short, given a simple example:

<pre>def x = x[1] + 1;</pre>

Can the value of x[1] be accessed/recreated in a way that avoids recursion so that it can be applied to x and be valid for a conditional order use?

I have managed (I think) to do a workaround with a boolean variable (values are based on Forex pips, hence the 0.0003 and this was purely for testing purposes):

<pre>

declare lower;

def test;
def testShadow;

if close > open + 0.0003 {
test = 1;
testShadow = 1;
} else if close < open – 0.0003 {
test = 0;
testShadow = 0;
} else {
testShadow = Double.NEGATIVE_INFINITY;
test = fold i = 0 to 1440 while testShadow[i] == Double.NEGATIVE_INFINITY do GetValue(testShadow, i + 1);
}
plot testplot = test;

</pre>

But it seems to get more complicated when actually trying to utilize a previous calculated value (such as the x = x[1] + 1), yet it seems like somehow a GetValue() might work in some way to retrieve needed info.

Have you come across any workarounds to get needed data without recursion?

Marked as spam
Posted by (Questions: 2, Answers: 1)
Asked on May 7, 2017 9:52 pm
1108 views
0
Private answer

In your case it may be easier if you explain what it is you are trying to do. The fold function is Thinkorswim very poor attempt at implementing a loop. It is very confusing and I think well beyond the ability of 99% of our viewers to understand.

It seems that you are testing for two conditions:

close > open + 0.0003 OR close < open – 0.0003

Then you want to count the number of times both of these conditions have been false for the past 1,440 bars. As of this writing I’ve only been awake for 30 minutes and haven’t even had caffein yet. So I may be way off. Please clarify if I missed something.

If my interpretation is correct, this code should do the trick without using recursion:

def twoConditions = close > open + 0.0003 or close < open - 0.0003;
def neitherCondition = !twoConditions;
def sumOfNeitherCondition = Sum(neitherCondition, 1440);
plot scan = sumOfNeitherCondition;

Screenshot below shows the output, using the Study Alert screen. Which does not accept recursion and is a great test ground for conditional orders.

 

Attachments:
Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 8, 2017 7:53 am
0

In the case of the Boolean condition example, I would want the value kept at the last condition met. So recursively, the final ”else” would normally simply be test = test[1]. But since recursion is not allowed in a study used for a conditional order, I am looping backwards to a maximum number of times that is a good chunk of the whole chart (1440 using a 5min chart) looking for the first time Double.NEGATIVE_INFINITY was *not* set on the shadow variable (which ends the loop), but as I’m looping I am setting to the previous value (so when the loop ends, I actually end up with the last value that was set at 1 or 0). But my real question is how to obtain a recursive value non-recursively to use in a more complex way of actually assigning the value. So a more complex example, look at the code for the VariableMA in ThinkorSwim, where it not only uses the CompoundValue function, but within the setting of asd uses a complex mathematical call to set the current asd in relation to the previous asd via asd[1]. I hope that makes sense. In short, I’m looking for a ”round about” method of obtaining recursive information to set a variable based off its past value, without directly using recursion since it is not allowed for conditional orders.

( at May 8, 2017 8:25 am)
0

I have not found a way to do so. Does not mean there is not some way to do it. But adding levels of obfuscation to try and trick the compiler that no recursion is taking place does not seem to be effective. It is able to detect the recursion anyway.
I suggest you consider moving to another platform with more robust support for placing orders that execute from code.

( at May 8, 2017 8:45 am)
0

I’ve had the same problems. If you’re calling a built in function with the same parameters then you can use recursion on that. e.g. foo(x,y)[1]. I’m not sure what the limits are or what exactly it’s doing but I’ve used it in places that don’t allow recursion.

( at July 22, 2017 5:05 pm)
0

Fascinating technique Stephen. I’ll have to test this out a bit and if I find anything usable I’ll be sure to post a follow-up.

( at July 22, 2017 5:54 pm)