AggregationPeriod variable

Norm6257

New member
VIP
Hi, my objective is to get the current aggregation of the chart, and then based on what the chart aggregation is, do conditional actions. In the first code example below I am getting the current aggregation with getAggregationPeriod() and feed it into a swich block and case compare with aggregationPeriod, but that gives an error.

Code:
def currentAggregation;
def trendAggregation;
def stopAggregation;

currentAggregation = getAggregationPeriod();

switch (currentAggregation) {
    case aggregationPeriod.FIVE_MIN:
        trendAggregation = aggregationPeriod.HOUR;
        stopAggregation = aggregationPeriod.FIFTEEN_MIN;
}

plot Data = close;


I also tried just using a string for the case compare and that gives fewer errors, but still shows as an error for the switch statement. This is shown in the below code block.

Code:
def currentAggregation;
def trendAggregation;
def stopAggregation;

currentAggregation = getAggregationPeriod();

switch (currentAggregation) {
    case "5m":
        trendAggregation = aggregationPeriod.HOUR;
        stopAggregation = aggregationPeriod.FIFTEEN_MIN;
}

plot Data = close;

Any ideas on why this is an error and how to use getCurrentAggregation() in a conditional?
 
Solution
def test = AggregationPeriod.HOUR;
#results in var test being a type constant with a value of something that represents an hourly aggregation period. I don't know what that is... I assume some integer, but even if the value is an integer, thinkscript doesn't see it as an integer because the var type is constant.

The reserved aggregation constants contain either an integer or a double equal to the number of milliseconds within the aggregate. I assume its an integer because there is no need for it to be floating point. Similar function parameters are documented as Int <Parameter>.

Inputs which default to a reserved constant can be a bit aggressive when defining what is expected from the parameter. I...
Hi, my objective is to get the current aggregation of the chart, and then based on what the chart aggregation is, do conditional actions. In the first code example below I am getting the current aggregation with getAggregationPeriod() and feed it into a swich block and case compare with aggregationPeriod, but that gives an error.

Code:
def currentAggregation;
def trendAggregation;
def stopAggregation;

currentAggregation = getAggregationPeriod();

switch (currentAggregation) {
    case aggregationPeriod.FIVE_MIN:
        trendAggregation = aggregationPeriod.HOUR;
        stopAggregation = aggregationPeriod.FIFTEEN_MIN;
}

plot Data = close;


I also tried just using a string for the case compare and that gives fewer errors, but still shows as an error for the switch statement. This is shown in the below code block.

Code:
def currentAggregation;
def trendAggregation;
def stopAggregation;

currentAggregation = getAggregationPeriod();

switch (currentAggregation) {
    case "5m":
        trendAggregation = aggregationPeriod.HOUR;
        stopAggregation = aggregationPeriod.FIFTEEN_MIN;
}

plot Data = close;

Any ideas on why this is an error and how to use getCurrentAggregation() in a conditional?

Here is one way using if/else if/else

Ruby:
def currentAggregation;
def trendAggregation;
def stopAggregation;

currentAggregation = GetAggregationPeriod();

if currentAggregation == AggregationPeriod.FIVE_MIN {
    trendAggregation = AggregationPeriod.HOUR;
    stopAggregation = AggregationPeriod.FIFTEEN_MIN;
} else if currentAggregation == AggregationPeriod.FIFTEEN_MIN {
    trendAggregation = AggregationPeriod.TWO_HOURS;
    stopAggregation = AggregationPeriod.THIRTY_MIN;
} else {
    trendAggregation = AggregationPeriod.DAY;
    stopAggregation = AggregationPeriod.HOUR;
}

plot Data = close;
 
Ah, ok, I get it... I can use the if ... then ... logic. I wonder why it doesn't work with switch.

i think i know what you mean.
you are expecting GetAggregationPeriod() to be a collection of all possible time choices. but it doesn't seem to work like that. ( i just tried variations of agg... words and couldn't get anything to work)

switch wants to look at a variable defined with input, with all the choices predefined , within { } .
then have case statements for each of the choices.

https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/switch
 
Last edited:
In the code below I have a simple script function with a single input var that just plots the close for an aggregation period.

Then, in the main thread I also have an input var for the aggregation period (plotAggregationPeriod), and also have some if...then statements to assign an aggregation period to another var (stopAggregation).

The issue that I'm facing is this: at the bottom of the script I can plot a call to script plotClose( plotAggregationPeriod) and it works fine.

But a plot call to script plotClose( stopAggregation) fails to compile with a Incompatible parameter error.

Any idea why I can pass one of the vars and it works, while the other var does not? Both of the vars have been assigned an AggregationPeriod.


Code:
script plotClose {
    input pag = AggregationPeriod.FIFTEEN_MIN;
    plot Data = close( period = pag);
}

###### Main Thread #####

input plotAggregationPeriod = AggregationPeriod.FIFTEEN_MIN;


def currentAggregation;
def stopAggregation ;

currentAggregation = GetAggregationPeriod();

if currentAggregation == AggregationPeriod.FIVE_MIN {
    stopAggregation = AggregationPeriod.FIFTEEN_MIN;
} else if currentAggregation == AggregationPeriod.FIFTEEN_MIN {
    stopAggregation = AggregationPeriod.THIRTY_MIN;
} else if currentAggregation == AggregationPeriod.DAY {
    stopAggregation = AggregationPeriod.WEEK;
} else {
    stopAggregation = AggregationPeriod.HOUR;
}


