Can some body please provide the syntax for the formula of the 240-bar average of this formula:
EVAL(BarsSince(19990101)<4100,CLOSE(1,GetSeries("CAPE-MA35")),CLOSE(1,GetSeries("CAPE")) / LoopAvg(`CLOSE(CTR,GetSeries("CAPE"))`, 420, 1, 1))
Here's a formula that uses nested LoopAvg, capturing CTR to a variable in the Eval condition:
LoopAvg(`Eval(SetVar(@c, CTR) & Close(CTR + 420, GetSeries("CAPE")) = NA, Close(CTR, GetSeries("CAPE-MA35")), Close(CTR, GetSeries("CAPE")) / LoopAvg("Close(@c + CTR, GetSeries(`CAPE`))", 420))`, 240, 1)
BarsSince can't be used because it can only check the as of date and not prior offsets, so it instead checks CAPE directly to see if the furthest needed bar is available.
However, since LoopAvg is just a simple moving average, the formula can be simplified by using the SMA function which is NA when there aren't enough bars:
LoopAvg(`IsNA(Close(CTR, GetSeries("CAPE")) / SMA(420, CTR, GetSeries("CAPE")), Close(CTR, GetSeries("CAPE-MA35")))`, 240, 1)
Thank you Adam for this explanation of why my formula did not work. I use the ratio of the CAPE divided by its 35-year (420 month) MA as a metric of stock market valuation. The 20-year average of this ratio is also used as one of the indicators for either investing in Growth, or Defensive ETFs.
This formula saves me from updating the monthly data series CAPE-MA35, because it is now calculate from the CAPE series directly.
P123 is really a great platform if one knows how to use it.