Trying to define variable properly

Trikortreat12

New member
I'm trying to write this script where I define 2 aggregation periods. 1 of them is daily, and other is hour/4 hour. My problem comes in when i want to define my 2nd aggregation period. I want to only have the aggregation period change if I have an ATR value greater than 3. I think it's a problem with the way integers/conditionals work in thinkscript, but am not sure.

Code:
input averagetype = AverageType.WILDERS;

input ATRlength = 10;
input Agg_1 = AggregationPeriod.Day;
def Agg_2;

def ATR = MovingAverage (averagetype, TrueRange(high(period = Agg_1)[1], close(period =  Agg_1)[1], low(period = Agg_1)[1]), ATRlength);

if (ATR > 3) {
Agg_2 = AggregationPeriod.FOUR_HOURS;
}
else {
Agg_2 = AggregationPeriod.HOUR;
}
 
Solution
I'm trying to write this script where I define 2 aggregation periods. 1 of them is daily, and other is hour/4 hour. My problem comes in when i want to define my 2nd aggregation period. I want to only have the aggregation period change if I have an ATR value greater than 3. I think it's a problem with the way integers/conditionals work in thinkscript, but am not sure.

Code:
input averagetype = AverageType.WILDERS;

input ATRlength = 10;
input Agg_1 = AggregationPeriod.Day;
def Agg_2;

def ATR = MovingAverage (averagetype, TrueRange(high(period = Agg_1)[1], close(period =  Agg_1)[1], low(period = Agg_1)[1]), ATRlength);

if (ATR > 3) {
Agg_2 = AggregationPeriod.FOUR_HOURS;
}
else {
Agg_2 = AggregationPeriod.HOUR;
}

This syntax...
I'm trying to write this script where I define 2 aggregation periods. 1 of them is daily, and other is hour/4 hour. My problem comes in when i want to define my 2nd aggregation period. I want to only have the aggregation period change if I have an ATR value greater than 3. I think it's a problem with the way integers/conditionals work in thinkscript, but am not sure.

Code:
input averagetype = AverageType.WILDERS;

input ATRlength = 10;
input Agg_1 = AggregationPeriod.Day;
def Agg_2;

def ATR = MovingAverage (averagetype, TrueRange(high(period = Agg_1)[1], close(period =  Agg_1)[1], low(period = Agg_1)[1]), ATRlength);

if (ATR > 3) {
Agg_2 = AggregationPeriod.FOUR_HOURS;
}
else {
Agg_2 = AggregationPeriod.HOUR;
}

This syntax seems to work with a Plot statement with 2 different aggs vs a Def Statement with 2 different aggs

Ruby:
input averagetype = AverageType.WILDERS;

input ATRlength = 10;
input Agg_1 = AggregationPeriod.DAY;
plot Agg_2;

def ATR = Round(MovingAverage (averagetype, TrueRange(high(period = Agg_1)[1], close(period =  Agg_1)[1], low(period = Agg_1)[1]), ATRlength), 2);

if (ATR > 3) {
    Agg_2 = close(period = AggregationPeriod.FOUR_HOURS);
}
else {
    Agg_2 = close(period = AggregationPeriod.HOUR);
}

plot x1 = close(period = Agg_1);
plot x2 = Agg_2;
 
Solution
I'm trying to write this script where I define 2 aggregation periods. 1 of them is daily, and other is hour/4 hour. My problem comes in when i want to define my 2nd aggregation period. I want to only have the aggregation period change if I have an ATR value greater than 3. I think it's a problem with the way integers/conditionals work in thinkscript, but am not sure.

Code:
input averagetype = AverageType.WILDERS;

input ATRlength = 10;
input Agg_1 = AggregationPeriod.Day;
def Agg_2;

def ATR = MovingAverage (averagetype, TrueRange(high(period = Agg_1)[1], close(period =  Agg_1)[1], low(period = Agg_1)[1]), ATRlength);

if (ATR > 3) {
Agg_2 = AggregationPeriod.FOUR_HOURS;
}
else {
Agg_2 = AggregationPeriod.HOUR;
}

i got distracted in the middle of my research on this and didn't realize sleepyz answered it, until i refreshed the page.
anyway, the more the merrier
merry christmas everyone

to expand on what @SleepyZ said

the problem you were running into, is that you can't define 2+ aggregation times in 1 formula.
. error: 2 different secondary periods can not be used in a variable

you can,
. define 2 different aggregation variables.
. create 2 formulas, each using one of the aggregation variables.
. then use an if-then to choose the desired value from the 2 formulas.

i don't know what you want to use the agg2 for, so i used close in my version.

i made a bubble to display several variables. it turns yellow when the if-then condition is true ( ATR > 3 )
i added a variable to represent the choosen aggregation minutes. it is the bottom value in the bubble.


Ruby:
# aggchoose_3rd

input averagetype = AverageType.WILDERS;
input ATRlength = 10;
input Agg_1 = AggregationPeriod.DAY;

def agg1_min = (Agg_1/60000);

def ATR = MovingAverage (averagetype, TrueRange(high(period = Agg_1)[1], close(period =  Agg_1)[1], low(period = Agg_1)[1]), ATRlength);

# -------------------------
# this section has an error
# error 2 different secondary periods can not be used in a variable

#def agg_2;
#if (ATR > 3) {
#  Agg_2 = AggregationPeriod.FOUR_HOURS;
#}
#else {
#  Agg_2 = AggregationPeriod.HOUR;
#}

#def cls2 = close(period = agg_2);
# ------------------------

input agg_3a = AggregationPeriod.FOUR_HOURS;
input agg_3b = AggregationPeriod.HOUR;
def agg3a_min = (Agg_3a/60000);
def agg3b_min = (Agg_3b/60000);

def cls_3a = close(period = agg_3a);
def cls_3b = close(period = agg_3b);

def cls3;
def aggmin;
if (ATR > 3) then {
  cls3 = cls_3a;
#  aggmin = (4 * 60);
  aggmin = agg3a_min;
} else {
  cls3 = cls_3b;
#  aggmin = (1 * 60);
  aggmin = agg3b_min;
}

# -------------------------
#def cls4 = if (ATR > 3) then cls_2a else cls_2b;

AddChartBubble(1, low * 0.99,
"len " + ATRlength
+ "\nA1 " + Agg1_min
+ "\natr " + ATR
#+ "\n------"
#+ "\nA2 " + (agg_2/60000)
#+ "\nc2" + cls2
+ "\n------"
+ "\n2a " + agg3a_min
+ "\n2b " + agg3b_min
+ "\nc3" + cls3
+ "\nAmin " + aggmin
, ( if (ATR > 3) then Color.YELLOW else Color.GRAY), no);
#

i tested with WMT 5day 15min
the bottom number shows the selected agg minutes
Ucwtqcw.jpg
 
Last edited:

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

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
414 Online
Create Post

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