# Using the input plotAggregationPeriod works
plot Test1 = plotClose( plotAggregationPeriod);

# Using the assigned variableMA stopAggregation fails
plot Test2 = plotClose( stopAggregation);
 
plotAggregationPeriod is an input. Inputs result in a constant, which is why that one works.

stopAggregation is a variable. So, basically, the aggregation constant is being converted into a variable, which results in an integer, and then its being passed to an input which is expecting a constant. Its the wrong data type, and there is no native conversion.

You may have to enumerate the input within the script itself.
 
plotAggregationPeriod is an input. Inputs result in a constant, which is why that one works.

stopAggregation is a variable. So, basically, the aggregation constant is being converted into a variable, which results in an integer, and then its being passed to an input which is expecting a constant. Its the wrong data type, and there is no native conversion.

You may have to enumerate the input within the script itself.

This works and may help

# Using the assigned variableMA stopAggregation fails
plot Test2 = Close(period=stopaggregation);
test2.setpaintingStrategy(PaintingStrategy.HORIZONTAL);
 
plotAggregationPeriod is an input. Inputs result in a constant, which is why that one works.

stopAggregation is a variable. So, basically, the aggregation constant is being converted into a variable, which results in an integer, and then its being passed to an input which is expecting a constant. Its the wrong data type, and there is no native conversion.

You may have to enumerate the input within the script itself.
Thanks Joshua! I've tried different things to find a solution but I didn't come up with one. But your comment "expecting a constant" got me thinking that thinkscript treats a constant different than the value that it represents. In most other languages that I work with, the constant is just a way of representing the underlying value. So if a constant is assigned a value of 100, then assigning that constant to a variable would result in the variable being a type integer with a value of 100.

But based on your comment it sounds like there is actually a variable type of "constant". The two would produce the type mismatch:

def test = AggregationPeriod.HOUR;
#results in var test being a type constant with a value of something that represents an hourly aggregation period. I don't know what that is... I assume some integer, but even if the value is an integer, thinkscript doesn't see it as an integer because the var type is constant.

def test = 100;
#results in var test being a type integer with a value of 100. thinkscript sees it as a valid integer value.

Soooo... based on that I just changed the input var pag in the script function to be an integer assignment so that the type is set as integer and then it works to pass though either form.

I don't quite get it yet, because in the main thread plotAggregationPeriod should be a var type constant, but the script function plotClose is expecting input pag to be in integer. Seems like it shouldn't work, but it does so I'm not going to argue ;).

Looks like you can pass constant -> integer type, but you can't pass integer -> constant type. Kinda makes sense.

Code:
script plotClose {
    #input pag must be assigned an integer to initialize the var to integer type
    input pag = 15;
    plot Data = close( period = pag);
}

###### Main Thread #####

input plotAggregationPeriod = AggregationPeriod.FIFTEEN_MIN;


def currentAggregation;
def stopAggregation ;

currentAggregation = GetAggregationPeriod();

if currentAggregation == AggregationPeriod.FIVE_MIN {
    stopAggregation = AggregationPeriod.FIFTEEN_MIN;
} else if currentAggregation == AggregationPeriod.FIFTEEN_MIN {
    stopAggregation = AggregationPeriod.THIRTY_MIN;
} else if currentAggregation == AggregationPeriod.DAY {
    stopAggregation = AggregationPeriod.WEEK;
} else {
    stopAggregation = AggregationPeriod.HOUR;
}


# Using the input plotAggregationPeriod works
plot Test1 = plotClose( plotAggregationPeriod);

# Using the assigned variable stopAggregation works
plot Test2 = plotClose( stopAggregation);
 
def test = AggregationPeriod.HOUR;
#results in var test being a type constant with a value of something that represents an hourly aggregation period. I don't know what that is... I assume some integer, but even if the value is an integer, thinkscript doesn't see it as an integer because the var type is constant.

The reserved aggregation constants contain either an integer or a double equal to the number of milliseconds within the aggregate. I assume its an integer because there is no need for it to be floating point. Similar function parameters are documented as Int <Parameter>.

Inputs which default to a reserved constant can be a bit aggressive when defining what is expected from the parameter. I believe it can create a situation where the parameter is expecting a valid selection from among only that specific class of reserved constant.

Other functions and standard date types are not as aggressive. In your updated script, you've set the input to expect a double. The remaining functions make no differentiation between, say, AggregationPeriod.FIVE_MIN and 300000, they are viewed as interchangeable in this case.

A constant isn't necessarily a data type, per say. I just don't know how else to phrase it. Reserved constants are obvious, but you can also create a constant with a static variable. Def X = 5; will always be 5, so X is compatible when a constant is expected. In at least most cases, if not all cases.

It boils down to usage more so than definition.
 
Solution
i think i know what you mean.
you are expecting GetAggregationPeriod() to be a collection of all possible time choices. but it doesn't seem to work like that. ( i just tried variations of agg... words and couldn't get anything to work)

switch wants to look at a variable defined with input, with all the choices predefined , within { } .
then have case statements for each of the choices.

https://tlc.thinkorswim.com/center/reference/thinkScript/Reserved-Words/switch
Based on the docs, switch should take a [def] type enum as well as the [input] type. This means that a def variable declared above the switch statement should work.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
366 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top