#### agirlhasnoname42

##### Member
Hi I'm building a script in another study to reference the near-term and momentum lines within Market Forecast. Within the larger study their positions (crossed over 40 in last bar, above last bar) help determine the color of a different moving average. I want the aggregation period referenced for both Market Forecast lines to in one of two states depending on the aggregation period being used. If the aggregation period is lower than Day, I want the line referenced to be the 2 hour. If the chart is day or higher I want the line to be whatever period is being used. I am lost.. I'm sure there's a really easy way to do this but I can't figure it out, I'm just too inexperienced can someone help please? This is what I have so far for the nested code.. I didn’t want to paste the whole thing because it’s so long so if anything else is needed please let me know.
Cheers, Misty

Code:
``````script MFTimeFrame {
def aP2 = AggregationPeriod.TWO_HOURS;
def aPD = GetAggregationPeriod();
def currentPeriod = GetAggregationPeriod();

def MFMomentum = reference MarketForecast().Momentum;
def MFNearTerm = reference MarketForecast().NearTerm;
def close_aP2 = close(period = aP2);
def close_aPD = close(period = aPD);

If (GetAggregationPeriod() > AggregationPeriod.FOUR_HOURS) then MFMomentum = period(close_aPD) else MFMomentum = period(close_aP2);

}``````

Last edited:
Solution
thank you, this second aggregation logic is very helpful for the future, even though I can't use it on MarketForecast. Too bad the authors won't fess up lol, maybe I wouldn't either. I'm going through the ADR part of the painting strategy and try to set a new variable for when ADRunder/ADRover is true/false and when either is true, place a dot under the tema line. here is what I have, I appreciate any feedback I know I must be doing this wrong because there's a dot under every bar! Thank you so much for your feedback, I appreciate it! Misty

snippet i'm working on:

def ADRunder = Round((close - close[1]) / close[1] * 100) < ADR;
This is some of the other script where these lines are colored.. if there's an easy way to just adjust it from there that would be even better Thanks for any help! Misty

Code:
``````input Tema_BVlength = 4;
TEMA_BV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_BV.AssignValueColor(
else if MarketForecast().NearTerm > MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] < 40 and MarketForecast().NearTerm > 40 then Color.YELLOW else Color.DARK_GREEN);
TEMA_BV.SetLineWeight(3);
#TEMA_BV.SetDefaultColor(Color.BLUE);

input Tema_SVlength = 4;
plot TEMA_SV = TEMA(BaseSellAvg, Tema_SVlength);
TEMA_SV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_SV.AssignValueColor(
else if MarketForecast().NearTerm < MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] > 40 and MarketForecast().NearTerm < 40 then Color.DARK_ORANGE else Color.DARK_RED);
TEMA_SV.SetLineWeight(3);``````

If I'm understanding, you want it to aggregate 2 hours for intraday charts, and then for the daily charts, you want to aggregate it with whatever aggregation period is on the chart?

The issue is that the getaggregationPeriod() returns a number, so you can't compare it to aggregationperiod.Four_Hours.

Try
Code:
``````input currentperiod = getaggregationPeriod();
def agg = if currentperiod < 14400000 then aggregationPeriod.Two_Hours else currentperiod;
def close_aP2 = close(period = agg);
def close_aPD = close(period = agg);``````

Last edited:
Here is a guide for the aggregationPeriods.

If I'm understanding, you want it to aggregate 2 hours for intraday charts, and then for the daily charts, you want to aggregate it with whatever aggregation period is on the chart?

The issue is that the getaggregationPeriod() returns a number, so you can't compare it to aggregationperiod.Four_Hours.

Try
Code:
``````input currentperiod = getaggregationPeriod();
def agg = if currentperiod < 14400000 then aggregationPeriod.Two_Hours else currentperiod;
def close_aP2 = close(period = agg);
def close_aPD = close(period = agg);``````

you can compare aggregation variables and aggregation times. you don't have to compare to a number.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Others/GetAggregationPeriod

