Recursion and Secondary Aggregation in Conditional Orders


Is there anything wrong with below script that is not working with conditional order?

declare lower;
def delta = close(period = aggregationPeriod.FIVE_MIN) – close(period = aggregationPeriod.FIVE_MIN)[1];

def deltaClose = deltaClose[1] + delta;

def priceAtBuy = close(period = aggregationPeriod.FIVE_MIN) – deltaClose;

def salePrice = close(period = aggregationPeriod.FIVE_MIN) * 1.05;

plot signal = priceAtBuy < salePrice;

Marked as spam
Posted by (Questions: 1, Answers: 1)
Asked on April 26, 2023 5:54 pm
Private answer

There were a few changes required before I could approve this question for the forum:

  1. Questions regarding the Conditional Order tool should be located in the "Strategy Guide" topic, so I moved this question out of "Frequently Asked Questions" (which should never be used anyway).
  2. The title of your question did not include the details which would provide other viewers the context required to find this solution using the search box. So your title has been updated to include those essential details.
  3. Ordinarily, this type of question would not be approved in this forum because we do not provide one-off solutions which only serve a single individual. However by changing the title I have adapted your question so that it does serve the interest of a wide range of our viewers.

I just wanted to clear up those details so when you post questions in the future you understand the goals of this forum. Which is to solve the most commonly asked questions among traders.

Now that we have cleared up those details I will explain why your code is not supported in the Conditional Order tool on Thinkorswim.

  1. Your code includes references to a secondary aggregation period, which is not supported in the current version of Thinkorswim.
  2. Your code includes a recursive variable, which is also not supported in the current version of Thinkorswim.

Where is this reference to secondary aggregation? Here:

(period = aggregationPeriod.FIVE_MIN)

Each section of code which includes this reference is not supported in the Conditional Order tool. If you paste your code directly into a Conditional Order you will find it generates an error stating "Secondary period cannot be less than primary". And this error persists no matter what time frame is selected for the Conditional Order. However if you tried to save this as a custom study and used only a reference to the study in the Conditional Order the compiler may miss this. Which is why we always recommend that we never reference custom chart studies in this tool. We always want to use the entire code.

Where is the recursive variable?

def deltaClose = deltaClose[1] + delta;

Any variable which refers to a previous value of itself (deltaClose[1]) is a recursive variable. These are not supported in the Conditional Order tool on the current version of Thinkorswim.

For those interested in learning how to avoid these types of mistakes I suggest the following videos:



Marked as spam
Posted by (Questions: 37, Answers: 4043)
Answered on April 26, 2023 6:38 pm