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);
}
my comments are to help you learn. keep asking questions.
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)