If I'm understanding, you want it to aggregate 2 hours for intraday charts, and then for the daily charts, you want to aggregate it with whatever aggregation period is on the chart?

The issue is that the getaggregationPeriod() returns a number, so you can't compare it to aggregationperiod.Four_Hours.

Try
Code:
``````input currentperiod = getaggregationPeriod();
def agg = if currentperiod < 14400000 then aggregationPeriod.Two_Hours else currentperiod;
def close_aP2 = close(period = agg);
def close_aPD = close(period = agg);``````

Thank you for the input! That's correct, for intraday, I would like the market forecast operators to aggregate on the two hour time frame, but revert to usual on all higher time frames. This is how I changed the code so far, but I can't figure out how to link the referenced studies to the defined variable for the period "test".. also below that is where I'm trying to have them color an average.. I'm unsure how to proceed to make them work with eachother and how to instruct it to define the period properly- "plot momoDay = MFMomentumDAY (period=close_test); returns no such function.. thanks for any help, I'm a real newbie!
Misty

Code:
``````#############################################################

def aP2 = AggregationPeriod.TWO_HOURS;
def aPD = GetAggregationPeriod();
def currentPeriod = GetAggregationPeriod();
def agg = if currentPeriod < 14400000 then AggregationPeriod.TWO_HOURS else currentPeriod;
def close_test = close(period = agg);
#def close_aPD = close(period = aPD);

def MFMomentumDAY = MarketForecast().Momentum;
def MFNearTerm = reference MarketForecast().NearTerm;

plot momoDay = MFMomentumDAY (period=close_test);

############################################################

#####################################################################
####### Tema on 2 period EMA of Buying and Selling Volume############

input Tema_BVlength = 4;
TEMA_BV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_BV.AssignValueColor(
else if MarketForecast().NearTerm > MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] < 40 and MarketForecast().NearTerm > 40 then Color.YELLOW else Color.DARK_GREEN);
TEMA_BV.SetLineWeight(3);
#TEMA_BV.SetDefaultColor(Color.BLUE);

input Tema_SVlength = 4;
plot TEMA_SV = TEMA(BaseSellAvg, Tema_SVlength);
TEMA_SV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_SV.AssignValueColor(
else if MarketForecast().NearTerm < MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] > 40 and MarketForecast().NearTerm < 40 then Color.DARK_ORANGE else Color.DARK_RED);
TEMA_SV.SetLineWeight(3);

####################################################################################``````

Hi I'm building a script in another study to reference the near-term and momentum lines within Market Forecast. Within the larger study their positions (crossed over 40 in last bar, above last bar) help determine the color of a different moving average. I want the aggregation period referenced for both Market Forecast lines to in one of two states depending on the aggregation period being used. If the aggregation period is lower than Day, I want the line referenced to be the 2 hour. If the chart is day or higher I want the line to be whatever period is being used. I am lost.. I'm sure there's a really easy way to do this but I can't figure it out, I'm just too inexperienced can someone help please? This is what I have so far for the nested code.. I didn’t want to paste the whole thing because it’s so long so if anything else is needed please let me know.
Cheers, Misty

Code:
``````script MFTimeFrame {
def aP2 = AggregationPeriod.TWO_HOURS;
def aPD = GetAggregationPeriod();
def currentPeriod = GetAggregationPeriod();

def MFMomentum = reference MarketForecast().Momentum;
def MFNearTerm = reference MarketForecast().NearTerm;
def close_aP2 = close(period = aP2);
def close_aPD = close(period = aPD);

If (GetAggregationPeriod() > AggregationPeriod.FOUR_HOURS) then MFMomentum = period(close_aPD) else MFMomentum = period(close_aP2);

}``````

referencing post #1

not sure what you are trying to do.
you can't change the aggregation time for MarketForecast. there aren't any input parameters for it.
the code for it is hidden, so we can't copy it and modify it.
https://tlc.thinkorswim.com/center/reference/Tech-Indicators/studies-library/M-N/MarketForecast
if you can find another study that mimics what marketforecast does, maybe 2nd agg could be applied to it.

