How do you set the AggregationPeriod for a study based on the chosen time frame instead of requiring to manually adjust it in a study's settings?

Mr_Wheeler

Active member
I have a study where if you don't manually change the AggregationPeriod's setting in the study's setttings it often leads to a blank study output because the AggregationPeriod & time frame are not correctly synced to each other.

If you for instance set the aggregation perion to the 3 minute chart and have your chart set to 10 minutes then the study becomes blank.

Code:
plot ZeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(0));

# TOS MTF AccumulationSwingIndex
#global variables
input agg  = AggregationPeriod.TEN_MIN;
def cclose = close(period = agg);
def oopen  = open(period = agg);
def hhigh  = high(period = agg);
def llow   = low(period = agg);
# ########################################################
input smaLength = 10;
def limit = 30;
def AbsHighClose = AbsValue(hhigh - cclose[1]);
def AbsLowClose = AbsValue(llow - cclose[1]);
def AbsCloseOpen = AbsValue(cclose[1] - oopen[1]);
def K = If(AbsHighClose >= AbsLowClose, AbsHighClose, AbsLowClose);
def R = If(AbsHighClose >= AbsLowClose,
If(AbsHighClose >= (hhigh - llow), AbsHighClose - 0.5 * AbsLowClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen),
If(AbsLowClose >= (hhigh - llow),  AbsLowClose - 0.5 * AbsHighClose + 0.25 * AbsCloseOpen, (hhigh - llow) + 0.25 * AbsCloseOpen));
def nRes = If(R != 0,
(50 * (((cclose - cclose[1]) + 0.50 * (cclose - oopen) + 0.25 * (cclose[1] - oopen[1])) / R ) * K / limit) + if !IsNaN(nRes[1]) then nRes[1] else 0,
0 + if !IsNaN(nRes[1]) then nRes[1] else 0);
def ASI = nRes;
def sma = SimpleMovingAvg(ASI,smaLength);
# ########################################################
# charting and formatting

plot pASI = ASI;
pASI.SetLineWeight(3);
pASI.AssignValueColor(if asi>sma then Color.Green else Color.RED);

plot pSMA = sma;
pSMA.SetPaintingStrategy(PaintingStrategy.DASHES);
pSMA.SetLineWeight(1);
pSMA.AssignValueColor(Color.VIOLET);

DefineGlobalColor("bear",  CreateColor(225, 0, 0)) ;
DefineGlobalColor("bull",  CreateColor(0, 165, 0)) ;

#AddLabel(yes, if asi > sma then  "MTF - Blue area & green dots" else "stand by - Red Area & red dots", if asi < sma then (Color.white) else (color.light_green));

###########################dual

plot Numbers = ASI;
NumberS.AssignValueColor(if asi>sma then Color.Green else Color.white);

Last edited:
Last edited:

Not the exact question you're looking for?

87k+ Posts
261 Online

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?