..cant define 2nd aggregation in a script
..there are 2 vars set to same thing, GetAggregationPeriod()
..ref external data in a script, might be redundant , slow. ref it once in main script, then pass on values to script
..the variable being assigned a value from an if-then, doesn't go inside of it. it is put at the start of the expression.
def MFMomentum = if x then y else if k then z else t;
..if you want data from a 2nd aggregation, you need to set the 2nd agg within a price function. then put the price funtion inside of the desired function , like close(). unfortunetely , can't use 2nd agg on MarketForecast().

i would rewrite these rules
. If the aggregation period is lower than Day,
. I want the line referenced to be the 2 hour.
. If the chart is day or higher,
. I want the line to be whatever period is being used.

to look like this
. if the chart agg < day then 2 hour
. if the chart agg >= day then chartagg

then try to create codes for them

Code:
``````#  example code
# ----------------------------------

# get agg of the chart
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;

# use an input to set a variable to desired agg when chart agg is < day
input lowagg = AggregationPeriod.two_hours;

# compare chart agg to day
def lineagg2 = If chartagg < AggregationPeriod.day then lowagg else chartagg;

# calc minutes of the agg
def lineaggmin = (lineagg2/60000);

# example , get price from an agg time
def test1 = open(period = lineagg2);
addlabel(1, "close: " + test1 + "  agg: " + lineagg2 + "  aggmin: " + lineaggmin, color.yellow);``````

i left 2nd aggregation formulas in this, although they aren't used
Code:
``````# my version of post1 code
declare lower;

def aP2 = AggregationPeriod.TWO_HOURS;
def currentPeriod = GetAggregationPeriod();

def lineagg = If currentPeriod < AggregationPeriod.day then ap2 else currentPeriod;

plot MFMomentum = reference MarketForecast().Momentum;
plot MFNearTerm = reference MarketForecast().NearTerm;

MFMomentum.setdefaultcolor(color.cyan);
MFNearTerm.setdefaultcolor(color.yellow);

#plot close_aP2 = close(period = aP2);
#close_aP2.setdefaultcolor(color.white);

# price = close(period = linagg)``````

Last edited:
Thank you for the input! That's correct, for intraday, I would like the market forecast operators to aggregate on the two hour time frame, but revert to usual on all higher time frames. This is how I changed the code so far, but I can't figure out how to link the referenced studies to the defined variable for the period "test".. also below that is where I'm trying to have them color an average.. I'm unsure how to proceed to make them work with eachother and how to instruct it to define the period properly- "plot momoDay = MFMomentumDAY (period=close_test); returns no such function.. thanks for any help, I'm a real newbie!
Misty

Code:
``````#############################################################

def aP2 = AggregationPeriod.TWO_HOURS;
def aPD = GetAggregationPeriod();
def currentPeriod = GetAggregationPeriod();
def agg = if currentPeriod < 14400000 then AggregationPeriod.TWO_HOURS else currentPeriod;
def close_test = close(period = agg);
#def close_aPD = close(period = aPD);

def MFMomentumDAY = MarketForecast().Momentum;
def MFNearTerm = reference MarketForecast().NearTerm;

plot momoDay = MFMomentumDAY (period=close_test);

############################################################

#####################################################################
####### Tema on 2 period EMA of Buying and Selling Volume############

input Tema_BVlength = 4;
TEMA_BV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_BV.AssignValueColor(
else if MarketForecast().NearTerm > MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] < 40 and MarketForecast().NearTerm > 40 then Color.YELLOW else Color.DARK_GREEN);
TEMA_BV.SetLineWeight(3);
#TEMA_BV.SetDefaultColor(Color.BLUE);

input Tema_SVlength = 4;
plot TEMA_SV = TEMA(BaseSellAvg, Tema_SVlength);
TEMA_SV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_SV.AssignValueColor(
else if MarketForecast().NearTerm < MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] > 40 and MarketForecast().NearTerm < 40 then Color.DARK_ORANGE else Color.DARK_RED);
TEMA_SV.SetLineWeight(3);

####################################################################################``````

your code is missing a couple of variables.
i set them = to a couple of averages, just to get something to plot.

regarding the plot color formula, i would pull out marketforecast and set it to a variable before the plot. that will minimize how many times the script makes external references, and make it easier to read.

Code:
``````def ADRover = average(close, 22);
def mrk_n = MarketForecast().NearTerm;

input Tema_BVlength = 4;
plot TEMA_BV = TEMA(close, Tema_BVlength);

TEMA_BV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_BV.SetLineWeight(3);
# TEMA_BV.AssignValueColor(
# if ADRover > ADRunder and ADRover[1] < ADRunder[1] then Color.GREEN else if MarketForecast().NearTerm >  MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] < 40 and MarketForecast().NearTerm > 40 then Color.YELLOW else Color.DARK_GREEN);

TEMA_BV.AssignValueColor(
else if mrk_n > Mrk_n[1] and mrk_n[1] < 40 and mrk_n > 40 then Color.YELLOW
else Color.DARK_GREEN);``````

thank you, this second aggregation logic is very helpful for the future, even though I can't use it on MarketForecast. Too bad the authors won't fess up lol, maybe I wouldn't either. I'm going through the ADR part of the painting strategy and try to set a new variable for when ADRunder/ADRover is true/false and when either is true, place a dot under the tema line. here is what I have, I appreciate any feedback I know I must be doing this wrong because there's a dot under every bar! Thank you so much for your feedback, I appreciate it! Misty

snippet i'm working on:

def ADRunder = Round((close - close[1]) / close[1] * 100) < ADR;

entire script - yes i know very messy :

####################################################################################

#Volume color coded by amount of volume on up-tick versus amount of volume on down-tick

declare lower;

def O = open;
def H = high;
def C = close;
def L = low;
def V = volume;
def Buying = V * (C - L) / (H - L);
def Selling = V * (H - C) / (H - L);

# Selling Volume
plot SV = Selling;
SV.SetPaintingStrategy(PaintingStrategy.LINE);
SV.SetDefaultColor(Color.RED);
SV.HideTitle();
SV.HideBubble();
SV.SetLineWeight(3);
SV.Hide();

# Note that Selling + Buying Volume = Volume.
plot BV = volume;
BV.SetPaintingStrategy(PaintingStrategy.LINE);
BV.SetDefaultColor(Color.DARK_GREEN);
BV.HideTitle();
BV.HideBubble();
BV.SetLineWeight(3);
BV.Hide();

############################################################
#Custom ADR plots for Noise Filter Color Coding

plot ATR = MovingAverage(AverageType.WILDERS, TrueRange(high, close, low), adrlength);
ATR.SetDefaultColor(GetColor(8));

##AddLabel (yes, "ATR:" + Round((ATR / close) * 100, 3) + "%", Color.LIGHT_GRAY);
##AddLabel (yes, "ATR:" + round((ATR) , 3), Color.LIGHT_GRAY);

#AddLabel (yes, "ADR:" + Round((ADR / close) * 100, 1) + "%", (if ((ADR / close) * 100 < 3) then Color.RED else if ((ADR / close) * 100 > 5) then Color.GREEN else Color.LIGHT_ORANGE));

###"today %" higher than the " Avg %" DOES IT WORK??

def ADRover1 = Round((close - close[1]) / close[1] * 100) > ADR;

###"today %" lower than the " Avg %" DOES IT WORK??

def ADRunder = Round((close - close[1]) / close[1] * 100) < ADR;

#############################################################
# plot average of Buying and Selling Pressure

input avg1_len = 2;
input avg1_type = AverageType.EXPONENTIAL;
def avg1 = MovingAverage(avg1_type, price1, avg1_len);

def price2 = Selling;
input avg2_len = 2;
input avg2_type = AverageType.EXPONENTIAL;
def avg2 = MovingAverage(avg2_type, price2, avg2_len);

plot BaseSellAvg = avg2;
BaseSellAvg.SetDefaultColor(Color.RED);
BaseSellAvg.HideBubble();
BaseSellAvg.Hide();

#####################################################################
####### Tema on 2 period EMA of Buying and Selling Volume############

input Tema_BVlength = 4;
TEMA_BV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_BV.AssignValueColor(
else if MarketForecast().NearTerm > MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] < 40 and MarketForecast().NearTerm > 40 then Color.YELLOW else Color.DARK_GREEN);
TEMA_BV.SetLineWeight(3);
#TEMA_BV.SetDefaultColor(Color.BLUE);

input Tema_SVlength = 4;
plot TEMA_SV = TEMA(BaseSellAvg, Tema_SVlength);
TEMA_SV.SetPaintingStrategy(PaintingStrategy.LINE);
TEMA_SV.AssignValueColor(
else if MarketForecast().NearTerm < MarketForecast().NearTerm[1] and MarketForecast().NearTerm[1] > 40 and MarketForecast().NearTerm < 40 then Color.DARK_ORANGE else Color.DARK_RED);
TEMA_SV.SetLineWeight(3);

####################################################################################
#Create an smoothed MA on Buying and Selling TEMAs for signal

input TemaMA_SellSignal_length = 2;
plot TemaMA_SellSignal = Average(TEMA_SV, TemaMA_SellSignal_length);
TemaMA_SellSignal.SetPaintingStrategy(PaintingStrategy.LINE);
TemaMA_SellSignal.SetDefaultColor(Color.DARK_RED);
TemaMA_SellSignal.SetLineWeight(1);

####################################################################################
###templates for syntax ref###########################################################################
#########################################################################################
#plot Momentum = marketForecast();
#Momentum.setDefaultColor(color.RED);
#AddLabel(yes, "Momentum: " + AsText(Momentum, NumberFormat.TWO_DECIMAL_PLACES) + (if #marketForecast() > marketForecast()[1] then " rising" else if marketForecast() < #marketForecast()[1] then " falling" else ""), color.RED);

#plot NearTerm = MarketForecast().NearTerm;
#NearTerm.setDefaultColor(color.BLUE);
#AddLabel(yes, "Near Term: " + AsText(NearTerm, NumberFormat.TWO_DECIMAL_PLACES) + (if #marketForecast().NearTerm > marketForecast().NearTerm[1] then " rising" else if #marketForecast().NearTerm < marketForecast().NearTerm[1] then " falling" else ""), color.BLUE);

#plot IntTerm = MarketForecast().Intermediate;
#IntTerm.setDefaultColor(createColor(51, 204, 0));
#AddLabel(yes, "(IT)-Intermediate Term: " + AsText(IntTerm, NumberFormat.TWO_DECIMAL_PLACES) + (if #marketForecast().Intermediate > marketForecast().Intermediate[1] then " rising" else if #marketForecast().Intermediate < marketForecast().Intermediate[1] then " falling" else #""),(createColor(51,204,0)));
#######################################################################################

Last edited:
also, I'll take a look at your code thank you for rewriting some of my adr stuff, I had some plots for the adr info, it was from a seperate script but since it was custom I couldn't reference it into this code so I smooshed it into the top of this study. ADRover and ADRunder are the percent "ADR%" are over average ADR% (20 length, Day)

I have it to my liking now, albeit without the Market Forecast timeframes, thank you to everyone who helped me.

I have it to my liking now, albeit without the Market Forecast timeframes, thank you to everyone who helped me.
If you don't mind would you share the finished code? Im curious about how the final results work

its a work in progress but I'm leaving it for now.. I'll leave it up for a while but don't want to share too loudly, I think it's my best tool I don't need anything else for volume really

Last edited:

87k+ Posts
150 Online

## The Market Trading Game Changer

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?